diff --git a/docs/rules/no-named-as-default.md b/docs/rules/no-named-as-default.md index 043d69942..0acc7f296 100644 --- a/docs/rules/no-named-as-default.md +++ b/docs/rules/no-named-as-default.md @@ -44,6 +44,18 @@ export foo from './foo.js'; export bar from './foo.js'; ``` +## Options + +This rule has an option for ignoring specific paths. This is useful for cases where the default export is aliased to a named export, or where the default export is not used. + +```json +{ + "rules": { + "import/no-named-as-default": ["warn", { "ignorePaths": ["./foo.js"] }] + } +} +``` + ## Further Reading - ECMAScript Proposal: [export ns from] diff --git a/src/rules/no-named-as-default.js b/src/rules/no-named-as-default.js index dacd294f4..4ac0db050 100644 --- a/src/rules/no-named-as-default.js +++ b/src/rules/no-named-as-default.js @@ -1,5 +1,6 @@ import ExportMapBuilder from '../exportMap/builder'; import importDeclaration from '../importDeclaration'; +import includes from 'array-includes'; import docsUrl from '../docsUrl'; module.exports = { @@ -10,10 +11,27 @@ module.exports = { description: 'Forbid use of exported name as identifier of default export.', url: docsUrl('no-named-as-default'), }, - schema: [], + schema: [ + { + type: 'object', + properties: { + ignorePaths: { + type: 'array', + items: { + type: 'string', + }, + uniqueItems: true, + }, + }, + additionalProperties: false, + }, + ], }, create(context) { + const options = context.options[0] || {}; + const ignorePaths = options.ignorePaths || []; + function checkDefault(nameKey, defaultSpecifier) { /** * For ImportDefaultSpecifier we're interested in the "local" name (`foo` for `import {bar as foo} ...`) @@ -45,6 +63,11 @@ module.exports = { return; } + if (includes(ignorePaths, declaration.source.value)) { + // The user has explicitly ignored this path + return; + } + /** * FIXME: We can verify if a default and a named export are pointing to the same symbol only * if they are both `reexports`. In case one of the symbols is not a re-export, but defined diff --git a/tests/src/rules/no-named-as-default.js b/tests/src/rules/no-named-as-default.js index 349372067..d191a7754 100644 --- a/tests/src/rules/no-named-as-default.js +++ b/tests/src/rules/no-named-as-default.js @@ -47,6 +47,12 @@ ruleTester.run('no-named-as-default', rule, { test({ code: 'import variable from "./no-named-as-default/misleading-re-exports.js";', }), + test({ + code: 'import foo from "./bar";', + options: [{ + ignorePaths: ['./bar'], + }], + }), test({ // incorrect import code: 'import foobar from "./no-named-as-default/no-default-export.js";',