-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
no-anonymous-default-export
rule
- Loading branch information
1 parent
c975742
commit 5a8d6ef
Showing
6 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# no-anonymous-default-export | ||
|
||
Reports if a module's default export is unnamed. This includes several types of unnamed data types; literals, object expressions, anonymous functions, arrow functions, and anonymous class declarations. | ||
|
||
Ensuring that default exports are named helps improve the grepability of the codebase by encouraging the re-use of the same identifier for the module's default export at its declaration site and at its import sites. | ||
|
||
By default, all types of anonymous default exports are forbidden, but each type can be selectively allowed using options. | ||
|
||
## Rule Details | ||
|
||
### Fail | ||
```js | ||
export default 123 | ||
|
||
export default {} | ||
|
||
export default function () {} | ||
|
||
export default () => {} | ||
|
||
export default class {} | ||
``` | ||
|
||
### Pass | ||
```js | ||
const foo = 123 | ||
export default foo | ||
|
||
export default function foo() {} | ||
|
||
/*eslint no-anonymous-default-export: [2, "allow-literal"]*/ | ||
export default 123 | ||
|
||
/*eslint no-anonymous-default-export: [2, "object-expression"]*/ | ||
export default {} | ||
|
||
/*eslint no-anonymous-default-export: [2, "allow-anonymous-function-declaration"]*/ | ||
export default function () {} | ||
|
||
/*eslint no-anonymous-default-export: [2, "allow-arrow-function-expression"]*/ | ||
export default () => {} | ||
|
||
/*eslint no-anonymous-default-export: [2, "allow-anonymous-class-declaration"]*/ | ||
export default class {} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* @fileoverview Rule to disallow anonymous default exports. | ||
* @author Duncan Beevers | ||
*/ | ||
|
||
module.exports = { | ||
meta: {}, | ||
|
||
create: function (context) { | ||
|
||
const forbid = (option) => !~context.options.indexOf('allow-' + option) | ||
|
||
return { | ||
'ExportDefaultDeclaration': (node) => { | ||
const type = node.declaration.type | ||
const noID = !node.declaration.id | ||
|
||
if (type === 'Literal' && forbid('literal')) { | ||
context.report({ | ||
node: node, | ||
message: 'Unexpected default export of literal', | ||
}) | ||
return | ||
} | ||
|
||
if (type === 'ObjectExpression' && forbid('object-expression')) { | ||
context.report({ | ||
node: node, | ||
message: 'Unexpected default export of object expression', | ||
}) | ||
return | ||
} | ||
|
||
if (type === 'FunctionDeclaration' && noID && forbid('anonymous-function-declaration')) { | ||
context.report({ | ||
node: node, | ||
message: 'Unexpected default export of anonymous function', | ||
}) | ||
return | ||
} | ||
|
||
if (type === 'ArrowFunctionExpression' && forbid('arrow-function-expression')) { | ||
context.report({ | ||
node: node, | ||
message: 'Unexpected default export of arrow function', | ||
}) | ||
return | ||
} | ||
|
||
if (type === 'ClassDeclaration' && noID && forbid('anonymous-class-declaration')) { | ||
context.report({ | ||
node: node, | ||
message: 'Unexpected default export of anonymous class', | ||
}) | ||
return | ||
} | ||
}, | ||
} | ||
|
||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { test, SYNTAX_CASES } from '../utils' | ||
|
||
import { RuleTester } from 'eslint' | ||
|
||
var ruleTester = new RuleTester() | ||
var rule = require('rules/no-anonymous-default-export') | ||
|
||
ruleTester.run('no-anonymous-default-export', rule, { | ||
valid: [ | ||
test({ code: 'const foo = 123\nexport default foo' }), | ||
test({ code: 'export default function foo() {}'}), | ||
test({ code: 'export default class MyClass {}'}), | ||
|
||
test({ code: 'export default 123', options: ['allow-literal'] }), | ||
test({ code: 'export default {}', options: ['allow-object-expression'] }), | ||
test({ code: 'export default class {}', options: ['allow-anonymous-class-declaration'] }), | ||
test({ code: 'export default function() {}', options: ['allow-anonymous-function-declaration'] }), | ||
test({ code: 'export default () => {}', options: ['allow-arrow-function-expression'] }), | ||
|
||
...SYNTAX_CASES, | ||
], | ||
|
||
invalid: [ | ||
test({ code: 'export default 123', errors: [{ message: 'Unexpected default export of literal' }] }), | ||
test({ code: 'export default {}', errors: [{ message: 'Unexpected default export of object expression' }] }), | ||
test({ code: 'export default class {}', errors: [{ message: 'Unexpected default export of anonymous class' }] }), | ||
test({ code: 'export default function() {}', errors: [{ message: 'Unexpected default export of anonymous function' }] }), | ||
test({ code: 'export default () => {}', errors: [{ message: 'Unexpected default export of arrow function' }] }), | ||
], | ||
}) |