Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure ESLint so that it disallows default exports

I've been searching the web and StackOverflow for this for quite some time with no success.

What I'm trying to do have ESLint mark the following as errors:

export default ...;

with default being the key here. So far the best I've got is a reference to eslint-plugin-import plugin and some of its rules that can make me closer to the target, namely the no-anonymous-default-export rule. But even with this rule the following default exports would be valid:

const foo = 123
export default foo

export default class MyClass() {}

export default function foo() {}

How can I configure ESLint in such a way that these four would also be considered errors?

like image 552
Dethariel Avatar asked Jun 05 '17 22:06

Dethariel


People also ask

How do I fix no default export?

The "Module has no default export" error occurs when we try to import as default from a module that doesn't have a default export. To solve the error make sure the module has a named export and wrap the import in curly braces, e.g. import {myFunction} from './myModule' .

How do I configure ESLint?

There are two primary ways to configure ESLint: Configuration Comments - use JavaScript comments to embed configuration information directly into a file. Configuration Files - use a JavaScript, JSON, or YAML file to specify configuration information for an entire directory and all of its subdirectories.

How do I turn off ESLint rule?

If you want to disable an ESLint rule in a file or on a specific line, you can add a comment. On a single line: const message = 'foo'; console. log(message); // eslint-disable-line no-console // eslint-disable-next-line no-console console.


1 Answers

You can do this with the no-restricted-syntax rule. Try pasting this in the demo to try it out (you'll need to change "Source Type" to "module" first in the options):

/* eslint "no-restricted-syntax": ["error", {
    "selector": "ExportDefaultDeclaration",
    "message": "Prefer named exports"
  }] */
export default class Foo { } // 5:1 - Prefer named exports (no-restricted-syntax)
like image 116
btmills Avatar answered Nov 15 '22 22:11

btmills