Skip to content

Commit

Permalink
[Changed]: no-default-import - limit highlight to the word "default"
Browse files Browse the repository at this point in the history
  • Loading branch information
pmcelhaney committed Nov 10, 2021
1 parent 7c239fe commit 7cb7df9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
This change log adheres to standards from [Keep a CHANGELOG](https://keepachangelog.com).

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

## [2.25.3] - 2021-11-09

Expand Down
7 changes: 4 additions & 3 deletions src/rules/no-default-export.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,20 @@ module.exports = {
`Do not alias \`${local.name}\` as \`default\`. Just export ` +
`\`${local.name}\` itself instead.`;


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

ExportNamedDeclaration(node) {
node.specifiers.forEach(specifier => {
if (specifier.type === 'ExportDefaultSpecifier' &&
specifier.exported.name === 'default') {
context.report({ node, message: preferNamed });
context.report({ node, message: preferNamed, loc: context.getSourceCode().getFirstTokens(node)[1].loc });
} else if (specifier.type === 'ExportSpecifier' &&
specifier.exported.name === 'default') {
context.report({ node, message: noAliasDefault(specifier) });
context.report({ node, message: noAliasDefault(specifier), loc: context.getSourceCode().getFirstTokens(node)[1].loc });
}
});
},
Expand Down
31 changes: 31 additions & 0 deletions tests/src/rules/no-default-export.js
Original file line number Diff line number Diff line change
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 7cb7df9

Please sign in to comment.