@@ -43,11 +43,10 @@ class RewriteCallerCodeLocation implements CompilerPass {
43
43
"Please make sure there is only one goog.callerLocation argument in your function's"
44
44
+ " parameter list, and it is the first optional argument in the list" );
45
45
46
- static final DiagnosticType JSC_CALLER_LOCATION_ERROR =
46
+ static final DiagnosticType JSC_CALLER_LOCATION_MISUSE_ERROR =
47
47
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" );
51
50
52
51
static final DiagnosticType JSC_UNDEFINED_CODE_LOCATION_ERROR =
53
52
DiagnosticType .error (
@@ -97,12 +96,27 @@ public void visit(NodeTraversal t, Node n, Node parent) {
97
96
visitParamListAndAddCallerLocationFunctionNames (n , t );
98
97
}
99
98
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.
102
100
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
+ }
106
120
}
107
121
}
108
122
}
0 commit comments