-
-
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-default
rule
#596
Changes from 8 commits
7c8e26e
c3a6026
72f882d
7c96d01
574e04a
b3bfe56
016672b
0ec1789
3ec2248
27836ba
db1343f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# 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 | ||
|
||
- *misleading*: consistent syntax usage is less likely to confuse new developers | ||
- *a mistake*: meant to import a named export and unintentionally specified `default` | ||
|
||
## 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'; | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module.exports = { | ||
meta: { | ||
docs: {}, | ||
}, | ||
|
||
create: function (context) { | ||
return { | ||
'ImportDeclaration': function (node) { | ||
if (node.source == null) return | ||
|
||
node.specifiers.forEach(function (im) { | ||
if (im.type === 'ImportSpecifier' && im.imported.name === 'default') { | ||
context.report({ | ||
node: im.local, | ||
message: 'Use default import syntax to ' + | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could use template literal syntax here by the way, makes it a bit simpler (and I think you're allowed to put this all on one line, but let me know if you aren't) `Use default import syntax to import '${im.local.name}'` There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good thought! I'm used to an 80-char line length, this is definitely under the project's max of 99. :) |
||
'import \'' + im.local.name + '\'.' }) | ||
} | ||
}) | ||
}, | ||
} | ||
}, | ||
} |
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', | ||
}], | ||
}), | ||
], | ||
}) |
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.
I'm curious, is there ever a case where the source is not defined? ❓
We can leave it in case you're not sure, but I don't when this would happen.
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.
TBH, this is here because other rules included it (
named
,no-deprecated
). It now seems likely that it's not needed here, will remove.