-
-
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 c7efe8e
Showing
7 changed files
with
140 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,37 @@ | ||
# no-anonymous-default-export | ||
|
||
Reports if an unnamed literal or anonymous function is exported as a module's default. | ||
This 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. | ||
|
||
## Rule Details | ||
|
||
### Fail | ||
```js | ||
export default 123 | ||
``` | ||
|
||
```js | ||
export default {} | ||
``` | ||
|
||
```js | ||
export default function () {} | ||
``` | ||
|
||
```js | ||
export default () => {} | ||
``` | ||
|
||
```js | ||
export default class {} | ||
``` | ||
|
||
### Pass | ||
```js | ||
const foo = 123 | ||
export default foo | ||
``` | ||
|
||
```js | ||
export default function foo() {} | ||
``` |
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,25 @@ | ||
# no-conditional-export | ||
|
||
Reports if a conditional is used as an export. | ||
This helps improve the readability of the codebase by forbidding potentially complex sub-expressions. | ||
|
||
## Rule Details | ||
|
||
### Fail | ||
```js | ||
export default 123 | ||
``` | ||
|
||
```js | ||
export default function () {} | ||
``` | ||
|
||
### Pass | ||
```js | ||
const foo = 123 | ||
export default foo | ||
``` | ||
|
||
```js | ||
export default function foo() {} | ||
``` |
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,48 @@ | ||
/** | ||
* @fileoverview Rule to disallow anonymous default exports. | ||
* @author Duncan Beevers | ||
*/ | ||
|
||
module.exports = { | ||
meta: {}, | ||
|
||
create: function (context) { | ||
|
||
return { | ||
'ExportDefaultDeclaration': (node) => { | ||
if (node.declaration.type === 'Literal' || node.declaration.type === 'ObjectExpression') { | ||
context.report({ | ||
node: node, | ||
message: 'Unexpected default export of literal', | ||
}) | ||
return | ||
} | ||
|
||
if (node.declaration.type === 'FunctionDeclaration' && !node.declaration.id) { | ||
context.report({ | ||
node: node, | ||
message: 'Unexpected default export of anonymous function', | ||
}) | ||
return | ||
} | ||
|
||
if (node.declaration.type === 'ClassDeclaration' && !node.declaration.id) { | ||
context.report({ | ||
node: node, | ||
message: 'Unexpected default export of anonymous class', | ||
}) | ||
return | ||
} | ||
|
||
if (node.declaration.type === 'ArrowFunctionExpression') { | ||
context.report({ | ||
node: node, | ||
message: 'Unexpected default export of arrow function', | ||
}) | ||
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,24 @@ | ||
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 {}'}), | ||
|
||
...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 literal' }] }), | ||
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' }] }), | ||
], | ||
}) |