Skip to content

Commit

Permalink
[Fix] no-unused-modules: avoid order-dependence
Browse files Browse the repository at this point in the history
Fixes #1743
  • Loading branch information
darkartur authored and ljharb committed May 2, 2020
1 parent 40ee069 commit a1c969f
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -20,6 +20,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
- [`no-unused-modules`]: Revert "[flow] `no-unused-modules`: add flow type support" ([#1770], thanks [@Hypnosphi])
- TypeScript: Add nested namespace handling ([#1763], thanks [@julien1619])
- [`namespace`]/`ExportMap`: Fix interface declarations for TypeScript ([#1764], thanks [@julien1619])
- [`no-unused-modules`]: avoid order-dependence ([#1744], thanks [@darkartur])

### Changed
- TypeScript config: Disable [`named`][] ([#1726], thanks [@astorije])
Expand Down Expand Up @@ -688,6 +689,7 @@ for info on changes for earlier releases.
[#1770]: https://github.com/benmosher/eslint-plugin-import/pull/1770
[#1764]: https://github.com/benmosher/eslint-plugin-import/pull/1764
[#1763]: https://github.com/benmosher/eslint-plugin-import/pull/1763
[#1744]: https://github.com/benmosher/eslint-plugin-import/pull/1744
[#1736]: https://github.com/benmosher/eslint-plugin-import/pull/1736
[#1726]: https://github.com/benmosher/eslint-plugin-import/pull/1726
[#1724]: https://github.com/benmosher/eslint-plugin-import/pull/1724
Expand Down Expand Up @@ -1174,3 +1176,4 @@ for info on changes for earlier releases.
[@nickofthyme]: https://github.com/nickofthyme
[@manuth]: https://github.com/manuth
[@julien1619]: https://github.com/julien1619
[@darkartur]: https://github.com/darkartur
9 changes: 4 additions & 5 deletions src/rules/no-unused-modules.js
Expand Up @@ -654,13 +654,12 @@ module.exports = {
if (astNode.source) {
resolvedPath = resolve(astNode.source.raw.replace(/('|")/g, ''), context)
astNode.specifiers.forEach(specifier => {
let name
if (specifier.exported.name === DEFAULT) {
name = IMPORT_DEFAULT_SPECIFIER
const name = specifier.local.name
if (specifier.local.name === DEFAULT) {
newDefaultImports.add(resolvedPath)
} else {
name = specifier.local.name
newImports.set(name, resolvedPath)
}
newImports.set(name, resolvedPath)
})
}
}
Expand Down
@@ -0,0 +1 @@
export default function ComponentA() {}
@@ -0,0 +1 @@
export default function ComponentB() {}
2 changes: 2 additions & 0 deletions tests/files/no-unused-modules/renameDefault-2/components.js
@@ -0,0 +1,2 @@
export { default as ComponentA } from "./ComponentA";
export { default as ComponentB } from "./ComponentB";
1 change: 1 addition & 0 deletions tests/files/no-unused-modules/renameDefault-2/usage.js
@@ -0,0 +1 @@
import { ComponentA, ComponentB } from './components'
1 change: 1 addition & 0 deletions tests/files/no-unused-modules/renameDefault/Component.js
@@ -0,0 +1 @@
export default function Component() {}
1 change: 1 addition & 0 deletions tests/files/no-unused-modules/renameDefault/components.js
@@ -0,0 +1 @@
export { default as Component } from './Component'
1 change: 1 addition & 0 deletions tests/files/no-unused-modules/renameDefault/usage.js
@@ -0,0 +1 @@
import { Component } from './components'
25 changes: 25 additions & 0 deletions tests/src/rules/no-unused-modules.js
Expand Up @@ -442,6 +442,31 @@ ruleTester.run('no-unused-modules', rule, {
invalid: [],
})

describe('renameDefault', () => {
ruleTester.run('no-unused-modules', rule, {
valid: [
test({ options: unusedExportsOptions,
code: 'export { default as Component } from "./Component"',
filename: testFilePath('./no-unused-modules/renameDefault/components.js')}),
test({ options: unusedExportsOptions,
code: 'export default function Component() {}',
filename: testFilePath('./no-unused-modules/renameDefault/Component.js')}),
],
invalid: [],
})
ruleTester.run('no-unused-modules', rule, {
valid: [
test({ options: unusedExportsOptions,
code: 'export { default as ComponentA } from "./ComponentA";export { default as ComponentB } from "./ComponentB";',
filename: testFilePath('./no-unused-modules/renameDefault-2/components.js')}),
test({ options: unusedExportsOptions,
code: 'export default function ComponentA() {};',
filename: testFilePath('./no-unused-modules/renameDefault-2/ComponentA.js')}),
],
invalid: [],
})
})

describe('test behaviour for new file', () => {
before(() => {
fs.writeFileSync(testFilePath('./no-unused-modules/file-added-0.js'), '', {encoding: 'utf8'})
Expand Down

0 comments on commit a1c969f

Please sign in to comment.