Skip to content

Commit

Permalink
logic: improve scoped module scoping
Browse files Browse the repository at this point in the history
  • Loading branch information
mplewis committed Jul 9, 2017
1 parent 307ddeb commit ed65a74
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
13 changes: 11 additions & 2 deletions src/core/importType.js
Expand Up @@ -8,14 +8,23 @@ function constant(value) {
return () => value
}

function baseModule(name) {
if (isScoped(name)) {
const [scope, pkg] = name.split('/')
return `${scope}/${pkg}`
}
const [pkg] = name.split('/')
return pkg
}

export function isAbsolute(name) {
return name.indexOf('/') === 0
}

export function isBuiltIn(name, settings) {
const baseModule = name.split('/')[0]
const base = baseModule(name)
const extras = (settings && settings['import/core-modules']) || []
return builtinModules.indexOf(baseModule) !== -1 || extras.indexOf(baseModule) > -1
return builtinModules.indexOf(base) !== -1 || extras.indexOf(base) > -1
}

function isExternalPath(path, name, settings) {
Expand Down
9 changes: 8 additions & 1 deletion tests/src/core/importType.js
Expand Up @@ -5,7 +5,7 @@ import importType from 'core/importType'

import { testContext } from '../utils'

describe('importType(name)', function () {
describe.only('importType(name)', function () {
const context = testContext()

it("should return 'absolute' for paths starting with a /", function() {
Expand Down Expand Up @@ -69,14 +69,21 @@ describe('importType(name)', function () {
it("should return 'builtin' for additional core modules", function() {
// without extra config, should be marked external
expect(importType('electron', context)).to.equal('external')
expect(importType('@org/foobar', context)).to.equal('external')

const electronContext = testContext({ 'import/core-modules': ['electron'] })
expect(importType('electron', electronContext)).to.equal('builtin')

const scopedContext = testContext({ 'import/core-modules': ['@org/foobar'] })
expect(importType('@org/foobar', scopedContext)).to.equal('builtin')
})

it("should return 'builtin' for resources inside additional core modules", function() {
const electronContext = testContext({ 'import/core-modules': ['electron'] })
expect(importType('electron/some/path/to/resource.json', electronContext)).to.equal('builtin')

const scopedContext = testContext({ 'import/core-modules': ['@org/foobar'] })
expect(importType('@org/foobar/some/path/to/resource.json', scopedContext)).to.equal('builtin')
})

it("should return 'external' for module from 'node_modules' with default config", function() {
Expand Down

0 comments on commit ed65a74

Please sign in to comment.