Skip to content

Commit

Permalink
fix: only add dependencies when no exported found (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin authored Mar 25, 2024
1 parent cdd6344 commit 7a6145c
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 52 deletions.
5 changes: 5 additions & 0 deletions .changeset/curly-files-dance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-import-x": patch
---

fix: only add `dependencies` when no `exported` found
2 changes: 1 addition & 1 deletion src/rules/group-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/rules/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -135,7 +135,7 @@ export = createRule<[Options], MessageId>({
allowComputed: false,
},
],
create: function namespaceRule(context) {
create(context) {
// read options
const { allowComputed } = context.options[0] || {}

Expand Down
2 changes: 1 addition & 1 deletion src/rules/no-restricted-paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion src/rules/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
10 changes: 6 additions & 4 deletions src/utils/export-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -693,6 +694,7 @@ export class ExportMap {
if (this.namespace.has(name)) {
return true
}

if (this.reexports.has(name)) {
return true
}
Expand Down
81 changes: 38 additions & 43 deletions test/rules/namespace.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 } },
})

Expand Down Expand Up @@ -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({
Expand All @@ -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 })

0 comments on commit 7a6145c

Please sign in to comment.