diff --git a/src/core/importType.js b/src/core/importType.js index d913d110c..86f01bc89 100644 --- a/src/core/importType.js +++ b/src/core/importType.js @@ -8,12 +8,13 @@ function constant(value) { return () => value } -export function isBuiltIn(name) { - return builtinModules.indexOf(name) !== -1 +export function isBuiltIn(name, settings) { + const extras = (settings && settings['import/core-modules']) || [] + return builtinModules.indexOf(name) !== -1 || extras.indexOf(name) > -1 } const externalModuleRegExp = /^\w/ -function isExternalModule(name, path) { +function isExternalModule(name, settings, path) { if (!externalModuleRegExp.test(name)) return false return (!path || -1 < path.indexOf(join('node_modules', name))) } @@ -23,7 +24,7 @@ function isScoped(name) { return scopedRegExp.test(name) } -function isInternalModule(name, path) { +function isInternalModule(name, settings, path) { if (!externalModuleRegExp.test(name)) return false return (path && -1 === path.indexOf(join('node_modules', name))) } @@ -53,5 +54,5 @@ const typeTest = cond([ ]) export default function resolveImportType(name, context) { - return typeTest(name, resolve(name, context)) + return typeTest(name, context.settings, resolve(name, context)) } diff --git a/src/core/resolve.js b/src/core/resolve.js index 4200e550a..1f561f948 100644 --- a/src/core/resolve.js +++ b/src/core/resolve.js @@ -54,9 +54,13 @@ function fileExistsWithCaseSync(filepath, cacheSettings) { } export function relative(modulePath, sourceFile, settings) { + return fullResolve(modulePath, sourceFile, settings).path +} + +function fullResolve(modulePath, sourceFile, settings) { // check if this is a bonus core module const coreSet = new Set(settings['import/core-modules']) - if (coreSet != null && coreSet.has(modulePath)) return { path: null, found: true } + if (coreSet != null && coreSet.has(modulePath)) return { found: true, path: null } const sourceDir = path.dirname(sourceFile) , cacheKey = sourceDir + hashObject(settings) + modulePath @@ -71,11 +75,10 @@ export function relative(modulePath, sourceFile, settings) { } const cachedPath = checkCache(cacheKey, cacheSettings) - if (cachedPath !== undefined) return cachedPath + if (cachedPath !== undefined) return { found: true, path: cachedPath } function cache(resolvedPath) { cachePath(cacheKey, resolvedPath) - return resolvedPath } function withResolver(resolver, config) { @@ -111,19 +114,21 @@ export function relative(modulePath, sourceFile, settings) { for (let [name, config] of resolvers) { const resolver = requireResolver(name, sourceFile) + , resolved = withResolver(resolver, config) - let { path: fullPath, found } = withResolver(resolver, config) + if (!resolved.found) continue // resolvers imply file existence, this double-check just ensures the case matches - if (found && !fileExistsWithCaseSync(fullPath, cacheSettings)) { - // reject resolved path - fullPath = undefined - } + if (!fileExistsWithCaseSync(resolved.path, cacheSettings)) continue - if (found) return cache(fullPath) + // else, counts + cache(resolved.path) + return resolved } - return cache(undefined) + // failed + // cache(undefined) + return { found: false } } function resolverReducer(resolvers, map) { diff --git a/src/rules/extensions.js b/src/rules/extensions.js index 175dc1d9d..f8bb86e18 100644 --- a/src/rules/extensions.js +++ b/src/rules/extensions.js @@ -28,7 +28,7 @@ module.exports = function (context) { const importPath = source.value // don't enforce anything on builtins - if (isBuiltIn(importPath)) return + if (isBuiltIn(importPath, context.settings)) return const resolvedPath = resolve(importPath, context) diff --git a/tests/src/core/importType.js b/tests/src/core/importType.js index 88de5eacb..53796ba13 100644 --- a/tests/src/core/importType.js +++ b/tests/src/core/importType.js @@ -58,4 +58,12 @@ describe('importType(name)', function () { expect(importType('/malformed', context)).to.equal('unknown') expect(importType(' foo', context)).to.equal('unknown') }) + + it("should return 'builtin' for additional core modules", function() { + // without extra config, should be marked external + expect(importType('electron', context)).to.equal('external') + + const electronContext = testContext({ 'import/core-modules': ['electron'] }) + expect(importType('electron', electronContext)).to.equal('builtin') + }) })