Skip to content

Commit 7a6145c

Browse files
authored
fix: only add dependencies when no exported found (#67)
1 parent cdd6344 commit 7a6145c

File tree

7 files changed

+54
-52
lines changed

7 files changed

+54
-52
lines changed

.changeset/curly-files-dance.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-plugin-import-x": patch
3+
---
4+
5+
fix: only add `dependencies` when no `exported` found

src/rules/group-exports.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export = createRule<[], 'ExportNamedDeclaration' | 'AssignmentExpression'>({
112112
}
113113
},
114114

115-
'Program:exit': function onExit() {
115+
'Program:exit'() {
116116
// Report multiple `export` declarations (ES2015 modules)
117117
if (nodes.modules.set.size > 1) {
118118
for (const node of nodes.modules.set) {

src/rules/namespace.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ function processBodyStatement(
6262
case 'ImportDefaultSpecifier':
6363
case 'ImportSpecifier': {
6464
const meta = imports.get(
65-
'imported' in specifier && specifier.imported
65+
'imported' in specifier
6666
? getValue(specifier.imported)
6767
: // default to 'default' for default
6868
'default',
@@ -135,7 +135,7 @@ export = createRule<[Options], MessageId>({
135135
allowComputed: false,
136136
},
137137
],
138-
create: function namespaceRule(context) {
138+
create(context) {
139139
// read options
140140
const { allowComputed } = context.options[0] || {}
141141

src/rules/no-restricted-paths.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ export = createRule<[Options?], MessageId>({
113113
},
114114
},
115115
defaultOptions: [],
116-
create: function noRestrictedPaths(context) {
116+
create(context) {
117117
const options = context.options[0] || {}
118118
const restrictedPaths = options.zones || []
119119
const basePath = options.basePath || process.cwd()

src/rules/order.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1061,7 +1061,7 @@ export = createRule<[Options?], MessageId>({
10611061
pathGroupsExcludedImportTypes,
10621062
)
10631063
},
1064-
'Program:exit': function reportAndReset() {
1064+
'Program:exit'() {
10651065
for (const imported of importMap.values()) {
10661066
if (newlinesBetweenImports !== 'ignore') {
10671067
makeNewlinesBetweenReport(

src/utils/export-map.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -442,13 +442,14 @@ export class ExportMap {
442442
}
443443

444444
if (n.type === 'ExportAllDeclaration') {
445-
const getter = captureDependency(n, n.exportKind === 'type')
446-
if (getter) {
447-
m.dependencies.add(getter)
448-
}
449445
if (n.exported) {
450446
namespaces.set(n.exported.name, n.source.value)
451447
processSpecifier(n, n.exported, m)
448+
} else {
449+
const getter = captureDependency(n, n.exportKind === 'type')
450+
if (getter) {
451+
m.dependencies.add(getter)
452+
}
452453
}
453454
continue
454455
}
@@ -693,6 +694,7 @@ export class ExportMap {
693694
if (this.namespace.has(name)) {
694695
return true
695696
}
697+
696698
if (this.reexports.has(name)) {
697699
return true
698700
}

test/rules/namespace.spec.ts

+38-43
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
import rule from 'eslint-plugin-import-x/rules/namespace'
1212

1313
const ruleTester = new TSESLint.RuleTester({
14-
parser: require.resolve('espree'),
1514
parserOptions: { env: { es6: true } },
1615
})
1716

@@ -401,11 +400,7 @@ const invalid = [
401400
///////////////////////
402401
// deep dereferences //
403402
//////////////////////
404-
for (const [folder, parser] of [
405-
['deep'],
406-
// FIXME: check and enable
407-
// ['deep-es7', parsers.BABEL],
408-
]) {
403+
for (const [folder, parser] of [['deep'], ['deep-es7', parsers.BABEL]]) {
409404
// close over params
410405
valid.push(
411406
test({
@@ -428,45 +423,45 @@ for (const [folder, parser] of [
428423
parser,
429424
code: `import { b } from "./${folder}/a"; var {c:{d:{e}}} = b`,
430425
}),
426+
// deep namespaces should include explicitly exported defaults
427+
test({
428+
parser,
429+
code: `import * as a from "./${folder}/a"; console.log(a.b.default)`,
430+
}),
431431
)
432432

433-
// deep namespaces should include explicitly exported defaults
434-
test({
435-
parser,
436-
code: `import * as a from "./${folder}/a"; console.log(a.b.default)`,
437-
}),
438-
invalid.push(
439-
test({
440-
parser,
441-
code: `import * as a from "./${folder}/a"; console.log(a.b.e)`,
442-
errors: ["'e' not found in deeply imported namespace 'a.b'."],
443-
}),
444-
test({
445-
parser,
446-
code: `import { b } from "./${folder}/a"; console.log(b.e)`,
447-
errors: ["'e' not found in imported namespace 'b'."],
448-
}),
449-
test({
450-
parser,
451-
code: `import * as a from "./${folder}/a"; console.log(a.b.c.e)`,
452-
errors: ["'e' not found in deeply imported namespace 'a.b.c'."],
453-
}),
454-
test({
455-
parser,
456-
code: `import { b } from "./${folder}/a"; console.log(b.c.e)`,
457-
errors: ["'e' not found in deeply imported namespace 'b.c'."],
458-
}),
459-
test({
460-
parser,
461-
code: `import * as a from "./${folder}/a"; var {b:{ e }} = a`,
462-
errors: ["'e' not found in deeply imported namespace 'a.b'."],
463-
}),
464-
test({
465-
parser,
466-
code: `import * as a from "./${folder}/a"; var {b:{c:{ e }}} = a`,
467-
errors: ["'e' not found in deeply imported namespace 'a.b.c'."],
468-
}),
469-
)
433+
invalid.push(
434+
test({
435+
parser,
436+
code: `import * as a from "./${folder}/a"; console.log(a.b.e)`,
437+
errors: ["'e' not found in deeply imported namespace 'a.b'."],
438+
}),
439+
test({
440+
parser,
441+
code: `import { b } from "./${folder}/a"; console.log(b.e)`,
442+
errors: ["'e' not found in imported namespace 'b'."],
443+
}),
444+
test({
445+
parser,
446+
code: `import * as a from "./${folder}/a"; console.log(a.b.c.e)`,
447+
errors: ["'e' not found in deeply imported namespace 'a.b.c'."],
448+
}),
449+
test({
450+
parser,
451+
code: `import { b } from "./${folder}/a"; console.log(b.c.e)`,
452+
errors: ["'e' not found in deeply imported namespace 'b.c'."],
453+
}),
454+
test({
455+
parser,
456+
code: `import * as a from "./${folder}/a"; var {b:{ e }} = a`,
457+
errors: ["'e' not found in deeply imported namespace 'a.b'."],
458+
}),
459+
test({
460+
parser,
461+
code: `import * as a from "./${folder}/a"; var {b:{c:{ e }}} = a`,
462+
errors: ["'e' not found in deeply imported namespace 'a.b.c'."],
463+
}),
464+
)
470465
}
471466

472467
ruleTester.run('namespace', rule, { valid, invalid })

0 commit comments

Comments
 (0)