-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add no-named-export
+ docs/tests
#1157
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# no-named-export | ||
|
||
Prohibit named exports. Mostly an inverse of [`no-default-export`]. | ||
|
||
[`no-default-export`]: ./no-default-export.md | ||
|
||
## Rule Details | ||
|
||
The following patterns are considered warnings: | ||
|
||
```javascript | ||
// bad1.js | ||
|
||
// There is only a single module export and it's a named export. | ||
export const foo = 'foo'; | ||
``` | ||
|
||
```javascript | ||
// bad2.js | ||
|
||
// There is more than one named export in the module. | ||
export const foo = 'foo'; | ||
export const bar = 'bar'; | ||
``` | ||
|
||
```javascript | ||
// bad3.js | ||
|
||
// There is more than one named export in the module. | ||
const foo = 'foo'; | ||
const bar = 'bar'; | ||
export { foo, bar } | ||
``` | ||
|
||
```javascript | ||
// bad4.js | ||
|
||
// There is more than one named export in the module. | ||
export * from './other-module' | ||
``` | ||
|
||
```javascript | ||
// bad5.js | ||
|
||
// There is a default and a named export. | ||
export const foo = 'foo'; | ||
const bar = 'bar'; | ||
export default 'bar'; | ||
``` | ||
|
||
The following patterns are not warnings: | ||
|
||
```javascript | ||
// good1.js | ||
|
||
// There is only a single module export and it's a default export. | ||
export default 'bar'; | ||
``` | ||
|
||
```javascript | ||
// good2.js | ||
|
||
// There is only a single module export and it's a default export. | ||
const foo = 'foo'; | ||
export { foo as default } | ||
``` | ||
|
||
```javascript | ||
// good3.js | ||
|
||
// There is only a single module export and it's a default export. | ||
export default from './other-module'; | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
If you don't care if named imports are used, or if you prefer named imports over default imports. |
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,33 @@ | ||
import docsUrl from '../docsUrl' | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { url: docsUrl('no-named-export') }, | ||
}, | ||
|
||
create(context) { | ||
// ignore non-modules | ||
if (context.parserOptions.sourceType !== 'module') { | ||
return {} | ||
} | ||
|
||
const preferDefault = 'Prefer default export.' | ||
|
||
return { | ||
ExportAllDeclaration(node) { | ||
context.report({node, message: preferDefault}) | ||
}, | ||
|
||
ExportNamedDeclaration(node) { | ||
if (node.specifiers.length === 0) { | ||
return context.report({node, message: preferDefault}) | ||
} | ||
|
||
const someNamed = node.specifiers.some(specifier => specifier.exported.name !== 'default') | ||
if (someNamed) { | ||
context.report({node, message: preferDefault}) | ||
} | ||
}, | ||
} | ||
}, | ||
} |
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,179 @@ | ||
import { RuleTester } from 'eslint' | ||
import { test } from '../utils' | ||
|
||
const ruleTester = new RuleTester() | ||
, rule = require('rules/no-named-export') | ||
|
||
ruleTester.run('no-named-export', rule, { | ||
valid: [ | ||
test({ | ||
code: 'export default function bar() {};', | ||
}), | ||
test({ | ||
code: 'export { foo as default }', | ||
}), | ||
test({ | ||
code: 'export default from "foo.js"', | ||
parser: 'babel-eslint', | ||
}), | ||
|
||
// no exports at all | ||
test({ | ||
code: `import * as foo from './foo';`, | ||
}), | ||
test({ | ||
code: `import foo from './foo';`, | ||
}), | ||
test({ | ||
code: `import {default as foo} from './foo';`, | ||
}), | ||
], | ||
invalid: [ | ||
test({ | ||
code: ` | ||
export const foo = 'foo'; | ||
export const bar = 'bar'; | ||
`, | ||
errors: [{ | ||
ruleId: 'ExportNamedDeclaration', | ||
message: 'Prefer default export.', | ||
}, { | ||
ruleId: 'ExportNamedDeclaration', | ||
message: 'Prefer default export.', | ||
}], | ||
}), | ||
test({ | ||
code: ` | ||
export const foo = 'foo'; | ||
export default bar;`, | ||
errors: [{ | ||
ruleId: 'ExportNamedDeclaration', | ||
message: 'Prefer default export.', | ||
}], | ||
}), | ||
test({ | ||
code: ` | ||
export const foo = 'foo'; | ||
export function bar() {}; | ||
`, | ||
errors: [{ | ||
ruleId: 'ExportNamedDeclaration', | ||
message: 'Prefer default export.', | ||
}, { | ||
ruleId: 'ExportNamedDeclaration', | ||
message: 'Prefer default export.', | ||
}], | ||
}), | ||
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.', | ||
}], | ||
}), | ||
test({ | ||
code: `export { foo, bar }`, | ||
errors: [{ | ||
ruleId: 'ExportNamedDeclaration', | ||
message: 'Prefer default export.', | ||
}], | ||
}), | ||
test({ | ||
code: `export const { foo, bar } = item;`, | ||
errors: [{ | ||
ruleId: 'ExportNamedDeclaration', | ||
message: 'Prefer default export.', | ||
}], | ||
}), | ||
test({ | ||
code: `export const { foo, bar: baz } = item;`, | ||
errors: [{ | ||
ruleId: 'ExportNamedDeclaration', | ||
message: 'Prefer default export.', | ||
}], | ||
}), | ||
test({ | ||
code: `export const { foo: { bar, baz } } = item;`, | ||
errors: [{ | ||
ruleId: 'ExportNamedDeclaration', | ||
message: 'Prefer default export.', | ||
}], | ||
}), | ||
test({ | ||
code: ` | ||
export const foo = item; | ||
export { item }; | ||
`, | ||
errors: [{ | ||
ruleId: 'ExportNamedDeclaration', | ||
message: 'Prefer default export.', | ||
}, { | ||
ruleId: 'ExportNamedDeclaration', | ||
message: 'Prefer default export.', | ||
}], | ||
}), | ||
test({ | ||
code: `export * from './foo';`, | ||
errors: [{ | ||
ruleId: 'ExportAllDeclaration', | ||
message: 'Prefer default export.', | ||
}], | ||
}), | ||
test({ | ||
code: `export const { foo } = { foo: "bar" };`, | ||
errors: [{ | ||
ruleId: 'ExportNamedDeclaration', | ||
message: 'Prefer default export.', | ||
}], | ||
}), | ||
test({ | ||
code: `export const { foo: { bar } } = { foo: { bar: "baz" } };`, | ||
errors: [{ | ||
ruleId: 'ExportNamedDeclaration', | ||
message: 'Prefer default export.', | ||
}], | ||
}), | ||
test({ | ||
code: 'export { a, b } from "foo.js"', | ||
parser: 'babel-eslint', | ||
errors: [{ | ||
ruleId: 'ExportNamedDeclaration', | ||
message: 'Prefer default export.', | ||
}], | ||
}), | ||
test({ | ||
code: `export type UserId = number;`, | ||
parser: 'babel-eslint', | ||
errors: [{ | ||
ruleId: 'ExportNamedDeclaration', | ||
message: 'Prefer default export.', | ||
}], | ||
}), | ||
test({ | ||
code: 'export foo from "foo.js"', | ||
parser: 'babel-eslint', | ||
errors: [{ | ||
ruleId: 'ExportNamedDeclaration', | ||
message: 'Prefer default export.', | ||
}], | ||
}), | ||
test({ | ||
code: `export Memory, { MemoryValue } from './Memory'`, | ||
parser: 'babel-eslint', | ||
errors: [{ | ||
ruleId: 'ExportNamedDeclaration', | ||
message: 'Prefer default export.', | ||
}], | ||
}), | ||
], | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This message could be a bit stricter.