Skip to content

Commit 794ed9f

Browse files
rahul-kamatcopybara-github
authored andcommitted
Throw an error if goog.callerLocation() is not used in a function parameter list as a default parameter initializer.
PiperOrigin-RevId: 693091444
1 parent dfa880a commit 794ed9f

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

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

+23-9
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,10 @@ class RewriteCallerCodeLocation implements CompilerPass {
4343
"Please make sure there is only one goog.callerLocation argument in your function's"
4444
+ " parameter list, and it is the first optional argument in the list");
4545

46-
static final DiagnosticType JSC_CALLER_LOCATION_ERROR =
46+
static final DiagnosticType JSC_CALLER_LOCATION_MISUSE_ERROR =
4747
DiagnosticType.error(
48-
"JSC_CALLER_LOCATION_ERROR",
49-
"goog.callerLocation should only be used as a default value in a function's parameter"
50-
+ " list");
48+
"JSC_CALLER_LOCATION_MISUSE_ERROR",
49+
"goog.callerLocation should only be used as a default parameter initializer");
5150

5251
static final DiagnosticType JSC_UNDEFINED_CODE_LOCATION_ERROR =
5352
DiagnosticType.error(
@@ -97,12 +96,27 @@ public void visit(NodeTraversal t, Node n, Node parent) {
9796
visitParamListAndAddCallerLocationFunctionNames(n, t);
9897
}
9998
if (n.isCall()) {
100-
// Throw an error when goog.callerLocation() is NOT used as a default value in a
101-
// function's parameter.
99+
// Check for misuse of goog.callerLocation.
102100
Node firstChild = n.getFirstChild();
103-
if (GOOG_CALLER_LOCATION_QUALIFIED_NAME.matches(firstChild)
104-
&& !n.getParent().isDefaultValue()) {
105-
compiler.report(JSError.make(firstChild, JSC_CALLER_LOCATION_ERROR));
101+
if (GOOG_CALLER_LOCATION_QUALIFIED_NAME.matches(firstChild)) {
102+
if (!parent.isDefaultValue()) {
103+
// Throw an error when goog.callerLocation() is NOT used as a default value in a
104+
// function's parameter.
105+
compiler.report(JSError.make(firstChild, JSC_CALLER_LOCATION_MISUSE_ERROR));
106+
}
107+
if (parent.getParent().isStringKey()) {
108+
// Throw an error when goog.callerLocation() is used in an object literal.
109+
// E.g:
110+
// function foo({val1, val2, here = goog.callerLocation()}) {}
111+
// The AST for `here = goog.callerLocation()`looks like:
112+
// STRING_KEY here (This node tells us we are in an object literal)
113+
// DEFAULT_VALUE
114+
// NAME here 1:26
115+
// CALL 1:33
116+
// GETPROP callerLocation
117+
// NAME goog 1:33
118+
compiler.report(JSError.make(parent.getParent(), JSC_CALLER_LOCATION_MISUSE_ERROR));
119+
}
106120
}
107121
}
108122
}

test/com/google/javascript/jscomp/RewriteCallerCodeLocationTest.java

+5-7
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package com.google.javascript.jscomp;
1818

1919
import static com.google.javascript.jscomp.RewriteCallerCodeLocation.JSC_ANONYMOUS_FUNCTION_CODE_LOCATION_ERROR;
20-
import static com.google.javascript.jscomp.RewriteCallerCodeLocation.JSC_CALLER_LOCATION_ERROR;
20+
import static com.google.javascript.jscomp.RewriteCallerCodeLocation.JSC_CALLER_LOCATION_MISUSE_ERROR;
2121
import static com.google.javascript.jscomp.RewriteCallerCodeLocation.JSC_CALLER_LOCATION_POSITION_ERROR;
2222
import static com.google.javascript.jscomp.RewriteCallerCodeLocation.JSC_UNDEFINED_CODE_LOCATION_ERROR;
2323

@@ -296,19 +296,17 @@ public void testErrorOnCallerLocationNotUsedAsDefaultParam() {
296296
"function foo() {", //
297297
" const x = goog.callerLocation();", //
298298
"}"),
299-
JSC_CALLER_LOCATION_ERROR);
299+
JSC_CALLER_LOCATION_MISUSE_ERROR);
300300
}
301301

302302
@Test
303303
public void testDestructuringOptionsBag() {
304-
// TODO(user): this should error
305-
test(
304+
// Error if `goog.callerLocation()` is used in an object literal
305+
testError(
306306
lines(
307307
"function foo({val1, val2, here = goog.callerLocation()}) {}", //
308308
"foo({val1:0, val2:0})"),
309-
lines(
310-
"function foo({val1, val2, here = goog.callerLocation()}) {}",
311-
"foo({val1:0, val2:0})"));
309+
JSC_CALLER_LOCATION_MISUSE_ERROR);
312310
}
313311

314312
@Test

0 commit comments

Comments
 (0)