Skip to content

Commit 47f912e

Browse files
fiskerljharb
authored andcommitted
[Fix] order: fix isExternalModule detection on windows
1 parent 8905007 commit 47f912e

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
55
This change log adheres to standards from [Keep a CHANGELOG](http://keepachangelog.com).
66

77
## [Unreleased]
8+
### Fixed
9+
- [`order`]: fix `isExternalModule` detect on windows ([#1651], thanks [@fisker])
810

911
## [2.20.1] - 2020-02-01
1012
### Fixed
@@ -652,6 +654,7 @@ for info on changes for earlier releases.
652654

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

657+
[#1651]: https://github.com/benmosher/eslint-plugin-import/pull/1651
655658
[#1635]: https://github.com/benmosher/eslint-plugin-import/issues/1635
656659
[#1625]: https://github.com/benmosher/eslint-plugin-import/pull/1625
657660
[#1620]: https://github.com/benmosher/eslint-plugin-import/pull/1620
@@ -1105,3 +1108,4 @@ for info on changes for earlier releases.
11051108
[@kentcdodds]: https://github.com/kentcdodds
11061109
[@IvanGoncharov]: https://github.com/IvanGoncharov
11071110
[@wschurman]: https://github.com/wschurman
1111+
[@fisker]: https://github.com/fisker

src/core/importType.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,16 @@ function isExternalPath(path, name, settings) {
2929
}
3030

3131
function isSubpath(subpath, path) {
32-
const normSubpath = subpath.replace(/[/]$/, '')
32+
const normPath = path.replace(/\\/g, '/')
33+
const normSubpath = subpath.replace(/\\/g, '/').replace(/\/$/, '')
3334
if (normSubpath.length === 0) {
3435
return false
3536
}
36-
const left = path.indexOf(normSubpath)
37+
const left = normPath.indexOf(normSubpath)
3738
const right = left + normSubpath.length
3839
return left !== -1 &&
39-
(left === 0 || normSubpath[0] !== '/' && path[left - 1] === '/') &&
40-
(right >= path.length || path[right] === '/')
40+
(left === 0 || normSubpath[0] !== '/' && normPath[left - 1] === '/') &&
41+
(right >= normPath.length || normPath[right] === '/')
4142
}
4243

4344
const externalModuleRegExp = /^\w/

tests/src/core/importType.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { expect } from 'chai'
22
import * as path from 'path'
33

4-
import importType from 'core/importType'
4+
import importType, {isExternalModule} from 'core/importType'
55

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

@@ -180,6 +180,12 @@ describe('importType(name)', function () {
180180
})
181181

182182
it('returns "external" for a scoped module from a symlinked directory which partial path is contained in "external-module-folders" (webpack resolver)', function() {
183+
const originalFoldersContext = testContext({
184+
'import/resolver': 'webpack',
185+
'import/external-module-folders': [],
186+
})
187+
expect(importType('@test-scope/some-module', originalFoldersContext)).to.equal('internal')
188+
183189
const foldersContext = testContext({
184190
'import/resolver': 'webpack',
185191
'import/external-module-folders': ['files/symlinked-module'],
@@ -224,4 +230,11 @@ describe('importType(name)', function () {
224230
})
225231
expect(importType('@test-scope/some-module', foldersContext)).to.equal('external')
226232
})
233+
234+
it('`isExternalModule` works with windows directory separator', function() {
235+
expect(isExternalModule('foo', {}, 'E:\\path\\to\\node_modules\\foo')).to.equal(true)
236+
expect(isExternalModule('foo', {
237+
'import/external-module-folders': ['E:\\path\\to\\node_modules'],
238+
}, 'E:\\path\\to\\node_modules\\foo')).to.equal(true)
239+
})
227240
})

0 commit comments

Comments
 (0)