From 7a6145c1d6d7fdf8b9610756e6c0bc4aa65ad36d Mon Sep 17 00:00:00 2001 From: JounQin Date: Mon, 25 Mar 2024 12:17:08 +0800 Subject: [PATCH] fix: only add `dependencies` when no `exported` found (#67) --- .changeset/curly-files-dance.md | 5 ++ src/rules/group-exports.ts | 2 +- src/rules/namespace.ts | 4 +- src/rules/no-restricted-paths.ts | 2 +- src/rules/order.ts | 2 +- src/utils/export-map.ts | 10 ++-- test/rules/namespace.spec.ts | 81 +++++++++++++++----------------- 7 files changed, 54 insertions(+), 52 deletions(-) create mode 100644 .changeset/curly-files-dance.md diff --git a/.changeset/curly-files-dance.md b/.changeset/curly-files-dance.md new file mode 100644 index 000000000..d9a12b244 --- /dev/null +++ b/.changeset/curly-files-dance.md @@ -0,0 +1,5 @@ +--- +"eslint-plugin-import-x": patch +--- + +fix: only add `dependencies` when no `exported` found diff --git a/src/rules/group-exports.ts b/src/rules/group-exports.ts index ed0373e77..9d953a88f 100644 --- a/src/rules/group-exports.ts +++ b/src/rules/group-exports.ts @@ -112,7 +112,7 @@ export = createRule<[], 'ExportNamedDeclaration' | 'AssignmentExpression'>({ } }, - 'Program:exit': function onExit() { + 'Program:exit'() { // Report multiple `export` declarations (ES2015 modules) if (nodes.modules.set.size > 1) { for (const node of nodes.modules.set) { diff --git a/src/rules/namespace.ts b/src/rules/namespace.ts index c3d4ad4a7..0d880a96e 100644 --- a/src/rules/namespace.ts +++ b/src/rules/namespace.ts @@ -62,7 +62,7 @@ function processBodyStatement( case 'ImportDefaultSpecifier': case 'ImportSpecifier': { const meta = imports.get( - 'imported' in specifier && specifier.imported + 'imported' in specifier ? getValue(specifier.imported) : // default to 'default' for default 'default', @@ -135,7 +135,7 @@ export = createRule<[Options], MessageId>({ allowComputed: false, }, ], - create: function namespaceRule(context) { + create(context) { // read options const { allowComputed } = context.options[0] || {} diff --git a/src/rules/no-restricted-paths.ts b/src/rules/no-restricted-paths.ts index 31c30fd4f..833619e7a 100644 --- a/src/rules/no-restricted-paths.ts +++ b/src/rules/no-restricted-paths.ts @@ -113,7 +113,7 @@ export = createRule<[Options?], MessageId>({ }, }, defaultOptions: [], - create: function noRestrictedPaths(context) { + create(context) { const options = context.options[0] || {} const restrictedPaths = options.zones || [] const basePath = options.basePath || process.cwd() diff --git a/src/rules/order.ts b/src/rules/order.ts index e97bae669..a8891263b 100644 --- a/src/rules/order.ts +++ b/src/rules/order.ts @@ -1061,7 +1061,7 @@ export = createRule<[Options?], MessageId>({ pathGroupsExcludedImportTypes, ) }, - 'Program:exit': function reportAndReset() { + 'Program:exit'() { for (const imported of importMap.values()) { if (newlinesBetweenImports !== 'ignore') { makeNewlinesBetweenReport( diff --git a/src/utils/export-map.ts b/src/utils/export-map.ts index 526dd6d5b..01f78855e 100644 --- a/src/utils/export-map.ts +++ b/src/utils/export-map.ts @@ -442,13 +442,14 @@ export class ExportMap { } if (n.type === 'ExportAllDeclaration') { - const getter = captureDependency(n, n.exportKind === 'type') - if (getter) { - m.dependencies.add(getter) - } if (n.exported) { namespaces.set(n.exported.name, n.source.value) processSpecifier(n, n.exported, m) + } else { + const getter = captureDependency(n, n.exportKind === 'type') + if (getter) { + m.dependencies.add(getter) + } } continue } @@ -693,6 +694,7 @@ export class ExportMap { if (this.namespace.has(name)) { return true } + if (this.reexports.has(name)) { return true } diff --git a/test/rules/namespace.spec.ts b/test/rules/namespace.spec.ts index 3c7541a72..4af4e8df8 100644 --- a/test/rules/namespace.spec.ts +++ b/test/rules/namespace.spec.ts @@ -11,7 +11,6 @@ import { import rule from 'eslint-plugin-import-x/rules/namespace' const ruleTester = new TSESLint.RuleTester({ - parser: require.resolve('espree'), parserOptions: { env: { es6: true } }, }) @@ -401,11 +400,7 @@ const invalid = [ /////////////////////// // deep dereferences // ////////////////////// -for (const [folder, parser] of [ - ['deep'], - // FIXME: check and enable - // ['deep-es7', parsers.BABEL], -]) { +for (const [folder, parser] of [['deep'], ['deep-es7', parsers.BABEL]]) { // close over params valid.push( test({ @@ -428,45 +423,45 @@ for (const [folder, parser] of [ parser, code: `import { b } from "./${folder}/a"; var {c:{d:{e}}} = b`, }), + // deep namespaces should include explicitly exported defaults + test({ + parser, + code: `import * as a from "./${folder}/a"; console.log(a.b.default)`, + }), ) - // deep namespaces should include explicitly exported defaults - test({ - parser, - code: `import * as a from "./${folder}/a"; console.log(a.b.default)`, - }), - invalid.push( - test({ - parser, - code: `import * as a from "./${folder}/a"; console.log(a.b.e)`, - errors: ["'e' not found in deeply imported namespace 'a.b'."], - }), - test({ - parser, - code: `import { b } from "./${folder}/a"; console.log(b.e)`, - errors: ["'e' not found in imported namespace 'b'."], - }), - test({ - parser, - code: `import * as a from "./${folder}/a"; console.log(a.b.c.e)`, - errors: ["'e' not found in deeply imported namespace 'a.b.c'."], - }), - test({ - parser, - code: `import { b } from "./${folder}/a"; console.log(b.c.e)`, - errors: ["'e' not found in deeply imported namespace 'b.c'."], - }), - test({ - parser, - code: `import * as a from "./${folder}/a"; var {b:{ e }} = a`, - errors: ["'e' not found in deeply imported namespace 'a.b'."], - }), - test({ - parser, - code: `import * as a from "./${folder}/a"; var {b:{c:{ e }}} = a`, - errors: ["'e' not found in deeply imported namespace 'a.b.c'."], - }), - ) + invalid.push( + test({ + parser, + code: `import * as a from "./${folder}/a"; console.log(a.b.e)`, + errors: ["'e' not found in deeply imported namespace 'a.b'."], + }), + test({ + parser, + code: `import { b } from "./${folder}/a"; console.log(b.e)`, + errors: ["'e' not found in imported namespace 'b'."], + }), + test({ + parser, + code: `import * as a from "./${folder}/a"; console.log(a.b.c.e)`, + errors: ["'e' not found in deeply imported namespace 'a.b.c'."], + }), + test({ + parser, + code: `import { b } from "./${folder}/a"; console.log(b.c.e)`, + errors: ["'e' not found in deeply imported namespace 'b.c'."], + }), + test({ + parser, + code: `import * as a from "./${folder}/a"; var {b:{ e }} = a`, + errors: ["'e' not found in deeply imported namespace 'a.b'."], + }), + test({ + parser, + code: `import * as a from "./${folder}/a"; var {b:{c:{ e }}} = a`, + errors: ["'e' not found in deeply imported namespace 'a.b.c'."], + }), + ) } ruleTester.run('namespace', rule, { valid, invalid })