Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions call-trace.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var fs = require('fs');
var profileReporter = require('./create-profile.min.js');

var acorn = require('acorn');
var walk = require('acorn/dist/walk');
var walk = require('acorn-walk');

var TRACE_VAR = '_$wɔk';

Expand All @@ -25,7 +25,8 @@ var argv = require('yargs')
.argv;

var src = fs.readFileSync(argv._[0], 'utf8');
console.log(instrumentCode(src));
const instrumentedCode = instrumentCode(src);
console.log(instrumentedCode);

// Adapted from acorn.walk.ancestor to do pre-order visits and track more state.
function walkAST(node, visitors, state) {
Expand Down Expand Up @@ -100,9 +101,9 @@ function extractFunctionInfo(code, node, parent) {
blockEnd: node.body.range[1]
};

// TODO(bckenny): ArrowFunctionExpression
if (node.type === 'ArrowFunctionExpression') {
throw new Error('arrow functions not yet supported.');
fnInfo.name = "(anon arrow)";
return fnInfo;
}

if (node.type === 'FunctionDeclaration') {
Expand All @@ -112,7 +113,7 @@ function extractFunctionInfo(code, node, parent) {

if (node.type === 'FunctionExpression') {
// If function expression has an identifier, just use that.
if (node.id && node.id.name) {
if (node.id?.name) {
fnInfo.name = node.id.name;
return fnInfo;
}
Expand All @@ -132,7 +133,12 @@ function extractFunctionInfo(code, node, parent) {
fnInfo.name = '(anonymous)';
return fnInfo;
}
if (parent.key.name) {
fnInfo.name = parent.key.name;
return fnInfo;
}

console.error({node});
throw new Error('unhandled type of FunctionExpression at line ' + node.loc.start.line);
}
}
Expand Down Expand Up @@ -235,6 +241,8 @@ function createExitTrace(exit) {

function instrumentCode(src) {
var ast = acorn.parse(src, {
sourceType: 'module',
ecmaVersion: 'latest',
allowHashBang: true,
ranges: true,
locations: true
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
},
"homepage": "https://github.com/brendankenny/call-trace#readme",
"dependencies": {
"acorn": "^3.0.2",
"acorn": "^8.8.2",
"acorn-walk": "^8.2.0",
"yargs": "^3.32.0"
},
"devDependencies": {
Expand Down