Skip to content

Commit

Permalink
[fix] aliased internal modules that look like core modules
Browse files Browse the repository at this point in the history
  • Loading branch information
echenley authored and ljharb committed Mar 5, 2019
1 parent 0ff1c83 commit a49ab8a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/core/importType.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ export function isAbsolute(name) {
return name.indexOf('/') === 0
}

export function isBuiltIn(name, settings) {
// path is defined only when a resolver resolves to a non-standard path
export function isBuiltIn(name, settings, path) {
if (path) return false
const base = baseModule(name)
const extras = (settings && settings['import/core-modules']) || []
return coreModules[base] || extras.indexOf(base) > -1
Expand Down
1 change: 1 addition & 0 deletions tests/files/constants/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const FOO = 'FOO'
24 changes: 21 additions & 3 deletions tests/src/core/importType.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { testContext } from '../utils'

describe('importType(name)', function () {
const context = testContext()
const pathToTestFiles = path.join(__dirname, '..', '..', 'files')

it("should return 'absolute' for paths starting with a /", function() {
expect(importType('/', context)).to.equal('absolute')
Expand Down Expand Up @@ -42,20 +43,37 @@ describe('importType(name)', function () {
})

it("should return 'internal' for non-builtins resolved outside of node_modules", function () {
const pathContext = testContext({ "import/resolver": { node: { paths: [ path.join(__dirname, '..', '..', 'files') ] } } })
const pathContext = testContext({ "import/resolver": { node: { paths: [pathToTestFiles] } } })
expect(importType('importType', pathContext)).to.equal('internal')
})

it.skip("should return 'internal' for scoped packages resolved outside of node_modules", function () {
const pathContext = testContext({ "import/resolver": { node: { paths: [ path.join(__dirname, '..', '..', 'files') ] } } })
const pathContext = testContext({ "import/resolver": { node: { paths: [pathToTestFiles] } } })
expect(importType('@importType/index', pathContext)).to.equal('internal')
})

it("should return 'internal' for internal modules that are referenced by aliases", function () {
const pathContext = testContext({ 'import/resolver': { node: { paths: [path.join(__dirname, '..', '..', 'files')] } } })
const pathContext = testContext({ 'import/resolver': { node: { paths: [pathToTestFiles] } } })
expect(importType('@my-alias/fn', pathContext)).to.equal('internal')
})

it("should return 'internal' for aliased internal modules that look like core modules (node resolver)", function () {
const pathContext = testContext({ 'import/resolver': { node: { paths: [pathToTestFiles] } } })
expect(importType('constants/index', pathContext)).to.equal('internal')
expect(importType('constants/', pathContext)).to.equal('internal')
// resolves exact core modules over internal modules
expect(importType('constants', pathContext)).to.equal('builtin')
})

it("should return 'internal' for aliased internal modules that look like core modules (webpack resolver)", function () {
const webpackConfig = { resolve: { modules: [pathToTestFiles, 'node_modules'] } }
const pathContext = testContext({ 'import/resolver': { webpack: { config: webpackConfig } } })
expect(importType('constants/index', pathContext)).to.equal('internal')
expect(importType('constants/', pathContext)).to.equal('internal')
// the following assertion fails because the webpack resolver runs an resolve.isCore('constants') without first checking paths config
// expect(importType('constants', pathContext)).to.equal('internal')
})

it("should return 'parent' for internal modules that go through the parent", function() {
expect(importType('../foo', context)).to.equal('parent')
expect(importType('../../foo', context)).to.equal('parent')
Expand Down

0 comments on commit a49ab8a

Please sign in to comment.