Skip to content

Commit

Permalink
Instrument: Fix insertion of tracker comments [fix]
Browse files Browse the repository at this point in the history
Workaround for babel/babel#15138
  • Loading branch information
overlookmotel committed Nov 5, 2022
1 parent c67f913 commit 1b27938
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
19 changes: 9 additions & 10 deletions lib/instrument/visitors/class.js
Expand Up @@ -279,9 +279,6 @@ function visitClass(classNode, parent, key, className, state) {
externalTrail.length -= 2;
if (!parentIsStrict) state.isStrict = false;

// Insert tracking comment
insertTrackerComment(fn.id, FN_TYPE_CLASS, classNode, 'inner', state);

// Temporarily remove from AST so is not included in parent function's serialized AST
parent[key] = null;

Expand Down Expand Up @@ -441,19 +438,21 @@ function instrumentClass(
}

// Wrap with var for `super` target if `super` used in class
// and restore to original place in AST
const superVarNode = getSuperVarNode(superBlock);
if (superVarNode) {
classNode = wrapClassWithSuperTargetVar(classNode, parent, key, superBlock, superVarNode, state);
}

// Restore to original place in AST
parent[key] = classNode;
parent[key] = superVarNode
? wrapClassWithSuperTargetVar(classNode, parent, key, superBlock, superVarNode, state)
: classNode;

// Add tracker code + block vars to constructor
instrumentFunctionOrClassConstructor(
constructorNode, fn, constructorParamsBlock, constructorBodyBlock, astJson, state
);

// Insert tracking comment
const commentHolderNode = classNode.id || classNode.superClass || classNode.body;
insertTrackerComment(fn.id, FN_TYPE_CLASS, commentHolderNode, 'leading', state);

// TODO Handle prototype properties
}

Expand Down Expand Up @@ -509,7 +508,7 @@ function wrapClassWithSuperTargetVar(classNode, parent, key, superBlock, superVa
function getClassDeclarationReplacement(classNode, superVarNode) {
classNode.type = 'ClassExpression';
return t.variableDeclaration(
'let', [t.variableDeclarator(classNode.id, t.assignmentExpression('=', superVarNode, classNode))]
'let', [t.variableDeclarator({...classNode.id}, t.assignmentExpression('=', superVarNode, classNode))]
);
}

Expand Down
5 changes: 3 additions & 2 deletions lib/instrument/visitors/function.js
Expand Up @@ -667,8 +667,9 @@ function createTrackerCall(scopes, fnInfoVarNode, state) {
* @returns {undefined}
*/
function insertFunctionDeclarationOrExpressionTrackerComment(fn, node, state) {
// Insert tracker comment after `function` keyword
insertTrackerComment(fn.id, getFunctionType(node), node, 'inner', state);
// Insert tracker comment before identifier, or before 1st param or before function body
const commentHolderNode = node.id || (node.params.length !== 0 ? node.params[0] : node.body);
insertTrackerComment(fn.id, getFunctionType(node), commentHolderNode, 'leading', state);
}

/**
Expand Down

0 comments on commit 1b27938

Please sign in to comment.