From f2da6417e82736119d775a85ff2d262c689a550f Mon Sep 17 00:00:00 2001 From: jeffshaver Date: Wed, 20 Feb 2019 21:13:45 -0500 Subject: [PATCH] closes #1293. allows aliases that start with @ to be caught as internal --- src/core/importType.js | 21 +++++++++++---------- tests/files/@my-alias/fn.js | 0 tests/src/core/importType.js | 5 +++++ 3 files changed, 16 insertions(+), 10 deletions(-) create mode 100644 tests/files/@my-alias/fn.js diff --git a/src/core/importType.js b/src/core/importType.js index 89b162aad3..600ee5d5be 100644 --- a/src/core/importType.js +++ b/src/core/importType.js @@ -8,8 +8,8 @@ function constant(value) { return () => value } -function baseModule(name) { - if (isScoped(name)) { +function baseModule(name, path) { + if (isScoped(name, path)) { const [scope, pkg] = name.split('/') return `${scope}/${pkg}` } @@ -21,8 +21,8 @@ export function isAbsolute(name) { return name.indexOf('/') === 0 } -export function isBuiltIn(name, settings) { - const base = baseModule(name) +export function isBuiltIn(name, settings, path) { + const base = baseModule(name, path) const extras = (settings && settings['import/core-modules']) || [] return coreModules[base] || extras.indexOf(base) > -1 } @@ -39,21 +39,22 @@ function isExternalModule(name, settings, path) { const externalModuleMainRegExp = /^[\w]((?!\/).)*$/ export function isExternalModuleMain(name, settings, path) { - return externalModuleMainRegExp.test(name) && isExternalPath(path, name, settings) + return (externalModuleMainRegExp.test(name) && isExternalPath(path, name, settings)) } const scopedRegExp = /^@[^/]+\/[^/]+/ -function isScoped(name) { - return scopedRegExp.test(name) +function isScoped(name, settings, path) { + return scopedRegExp.test(name) && isExternalPath(path, name, settings) } const scopedMainRegExp = /^@[^/]+\/?[^/]+$/ -export function isScopedMain(name) { - return scopedMainRegExp.test(name) +export function isScopedMain(name, settings, path) { + return scopedMainRegExp.test(name) && isExternalPath(path, settings, name) } function isInternalModule(name, settings, path) { - return externalModuleRegExp.test(name) && !isExternalPath(path, name, settings) + const matchesScopedOrExternal = scopedRegExp.test(name) || externalModuleRegExp.test(name) + return (matchesScopedOrExternal&& !isExternalPath(path, name, settings)) } function isRelativeToParent(name) { diff --git a/tests/files/@my-alias/fn.js b/tests/files/@my-alias/fn.js new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/src/core/importType.js b/tests/src/core/importType.js index abf9b95228..1cb5c8d67e 100644 --- a/tests/src/core/importType.js +++ b/tests/src/core/importType.js @@ -41,6 +41,11 @@ describe('importType(name)', function () { expect(importType('importType', 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')] } } }) + expect(importType('@my-alias/fn', 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')