Skip to content

Commit

Permalink
[Fix] extensions/importType: Fix @/abc being treated as scoped module
Browse files Browse the repository at this point in the history
Fixes #1851

Before this commit, @/abc was being wrongly detected as a scoped module.
This was a problem when somebody had a webpack alias `@`. (eg. @ -> ./src)

In general, scoped modules can't start with @/, I think they have to be like @/abcd.
  • Loading branch information
Artur Tagisow authored and ljharb committed Jul 12, 2020
1 parent 843055c commit 3e65a70
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel

### Fixed
- [`default`]/TypeScript: avoid crash on `export =` with a MemberExpression ([#1841], thanks [@ljharb])
- [`extensions`]/importType: Fix @/abc being treated as scoped module ([#1854], thanks [@3nuc])

## [2.22.0] - 2020-06-26
### Added
Expand Down Expand Up @@ -725,6 +726,7 @@ for info on changes for earlier releases.

[`memo-parser`]: ./memo-parser/README.md

[#1854]: https://github.com/benmosher/eslint-plugin-import/issues/1854
[#1841]: https://github.com/benmosher/eslint-plugin-import/issues/1841
[#1836]: https://github.com/benmosher/eslint-plugin-import/pull/1836
[#1835]: https://github.com/benmosher/eslint-plugin-import/pull/1835
Expand Down Expand Up @@ -1260,3 +1262,4 @@ for info on changes for earlier releases.
[@be5invis]: https://github.com/be5invis
[@noelebrun]: https://github.com/noelebrun
[@beatrizrezener]: https://github.com/beatrizrezener
[@3nuc]: https://github.com/3nuc
2 changes: 1 addition & 1 deletion src/core/importType.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function typeTest(name, settings, path) {
}

export function isScopedModule(name) {
return name.indexOf('@') === 0
return name.indexOf('@') === 0 && !name.startsWith('@/')
}

export default function resolveImportType(name, context) {
Expand Down
7 changes: 6 additions & 1 deletion tests/src/core/importType.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect } from 'chai'
import * as path from 'path'

import importType, {isExternalModule} from 'core/importType'
import importType, {isExternalModule, isScopedModule} from 'core/importType'

import { testContext, testFilePath } from '../utils'

Expand Down Expand Up @@ -237,4 +237,9 @@ describe('importType(name)', function () {
'import/external-module-folders': ['E:\\path\\to\\node_modules'],
}, 'E:\\path\\to\\node_modules\\foo')).to.equal(true)
})

it('correctly identifies scoped modules with `isScopedModule`', () => {
expect(isScopedModule('@/abc')).to.equal(false)
expect(isScopedModule('@a/abc')).to.equal(true)
})
})
10 changes: 10 additions & 0 deletions tests/src/rules/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,5 +444,15 @@ ruleTester.run('extensions', rule, {
},
],
}),
test({
code: 'import foo from "@/ImNotAScopedModule"',
options: ['always'],
errors: [
{
message: 'Missing file extension for "@/ImNotAScopedModule"',
line: 1,
},
],
}),
],
})

1 comment on commit 3e65a70

@alexandre-le-borgne
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need a release with this fix

Please sign in to comment.