Skip to content

Commit a1c969f

Browse files
darkarturljharb
authored andcommitted
[Fix] no-unused-modules: avoid order-dependence
Fixes #1743
1 parent 40ee069 commit a1c969f

File tree

10 files changed

+40
-5
lines changed

10 files changed

+40
-5
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
2020
- [`no-unused-modules`]: Revert "[flow] `no-unused-modules`: add flow type support" ([#1770], thanks [@Hypnosphi])
2121
- TypeScript: Add nested namespace handling ([#1763], thanks [@julien1619])
2222
- [`namespace`]/`ExportMap`: Fix interface declarations for TypeScript ([#1764], thanks [@julien1619])
23+
- [`no-unused-modules`]: avoid order-dependence ([#1744], thanks [@darkartur])
2324

2425
### Changed
2526
- TypeScript config: Disable [`named`][] ([#1726], thanks [@astorije])
@@ -688,6 +689,7 @@ for info on changes for earlier releases.
688689
[#1770]: https://github.com/benmosher/eslint-plugin-import/pull/1770
689690
[#1764]: https://github.com/benmosher/eslint-plugin-import/pull/1764
690691
[#1763]: https://github.com/benmosher/eslint-plugin-import/pull/1763
692+
[#1744]: https://github.com/benmosher/eslint-plugin-import/pull/1744
691693
[#1736]: https://github.com/benmosher/eslint-plugin-import/pull/1736
692694
[#1726]: https://github.com/benmosher/eslint-plugin-import/pull/1726
693695
[#1724]: https://github.com/benmosher/eslint-plugin-import/pull/1724
@@ -1174,3 +1176,4 @@ for info on changes for earlier releases.
11741176
[@nickofthyme]: https://github.com/nickofthyme
11751177
[@manuth]: https://github.com/manuth
11761178
[@julien1619]: https://github.com/julien1619
1179+
[@darkartur]: https://github.com/darkartur

src/rules/no-unused-modules.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -654,13 +654,12 @@ module.exports = {
654654
if (astNode.source) {
655655
resolvedPath = resolve(astNode.source.raw.replace(/('|")/g, ''), context)
656656
astNode.specifiers.forEach(specifier => {
657-
let name
658-
if (specifier.exported.name === DEFAULT) {
659-
name = IMPORT_DEFAULT_SPECIFIER
657+
const name = specifier.local.name
658+
if (specifier.local.name === DEFAULT) {
659+
newDefaultImports.add(resolvedPath)
660660
} else {
661-
name = specifier.local.name
661+
newImports.set(name, resolvedPath)
662662
}
663-
newImports.set(name, resolvedPath)
664663
})
665664
}
666665
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default function ComponentA() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default function ComponentB() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { default as ComponentA } from "./ComponentA";
2+
export { default as ComponentB } from "./ComponentB";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import { ComponentA, ComponentB } from './components'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default function Component() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default as Component } from './Component'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import { Component } from './components'

tests/src/rules/no-unused-modules.js

+25
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,31 @@ ruleTester.run('no-unused-modules', rule, {
442442
invalid: [],
443443
})
444444

445+
describe('renameDefault', () => {
446+
ruleTester.run('no-unused-modules', rule, {
447+
valid: [
448+
test({ options: unusedExportsOptions,
449+
code: 'export { default as Component } from "./Component"',
450+
filename: testFilePath('./no-unused-modules/renameDefault/components.js')}),
451+
test({ options: unusedExportsOptions,
452+
code: 'export default function Component() {}',
453+
filename: testFilePath('./no-unused-modules/renameDefault/Component.js')}),
454+
],
455+
invalid: [],
456+
})
457+
ruleTester.run('no-unused-modules', rule, {
458+
valid: [
459+
test({ options: unusedExportsOptions,
460+
code: 'export { default as ComponentA } from "./ComponentA";export { default as ComponentB } from "./ComponentB";',
461+
filename: testFilePath('./no-unused-modules/renameDefault-2/components.js')}),
462+
test({ options: unusedExportsOptions,
463+
code: 'export default function ComponentA() {};',
464+
filename: testFilePath('./no-unused-modules/renameDefault-2/ComponentA.js')}),
465+
],
466+
invalid: [],
467+
})
468+
})
469+
445470
describe('test behaviour for new file', () => {
446471
before(() => {
447472
fs.writeFileSync(testFilePath('./no-unused-modules/file-added-0.js'), '', {encoding: 'utf8'})

0 commit comments

Comments
 (0)