Skip to content

Commit 7eacf22

Browse files
pmcelhaneyljharb
authored andcommitted
[Fix] no-default-import: report on the token "default" instead of the entire node
1 parent ceb5fe1 commit 7eacf22

File tree

4 files changed

+79
-29
lines changed

4 files changed

+79
-29
lines changed

Diff for: CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
66

77
## [Unreleased]
88

9+
### Changed
10+
- [`no-default-import`]: report on the token "default" instead of the entire node ([#2299], [@pmcelhaney])
11+
912
## [2.25.3] - 2021-11-09
1013

1114
### Fixed
@@ -942,6 +945,7 @@ for info on changes for earlier releases.
942945

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

948+
[#2299]: https://github.com/import-js/eslint-plugin-import/pull/2299
945949
[#2297]: https://github.com/import-js/eslint-plugin-import/pull/2297
946950
[#2287]: https://github.com/import-js/eslint-plugin-import/pull/2287
947951
[#2282]: https://github.com/import-js/eslint-plugin-import/pull/2282

Diff for: src/rules/no-default-export.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,17 @@ module.exports = {
2020

2121
return {
2222
ExportDefaultDeclaration(node) {
23-
context.report({ node, message: preferNamed });
23+
const { loc } = context.getSourceCode().getFirstTokens(node)[1] || {};
24+
context.report({ node, message: preferNamed, loc });
2425
},
2526

2627
ExportNamedDeclaration(node) {
2728
node.specifiers.filter(specifier => specifier.exported.name === 'default').forEach(specifier => {
29+
const { loc } = context.getSourceCode().getFirstTokens(node)[1] || {};
2830
if (specifier.type === 'ExportDefaultSpecifier') {
29-
context.report({ node, message: preferNamed });
31+
context.report({ node, message: preferNamed, loc });
3032
} else if (specifier.type === 'ExportSpecifier') {
31-
context.report({ node, message: noAliasDefault(specifier) });
33+
context.report({ node, message: noAliasDefault(specifier), loc });
3234
}
3335
});
3436
},

Diff for: tests/src/rules/no-default-export.js

+69-25
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { test } from '../utils';
1+
import { test, testVersion } from '../utils';
22

33
import { RuleTester } from 'eslint';
44

@@ -85,38 +85,82 @@ ruleTester.run('no-default-export', rule, {
8585
parser: require.resolve('babel-eslint'),
8686
}),
8787
],
88-
invalid: [
89-
test({
88+
invalid: [].concat(
89+
testVersion('> 2', () => ({
9090
code: 'export default function bar() {};',
91-
errors: [{
92-
type: 'ExportDefaultDeclaration',
93-
message: 'Prefer named exports.',
94-
}],
95-
}),
96-
test({
91+
errors: [
92+
{
93+
type: 'ExportDefaultDeclaration',
94+
message: 'Prefer named exports.',
95+
line: 1,
96+
column: 8,
97+
},
98+
],
99+
})),
100+
testVersion('> 2', () => ({
97101
code: `
98102
export const foo = 'foo';
99103
export default bar;`,
100-
errors: [{
101-
type: 'ExportDefaultDeclaration',
102-
message: 'Prefer named exports.',
103-
}],
104-
}),
104+
errors: [
105+
{
106+
type: 'ExportDefaultDeclaration',
107+
message: 'Prefer named exports.',
108+
line: 3,
109+
column: 16,
110+
},
111+
],
112+
})),
113+
testVersion('> 2', () => ({
114+
code: 'export default class Bar {};',
115+
errors: [
116+
{
117+
type: 'ExportDefaultDeclaration',
118+
message: 'Prefer named exports.',
119+
line: 1,
120+
column: 8,
121+
},
122+
],
123+
})),
124+
testVersion('> 2', () => ({
125+
code: 'export default function() {};',
126+
errors: [
127+
{
128+
type: 'ExportDefaultDeclaration',
129+
message: 'Prefer named exports.',
130+
line: 1,
131+
column: 8,
132+
},
133+
],
134+
})),
135+
testVersion('> 2', () => ({
136+
code: 'export default class {};',
137+
errors: [
138+
{
139+
type: 'ExportDefaultDeclaration',
140+
message: 'Prefer named exports.',
141+
line: 1,
142+
column: 8,
143+
},
144+
],
145+
})),
105146
test({
106147
code: 'let foo; export { foo as default }',
107-
errors: [{
108-
type: 'ExportNamedDeclaration',
109-
message: 'Do not alias `foo` as `default`. Just export `foo` itself ' +
110-
'instead.',
111-
}],
148+
errors: [
149+
{
150+
type: 'ExportNamedDeclaration',
151+
message: 'Do not alias `foo` as `default`. Just export `foo` itself instead.',
152+
},
153+
],
112154
}),
113155
test({
114156
code: 'export default from "foo.js"',
115157
parser: require.resolve('babel-eslint'),
116-
errors: [{
117-
type: 'ExportNamedDeclaration',
118-
message: 'Prefer named exports.',
119-
}],
120-
}),
121-
],
158+
errors: [
159+
{
160+
type: 'ExportNamedDeclaration',
161+
message: 'Prefer named exports.',
162+
},
163+
],
164+
}),
165+
),
122166
});

Diff for: tests/src/utils.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export function getNonDefaultParsers() {
2828
export const FILENAME = testFilePath('foo.js');
2929

3030
export function testVersion(specifier, t) {
31-
return semver.satisfies(eslintPkg.version, specifier) && test(t());
31+
return semver.satisfies(eslintPkg.version, specifier) ? test(t()) : [];
3232
}
3333

3434
export function test(t) {

0 commit comments

Comments
 (0)