-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
ScopedAliases.java
791 lines (702 loc) · 28.7 KB
/
ScopedAliases.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
/*
* Copyright 2010 The Closure Compiler Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.javascript.jscomp;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import com.google.common.base.Preconditions;
import com.google.common.collect.HashMultiset;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Multiset;
import com.google.javascript.jscomp.CompilerOptions.AliasTransformation;
import com.google.javascript.jscomp.CompilerOptions.AliasTransformationHandler;
import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback;
import com.google.javascript.jscomp.NodeTraversal.Callback;
import com.google.javascript.rhino.IR;
import com.google.javascript.rhino.JSDocInfo;
import com.google.javascript.rhino.JSDocInfoBuilder;
import com.google.javascript.rhino.Node;
import com.google.javascript.rhino.SourcePosition;
import com.google.javascript.rhino.Token;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.Nullable;
/**
* Process aliases in goog.scope blocks.
*
* <pre>
* goog.scope(function() {
* var dom = goog.dom;
* var DIV = dom.TagName.DIV;
*
* dom.createElement(DIV);
* });
* </pre>
*
* should become
*
* <pre>
* goog.dom.createElement(goog.dom.TagName.DIV);
* </pre>
*
* The advantage of using goog.scope is that the compiler will *guarantee*
* the anonymous function will be inlined, even if it can't prove
* that it's semantically correct to do so. For example, consider this case:
*
* <pre>
* goog.scope(function() {
* goog.getBar = function () { return alias; };
* ...
* var alias = foo.bar;
* })
* </pre>
*
* <p>In theory, the compiler can't inline 'alias' unless it can prove that
* goog.getBar is called only after 'alias' is defined. In practice, the
* compiler will inline 'alias' anyway, at the risk of 'fixing' bad code.
*
* @author robbyw@google.com (Robby Walker)
*/
class ScopedAliases implements HotSwapCompilerPass {
/** Name used to denote an scoped function block used for aliasing. */
static final String SCOPING_METHOD_NAME = "goog.scope";
private final AbstractCompiler compiler;
private final PreprocessorSymbolTable preprocessorSymbolTable;
private final AliasTransformationHandler transformationHandler;
// Errors
static final DiagnosticType GOOG_SCOPE_MUST_BE_ALONE = DiagnosticType.error(
"JSC_GOOG_SCOPE_MUST_BE_ALONE",
"The call to goog.scope must be alone in a single statement.");
static final DiagnosticType GOOG_SCOPE_MUST_BE_IN_GLOBAL_SCOPE = DiagnosticType.error(
"JSC_GOOG_SCOPE_MUST_BE_IN_GLOBAL_SCOPE",
"The call to goog.scope must be in the global scope.");
static final DiagnosticType GOOG_SCOPE_HAS_BAD_PARAMETERS =
DiagnosticType.error(
"JSC_GOOG_SCOPE_HAS_BAD_PARAMETERS",
"The call to goog.scope must take only a single parameter. It must"
+ " be an anonymous function that itself takes no parameters.");
static final DiagnosticType GOOG_SCOPE_REFERENCES_THIS = DiagnosticType.error(
"JSC_GOOG_SCOPE_REFERENCES_THIS",
"The body of a goog.scope function cannot reference 'this'.");
static final DiagnosticType GOOG_SCOPE_USES_RETURN = DiagnosticType.error(
"JSC_GOOG_SCOPE_USES_RETURN",
"The body of a goog.scope function cannot use 'return'.");
static final DiagnosticType GOOG_SCOPE_USES_THROW = DiagnosticType.error(
"JSC_GOOG_SCOPE_USES_THROW",
"The body of a goog.scope function cannot use 'throw'.");
static final DiagnosticType GOOG_SCOPE_ALIAS_REDEFINED = DiagnosticType.error(
"JSC_GOOG_SCOPE_ALIAS_REDEFINED",
"The alias {0} is assigned a value more than once.");
static final DiagnosticType GOOG_SCOPE_ALIAS_CYCLE = DiagnosticType.error(
"JSC_GOOG_SCOPE_ALIAS_CYCLE",
"The aliases {0} has a cycle.");
static final DiagnosticType GOOG_SCOPE_NON_ALIAS_LOCAL = DiagnosticType.error(
"JSC_GOOG_SCOPE_NON_ALIAS_LOCAL",
"The local variable {0} is in a goog.scope and is not an alias.");
static final DiagnosticType GOOG_SCOPE_INVALID_VARIABLE = DiagnosticType.error(
"JSC_GOOG_SCOPE_INVALID_VARIABLE",
"The variable {0} cannot be declared in this scope");
private final Multiset<String> scopedAliasNames = HashMultiset.create();
ScopedAliases(AbstractCompiler compiler,
@Nullable PreprocessorSymbolTable preprocessorSymbolTable,
AliasTransformationHandler transformationHandler) {
this.compiler = compiler;
this.preprocessorSymbolTable = preprocessorSymbolTable;
this.transformationHandler = transformationHandler;
}
@Override
public void process(Node externs, Node root) {
hotSwapScript(root, null);
}
@Override
public void hotSwapScript(Node root, Node originalRoot) {
Traversal traversal = new Traversal();
NodeTraversal.traverseEs6(compiler, root, traversal);
if (!traversal.hasErrors()) {
// Apply the aliases.
List<AliasUsage> aliasWorkQueue =
new ArrayList<>(traversal.getAliasUsages());
while (!aliasWorkQueue.isEmpty()) {
List<AliasUsage> newQueue = new ArrayList<>();
for (AliasUsage aliasUsage : aliasWorkQueue) {
if (aliasUsage.referencesOtherAlias()) {
newQueue.add(aliasUsage);
} else {
aliasUsage.applyAlias(compiler);
}
}
// Prevent an infinite loop.
if (newQueue.size() == aliasWorkQueue.size()) {
Var cycleVar = newQueue.get(0).aliasVar;
compiler.report(JSError.make(
cycleVar.getNode(), GOOG_SCOPE_ALIAS_CYCLE, cycleVar.getName()));
break;
} else {
aliasWorkQueue = newQueue;
}
}
// Remove the alias definitions.
for (Node aliasDefinition : traversal.getAliasDefinitionsInOrder()) {
compiler.reportChangeToEnclosingScope(aliasDefinition);
if (NodeUtil.isNameDeclaration(aliasDefinition.getParent())
&& aliasDefinition.getParent().hasOneChild()) {
aliasDefinition.getParent().detach();
} else {
aliasDefinition.detach();
}
}
// Collapse the scopes.
for (Node scopeCall : traversal.getScopeCalls()) {
Node expressionWithScopeCall = scopeCall.getParent();
Node scopeClosureBlock = scopeCall.getLastChild().getLastChild();
scopeClosureBlock.detach();
expressionWithScopeCall.replaceWith(scopeClosureBlock);
NodeUtil.markFunctionsDeleted(expressionWithScopeCall, compiler);
compiler.reportChangeToEnclosingScope(scopeClosureBlock);
NodeUtil.tryMergeBlock(scopeClosureBlock, false);
}
}
}
private abstract static class AliasUsage {
final Var aliasVar;
final Node aliasReference;
AliasUsage(Var aliasVar, Node aliasReference) {
this.aliasVar = aliasVar;
this.aliasReference = aliasReference;
}
/** Checks to see if this references another alias. */
public boolean referencesOtherAlias() {
Node aliasDefinition = aliasVar.getInitialValue();
String qname = getAliasedNamespace(aliasDefinition);
int dotIndex = qname.indexOf('.');
String rootName = dotIndex == -1 ? qname : qname.substring(0, dotIndex);
Var otherAliasVar = aliasVar.getScope().getOwnSlot(rootName);
return otherAliasVar != null;
}
public abstract void applyAlias(AbstractCompiler compiler);
}
private static boolean isValidAliasRhs(Node rhs) {
switch (rhs.getToken()) {
case GETPROP:
return isValidAliasRhs(rhs.getFirstChild());
case NAME:
return true;
case CALL:
return NodeUtil.isCallTo(rhs, "goog.module.get");
default:
return false;
}
}
private static boolean isAliasDefinition(Node nameNode) {
if (!nameNode.hasChildren()) {
return false;
}
Node rhs = nameNode.getLastChild();
return isValidAliasRhs(rhs);
}
private static String getAliasedNamespace(Node rhs) {
switch (rhs.getToken()) {
case GETPROP:
return getAliasedNamespace(rhs.getFirstChild()) + '.' + rhs.getLastChild().getString();
case NAME:
return rhs.getString();
case CALL:
checkState(NodeUtil.isCallTo(rhs, "goog.module.get"), rhs);
checkState(rhs.hasTwoChildren(), rhs);
return rhs.getLastChild().getString();
default:
throw new RuntimeException("Invalid alias RHS:" + rhs);
}
}
private static class AliasedNode extends AliasUsage {
AliasedNode(Var aliasVar, Node aliasReference) {
super(aliasVar, aliasReference);
}
@Override
public void applyAlias(AbstractCompiler compiler) {
Node aliasDefinition = aliasVar.getInitialValue();
Node replacement = aliasDefinition.cloneTree();
replacement.useSourceInfoFromForTree(aliasReference);
// Given alias "var Bar = foo.Bar;" here we replace a usage of Bar with foo.Bar.
// foo is generated and never visible to user. Because of that we should mark all new nodes as
// non-indexable leaving only Bar indexable.
// Given that replacement is GETPROP node, prefix is first child. It's also possible that
// replacement is single-part namespace. Like goog.provide('Foo') in that case replacement
// won't have children.
if (replacement.getFirstChild() != null) {
replacement.getFirstChild().makeNonIndexableRecursive();
}
if (aliasReference.isStringKey()) {
checkState(!aliasReference.hasChildren());
aliasReference.addChildToFront(replacement);
} else {
aliasReference.replaceWith(replacement);
}
compiler.reportChangeToEnclosingScope(replacement);
}
}
private static class AliasedTypeNode extends AliasUsage {
AliasedTypeNode(Var aliasVar, Node aliasReference) {
super(aliasVar, aliasReference);
}
@Override
public void applyAlias(AbstractCompiler compiler) {
Node aliasDefinition = aliasVar.getInitialValue();
String aliasName = aliasVar.getName();
String typeName = aliasReference.getString();
if (typeName.startsWith("$jscomp.scope.")) {
// Already visited.
return;
}
String aliasExpanded = checkNotNull(getAliasedNamespace(aliasDefinition));
Preconditions.checkState(typeName.startsWith(aliasName),
"%s must start with %s", typeName, aliasName);
String replacement =
aliasExpanded + typeName.substring(aliasName.length());
aliasReference.setString(replacement);
}
}
private class Traversal extends AbstractPostOrderCallback
implements NodeTraversal.ScopedCallback {
// The job of this class is to collect these three data sets.
// The order of this list determines the order that aliases are applied.
private final List<Node> aliasDefinitionsInOrder = new ArrayList<>();
private final List<Node> scopeCalls = new ArrayList<>();
private final List<AliasUsage> aliasUsages = new ArrayList<>();
// This map is temporary and cleared for each scope.
private final Map<String, Var> aliases = new HashMap<>();
// Also temporary and cleared for each scope.
private final Set<Node> injectedDecls = new HashSet<>();
// Suppose you create an alias.
// var x = goog.x;
// As a side-effect, this means you can shadow the namespace 'goog'
// in inner scopes. When we inline the namespaces, we have to rename
// these shadows.
//
// Fortunately, we already have a name uniquifier that runs during tree
// normalization (before optimizations). We run it here on a limited
// set of variables, but only as a last resort (because this will screw
// up warning messages downstream).
private final Set<String> forbiddenLocals = new HashSet<>(
ImmutableSet.of("$jscomp"));
private boolean hasNamespaceShadows = false;
private boolean hasErrors = false;
private AliasTransformation transformation = null;
// The body of the function that is passed to goog.scope.
// Set when the traversal enters the body, and set back to null when it exits.
private Node scopeFunctionBody = null;
Collection<Node> getAliasDefinitionsInOrder() {
return aliasDefinitionsInOrder;
}
private List<AliasUsage> getAliasUsages() {
return aliasUsages;
}
List<Node> getScopeCalls() {
return scopeCalls;
}
boolean hasErrors() {
return hasErrors;
}
/**
* Returns true if this NodeTraversal is currently within a goog.scope function body
*/
private boolean inGoogScopeBody() {
return scopeFunctionBody != null;
}
/**
* Returns true if n is the goog.scope function body
*/
private boolean isGoogScopeFunctionBody(Node n) {
return inGoogScopeBody() && n == scopeFunctionBody;
}
private boolean isCallToScopeMethod(Node n) {
return n.isCall() && n.getFirstChild().matchesQualifiedName(SCOPING_METHOD_NAME);
}
/**
* @param scopeRoot the Node which is the root of the current scope
* @return the goog.scope() CALL node containing the scopeRoot, or null if scopeRoot is not
* in a goog.scope() call.
*/
private Node findScopeMethodCall(Node scopeRoot) {
Node n = scopeRoot.getGrandparent();
if (isCallToScopeMethod(n)) {
return n;
}
return null;
}
@Override
public void enterScope(NodeTraversal t) {
if (t.inGlobalHoistScope()) {
return;
}
if (inGoogScopeBody()) {
Scope hoistedScope = t.getClosestHoistScope().untyped();
if (isGoogScopeFunctionBody(hoistedScope.getRootNode())) {
findAliases(t, hoistedScope);
}
}
Node scopeMethodCall = findScopeMethodCall(t.getScopeRoot());
if (scopeMethodCall != null) {
transformation = transformationHandler.logAliasTransformation(
scopeMethodCall.getSourceFileName(), getSourceRegion(scopeMethodCall));
findAliases(t, t.getScope());
scopeFunctionBody = scopeMethodCall.getLastChild().getLastChild();
}
}
@Override
public void exitScope(NodeTraversal t) {
if (isGoogScopeFunctionBody(t.getScopeRoot())) {
scopeFunctionBody = null;
renameNamespaceShadows(t);
injectedDecls.clear();
aliases.clear();
forbiddenLocals.clear();
transformation = null;
hasNamespaceShadows = false;
} else if (inGoogScopeBody()) {
findNamespaceShadows(t);
reportInvalidVariables(t);
}
}
private void reportInvalidVariables(NodeTraversal t) {
Node scopeRoot = t.getScopeRoot();
Node enclosingFunctionBody = t.getEnclosingFunction().getLastChild();
if (isGoogScopeFunctionBody(enclosingFunctionBody)
&& scopeRoot.isNormalBlock()
&& !scopeRoot.getParent().isFunction()) {
for (Var v : t.getScope().getVarIterable()) {
Node parent = v.getNameNode().getParent();
if (NodeUtil.isFunctionDeclaration(parent)) {
// Disallow block-scoped function declarations that leak into the goog.scope
// function body. Technically they shouldn't leak in ES6 but the browsers don't agree
// on that yet.
report(t, v.getNode(), GOOG_SCOPE_INVALID_VARIABLE, v.getName());
}
}
}
}
private SourcePosition<AliasTransformation> getSourceRegion(Node n) {
Node testNode = n;
Node next = null;
for (; next != null || testNode.isScript();) {
next = testNode.getNext();
testNode = testNode.getParent();
}
int endLine = next == null ? Integer.MAX_VALUE : next.getLineno();
int endChar = next == null ? Integer.MAX_VALUE : next.getCharno();
SourcePosition<AliasTransformation> pos =
new SourcePosition<AliasTransformation>() {};
pos.setPositionInformation(
n.getLineno(), n.getCharno(), endLine, endChar);
return pos;
}
private void report(NodeTraversal t, Node n, DiagnosticType error,
String... arguments) {
compiler.report(t.makeError(n, error, arguments));
hasErrors = true;
}
private void findAliases(NodeTraversal t, Scope scope) {
for (Var v : scope.getVarIterable()) {
Node n = v.getNode();
Node parent = n.getParent();
// We use isBlock to avoid variables declared in loop headers.
boolean isVar = NodeUtil.isNameDeclaration(parent) && parent.getParent().isNormalBlock();
boolean isFunctionDecl = NodeUtil.isFunctionDeclaration(parent);
if (isVar && isAliasDefinition(n)) {
recordAlias(v);
} else if (v.isBleedingFunction()) {
// Bleeding functions already get a BAD_PARAMETERS error, so just
// do nothing.
} else if (parent.isParamList()) {
// Parameters of the scope function also get a BAD_PARAMETERS
// error.
} else if (isVar || isFunctionDecl || NodeUtil.isClassDeclaration(parent)) {
boolean isHoisted = NodeUtil.isHoistedFunctionDeclaration(parent);
Node grandparent = parent.getParent();
Node value = v.getInitialValue();
Node varNode = null;
// Pull out inline type declaration if present.
if (n.getJSDocInfo() != null) {
JSDocInfoBuilder builder = JSDocInfoBuilder.maybeCopyFrom(parent.getJSDocInfo());
if (isFunctionDecl) { // Fix inline return type.
builder.recordReturnType(n.getJSDocInfo().getType());
} else {
builder.recordType(n.getJSDocInfo().getType());
}
parent.setJSDocInfo(builder.build());
n.setJSDocInfo(null);
}
// Grab the docinfo before we do any AST manipulation.
JSDocInfo varDocInfo = v.getJSDocInfo();
String name = n.getString();
int nameCount = scopedAliasNames.count(name);
scopedAliasNames.add(name);
String globalName =
"$jscomp.scope." + name + (nameCount == 0 ? "" : ("$jscomp$" + nameCount));
Node lastInjectedNode = compiler.ensureLibraryInjected("base", false);
if (lastInjectedNode != null) {
compiler.reportChangeToEnclosingScope(lastInjectedNode);
}
// First, we need to free up the function expression (EXPR)
// to be used in another expression.
if (isFunctionDecl || NodeUtil.isClassDeclaration(parent)) {
// Replace "function NAME() { ... }" with "var NAME;".
// Replace "class NAME { ... }" with "var NAME;".
// We can't keep the local name on the function expression,
// because IE is buggy and will leak the name into the global
// scope. This is covered in more detail here:
// http://wiki.ecmascript.org/lib/exe/fetch.php?id=resources:resources&cache=cache&media=resources:jscriptdeviationsfromes3.pdf
//
// This will only cause problems if this is a hoisted, recursive
// function, and the programmer is using the hoisting.
Node newName;
if (isFunctionDecl) {
newName = IR.name("");
} else {
newName = IR.empty();
}
newName.useSourceInfoFrom(n);
value.replaceChild(n, newName);
compiler.reportChangeToEnclosingScope(newName);
varNode = IR.var(n).useSourceInfoFrom(n);
grandparent.replaceChild(parent, varNode);
} else {
if (value != null) {
// If this is a VAR, we can just detach the expression and
// the tree will still be valid.
value.detach();
}
varNode = parent;
}
// Add $jscomp.scope.name = EXPR;
// Make sure we copy over all the jsdoc and debug info.
if (value != null || varDocInfo != null) {
Node newDecl = NodeUtil.newQNameDeclaration(
compiler,
globalName,
value,
varDocInfo)
.useSourceInfoIfMissingFromForTree(n);
newDecl.getFirstFirstChild().useSourceInfoFrom(n);
newDecl.getFirstFirstChild().setOriginalName(name);
if (isHoisted) {
grandparent.addChildToFront(newDecl);
} else {
grandparent.addChildBefore(newDecl, varNode);
}
compiler.reportChangeToEnclosingScope(newDecl);
injectedDecls.add(newDecl.getFirstChild());
}
// Rewrite "var name = EXPR;" to "var name = $jscomp.scope.name;"
v.getNameNode().addChildToFront(
NodeUtil.newQName(
compiler, globalName, n, name));
recordAlias(v);
} else {
// Do not other kinds of local symbols, like catch params.
report(t, n, GOOG_SCOPE_NON_ALIAS_LOCAL, n.getString());
}
}
}
private void recordAlias(Var aliasVar) {
String name = aliasVar.getName();
aliases.put(name, aliasVar);
String qualifiedName = getAliasedNamespace(aliasVar.getInitialValue());
transformation.addAlias(name, qualifiedName);
int rootIndex = qualifiedName.indexOf('.');
if (rootIndex != -1) {
String qNameRoot = qualifiedName.substring(0, rootIndex);
if (!aliases.containsKey(qNameRoot)) {
forbiddenLocals.add(qNameRoot);
}
}
}
/** Find out if there are any local shadows of namespaces. */
private void findNamespaceShadows(NodeTraversal t) {
if (hasNamespaceShadows) {
return;
}
Scope scope = t.getScope();
for (Var v : scope.getVarIterable()) {
if (forbiddenLocals.contains(v.getName())) {
hasNamespaceShadows = true;
return;
}
}
}
/**
* Rename any local shadows of namespaces.
* This should be a very rare occurrence, so only do this traversal
* if we know that we need it.
*/
private void renameNamespaceShadows(NodeTraversal t) {
checkState(NodeUtil.isFunctionBlock(t.getScopeRoot()), t.getScopeRoot());
if (hasNamespaceShadows) {
MakeDeclaredNamesUnique.Renamer renamer =
new MakeDeclaredNamesUnique.WhitelistedRenamer(
new MakeDeclaredNamesUnique.ContextualRenamer(),
forbiddenLocals);
for (String s : forbiddenLocals) {
renamer.addDeclaredName(s, false);
}
MakeDeclaredNamesUnique uniquifier = new MakeDeclaredNamesUnique(renamer);
NodeTraversal.traverseEs6ScopeRoots(
compiler, null, ImmutableList.of(t.getScopeRoot()), uniquifier, true);
}
}
private void renameBleedingFunctionName(NodeTraversal t, final Node fnName) {
final String name = fnName.getString();
final String suffix = compiler.getUniqueNameIdSupplier().get();
Callback cb =
new AbstractPostOrderCallback() {
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
if (n.isName()
&& n.getString().equals(name)
&& t.getScope().getVar(name).getNode() == fnName) {
n.setString(name + "$jscomp$scopedAliases$" + suffix);
compiler.reportChangeToEnclosingScope(n);
}
}
};
(new NodeTraversal(compiler, cb, t.getScopeCreator())).traverseAtScope(t.getScope());
fnName.setString(name + "$jscomp$scopedAliases$" + suffix);
}
private void validateScopeCall(NodeTraversal t, Node n, Node parent) {
if (preprocessorSymbolTable != null) {
preprocessorSymbolTable.addReference(n.getFirstChild());
}
if (!parent.isExprResult()) {
report(t, n, GOOG_SCOPE_MUST_BE_ALONE);
}
if (t.getEnclosingFunction() != null) {
report(t, n, GOOG_SCOPE_MUST_BE_IN_GLOBAL_SCOPE);
}
if (!n.hasTwoChildren()) {
// The goog.scope call should have exactly 1 parameter. The first
// child is the "goog.scope" and the second should be the parameter.
report(t, n, GOOG_SCOPE_HAS_BAD_PARAMETERS);
} else {
Node anonymousFnNode = n.getSecondChild();
if (!anonymousFnNode.isFunction()
|| NodeUtil.getName(anonymousFnNode) != null
|| NodeUtil.getFunctionParameters(anonymousFnNode).hasChildren()) {
report(t, anonymousFnNode, GOOG_SCOPE_HAS_BAD_PARAMETERS);
} else {
scopeCalls.add(n);
}
}
}
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
if (isCallToScopeMethod(n)) {
validateScopeCall(t, n, n.getParent());
}
if (!inGoogScopeBody()) {
return;
}
Token type = n.getToken();
Var aliasVar = null;
if (type == Token.NAME) {
String name = n.getString();
Var lexicalVar = t.getScope().getVar(name);
if (lexicalVar != null && lexicalVar == aliases.get(name)) {
aliasVar = lexicalVar;
// For nodes that are referencing the aliased type, set the original name so it
// can be accessed later in tools such as the CodePrinter or refactoring tools.
if (compiler.getOptions().preservesDetailedSourceInfo() && n.isName()) {
n.setOriginalName(name);
}
}
}
if (isGoogScopeFunctionBody(t.getEnclosingFunction().getLastChild())) {
if (aliasVar != null && NodeUtil.isLValue(n)) {
if (aliasVar.getNode() == n) {
aliasDefinitionsInOrder.add(n);
// Return early, to ensure that we don't record a definition
// twice.
return;
} else {
report(t, n, GOOG_SCOPE_ALIAS_REDEFINED, n.getString());
}
}
if (type == Token.RETURN) {
report(t, n, GOOG_SCOPE_USES_RETURN);
} else if (type == Token.THIS) {
report(t, n, GOOG_SCOPE_REFERENCES_THIS);
} else if (type == Token.THROW) {
report(t, n, GOOG_SCOPE_USES_THROW);
}
}
// If this is a bleeding function expression, like
// var x = function y() { ... }
// then old versions of IE declare "y" in the current scope. We don't
// want the scope unboxing to add "y" to the global scope, so we
// need to rename it.
//
// TODO(moz): Remove this once we stop supporting IE8.
if (NodeUtil.isBleedingFunctionName(n)) {
renameBleedingFunctionName(t, n);
}
// Check if this name points to an alias.
if (aliasVar != null) {
// Note, to support the transitive case, it's important we don't
// clone aliasedNode here. For example,
// var g = goog; var d = g.dom; d.createElement('DIV');
// The node in aliasedNode (which is "g") will be replaced in the
// changes pass above with "goog". If we cloned here, we'd end up
// with <code>g.dom.createElement('DIV')</code>.
aliasUsages.add(new AliasedNode(aliasVar, n));
}
// When we inject declarations, we duplicate jsdoc. Make sure
// we only process that jsdoc once.
JSDocInfo info = n.getJSDocInfo();
if (info != null && !injectedDecls.contains(n)) {
for (Node node : info.getTypeNodes()) {
fixTypeNode(node);
}
}
}
private void fixTypeNode(Node typeNode) {
if (typeNode.isString()) {
String name = typeNode.getString();
int endIndex = name.indexOf('.');
if (endIndex == -1) {
endIndex = name.length();
}
String baseName = name.substring(0, endIndex);
Var aliasVar = aliases.get(baseName);
if (aliasVar != null) {
aliasUsages.add(new AliasedTypeNode(aliasVar, typeNode));
}
// For nodes that are referencing the aliased type, set the original name so it
// can be accessed later in tools such as the CodePrinter or refactoring tools.
if (compiler.getOptions().preservesDetailedSourceInfo()) {
typeNode.setOriginalName(name);
}
}
for (Node child = typeNode.getFirstChild(); child != null;
child = child.getNext()) {
fixTypeNode(child);
}
}
}
}