Skip to content

Commit

Permalink
[Fix] no-default-import: report on the token "default" instead of t…
Browse files Browse the repository at this point in the history
…he entire node
  • Loading branch information
pmcelhaney authored and ljharb committed Nov 10, 2021
1 parent aeabc64 commit 34c6d16
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,9 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange

## [Unreleased]

### Changed
- [`no-default-import`]: report on the token "default" instead of the entire node ([#2299], [@pmcelhaney])

## [2.25.3] - 2021-11-09

### Fixed
Expand Down Expand Up @@ -942,6 +945,7 @@ for info on changes for earlier releases.

[`memo-parser`]: ./memo-parser/README.md

[#2299]: https://github.com/import-js/eslint-plugin-import/pull/2299
[#2297]: https://github.com/import-js/eslint-plugin-import/pull/2297
[#2287]: https://github.com/import-js/eslint-plugin-import/pull/2287
[#2282]: https://github.com/import-js/eslint-plugin-import/pull/2282
Expand Down
8 changes: 5 additions & 3 deletions src/rules/no-default-export.js
Expand Up @@ -20,15 +20,17 @@ module.exports = {

return {
ExportDefaultDeclaration(node) {
context.report({ node, message: preferNamed });
const { loc } = context.getSourceCode().getFirstTokens(node)[1];
context.report({ node, message: preferNamed, loc });
},

ExportNamedDeclaration(node) {
node.specifiers.filter(specifier => specifier.exported.name === 'default').forEach(specifier => {
const { loc } = context.getSourceCode().getFirstTokens(node)[1];
if (specifier.type === 'ExportDefaultSpecifier') {
context.report({ node, message: preferNamed });
context.report({ node, message: preferNamed, loc });
} else if (specifier.type === 'ExportSpecifier') {
context.report({ node, message: noAliasDefault(specifier) });
context.report({ node, message: noAliasDefault(specifier), loc });
}
});
},
Expand Down
31 changes: 31 additions & 0 deletions tests/src/rules/no-default-export.js
Expand Up @@ -91,6 +91,8 @@ ruleTester.run('no-default-export', rule, {
errors: [{
type: 'ExportDefaultDeclaration',
message: 'Prefer named exports.',
line: 1,
column: 8,
}],
}),
test({
Expand All @@ -100,6 +102,35 @@ ruleTester.run('no-default-export', rule, {
errors: [{
type: 'ExportDefaultDeclaration',
message: 'Prefer named exports.',
line: 3,
column: 16,
}],
}),
test({
code: 'export default class Bar {};',
errors: [{
type: 'ExportDefaultDeclaration',
message: 'Prefer named exports.',
line: 1,
column: 8,
}],
}),
test({
code: 'export default function() {};',
errors: [{
type: 'ExportDefaultDeclaration',
message: 'Prefer named exports.',
line: 1,
column: 8,
}],
}),
test({
code: 'export default class {};',
errors: [{
type: 'ExportDefaultDeclaration',
message: 'Prefer named exports.',
line: 1,
column: 8,
}],
}),
test({
Expand Down

0 comments on commit 34c6d16

Please sign in to comment.