diff --git a/src/core/importType.js b/src/core/importType.js index 6f37743f1..23bc6350d 100644 --- a/src/core/importType.js +++ b/src/core/importType.js @@ -8,6 +8,10 @@ function constant(value) { return () => value } +export function isAbsolute(name) { + return name.indexOf('/') === 0 +} + export function isBuiltIn(name, settings) { const extras = (settings && settings['import/core-modules']) || [] return builtinModules.indexOf(name) !== -1 || extras.indexOf(name) > -1 @@ -46,6 +50,7 @@ function isRelativeToSibling(name) { } const typeTest = cond([ + [isAbsolute, constant('absolute')], [isBuiltIn, constant('builtin')], [isExternalModule, constant('external')], [isScoped, constant('external')], diff --git a/src/rules/no-absolute-path.js b/src/rules/no-absolute-path.js index 8503be0d5..e4cf18a63 100644 --- a/src/rules/no-absolute-path.js +++ b/src/rules/no-absolute-path.js @@ -1,3 +1,4 @@ +import { isAbsolute } from '../core/importType' import isStaticRequire from '../core/staticRequire' function reportIfAbsolute(context, node, name) { @@ -6,10 +7,6 @@ function reportIfAbsolute(context, node, name) { } } -function isAbsolute(name) { - return name.indexOf('/') === 0 -} - module.exports = { meta: { docs: {}, diff --git a/tests/src/core/importType.js b/tests/src/core/importType.js index 9e552e60b..5b63910af 100644 --- a/tests/src/core/importType.js +++ b/tests/src/core/importType.js @@ -8,6 +8,12 @@ import { testContext } from '../utils' describe('importType(name)', function () { const context = testContext() + it("should return 'absolute' for paths starting with a /", function() { + expect(importType('/', context)).to.equal('absolute') + expect(importType('/path', context)).to.equal('absolute') + expect(importType('/some/path', context)).to.equal('absolute') + }) + it("should return 'builtin' for node.js modules", function() { expect(importType('fs', context)).to.equal('builtin') expect(importType('path', context)).to.equal('builtin')