diff --git a/CHANGELOG.md b/CHANGELOG.md index 0d4d920a4c..d6cca181e4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/rules/no-default-export.js b/src/rules/no-default-export.js index cb7c0bb724..c9cfbb52ad 100644 --- a/src/rules/no-default-export.js +++ b/src/rules/no-default-export.js @@ -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 }); } }); }, diff --git a/tests/src/rules/no-default-export.js b/tests/src/rules/no-default-export.js index bc0119a019..55ca65ba46 100644 --- a/tests/src/rules/no-default-export.js +++ b/tests/src/rules/no-default-export.js @@ -91,6 +91,8 @@ ruleTester.run('no-default-export', rule, { errors: [{ type: 'ExportDefaultDeclaration', message: 'Prefer named exports.', + line: 1, + column: 8, }], }), test({ @@ -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({