Skip to content

Commit

Permalink
Simplify rule logic, add new test, fix syntax error in docs
Browse files Browse the repository at this point in the history
  • Loading branch information
ntdb committed Oct 1, 2016
1 parent 016672b commit 0ec1789
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 33 deletions.
2 changes: 1 addition & 1 deletion docs/rules/no-named-default.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const bar = 'baz';
...these would be valid:
```js
import foo from './foo.js';
import foo { bar } from './foo.js';
import foo, { bar } from './foo.js';
```

...and these would be reported:
Expand Down
34 changes: 12 additions & 22 deletions src/rules/no-named-default.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,19 @@ module.exports = {
},

create: function (context) {
function checkSpecifiers(type, node) {
if (node.source == null) return

const hasImportSpecifier = node.specifiers.some(function (im) {
return im.type === type
})

if (!hasImportSpecifier) {
return
}

node.specifiers.forEach(function (im) {
if (im.type !== type) return

if (im.imported.name === 'default') {
context.report(im.local,
'Using name \'' + im.local.name +
'\' as identifier for default export.')
}
})
}
return {
'ImportDeclaration': checkSpecifiers.bind(null, 'ImportSpecifier'),
'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 ' +
'import \'' + im.local.name + '\'.' })
}
})
},
}
},
}
28 changes: 18 additions & 10 deletions tests/src/rules/no-named-default.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,31 @@ ruleTester.run('no-named-default', rule, {
test({code: 'import bar from "./bar";'}),
test({code: 'import bar, { foo } from "./bar";'}),

// es7
test({ code: 'import bar from "./bar";', parser: 'babel-eslint' }),
test({ code: 'import bar, { foo } from "./bar";', parser: 'babel-eslint' }),

...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: 'Using name \'bar\' as identifier for default export.'
, type: 'Identifier' } ] }),
errors: [{
message: 'Use default import syntax to import \'bar\'.',
type: 'Identifier',
}],
}),
test({
code: 'import { foo, default as bar } from "./bar";',
errors: [ {
message: 'Using name \'bar\' as identifier for default export.'
, type: 'Identifier' } ] }),
errors: [{
message: 'Use default import syntax to import \'bar\'.',
type: 'Identifier',
}],
}),
],
})

0 comments on commit 0ec1789

Please sign in to comment.