eslint-plugin-codebox
Version:
A set of ESLint rules we use at Codebox
87 lines (70 loc) • 2.41 kB
JavaScript
import cond from 'lodash/cond'
import builtinModules from 'builtin-modules'
import { join } from 'path'
import resolve from 'eslint-module-utils/resolve'
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 base = baseModule(name)
const extras = (settings && settings['codebox/core-modules']) || []
return builtinModules.indexOf(base) !== -1 || extras.indexOf(base) > -1
}
function isExternalPath(path, name, settings) {
const folders = (settings && settings['codebox/external-module-folders']) || ['node_modules']
return !path || folders.some(folder => path.indexOf(join(folder, name)) > -1)
}
const externalModuleRegExp = /^\w/
function isExternalModule(name, settings, path) {
return externalModuleRegExp.test(name) && isExternalPath(path, name, settings)
}
const externalModuleMainRegExp = /^[\w]((?!\/).)*$/
export function isExternalModuleMain(name, settings, path) {
return externalModuleMainRegExp.test(name) && isExternalPath(path, name, settings)
}
const scopedRegExp = /^@[^/]+\/[^/]+/
function isScoped(name) {
return scopedRegExp.test(name)
}
const scopedMainRegExp = /^@[^/]+\/?[^/]+$/
export function isScopedMain(name) {
return scopedMainRegExp.test(name)
}
function isInternalModule(name, settings, path) {
return externalModuleRegExp.test(name) && !isExternalPath(path, name, settings)
}
function isRelativeToParent(name) {
return name.indexOf('../') === 0
}
const indexFiles = ['.', './', './index', './index.js']
function isIndex(name) {
return indexFiles.indexOf(name) !== -1
}
function isRelativeToSibling(name) {
return name.indexOf('./') === 0
}
const typeTest = cond([
[isAbsolute, constant('absolute')],
[isBuiltIn, constant('builtin')],
[isExternalModule, constant('external')],
[isScoped, constant('external')],
[isInternalModule, constant('internal')],
[isRelativeToParent, constant('parent')],
[isIndex, constant('index')],
[isRelativeToSibling, constant('sibling')],
[constant(true), constant('unknown')],
])
export default function getImportGroup(name, context) {
return typeTest(name, context.settings, resolve(name, context))
}