-
-
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-default-export
+ docs/tests (#936)
- Loading branch information
1 parent
ff3d883
commit 5b0777d
Showing
5 changed files
with
224 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# no-default-export | ||
|
||
Prohibit default exports. Mostly an inverse of [`prefer-default-export`]. | ||
|
||
[`prefer-default-export`]: ./prefer-default-export.md | ||
|
||
## Rule Details | ||
|
||
The following patterns are considered warnings: | ||
|
||
```javascript | ||
// bad1.js | ||
|
||
// There is a default export. | ||
export const foo = 'foo'; | ||
const bar = 'bar'; | ||
export default 'bar'; | ||
``` | ||
|
||
```javascript | ||
// bad2.js | ||
|
||
// There is a default export. | ||
const foo = 'foo'; | ||
export { foo as default } | ||
``` | ||
|
||
The following patterns are not warnings: | ||
|
||
```javascript | ||
// good1.js | ||
|
||
// There is only a single module export and it's a named export. | ||
export const foo = 'foo'; | ||
``` | ||
|
||
```javascript | ||
// good2.js | ||
|
||
// There is more than one named export in the module. | ||
export const foo = 'foo'; | ||
export const bar = 'bar'; | ||
``` | ||
|
||
```javascript | ||
// good3.js | ||
|
||
// There is more than one named export in the module | ||
const foo = 'foo'; | ||
const bar = 'bar'; | ||
export { foo, bar } | ||
``` | ||
|
||
```javascript | ||
// export-star.js | ||
|
||
// Any batch export will disable this rule. The remote module is not inspected. | ||
export * from './other-module' | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
If you don't care if default imports are used, or if you prefer default imports over named 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,35 @@ | ||
module.exports = { | ||
meta: { | ||
docs: {}, | ||
}, | ||
|
||
create(context) { | ||
// ignore non-modules | ||
if (context.parserOptions.sourceType !== 'module') { | ||
return {} | ||
} | ||
|
||
const preferNamed = 'Prefer named exports.' | ||
const noAliasDefault = ({local}) => | ||
`Do not alias \`${local.name}\` as \`default\`. Just export ` + | ||
`\`${local.name}\` itself instead.` | ||
|
||
return { | ||
ExportDefaultDeclaration(node) { | ||
context.report({node, message: preferNamed}) | ||
}, | ||
|
||
ExportNamedDeclaration(node) { | ||
node.specifiers.forEach(specifier => { | ||
if (specifier.type === 'ExportDefaultSpecifier' && | ||
specifier.exported.name === 'default') { | ||
context.report({node, message: preferNamed}) | ||
} else if (specifier.type === 'ExportSpecifier' && | ||
specifier.exported.name === 'default') { | ||
context.report({node, message: noAliasDefault(specifier)}) | ||
} | ||
}) | ||
}, | ||
} | ||
}, | ||
} |
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,121 @@ | ||
import { test } from '../utils' | ||
|
||
import { RuleTester } from 'eslint' | ||
|
||
const ruleTester = new RuleTester() | ||
, rule = require('rules/no-default-export') | ||
|
||
ruleTester.run('no-default-export', rule, { | ||
valid: [ | ||
test({ | ||
code: ` | ||
export const foo = 'foo'; | ||
export const bar = 'bar'; | ||
`, | ||
}), | ||
test({ | ||
code: ` | ||
export const foo = 'foo'; | ||
export function bar() {}; | ||
`, | ||
}), | ||
test({ | ||
code: `export const foo = 'foo';`, | ||
}), | ||
test({ | ||
code: ` | ||
const foo = 'foo'; | ||
export { foo }; | ||
`, | ||
}), | ||
test({ | ||
code: `export { foo, bar }`, | ||
}), | ||
test({ | ||
code: `export const { foo, bar } = item;`, | ||
}), | ||
test({ | ||
code: `export const { foo, bar: baz } = item;`, | ||
}), | ||
test({ | ||
code: `export const { foo: { bar, baz } } = item;`, | ||
}), | ||
test({ | ||
code: ` | ||
export const foo = item; | ||
export { item }; | ||
`, | ||
}), | ||
test({ | ||
code: `export * from './foo';`, | ||
}), | ||
test({ | ||
code: `export const { foo } = { foo: "bar" };`, | ||
}), | ||
test({ | ||
code: `export const { foo: { bar } } = { foo: { bar: "baz" } };`, | ||
}), | ||
test({ | ||
code: 'export { a, b } 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';`, | ||
}), | ||
|
||
test({ | ||
code: `export type UserId = number;`, | ||
parser: 'babel-eslint', | ||
}), | ||
test({ | ||
code: 'export foo from "foo.js"', | ||
parser: 'babel-eslint', | ||
}), | ||
test({ | ||
code: `export Memory, { MemoryValue } from './Memory'`, | ||
parser: 'babel-eslint', | ||
}), | ||
], | ||
invalid: [ | ||
test({ | ||
code: 'export default function bar() {};', | ||
errors: [{ | ||
ruleId: 'ExportDefaultDeclaration', | ||
message: 'Prefer named exports.', | ||
}], | ||
}), | ||
test({ | ||
code: ` | ||
export const foo = 'foo'; | ||
export default bar;`, | ||
errors: [{ | ||
ruleId: 'ExportDefaultDeclaration', | ||
message: 'Prefer named exports.', | ||
}], | ||
}), | ||
test({ | ||
code: 'export { foo as default }', | ||
errors: [{ | ||
ruleId: 'ExportNamedDeclaration', | ||
message: 'Do not alias `foo` as `default`. Just export `foo` itself ' + | ||
'instead.', | ||
}], | ||
}), | ||
test({ | ||
code: 'export default from "foo.js"', | ||
parser: 'babel-eslint', | ||
errors: [{ | ||
ruleId: 'ExportNamedDeclaration', | ||
message: 'Prefer named exports.', | ||
}], | ||
}), | ||
], | ||
}) |