Skip to content

Commit

Permalink
Update RewriteCallerCodeLocation to use goog.callerLocationIdInternal…
Browse files Browse the repository at this point in the history
…DoNotCallOrElse instead of goog.xid.

PiperOrigin-RevId: 700484608
  • Loading branch information
rahul-kamat authored and copybara-github committed Nov 26, 2024
1 parent dab888f commit b5097d0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 29 deletions.
25 changes: 16 additions & 9 deletions src/com/google/javascript/jscomp/RewriteCallerCodeLocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
* <p>When a function is called without providing the optional `here` argument, we will rewrite the
* function call to include the code location.
*
* <p>i.e. `signal(0,goog.xid(path/to/file.ts:lineno:charno))`
* <p>i.e. `signal(0, goog.callerLocationIdInternalDoNotCallOrElse(path/to/file.ts:lineno:charno))`
*/
class RewriteCallerCodeLocation implements CompilerPass {

Expand Down Expand Up @@ -216,7 +216,8 @@ public void visit(NodeTraversal t, Node n, Node parent) {
* Visits call expression nodes and checks if they require transformations. If they do, it
* rewrites the call expression node to include the code location.
*
* <p>E.g: `signal(0)` will be rewritten to `signal(0, goog.xid(path/to/file.ts:lineno:charno))`
* <p>E.g: `signal(0)` will be rewritten to `signal(0,
* goog.callerLocationIdInternalDoNotCallOrElse(path/to/file.ts:lineno:charno))`
*
* @param n call node
*/
Expand Down Expand Up @@ -272,28 +273,33 @@ private void visitCallNodeAndRewrite(Node n, NodeTraversal t) {
return;
}

// create the goog.xid(path/to/file.ts:lineno:charno) and add it as the last parameter.
// create the goog.callerLocationIdInternalDoNotCallOrElse(path/to/file.ts:lineno:charno) and
// add it as the last parameter.
Node xidCall = createGoogXidFilePathNode(n);
compiler.reportChangeToEnclosingScope(n);
n.addChildToBack(xidCall);
}

/**
* Creates a call node for "goog.xid(path/to/file.ts:lineno:charno)"
* Creates a call node for
* "goog.callerLocationIdInternalDoNotCallOrElse(path/to/file.ts:lineno:charno)"
*
* @param n call node of a function that needs to be rewritten to include the code location
* @return call node including code location i.e. "goog.xid(path/to/file.ts:lineno:charno)"
* @return call node including code location i.e.
* "goog.callerLocationIdInternalDoNotCallOrElse(path/to/file.ts:lineno:charno)"
*/
private Node createGoogXidFilePathNode(Node n) {
// googNode is "goog"
Node googNode = IR.name("goog");
googNode.srcrefIfMissing(n);

// googXid is "goog.xid" node
Node googXid = astFactory.createGetPropWithUnknownType(googNode, "xid");
// googXid is "goog.callerLocationIdInternalDoNotCallOrElse" node
Node googXid =
astFactory.createGetPropWithUnknownType(
googNode, "callerLocationIdInternalDoNotCallOrElse");
googXid.srcrefIfMissing(n);

// callNode is "goog.xid()" node
// callNode is "goog.callerLocationIdInternalDoNotCallOrElse()" node
Node callNode = astFactory.createCallWithUnknownType(googXid);
callNode.srcrefIfMissing(n);

Expand All @@ -303,7 +309,8 @@ private Node createGoogXidFilePathNode(Node n) {
n.getSourceFileName() + ":" + n.getLineno() + ":" + n.getCharno());
stringNode.srcrefIfMissing(n);

// turns callNode from "goog.xid()" to "goog.xid(path/to/file.ts:lineno:charno)"
// turns callNode from "goog.callerLocationIdInternalDoNotCallOrElse()" to
// "goog.callerLocationIdInternalDoNotCallOrElse(path/to/file.ts:lineno:charno)"
callNode.addChildToBack(stringNode);

return callNode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void testSimpleCallee() {
"signal();"),
lines(
"function signal(here = goog.callerLocation()) {}",
"signal(goog.xid('testcode:2:0'))"));
"signal(goog.callerLocationIdInternalDoNotCallOrElse('testcode:2:0'))"));
}

@Test
Expand All @@ -55,7 +55,8 @@ public void testSimpleCallee2() {
"const mySignal = (0, signal)(0);"),
lines(
"function signal(val, here = goog.callerLocation()) {}",
"const mySignal = signal(0, goog.xid('testcode:2:17'));"));
"const mySignal = signal(0,"
+ " goog.callerLocationIdInternalDoNotCallOrElse('testcode:2:17'));"));
}

@Test
Expand Down Expand Up @@ -101,7 +102,8 @@ public void testAssigned() {
"const mySignal = signal(0);"),
lines(
"function signal(val, here = goog.callerLocation()) {}",
"const mySignal = signal(0, goog.xid('testcode:2:17'));"));
"const mySignal = signal(0,"
+ " goog.callerLocationIdInternalDoNotCallOrElse('testcode:2:17'));"));
}

@Test
Expand All @@ -115,7 +117,8 @@ public void testAliasedFunctionDoesNotRewrite() {
"const myComputed = computed(() => mySignal[0]() % 2 === 0);"),
lines(
"function signal(val, here = goog.callerLocation()) {}",
"const mySignal = signal(0, goog.xid('testcode:2:17'));",
"const mySignal = signal(0,"
+ " goog.callerLocationIdInternalDoNotCallOrElse('testcode:2:17'));",
"const myComputed = computed(() => mySignal[0]() % 2 === 0);"));
}

