Skip to content

Commit dff5344

Browse files
rahul-kamatcopybara-github
authored andcommitted
Enable RewriteCallerCodeLocation compiler pass
This CL enables the `goog.callerLocation` feature for all users in google3, where JSCompiler will add tracing data to the call sites of functions. To get tracing data, users need to annotate their function with a parameter that has `goog.callerLocation()` as the default value This is the flow of how JSCompiler will transform code: User writes: ``` function signal(here = goog.callerLocation()) {} signal(); signal(); ``` RewriteCallerCodeLocation will rewrite the callsite of this function to include the file paths + lineno + charno of the callsite: ``` function signal(here = goog.callerLocation()) {} signal(goog.callerLocationIdInternalDoNotCallOrElse('testcode:2:0')) signal(goog.callerLocationIdInternalDoNotCallOrElse('testcode:3:0')) ``` ReplaceIdGenerators will obfuscate the file paths: ``` function signal(here = goog.callerLocation()) {} signal('a') signal('b') ``` PiperOrigin-RevId: 702883718
1 parent c564728 commit dff5344

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/com/google/javascript/jscomp/DefaultPassConfig.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -443,8 +443,7 @@ protected PassListBuilder getChecks() {
443443

444444
checks.maybeAdd(checkConsts);
445445

446-
// TODO(user): Enable this pass when it is ready.
447-
// checks.maybeAdd(rewriteCallerCodeLocation);
446+
checks.maybeAdd(rewriteCallerCodeLocation);
448447

449448
if (!options.getConformanceConfigs().isEmpty()) {
450449
checks.maybeAdd(checkConformance);

test/com/google/javascript/jscomp/integration/IntegrationTest.java

+33
Original file line numberDiff line numberDiff line change
@@ -4592,6 +4592,39 @@ public void testGoogScopeWithAngular() {
45924592
"$jscomp$scope$98447280$0$fn[\"$inject\"] = [\"a\", \"b\"];"));
45934593
}
45944594

4595+
@Test
4596+
public void testRewriteCallerCodeLocation() {
4597+
// This unit test tests the following:
4598+
// (1) RewriteCallerCodeLocation pass adds the code location to the call-site of functions
4599+
// that have goog.callerLocation as a default parameter.
4600+
// (2) ReplaceIdGenerators pass replaces the code location with an obfuscated string.
4601+
CompilerOptions options = createCompilerOptions();
4602+
4603+
options.setReplaceIdGenerators(true);
4604+
4605+
test(
4606+
options,
4607+
lines(
4608+
"/** @idGenerator {consistent} */",
4609+
"goog.callerLocationIdInternalDoNotCallOrElse = function(id) {",
4610+
" return /** @type {!goog.CodeLocation} */ (id);",
4611+
"};",
4612+
"function signal(here = goog.callerLocation()) {}",
4613+
"const mySignal = signal();",
4614+
"const mySignal2 = signal();",
4615+
"const mySignal3 = signal();"),
4616+
lines(
4617+
"goog.callerLocationIdInternalDoNotCallOrElse = function(id) {",
4618+
" return id;",
4619+
"};",
4620+
"function signal(here) {",
4621+
" here = here === void 0 ? goog.callerLocation() : here;",
4622+
"}",
4623+
"var mySignal = signal('a');",
4624+
"var mySignal2 = signal('b');",
4625+
"var mySignal3 = signal('c');"));
4626+
}
4627+
45954628
@Test
45964629
public void testGitHubIssue3861() {
45974630
CompilerOptions options = createCompilerOptions();

0 commit comments

Comments
 (0)