Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import * as esquery from 'esquery';
const AST = esprima.parseScript(`const x = 2;
function f (n) {
const y = 4;
if (n > y) return true;
return false;
}`);
const s = 'FunctionDeclaration !VariableDeclaration > VariableDeclarator[init.value > 3]';
// $ExpectType Selector
const selector = esquery.parse(s);
// $ExpectType Node[]
const nodes = esquery.query(AST, s);
// $ExpectType Node[]
esquery(AST, s);
// $ExpectError
esquery.parse(3);
// $ExpectType Node[]
esquery.match(AST, selector);
// $ExpectError
esquery.match(AST, 'VariableDeclarator');
// $ExpectType boolean
esquery.matches(nodes[0], selector, esquery(AST, 'FunctionDeclaration'));
function parseJSDependencies(sourceContents, destinationFolder, sourceFolder) {
const sourceTree = espree.parse(sourceContents, { sourceType: 'module', ecmaVersion: 2018 });
const importNodes = esquery.query(
sourceTree,
'[type=/(ImportDeclaration|ExportNamedDeclaration|ExportAllDeclaration)/]'
);
importNodes.forEach(node => {
if (node.source) {
const importPath = node.source.value;
// Prefix any resolved files with an underscore as they are not part of the core spec
const replacePath = resolveDependenciesAndCopy({
sourcePath: importPath,
destinationFolder,
sourceFolder,
prefix: '_',
});
sourceContents = sourceContents.replace(importPath, replacePath);
}
});
function getAMDDefineDeps(ast: Program) {
const selector = 'CallExpression[callee.name=define]';
const defineCalls = esquery.query(ast, selector) as SimpleCallExpression[];
let incompleteAnalysis = false;
const deps: string[] = [];
for (const call of defineCalls) {
const [firstArg, secondArg] = call.arguments;
// Anonymous AMD module with dependencies
if (firstArg && firstArg.type === 'ArrayExpression') {
const results = extractDepsFromArrayExpression(firstArg);
deps.push(...results.deps);
if (results.incompleteAnalysis) incompleteAnalysis = true;
}
// Named AMD module with dependencies
if (secondArg && secondArg.type === 'ArrayExpression') {
const results = extractDepsFromArrayExpression(secondArg);
deps.push(...results.deps);
function getAMDSyncRequireDeps(ast: Program) {
const selector =
'CallExpression[callee.name=define] CallExpression[callee.name=require][arguments.0.type=Literal]';
const syncRequireCalls = esquery.query(
ast,
selector,
) as SimpleCallExpression[];
let incompleteAnalysis = false;
const deps: string[] = [];
for (const call of syncRequireCalls) {
const firstArg = call.arguments[0] as Literal;
if (typeof firstArg.value === 'string') {
deps.push(firstArg.value);
} else {
incompleteAnalysis = true;
}
}
return { deps, incompleteAnalysis };
function getAMDAsyncRequireDeps(ast: Program) {
const selector =
'CallExpression[callee.name=require][arguments.0.type=ArrayExpression]';
const requireCalls = esquery.query(ast, selector) as SimpleCallExpression[];
let incompleteAnalysis = false;
const deps: string[] = [];
for (const call of requireCalls) {
const firstArg = call.arguments[0] as ArrayExpression;
const results = extractDepsFromArrayExpression(firstArg);
deps.push(...results.deps);
if (results.incompleteAnalysis) incompleteAnalysis = true;
}
return { deps, incompleteAnalysis };
}