Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
node.body.forEach(astNode => {
let resolvedPath
// support for export { value } from 'module'
if (astNode.type === EXPORT_NAMED_DECLARATION) {
if (astNode.source) {
resolvedPath = resolve(astNode.source.raw.replace(/('|")/g, ''), context)
astNode.specifiers.forEach(specifier => {
let name
if (specifier.exported.name === DEFAULT) {
name = IMPORT_DEFAULT_SPECIFIER
} else {
name = specifier.local.name
}
newImports.set(name, resolvedPath)
})
}
}
if (astNode.type === EXPORT_ALL_DECLARATION) {
resolvedPath = resolve(astNode.source.raw.replace(/('|")/g, ''), context)
newExportAll.add(resolvedPath)
}
} else {
return acc.concat(step)
}
}, [])
const nonScopeSteps = steps.filter(step => step.indexOf('@') !== 0)
if (nonScopeSteps.length <= 1) return false
// before trying to resolve, see if the raw import (with relative
// segments resolved) matches an allowed pattern
const justSteps = steps.join('/')
if (reachingAllowed(justSteps) || reachingAllowed(`/${justSteps}`)) return false
// if the import statement doesn't match directly, try to match the
// resolved path if the import is resolvable
const resolved = resolve(importPath, context)
if (!resolved || reachingAllowed(normalizeSep(resolved))) return false
// this import was not allowed by the allowed paths, and reaches
// so it is a violation
return true
}
it('gets correct values after cache lifetime', function () {
expect(resolve(original, context)).not.to.exist
expect(resolve(changed, context)).to.exist
})
})
it('resolves via a custom resolver with interface version 1 assumed if not specified', function () {
const testContext = utils.testContext({ 'import/resolver': './foo-bar-resolver-no-version' })
expect(resolve( '../files/foo'
, Object.assign({}, testContext, { getFilename: function () { return utils.getFilename('foo.js') } }),
)).to.equal(utils.testFilePath('./bar.jsx'))
expect(resolve( '../files/exception'
, Object.assign({}, testContext, { getFilename: function () { return utils.getFilename('exception.js') } }),
)).to.equal(undefined)
expect(resolve( '../files/not-found'
, Object.assign({}, testContext, { getFilename: function () { return utils.getFilename('not-found.js') } }),
)).to.equal(undefined)
})
it('respects import/resolve extensions', function () {
const testContext = utils.testContext({ 'import/resolve': { 'extensions': ['.jsx'] }})
expect(resolve( './jsx/MyCoolComponent'
, testContext,
)).to.equal(utils.testFilePath('./jsx/MyCoolComponent.jsx'))
})
infiniteContexts.forEach(([,c]) => {
expect(resolve(original, c)).to.exist
})
})
export default function resolveImportType(name, context) {
return typeTest(name, context.settings, resolve(name, context))
}
const defaultResolver = sourcePath => resolve(sourcePath, context) || sourcePath
const resolver = considerQueryStringOption ? (sourcePath => {
function isResolvableWithoutExtension(file) {
const extension = path.extname(file)
const fileWithoutExtension = file.slice(0, -extension.length)
const resolvedFileWithoutExtension = resolve(fileWithoutExtension, context)
return resolvedFileWithoutExtension === resolve(file, context)
}
function checkForRestrictedImportPath(importPath, node) {
const absoluteImportPath = resolve(importPath, context)
if (!absoluteImportPath) {
return
}
matchingZones.forEach((zone) => {
const absoluteFrom = path.resolve(basePath, zone.from)
if (containsPath(absoluteImportPath, absoluteFrom)) {
context.report({
node,
message: `Unexpected path "${importPath}" imported in restricted zone.`,
})
}
})
}