-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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 6a47f2d
Showing
7 changed files
with
115 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,25 @@ | ||
# 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 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
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,37 @@ | ||
/** | ||
* @fileoverview Rule to disallow anonymous default exports. | ||
* @author Duncan Beevers | ||
*/ | ||
|
||
module.exports = { | ||
meta: {}, | ||
|
||
create: function (context) { | ||
|
||
return { | ||
'ExportDefaultDeclaration': (node) => { | ||
if (!node.declaration) { | ||
return | ||
} | ||
|
||
// Export function | ||
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 | ||
} | ||
}, | ||
} | ||
|
||
}, | ||
} |
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,22 @@ | ||
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() {}'}), | ||
|
||
...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 true', errors: [{ message: 'Unexpected default export of literal' }] }), | ||
test({ code: 'export default function() {}', errors: [{ message: 'Unexpected default export of anonymous function' }] }), | ||
], | ||
}) |