Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] order: fix isExternalModule detect on windows #1651

Merged
merged 2 commits into from
Feb 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
This change log adheres to standards from [Keep a CHANGELOG](http://keepachangelog.com).

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

## [2.20.1] - 2020-02-01
### Fixed
Expand Down Expand Up @@ -652,6 +654,7 @@ for info on changes for earlier releases.

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

[#1651]: https://github.com/benmosher/eslint-plugin-import/pull/1651
[#1635]: https://github.com/benmosher/eslint-plugin-import/issues/1635
[#1625]: https://github.com/benmosher/eslint-plugin-import/pull/1625
[#1620]: https://github.com/benmosher/eslint-plugin-import/pull/1620
Expand Down Expand Up @@ -1105,3 +1108,4 @@ for info on changes for earlier releases.
[@kentcdodds]: https://github.com/kentcdodds
[@IvanGoncharov]: https://github.com/IvanGoncharov
[@wschurman]: https://github.com/wschurman
[@fisker]: https://github.com/fisker
5 changes: 4 additions & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ environment:
# - nodejs_version: "4"

matrix:
fast_finish: true
fast_finish: false

# allow_failures:
# - nodejs_version: "4" # for eslint 5
Expand All @@ -27,6 +27,9 @@ install:
if ($env:nodejs_version -eq "4") {
npm install -g npm@3;
}
if ($env:nodejs_version -in @("8", "10", "12")) {
npm install -g npm@6.10.3;
}
- npm install

# fix symlinks
Expand Down
9 changes: 5 additions & 4 deletions src/core/importType.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@ function isExternalPath(path, name, settings) {
}

function isSubpath(subpath, path) {
const normSubpath = subpath.replace(/[/]$/, '')
const normPath = path.replace(/\\/g, '/')
const normSubpath = subpath.replace(/\\/g, '/').replace(/\/$/, '')
if (normSubpath.length === 0) {
return false
}
const left = path.indexOf(normSubpath)
const left = normPath.indexOf(normSubpath)
const right = left + normSubpath.length
return left !== -1 &&
(left === 0 || normSubpath[0] !== '/' && path[left - 1] === '/') &&
(right >= path.length || path[right] === '/')
(left === 0 || normSubpath[0] !== '/' && normPath[left - 1] === '/') &&
(right >= normPath.length || normPath[right] === '/')
}

const externalModuleRegExp = /^\w/
Expand Down
15 changes: 14 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 from 'core/importType'
import importType, {isExternalModule} from 'core/importType'

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

Expand Down Expand Up @@ -180,6 +180,12 @@ describe('importType(name)', function () {
})

it('returns "external" for a scoped module from a symlinked directory which partial path is contained in "external-module-folders" (webpack resolver)', function() {
const originalFoldersContext = testContext({
'import/resolver': 'webpack',
'import/external-module-folders': [],
})
expect(importType('@test-scope/some-module', originalFoldersContext)).to.equal('internal')

const foldersContext = testContext({
'import/resolver': 'webpack',
'import/external-module-folders': ['files/symlinked-module'],
Expand Down Expand Up @@ -224,4 +230,11 @@ describe('importType(name)', function () {
})
expect(importType('@test-scope/some-module', foldersContext)).to.equal('external')
})

it('`isExternalModule` works with windows directory separator', function() {
expect(isExternalModule('foo', {}, 'E:\\path\\to\\node_modules\\foo')).to.equal(true)
expect(isExternalModule('foo', {
'import/external-module-folders': ['E:\\path\\to\\node_modules'],
Copy link
Member

Choose a reason for hiding this comment

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

it doesn't seem like we're testing external-module-folders here if it was true before? meaning, maybe we need to add a test that returns false without external-module-folders, and true with it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, without external-module-folders still true, because subpath defualts to node_modules, the test make sure we need replace subpath, remove that line will cause error

Copy link
Member

Choose a reason for hiding this comment

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

Sorry, to clarify; I'm glad these tests fail without your fix! However, don't we want to also test that a windows path would not be external without the right external-module-folders setting?

}, 'E:\\path\\to\\node_modules\\foo')).to.equal(true)
})
})