test('works with var VariableDeclarator', () => { const ast = acorn.parse('var bob = 0;'); const functionTracer = new FunctionTracer(ast); const { bob } = functionTracer.contexts[0]; assert.equal(bob.context['@contextType'], 'function'); });
test('works with var & let VariableDeclarator together', () => { const ast = acorn.parse(`var bob = 0; for (let i = 0; i < 1; i++) { let pop = 0; }`); const functionTracer = new FunctionTracer(ast); assert.equal(functionTracer.contexts[0].bob.context['@contextType'], 'function'); assert.equal(functionTracer.contexts[0].i, undefined); assert.equal(functionTracer.contexts[0].pop, undefined); assert.equal(functionTracer.contexts[1].bob.context['@contextType'], 'function'); assert.equal(functionTracer.contexts[1].i.context['@contextType'], 'function'); assert.equal(functionTracer.contexts[1].pop, undefined); assert.equal(functionTracer.contexts[2].bob.context['@contextType'], 'function'); assert.equal(functionTracer.contexts[2].i.context['@contextType'], 'function'); assert.equal(functionTracer.contexts[2].pop, undefined); assert.equal(functionTracer.contexts[3].bob.context['@contextType'], 'function'); assert.equal(functionTracer.contexts[3].i.context['@contextType'], 'function'); assert.equal(functionTracer.contexts[3].pop.context['@contextType'], 'function'); });
function run(code) { let result try { if (!tokenize) { result = acorn.parse(code, options) } else { result = [] let tokenizer = acorn.tokenizer(code, options), token do { token = tokenizer.getToken() result.push(token) } while (token.type !== acorn.tokTypes.eof) } } catch (e) { console.error(infile && infile !== "-" ? e.message.replace(/\(\d+:\d+\)$/, m => m.slice(0, 1) + infile + " " + m.slice(1)) : e.message) process.exit(1) } if (!silent) console.log(JSON.stringify(result, null, compact ? null : 2)) }
test('works with generic VariableDeclarator', () => { const ast = acorn.parse('var bob = 0;'); const functionTracer = new FunctionTracer(ast); const { bob } = functionTracer.contexts[0]; assert.equal(bob.ast, ast.body[0].declarations[0]); assert.equal(bob.context, functionTracer.contexts[0]); assert.equal(bob.name, 'bob'); assert.equal(bob.origin, 'declaration'); assert.equal(bob.assignable, true); assert.equal(bob.inForLoopTest, null); assert.equal(bob.inForLoopInit, false); assert.equal(functionTracer.declarations[0], bob); });
const AST = acorn.parse(text, { ecmaVersion: 2020 });
test('works with Program', () => { const ast = acorn.parse(`var i;`); const functionTracer = new FunctionTracer(ast); assert.ok(functionTracer.functionContexts.length > 0); });
test('works with ForStatement', () => { const ast = acorn.parse(`for (let i = 0; i < 1; i++) { call(); }`); const functionTracer = new FunctionTracer(ast.body[0]); assert.equal(functionTracer.declarations[0].name, 'i'); assert.equal(functionTracer.contexts.length, 4); });
fn() { return payloads.map(({ payload, options }) => { let count = 0; // Test the tokenizer by counting the resulting tokens. for (const token of acorn.tokenizer(payload, options)) { count++; } // Test the parser. const ast = acorn.parse(payload, options); // Test the AST walker. walk.full(ast, node => count++); return count; }); }
function getOriginalArguments(site) { const lineNr = site.getLineNumber(); const colNr = site.getColumnNumber(); const fileContent = fs.readFileSync(site.getFileName()).toString(); const parsed = acorn.parse(fileContent, { locations: true }); let original; // Walk the AST to find the function call walk.simple(parsed, { CallExpression(node) { if (node.loc.start.line === lineNr && node.loc.start.column === colNr - 1) { // Convert the arguments from the AST to a readable string original = node.arguments.map(arg => astring.generate(arg)); } }, }); return original; }
export function generateBinding (exp: ?string): any { if (exp && typeof exp === 'string') { let ast = null try { ast = acorn.parse(`(${exp})`) } catch (e) { // warn(`Failed to parse the expression: "${exp}"`) return '' } let output = '' walk.simple(ast, { Expression (node) { output = nodeToBinding(node) } }) return output } }
let ast; try { ast = acorn.parse(code, { ecmaVersion: 2019 }); } catch (err) { return { head: '', example: code };
tree = acorn.parse(code, { allowReturnOutsideFunction: true, // used when rewriting function bodies plugins: {
test('works with let VariableDeclarator', () => { const ast = acorn.parse('let bob = 0;'); const functionTracer = new FunctionTracer(ast); const { bob } = functionTracer.contexts[0]; assert.equal(bob.context['@contextType'], 'function'); });