-
-
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 empty `no-named-default` rule and tests (failing) * Functioning `no-named-default` rule with tests * Document `no-named-default` rule * Add `no-named-default` to README * Simplify docs * Add `no-named-default` note to CHANGELOG * Remove unnecessary check for `name` key * Simplify rule logic, add new test, fix syntax error in docs * Remove source check, use template literal syntax for message * Simplify rationale in docs
- Loading branch information
Showing
6 changed files
with
93 additions
and
2 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,27 @@ | ||
# no-named-default | ||
|
||
Reports use of a default export as a locally named import. | ||
|
||
Rationale: the syntax exists to import default exports expressively, let's use it. | ||
|
||
## Rule Details | ||
|
||
Given: | ||
```js | ||
// foo.js | ||
export default 'foo'; | ||
export const bar = 'baz'; | ||
``` | ||
|
||
...these would be valid: | ||
```js | ||
import foo from './foo.js'; | ||
import foo, { bar } from './foo.js'; | ||
``` | ||
|
||
...and these would be reported: | ||
```js | ||
// message: Using exported name 'bar' as identifier for default export. | ||
import { default as foo } from './foo.js'; | ||
import { default as foo, bar } from './foo.js'; | ||
``` |
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,19 @@ | ||
module.exports = { | ||
meta: { | ||
docs: {}, | ||
}, | ||
|
||
create: function (context) { | ||
return { | ||
'ImportDeclaration': function (node) { | ||
node.specifiers.forEach(function (im) { | ||
if (im.type === 'ImportSpecifier' && im.imported.name === 'default') { | ||
context.report({ | ||
node: im.local, | ||
message: `Use default import syntax to import \'${im.local.name}\'.` }) | ||
} | ||
}) | ||
}, | ||
} | ||
}, | ||
} |
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,39 @@ | ||
import { test, SYNTAX_CASES } from '../utils' | ||
import { RuleTester } from 'eslint' | ||
|
||
const ruleTester = new RuleTester() | ||
, rule = require('rules/no-named-default') | ||
|
||
ruleTester.run('no-named-default', rule, { | ||
valid: [ | ||
test({code: 'import bar from "./bar";'}), | ||
test({code: 'import bar, { foo } from "./bar";'}), | ||
|
||
...SYNTAX_CASES, | ||
], | ||
|
||
invalid: [ | ||
test({ | ||
code: 'import { default } from "./bar";', | ||
errors: [{ | ||
message: 'Use default import syntax to import \'default\'.', | ||
type: 'Identifier', | ||
}], | ||
parser: 'babel-eslint', | ||
}), | ||
test({ | ||
code: 'import { default as bar } from "./bar";', | ||
errors: [{ | ||
message: 'Use default import syntax to import \'bar\'.', | ||
type: 'Identifier', | ||
}], | ||
}), | ||
test({ | ||
code: 'import { foo, default as bar } from "./bar";', | ||
errors: [{ | ||
message: 'Use default import syntax to import \'bar\'.', | ||
type: 'Identifier', | ||
}], | ||
}), | ||
], | ||
}) |