Skip to content

Commit

Permalink
support extra core modules in importType
Browse files Browse the repository at this point in the history
  • Loading branch information
benmosher committed Jun 16, 2016
1 parent b447f92 commit aa395ab
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
11 changes: 6 additions & 5 deletions src/core/importType.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
}
Expand All @@ -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)))
}
Expand Down Expand Up @@ -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))
}
25 changes: 15 additions & 10 deletions src/core/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,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 = dirname(sourceFile)
, cacheKey = sourceDir + hashObject(settings) + modulePath
Expand All @@ -74,11 +78,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(path) {
cachePath(cacheKey, path)
return path
}

function withResolver(resolver, config) {
Expand Down Expand Up @@ -114,19 +117,21 @@ export function relative(modulePath, sourceFile, settings) {

for (let [name, config] of resolvers) {
const resolver = requireResolver(name, modulePath)
, 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) {
Expand Down
2 changes: 1 addition & 1 deletion src/rules/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
8 changes: 8 additions & 0 deletions tests/src/core/importType.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
})

0 comments on commit aa395ab

Please sign in to comment.