-
-
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.
- Loading branch information
Showing
6 changed files
with
138 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,51 @@ | ||
# prefer-default-export | ||
|
||
When there is only a single export from a module prefer using default export over named export. | ||
|
||
## Rule Details | ||
|
||
The following patterns are considered warnings: | ||
|
||
```javascript | ||
// bad.js | ||
|
||
// There is only a single module export and its a named export. | ||
export const foo = 'foo'; | ||
|
||
``` | ||
|
||
The following patterns are not warnings: | ||
|
||
```javascript | ||
// good1.js | ||
|
||
// There is a default export. | ||
export const foo = 'foo'; | ||
const bar = 'bar'; | ||
export default 'bar'; | ||
``` | ||
|
||
```javascript | ||
// good2.js | ||
|
||
// There is more thank one named exports in the module. | ||
export const foo = 'foo'; | ||
export const bar = 'bar'; | ||
``` | ||
|
||
```javascript | ||
// good3.js | ||
|
||
// There is more thank one named exports in the module | ||
const foo = 'foo'; | ||
const bar = 'bar'; | ||
export { foo, bar } | ||
``` | ||
|
||
```javascript | ||
// good4.js | ||
|
||
// There is a default export. | ||
const foo = 'foo'; | ||
export { foo as default } | ||
``` |
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,31 @@ | ||
'use strict' | ||
|
||
module.exports = function(context) { | ||
let namedExportCount = 0 | ||
let specifierExportCount = 0 | ||
let hasDefaultExport = false | ||
let namedExportNode = null | ||
return { | ||
'ExportSpecifier': function(node) { | ||
if (node.exported.name === 'default') { | ||
hasDefaultExport = true | ||
} else { | ||
specifierExportCount++ | ||
namedExportNode = node | ||
} | ||
}, | ||
'ExportNamedDeclaration': function(node) { | ||
namedExportCount++ | ||
namedExportNode = node | ||
}, | ||
'ExportDefaultDeclaration': function() { | ||
hasDefaultExport = true | ||
}, | ||
|
||
'Program:exit': function() { | ||
if (namedExportCount === 1 && specifierExportCount < 2 && !hasDefaultExport) { | ||
context.report(namedExportNode, 'Prefer default export.') | ||
} | ||
}, | ||
} | ||
} |
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 @@ | ||
import { test } from '../utils' | ||
|
||
import { RuleTester } from 'eslint' | ||
|
||
const ruleTester = new RuleTester() | ||
, rule = require('rules/prefer-default-export') | ||
|
||
ruleTester.run('prefer-default-export', rule, { | ||
valid: [ | ||
test({ | ||
code: ` | ||
export const foo = 'foo'; | ||
export const bar = 'bar';`, | ||
}), | ||
test({ | ||
code: ` | ||
export const foo = 'foo'; | ||
export default bar;`, | ||
}), | ||
test({ | ||
code: ` | ||
export { foo, bar }`, | ||
}), | ||
test({ | ||
code: ` | ||
export { foo as default }`, | ||
}), | ||
], | ||
invalid: [ | ||
test({ | ||
code: ` | ||
export const foo = 'foo';`, | ||
errors: [{ | ||
ruleId: 'ExportNamedDeclaration', | ||
message: 'Prefer default export.', | ||
}], | ||
}), | ||
test({ | ||
code: ` | ||
const foo = 'foo'; | ||
export { foo };`, | ||
errors: [{ | ||
ruleId: 'ExportNamedDeclaration', | ||
message: 'Prefer default export.', | ||
}], | ||
}), | ||
], | ||
}) |