Expand All @@ -127,7 +130,8 @@ public void testArrayDestructuring() {
"const [foo, setFoo] = signal(0);"),
lines(
"function signal(val, here = goog.callerLocation()) {}",
"const [foo, setFoo] = signal(0, goog.xid('testcode:2:22'));"));
"const [foo, setFoo] = signal(0,"
+ " goog.callerLocationIdInternalDoNotCallOrElse('testcode:2:22'));"));
}

@Test
Expand All @@ -138,7 +142,8 @@ public void testObjectDestructuring() {
"const {foo, setFoo} = signal(0);"),
lines(
"function signal(val, here = goog.callerLocation()) {}",
"const {foo, setFoo} = signal(0, goog.xid('testcode:2:22'));"));
"const {foo, setFoo} = signal(0,"
+ " goog.callerLocationIdInternalDoNotCallOrElse('testcode:2:22'));"));
}

@Test
Expand All @@ -149,7 +154,8 @@ public void testPropertyAssignment() {
"const obj = {prop: signal(0)};"),
lines(
"function signal(val, here = goog.callerLocation()) {}",
"const obj = {prop: signal(0, goog.xid('testcode:2:19'))};"));
"const obj = {prop: signal(0,"
+ " goog.callerLocationIdInternalDoNotCallOrElse('testcode:2:19'))};"));
}

@Test
Expand All @@ -160,7 +166,8 @@ public void testTernaryAssignment() {
"const maybeSignal = Math.random() ? signal(0) : null;"),
lines(
"function signal(val, here = goog.callerLocation()) {}",
"const maybeSignal = Math.random() ? signal(0, goog.xid('testcode:2:36')) : null;"));
"const maybeSignal = Math.random() ? signal(0,"
+ " goog.callerLocationIdInternalDoNotCallOrElse('testcode:2:36')) : null;"));
}

@Test
Expand All @@ -171,7 +178,8 @@ public void testIntermediateFunction() {
"const intermediateFunction = (() => signal(0))();"),
lines(
"function signal(val, here = goog.callerLocation()) {}",
"const intermediateFunction = (() => signal(0, goog.xid('testcode:2:36')))();"));
"const intermediateFunction = (() => signal(0,"
+ " goog.callerLocationIdInternalDoNotCallOrElse('testcode:2:36')))();"));
}

@Test
Expand All @@ -182,8 +190,9 @@ public void testArray() {
"const signalArray = [signal(0), signal(1)];"),
lines(
"function signal(val, here = goog.callerLocation()) {}",
"const signalArray = [signal(0, goog.xid('testcode:2:21')), signal(1,"
+ " goog.xid('testcode:2:32'))];"));
"const signalArray = [signal(0,"
+ " goog.callerLocationIdInternalDoNotCallOrElse('testcode:2:21')), signal(1,"
+ " goog.callerLocationIdInternalDoNotCallOrElse('testcode:2:32'))];"));
}

@Test
Expand Down Expand Up @@ -281,7 +290,7 @@ public void testTransitiveUsage() {
lines(
"function foo(val, here = goog.callerLocation()) {}",
"function bar(val) {",
" foo(val, goog.xid('testcode:3:2'));",
" foo(val, goog.callerLocationIdInternalDoNotCallOrElse('testcode:3:2'));",
"}",
"bar(0)"));

Expand All @@ -297,9 +306,9 @@ public void testTransitiveUsage() {
lines(
"function foo(val, here = goog.callerLocation()) {}",
"function bar(val, here = goog.callerLocation()) {",
" foo(val, goog.xid('testcode:3:2'));",
" foo(val, goog.callerLocationIdInternalDoNotCallOrElse('testcode:3:2'));",
"}",
"bar(0, goog.xid('testcode:5:0'));"));
"bar(0, goog.callerLocationIdInternalDoNotCallOrElse('testcode:5:0'));"));
}

@Test
Expand Down Expand Up @@ -340,9 +349,9 @@ public void testMultipleDefaultParameters() {
"foo()",
"foo(1)",
"foo(1, 2)",
"foo(1, 2, goog.xid('customString'))",
"foo(1, 2, goog.xid('customString'), 3)",
"foo(1, 2, goog.xid('customString'), 3, 4)"),
"foo(1, 2, goog.callerLocationIdInternalDoNotCallOrElse('customString'))",
"foo(1, 2, goog.callerLocationIdInternalDoNotCallOrElse('customString'), 3)",
"foo(1, 2, goog.callerLocationIdInternalDoNotCallOrElse('customString'), 3, 4)"),
JSC_CALLER_LOCATION_POSITION_ERROR);
}

Expand Down Expand Up @@ -384,7 +393,8 @@ public void testModuleRewriting() {
" return val;",
"}",
"module$exports$main.signal = module$contents$main_signal;",
"(0,module$exports$main.signal)(0, goog.xid('testcode:5:0'));"));
"(0,module$exports$main.signal)(0,"
+ " goog.callerLocationIdInternalDoNotCallOrElse('testcode:5:0'));"));
}

@Test
Expand All @@ -406,7 +416,7 @@ public void testScopeRewriting() {
" signal();", // not rewritten
" }",
"}",
"signal(goog.xid('testcode:7:0'));" // rewritten
"signal(goog.callerLocationIdInternalDoNotCallOrElse('testcode:7:0'));" // rewritten
));

test(
Expand All @@ -422,7 +432,7 @@ public void testScopeRewriting() {
"function signal() {}",
"function outter() {",
" function signal(here = goog.callerLocation()) {}",
" signal(goog.xid('testcode:4:2'));", // rewritten
" signal(goog.callerLocationIdInternalDoNotCallOrElse('testcode:4:2'));", // rewritten
"}",
"signal();" // not rewritten
));
Expand Down

0 comments on commit b5097d0

Please sign in to comment.