From 32173f1a8db0707b2783518659d13995cfd0fb6e Mon Sep 17 00:00:00 2001 From: Sahar Levy Date: Thu, 26 Dec 2024 12:18:35 +0200 Subject: [PATCH 1/3] Add ignorePaths option to no-named-as-default include tests and doc update --- docs/rules/no-named-as-default.md | 12 ++++++++++++ src/rules/no-named-as-default.js | 24 +++++++++++++++++++++++- tests/src/rules/no-named-as-default.js | 6 ++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/docs/rules/no-named-as-default.md b/docs/rules/no-named-as-default.md index 043d69942..b4c4845b6 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", { "ignore": ["./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..6c0324ad4 100644 --- a/src/rules/no-named-as-default.js +++ b/src/rules/no-named-as-default.js @@ -10,10 +10,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 +62,11 @@ module.exports = { return; } + if (ignorePaths.includes(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";', From 51ad0330014f121c7c912757456f88e22a11888f Mon Sep 17 00:00:00 2001 From: Sahar Levy Date: Sun, 29 Dec 2024 10:48:26 +0200 Subject: [PATCH 2/3] use array-includes instead of includes --- src/rules/no-named-as-default.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/rules/no-named-as-default.js b/src/rules/no-named-as-default.js index 6c0324ad4..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 = { @@ -62,7 +63,7 @@ module.exports = { return; } - if (ignorePaths.includes(declaration.source.value)) { + if (includes(ignorePaths, declaration.source.value)) { // The user has explicitly ignored this path return; } From 5b3c3f34df47d21014e61914032905ea3bc84832 Mon Sep 17 00:00:00 2001 From: Sahar Levy <43012075+Sahar541998@users.noreply.github.com> Date: Tue, 31 Dec 2024 16:31:01 +0200 Subject: [PATCH 3/3] Update docs/rules/no-named-as-default.md --- docs/rules/no-named-as-default.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/rules/no-named-as-default.md b/docs/rules/no-named-as-default.md index b4c4845b6..0acc7f296 100644 --- a/docs/rules/no-named-as-default.md +++ b/docs/rules/no-named-as-default.md @@ -51,7 +51,7 @@ This rule has an option for ignoring specific paths. This is useful for cases wh ```json { "rules": { - "import/no-named-as-default": ["warn", { "ignore": ["./foo.js"] }] + "import/no-named-as-default": ["warn", { "ignorePaths": ["./foo.js"] }] } } ```