Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
| es.IfStatement
| es.ConditionalExpression
| es.LogicalExpression
| es.ForStatement
| es.WhileStatement
) {
const { line, column } = node.loc!.start
const test = node.type === 'LogicalExpression' ? 'left' : 'test'
node[test] = create.callExpression(globalIds.boolOrErr, [
node[test],
create.literal(line),
create.literal(column)
])
}
simple(program, {
IfStatement: transform,
ConditionalExpression: transform,
LogicalExpression: transform,
ForStatement: transform,
WhileStatement: transform
})
}
function transformPropertyAssignment(program: es.Program) {
simple(program, {
AssignmentExpression(node: es.AssignmentExpression) {
if (node.left.type === 'MemberExpression') {
const { object, property, computed, loc } = node.left
const { line, column } = loc!.start
create.mutateToCallExpression(node, globalIds.setProp, [
object as es.Expression,
getComputedProperty(computed, property),
node.right,
create.literal(line),
create.literal(column)
])
}
}
})
}
function getAllIdentifiersUsed(program: es.Program) {
const identifiers = new Set()
let variableScope = NATIVE_STORAGE[contextId].globals
while (variableScope !== null) {
for (const name of variableScope.variables.keys()) {
identifiers.add(name)
}
variableScope = variableScope.previousScope
}
simple(program, {
Identifier(node: es.Identifier) {
identifiers.add(node.name)
},
Pattern(node: es.Pattern) {
if (node.type === 'Identifier') {
identifiers.add(node.name)
} else if (node.type === 'MemberExpression') {
if (node.object.type === 'Identifier') {
identifiers.add(node.object.name)
}
}
}
})
return identifiers
}
function generateFunctionsToStringMap(program: es.Program) {
const map: Map = new Map()
simple(program, {
ArrowFunctionExpression(node: es.ArrowFunctionExpression) {
map.set(node, generate(node))
},
FunctionDeclaration(node: es.FunctionDeclaration) {
map.set(node, generate(node))
}
})
return map
}
create.callExpression(globalIds.throwIfTimeout, [
create.identifier(startTimeConst),
getRuntimeAst(),
create.literal(line),
create.literal(column)
])
)
)
}
}
newStatements.push(statement)
}
node.body = newStatements
}
simple(program, {
Program: instrumentLoops,
BlockStatement: instrumentLoops
})
}
function transformUnaryAndBinaryOperationsToFunctionCalls(program: es.Program) {
simple(program, {
BinaryExpression(node: es.BinaryExpression) {
const { line, column } = node.loc!.start
const { operator, left, right } = node
create.mutateToCallExpression(node, globalIds.binaryOp, [
create.literal(operator),
left,
right,
create.literal(line),
create.literal(column)
])
},
UnaryExpression(node: es.UnaryExpression) {
const { line, column } = node.loc!.start
const { operator, argument } = node as es.UnaryExpression
create.mutateToCallExpression(node, globalIds.unaryOp, [
create.literal(operator),
function transformPropertyAccess(program: es.Program) {
simple(program, {
MemberExpression(node: es.MemberExpression) {
const { object, property, computed, loc } = node
const { line, column } = loc!.start
create.mutateToCallExpression(node, globalIds.getProp, [
object as es.Expression,
getComputedProperty(computed, property),
create.literal(line),
create.literal(column)
])
}
})
}
function determineExecutionMethod(theOptions: IOptions, context: Context, program: Program) {
let isNativeRunnable
if (theOptions.executionMethod === 'auto') {
if (context.executionMethod === 'auto') {
if (verboseErrors) {
isNativeRunnable = false
} else if (areBreakpointsSet()) {
isNativeRunnable = false
} else {
let hasDeuggerStatement = false
simple(program, {
DebuggerStatement(node: DebuggerStatement) {
hasDeuggerStatement = true
}
})
isNativeRunnable = !hasDeuggerStatement
}
context.executionMethod = isNativeRunnable ? 'native' : 'interpreter'
} else {
isNativeRunnable = context.executionMethod === 'native'
}
} else {
isNativeRunnable = theOptions.executionMethod === 'native'
}
return isNativeRunnable
}
function transformFunctionDeclarationsToArrowFunctions(
program: es.Program,
functionsToStringMap: Map
) {
simple(program, {
FunctionDeclaration(node) {
const { id, params, body } = node as es.FunctionDeclaration
node.type = 'VariableDeclaration'
node = node as es.VariableDeclaration
const asArrowFunction = create.blockArrowFunction(params as es.Identifier[], body)
functionsToStringMap.set(asArrowFunction, functionsToStringMap.get(node)!)
node.declarations = [
{
type: 'VariableDeclarator',
id: id as es.Identifier,
init: asArrowFunction
}
]
node.kind = 'const'
}
})