Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function resolveImportType(name, context, regExpGroups) {
let matchingRegExpGroup = regExpGroups.find(([_groupName, regExp]) => regExp.test(name));
if (matchingRegExpGroup) {
return matchingRegExpGroup[0];
}
return typeTest(name, context.settings, resolve(name, context));
}
ImportDeclaration(node) {
const resolvedPath = resolve(node.source.value, context)
if (!resolvedPath) {
return
}
recordImport(resolvedPath)
node.specifiers.forEach(specifier => {
if (specifier.type === 'ImportDefaultSpecifier') {
recordImport(resolvedPath, DEFAULT)
} else if (specifier.type === 'ImportSpecifier') {
recordImport(resolvedPath, specifier.imported.name)
}
})
},
ExportDefaultDeclaration() {
function checkImportForRelativePackage (importPath, node) {
const potentialViolationTypes = ['parent', 'index', 'sibling']
if (potentialViolationTypes.indexOf(resolveImportType(importPath, context)) === -1) {
return
}
const resolvedImport = resolve(importPath, context)
const resolvedContext = context.getFilename()
if (!resolvedImport || !resolvedContext) {
return
}
const importPkg = findNamedPackage(resolvedImport)
const contextPkg = findNamedPackage(resolvedContext)
if (importPkg.package && contextPkg.package && importPkg.package.name !== contextPkg.package.name) {
const importBaseName = path.basename(importPath)
const importRoot = path.dirname(importPkg.path)
const properPath = path.relative(importRoot, resolvedImport)
const properImport = path.join(
importPkg.package.name,
path.dirname(properPath),
function checkForRestrictedImportPath(importPath, node) {
const absoluteImportPath = importPath[0] === '.' ? resolve(importPath, context) : undefined;
const currentFilename = context.getFilename();
for (const { target, from, allowSameFolder, errorMessage = '' } of zones) {
const srcFilePath = resolve(currentFilename, context);
const relativeSrcFile = path.relative(basePath, srcFilePath);
const relativeImportFile = absoluteImportPath
? path.relative(basePath, absoluteImportPath)
: importPath;
if (
!mm([relativeSrcFile], target).length ||
!mm([relativeImportFile], from).length ||
(allowSameFolder && isSameFolderOrDescendent(relativeSrcFile, relativeImportFile, from))
)
continue;
context.report({
node,
message: `Unexpected path "${importPath}" imported in restricted zone.${
CallExpression(node) {
if (node.callee.type === 'Identifier' && node.callee.name === 'require' && node.arguments.length === 1) {
const pathNode = node.arguments[0]
if (pathNode.type === 'Literal' && typeof pathNode.value === 'string') {
const resolvedPath =
pathNode.type === 'Literal' && typeof pathNode.value === 'string' && resolve(pathNode.value, context)
if (resolvedPath) {
recordImport(resolvedPath, STAR)
}
}
}
},
MemberExpression(node) {
function checkForRestrictedImportPath(importPath, node) {
const absoluteImportPath = importPath[0] === '.' ? resolve(importPath, context) : undefined;
const currentFilename = context.getFilename();
for (const { target, from, allowSameFolder, errorMessage = '' } of zones) {
const srcFilePath = resolve(currentFilename, context);
const relativeSrcFile = path.relative(basePath, srcFilePath);
const relativeImportFile = absoluteImportPath
? path.relative(basePath, absoluteImportPath)
: importPath;
if (
!mm([relativeSrcFile], target).length ||
!mm([relativeImportFile], from).length ||
(allowSameFolder && isSameFolderOrDescendent(relativeSrcFile, relativeImportFile, from))
)
continue;
function resolveImportType(name, context) {
return typeTest(name, context.settings, resolve(name, context))
}