Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Add in the function wrapper.
var wrapper = defineWrapper(options);
var code = makeDefineFunctionCode(
name,
options.duplicateCjsDependencies ? collectDependenciesIds(body) : [],
wrapper
);
var ast = esprima.parse(code);
var astBody = body || [];
var innerFunctions = 0;
var expectedFunctions = options.needsFunctionWrapper ? 2 : 1;
types.visit(ast, {
visitFunctionExpression: function(path) {
if (this.isModuleFactory()) {
var functionBody = path.getValueProperty("body").body;
astBody.forEach(function(part) {
functionBody.push(part);
});
// stop traversing the tree
this.abort();
}
// keep traversing the tree
this.traverse(path);
},
function processComments(node: n.ASTNode, consts: Dictionary, scenario: IScenarioFile) {
const sandbox = vm.createContext({});
visit(node, {
visitComment(path) {
const expr = parseComment(path.value.value);
if (!Array.isArray(expr)) return void this.traverse(path);
path.parentPath.node.comments.pop();
const parentPath = path.parentPath.parentPath;
switch (expr[0]) {
case 'inject':
if (!n.Identifier.check(expr[1]) || !(expr[1].name in consts)) {
parentPath.replace(b.unaryExpression('void', b.numericLiteral(0)));
} else if (n.Literal.check(parentPath.node)) {
parentPath.node.value = consts[expr[1].name];
} else {
export function parseCode(userCode: string): string {
try {
const vars = {};
const ast = astFromUserCode(userCode);
types.visit(ast, {
visitLiteral: (path) => {
const key = nodeToKey(path, vars);
vars[key] = path.value.value;
path.replace(
typeBuilders.memberExpression(
typeBuilders.identifier(AllVarsVariableName),
typeBuilders.identifier(key)));
return false;
}
});
const modifiedUserCode = recast.prettyPrint(ast).code;
previousCode = astFromUserCode(userCode);
async function injectGlobal(ast: ESTree.Program): Promise {
visit(ast, {
visitProgram(path: Path): boolean {
if (path.scope.lookup('global') === null) {
const body = path.get('body');
body.get(body.value.length - 1).insertBefore(
b.variableDeclaration(
'var',
[
b.variableDeclarator(
b.identifier('global'),
b.thisExpression()
)
]
)
);
}
return false;
findNodeAtPosition(
this: Collection,
pos: number
): Collection {
const path = this.firstPath();
if (!path) {
return createCollection([]);
}
let targets: Array> = [];
let r = astTypes.visit(path, {
visitNode(p: NodePath) {
if (isPositionWithinNode(pos, p.node as Printable)) {
targets.push(p);
this.traverse(p);
return;
} else {
return false;
}
}
});
const last = targets[targets.length - 1];
return createCollection(last || []);
},
export async function rewriteGlobalLocals(program: ESTree.Program, currentModule: string,
context: PaeckchenContext): Promise {
context.logger.trace('plugin', `rewriteGlobalLocals [currentModule=${currentModule}]`);
let detectedFilename = false;
let detectedDirname = false;
const isKnownSymbol = (path: Path, name: string) =>
path.node.name === name && path.scope.lookup(name) === null;
visit(program, {
visitIdentifier(this: Visitor, path: Path): void {
if (isKnownSymbol(path, '__filename')) {
detectedFilename = true;
} else if (isKnownSymbol(path, '__dirname')) {
detectedDirname = true;
}
if (detectedFilename && detectedDirname) {
this.abort();
}
this.traverse(path);
}
});
if (detectedFilename || detectedDirname) {
program.body = [
b.expressionStatement(
function visitRequireArgument(ast, cb) {
types.visit(ast, {
visitCallExpression: function(path) {
if (this.isRequireExpression(path.node)) {
var arg = path.getValueProperty("arguments")[0];
if (n.Literal.check(arg)) {
cb(arg);
}
}
this.traverse(path);
},
isRequireExpression(node) {
return n.Identifier.check(node.callee) && node.callee.name === "require";
}
});
function rewriteNamespaceThisReferences(
program: estree.Program, namespaceName: string) {
astTypes.visit(program, {
visitExportNamedDeclaration:
(path: NodePath) => {
if (path.node.declaration &&
path.node.declaration.type === 'FunctionDeclaration') {
rewriteSingleScopeThisReferences(
path.node.declaration.body, namespaceName);
}
return false;
},
visitExportDefaultDeclaration:
(path: NodePath) => {
if (path.node.declaration &&
path.node.declaration.type === 'FunctionDeclaration') {
rewriteSingleScopeThisReferences(
path.node.declaration.body, namespaceName);
}
export function collectIdentifierNames(
program: estree.Program,
ignored: ReadonlySet): Set {
const identifiers = new Set();
astTypes.visit(program, {
visitIdentifier(path: NodePath): (boolean | void) {
const node = path.node;
if (!ignored.has(node)) {
identifiers.add(path.node.name);
}
this.traverse(path);
},
});
return identifiers;
}