From b9e28ac0243e0fa6e525a6819b70a71e1fb89b40 Mon Sep 17 00:00:00 2001 From: tbreisacher Date: Fri, 19 Aug 2016 17:52:08 -0700 Subject: [PATCH] Rollback of "Skip most ES6 check and transpilation passes if the parser detects no ES6 feature in a file" *** Reason for rollback *** breaks cultural frontend. https://sponge.corp.google.com/target?id=8f7e95b3-d96d-4281-8f30-a75b6148b6c1&target=//javatests/com/google/cultural/frontend/common:all_compiled_js_test *** Original change description *** Rollforward of cl/130339419: Skip most ES6 check and transpilation passes if the parser detects no ES6 feature in a file NEW: - Changed VariableReferenceCheck to allow patterns like "var foo = foo || window['foo'] || {};". - Fixed PolymerPass to carry over potential new language features from behavior files to class files when extracting behaviors. - Run Es6RewriteArrowFunction on externs since some project use arrow functions in externs. - Run VariableReferenceCheck on exte... *** ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=130811418 --- .../javascript/jscomp/CheckMissingSuper.java | 7 +- .../google/javascript/jscomp/Compiler.java | 10 +- .../javascript/jscomp/DefaultPassConfig.java | 11 +-- .../javascript/jscomp/Es6ConvertSuper.java | 7 +- .../javascript/jscomp/Es6ExtractClasses.java | 7 +- .../Es6RenameVariablesInParamLists.java | 6 +- .../jscomp/Es6RewriteArrowFunction.java | 6 +- .../Es6RewriteBlockScopedDeclaration.java | 20 ++-- .../jscomp/Es6RewriteDestructuring.java | 5 +- .../jscomp/Es6RewriteGenerators.java | 7 +- .../jscomp/Es6SplitVariableDeclarations.java | 5 +- .../javascript/jscomp/Es6SuperCheck.java | 2 +- .../javascript/jscomp/Es6ToEs3Converter.java | 6 +- .../jscomp/PolymerBehaviorExtractor.java | 16 +--- .../jscomp/PolymerClassDefinition.java | 19 +--- .../jscomp/PolymerClassRewriter.java | 12 +-- .../jscomp/ReferenceCollectingCallback.java | 5 +- src/com/google/javascript/jscomp/Result.java | 13 +-- .../jscomp/TranspilationPasses.java | 91 +------------------ .../jscomp/VariableReferenceCheck.java | 52 ++--------- .../deps/TranspilingClosureBundler.java | 10 +- .../jscomp/parsing/parser/FeatureSet.java | 5 - src/com/google/javascript/rhino/Node.java | 4 +- .../jscomp/Es6ExtractClassesTest.java | 1 - .../jscomp/Es6ToEs3ConverterTest.java | 12 ++- .../jscomp/PolymerPassStaticUtilsTest.java | 4 +- .../javascript/jscomp/TypeCheckTest.java | 4 +- .../jscomp/VariableReferenceCheckTest.java | 1 - 28 files changed, 84 insertions(+), 264 deletions(-) diff --git a/src/com/google/javascript/jscomp/CheckMissingSuper.java b/src/com/google/javascript/jscomp/CheckMissingSuper.java index 54bdfdb7f56..2e289855e5a 100644 --- a/src/com/google/javascript/jscomp/CheckMissingSuper.java +++ b/src/com/google/javascript/jscomp/CheckMissingSuper.java @@ -36,12 +36,15 @@ public CheckMissingSuper(AbstractCompiler compiler) { @Override public void process(Node externs, Node root) { - TranspilationPasses.processCheck(compiler, root, this); + if (!compiler.getOptions().getLanguageIn().isEs6OrHigher()) { + return; + } + NodeTraversal.traverseEs6(compiler, root, this); } @Override public void hotSwapScript(Node scriptRoot, Node originalRoot) { - TranspilationPasses.hotSwapCheck(compiler, scriptRoot, this); + process(null, scriptRoot); } @Override diff --git a/src/com/google/javascript/jscomp/Compiler.java b/src/com/google/javascript/jscomp/Compiler.java index 50a64289c24..2f4914852c4 100644 --- a/src/com/google/javascript/jscomp/Compiler.java +++ b/src/com/google/javascript/jscomp/Compiler.java @@ -998,18 +998,10 @@ void stopTracer(Tracer t, String passName) { */ public Result getResult() { PassConfig.State state = getPassConfig().getIntermediateState(); - Set transpiledFiles = new HashSet<>(); - if (jsRoot != null) { - for (Node scriptNode : jsRoot.children()) { - if (scriptNode.getBooleanProp(Node.TRANSPILED)) { - transpiledFiles.add(getSourceFileByName(scriptNode.getSourceFileName())); - } - } - } return new Result(getErrors(), getWarnings(), debugLog.toString(), state.variableMap, state.propertyMap, state.anonymousFunctionNameMap, state.stringMap, functionInformationMap, - sourceMap, externExports, state.cssNames, state.idGeneratorMap, transpiledFiles); + sourceMap, externExports, state.cssNames, state.idGeneratorMap); } /** diff --git a/src/com/google/javascript/jscomp/DefaultPassConfig.java b/src/com/google/javascript/jscomp/DefaultPassConfig.java index 9ff48aa641b..d1cb8414540 100644 --- a/src/com/google/javascript/jscomp/DefaultPassConfig.java +++ b/src/com/google/javascript/jscomp/DefaultPassConfig.java @@ -195,7 +195,7 @@ protected List getTranspileOnlyPasses() { } passes.add(checkMissingSuper); - passes.add(checkVariableReferencesForTranspileOnly); + passes.add(checkVariableReferences); // It's important that the Dart super accessors pass run *before* es6ConvertSuper, // which is a "late" ES6 pass. This is enforced in the assertValidOrder method. @@ -1549,15 +1549,6 @@ public void process(Node externs, Node root) { } }; - /** Checks that references to variables look reasonable. */ - private final HotSwapPassFactory checkVariableReferencesForTranspileOnly = - new HotSwapPassFactory("checkVariableReferences", true) { - @Override - protected HotSwapCompilerPass create(AbstractCompiler compiler) { - return new VariableReferenceCheck(compiler, true); - } - }; - /** Checks that references to variables look reasonable. */ private final HotSwapPassFactory checkVariableReferences = new HotSwapPassFactory("checkVariableReferences", true) { diff --git a/src/com/google/javascript/jscomp/Es6ConvertSuper.java b/src/com/google/javascript/jscomp/Es6ConvertSuper.java index c55e7073b1d..56d6f01d7e0 100644 --- a/src/com/google/javascript/jscomp/Es6ConvertSuper.java +++ b/src/com/google/javascript/jscomp/Es6ConvertSuper.java @@ -179,13 +179,12 @@ private Node baseCall(String baseClass, String methodName, Node arguments) { @Override public void process(Node externs, Node root) { - // Might need to synthesize constructors for ambient classes in .d.ts externs - TranspilationPasses.processTranspile(compiler, externs, this); - TranspilationPasses.processTranspile(compiler, root, this); + NodeTraversal.traverseEs6(compiler, externs, this); + NodeTraversal.traverseEs6(compiler, root, this); } @Override public void hotSwapScript(Node scriptRoot, Node originalRoot) { - TranspilationPasses.hotSwapTranspile(compiler, scriptRoot, this); + NodeTraversal.traverseEs6(compiler, scriptRoot, this); } } diff --git a/src/com/google/javascript/jscomp/Es6ExtractClasses.java b/src/com/google/javascript/jscomp/Es6ExtractClasses.java index 241ee6f95b0..89a702c20b8 100644 --- a/src/com/google/javascript/jscomp/Es6ExtractClasses.java +++ b/src/com/google/javascript/jscomp/Es6ExtractClasses.java @@ -26,6 +26,7 @@ import com.google.javascript.rhino.JSDocInfoBuilder; import com.google.javascript.rhino.Node; import com.google.javascript.rhino.Token; + import java.util.Deque; import java.util.HashSet; import java.util.LinkedList; @@ -67,12 +68,14 @@ public final class Es6ExtractClasses @Override public void process(Node externs, Node root) { - TranspilationPasses.processTranspile(compiler, root, this, new SelfReferenceRewriter()); + NodeTraversal.traverseRootsEs6(compiler, this, externs, root); + NodeTraversal.traverseRootsEs6(compiler, new SelfReferenceRewriter(), externs, root); } @Override public void hotSwapScript(Node scriptRoot, Node originalRoot) { - TranspilationPasses.hotSwapTranspile(compiler, scriptRoot, this, new SelfReferenceRewriter()); + NodeTraversal.traverseEs6(compiler, scriptRoot, this); + NodeTraversal.traverseEs6(compiler, scriptRoot, new SelfReferenceRewriter()); } @Override diff --git a/src/com/google/javascript/jscomp/Es6RenameVariablesInParamLists.java b/src/com/google/javascript/jscomp/Es6RenameVariablesInParamLists.java index dd9485df52c..19a51524622 100644 --- a/src/com/google/javascript/jscomp/Es6RenameVariablesInParamLists.java +++ b/src/com/google/javascript/jscomp/Es6RenameVariablesInParamLists.java @@ -18,6 +18,7 @@ import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback; import com.google.javascript.rhino.Node; + import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashMap; @@ -85,12 +86,13 @@ public final boolean shouldTraverse(NodeTraversal t, Node n, Node parent) { @Override public void process(Node externs, Node root) { - TranspilationPasses.processTranspile(compiler, root, this); + NodeTraversal.traverseEs6(compiler, externs, this); + NodeTraversal.traverseEs6(compiler, root, this); } @Override public void hotSwapScript(Node scriptRoot, Node originalRoot) { - TranspilationPasses.hotSwapTranspile(compiler, scriptRoot, this); + NodeTraversal.traverseEs6(compiler, scriptRoot, this); } /** diff --git a/src/com/google/javascript/jscomp/Es6RewriteArrowFunction.java b/src/com/google/javascript/jscomp/Es6RewriteArrowFunction.java index 44040fbe537..a21b657df6d 100644 --- a/src/com/google/javascript/jscomp/Es6RewriteArrowFunction.java +++ b/src/com/google/javascript/jscomp/Es6RewriteArrowFunction.java @@ -39,13 +39,13 @@ public Es6RewriteArrowFunction(AbstractCompiler compiler) { @Override public void process(Node externs, Node root) { - TranspilationPasses.processTranspile(compiler, externs, this); - TranspilationPasses.processTranspile(compiler, root, this); + NodeTraversal.traverseEs6(compiler, externs, this); + NodeTraversal.traverseEs6(compiler, root, this); } @Override public void hotSwapScript(Node scriptRoot, Node originalRoot) { - TranspilationPasses.hotSwapTranspile(compiler, scriptRoot, this); + NodeTraversal.traverseEs6(compiler, scriptRoot, this); } @Override diff --git a/src/com/google/javascript/jscomp/Es6RewriteBlockScopedDeclaration.java b/src/com/google/javascript/jscomp/Es6RewriteBlockScopedDeclaration.java index 7a74a8f33fb..b98f4a7db50 100644 --- a/src/com/google/javascript/jscomp/Es6RewriteBlockScopedDeclaration.java +++ b/src/com/google/javascript/jscomp/Es6RewriteBlockScopedDeclaration.java @@ -28,6 +28,7 @@ import com.google.javascript.rhino.JSTypeExpression; import com.google.javascript.rhino.Node; import com.google.javascript.rhino.Token; + import java.util.Collection; import java.util.HashMap; import java.util.HashSet; @@ -110,20 +111,16 @@ && inLoop(n)) { @Override public void process(Node externs, Node root) { - // Since block-scoped function declarations can appear in any language mode, we need to run - // this pass unconditionally. - NodeTraversal.traverseEs6(compiler, root, new CollectUndeclaredNames()); - NodeTraversal.traverseEs6(compiler, root, this); - // Needed for let / const declarations in .d.ts externs. - TranspilationPasses.processTranspile(compiler, externs, this); - NodeTraversal.traverseEs6(compiler, root, new Es6RenameReferences(renameMap)); + NodeTraversal.traverseRootsEs6(compiler, new CollectUndeclaredNames(), externs, root); + NodeTraversal.traverseRootsEs6(compiler, this, externs, root); + NodeTraversal.traverseRootsEs6(compiler, new Es6RenameReferences(renameMap), externs, root); + LoopClosureTransformer transformer = new LoopClosureTransformer(); - NodeTraversal.traverseEs6(compiler, root, transformer); + NodeTraversal.traverseRootsEs6(compiler, transformer, externs, root); transformer.transformLoopClosure(); varify(); - TranspilationPasses.processTranspile( - compiler, externs, new RewriteBlockScopedFunctionDeclaration()); - NodeTraversal.traverseEs6(compiler, root, new RewriteBlockScopedFunctionDeclaration()); + NodeTraversal.traverseRootsEs6( + compiler, new RewriteBlockScopedFunctionDeclaration(), externs, root); } @Override @@ -131,6 +128,7 @@ public void hotSwapScript(Node scriptRoot, Node originalRoot) { NodeTraversal.traverseEs6(compiler, scriptRoot, new CollectUndeclaredNames()); NodeTraversal.traverseEs6(compiler, scriptRoot, this); NodeTraversal.traverseEs6(compiler, scriptRoot, new Es6RenameReferences(renameMap)); + LoopClosureTransformer transformer = new LoopClosureTransformer(); NodeTraversal.traverseEs6(compiler, scriptRoot, transformer); transformer.transformLoopClosure(); diff --git a/src/com/google/javascript/jscomp/Es6RewriteDestructuring.java b/src/com/google/javascript/jscomp/Es6RewriteDestructuring.java index 71066d9b922..0b2a18f950c 100644 --- a/src/com/google/javascript/jscomp/Es6RewriteDestructuring.java +++ b/src/com/google/javascript/jscomp/Es6RewriteDestructuring.java @@ -41,12 +41,13 @@ public Es6RewriteDestructuring(AbstractCompiler compiler) { @Override public void process(Node externs, Node root) { - TranspilationPasses.processTranspile(compiler, root, this); + NodeTraversal.traverseEs6(compiler, externs, this); + NodeTraversal.traverseEs6(compiler, root, this); } @Override public void hotSwapScript(Node scriptRoot, Node originalRoot) { - TranspilationPasses.hotSwapTranspile(compiler, scriptRoot, this); + NodeTraversal.traverseEs6(compiler, scriptRoot, this); } @Override diff --git a/src/com/google/javascript/jscomp/Es6RewriteGenerators.java b/src/com/google/javascript/jscomp/Es6RewriteGenerators.java index faca118d19e..7c5eaea90c1 100644 --- a/src/com/google/javascript/jscomp/Es6RewriteGenerators.java +++ b/src/com/google/javascript/jscomp/Es6RewriteGenerators.java @@ -26,6 +26,7 @@ import com.google.javascript.rhino.JSDocInfoBuilder; import com.google.javascript.rhino.Node; import com.google.javascript.rhino.Token; + import java.util.ArrayList; import java.util.HashSet; import java.util.List; @@ -97,12 +98,14 @@ public Es6RewriteGenerators(AbstractCompiler compiler) { @Override public void process(Node externs, Node root) { - TranspilationPasses.processTranspile(compiler, root, new DecomposeYields(compiler), this); + NodeTraversal.traverseEs6(compiler, root, new DecomposeYields(compiler)); + NodeTraversal.traverseEs6(compiler, root, this); } @Override public void hotSwapScript(Node scriptRoot, Node originalRoot) { - TranspilationPasses.hotSwapTranspile(compiler, scriptRoot, new DecomposeYields(compiler), this); + NodeTraversal.traverseEs6(compiler, scriptRoot, new DecomposeYields(compiler)); + NodeTraversal.traverseEs6(compiler, scriptRoot, this); } @Override diff --git a/src/com/google/javascript/jscomp/Es6SplitVariableDeclarations.java b/src/com/google/javascript/jscomp/Es6SplitVariableDeclarations.java index 186e31789a4..6bf7f5ec32e 100644 --- a/src/com/google/javascript/jscomp/Es6SplitVariableDeclarations.java +++ b/src/com/google/javascript/jscomp/Es6SplitVariableDeclarations.java @@ -45,12 +45,13 @@ public Es6SplitVariableDeclarations(AbstractCompiler compiler) { @Override public void process(Node externs, Node root) { - TranspilationPasses.processTranspile(compiler, root, this); + NodeTraversal.traverseEs6(compiler, externs, this); + NodeTraversal.traverseEs6(compiler, root, this); } @Override public void hotSwapScript(Node scriptRoot, Node originalRoot) { - TranspilationPasses.hotSwapTranspile(compiler, scriptRoot, this); + NodeTraversal.traverseEs6(compiler, scriptRoot, this); } @Override diff --git a/src/com/google/javascript/jscomp/Es6SuperCheck.java b/src/com/google/javascript/jscomp/Es6SuperCheck.java index 8d617a3f142..8698fae45d1 100644 --- a/src/com/google/javascript/jscomp/Es6SuperCheck.java +++ b/src/com/google/javascript/jscomp/Es6SuperCheck.java @@ -38,7 +38,7 @@ final class Es6SuperCheck extends AbstractPostOrderCallback implements CompilerP @Override public void process(Node externs, Node root) { - TranspilationPasses.processCheck(compiler, root, this); + NodeTraversal.traverseEs6(compiler, root, this); } @Override diff --git a/src/com/google/javascript/jscomp/Es6ToEs3Converter.java b/src/com/google/javascript/jscomp/Es6ToEs3Converter.java index 38416568888..33cb8ecfe03 100644 --- a/src/com/google/javascript/jscomp/Es6ToEs3Converter.java +++ b/src/com/google/javascript/jscomp/Es6ToEs3Converter.java @@ -97,13 +97,13 @@ public Es6ToEs3Converter(AbstractCompiler compiler) { @Override public void process(Node externs, Node root) { - TranspilationPasses.processTranspile(compiler, externs, this); - TranspilationPasses.processTranspile(compiler, root, this); + NodeTraversal.traverseEs6(compiler, externs, this); + NodeTraversal.traverseEs6(compiler, root, this); } @Override public void hotSwapScript(Node scriptRoot, Node originalRoot) { - TranspilationPasses.hotSwapTranspile(compiler, scriptRoot, this); + NodeTraversal.traverseEs6(compiler, scriptRoot, this); } /** diff --git a/src/com/google/javascript/jscomp/PolymerBehaviorExtractor.java b/src/com/google/javascript/jscomp/PolymerBehaviorExtractor.java index 3f02a8c4196..89824951da1 100644 --- a/src/com/google/javascript/jscomp/PolymerBehaviorExtractor.java +++ b/src/com/google/javascript/jscomp/PolymerBehaviorExtractor.java @@ -21,7 +21,6 @@ import com.google.javascript.jscomp.GlobalNamespace.Name; import com.google.javascript.jscomp.GlobalNamespace.Ref; import com.google.javascript.jscomp.PolymerPass.MemberDefinition; -import com.google.javascript.jscomp.parsing.parser.FeatureSet; import com.google.javascript.rhino.JSDocInfo; import com.google.javascript.rhino.Node; @@ -71,8 +70,7 @@ ImmutableList extractBehaviors(Node behaviorArray) { PolymerPassStaticUtils.extractProperties(behaviorName), getBehaviorFunctionsToCopy(behaviorName), getNonPropertyMembersToCopy(behaviorName), - !NodeUtil.isInFunction(behaviorName), - (FeatureSet) NodeUtil.getEnclosingScript(behaviorName).getProp(Node.FEATURE_SET))); + !NodeUtil.isInFunction(behaviorName))); continue; } @@ -123,8 +121,7 @@ ImmutableList extractBehaviors(Node behaviorArray) { PolymerPassStaticUtils.extractProperties(behaviorValue), getBehaviorFunctionsToCopy(behaviorValue), getNonPropertyMembersToCopy(behaviorValue), - isGlobalDeclaration, - (FeatureSet) NodeUtil.getEnclosingScript(behaviorValue).getProp(Node.FEATURE_SET))); + isGlobalDeclaration)); } else { compiler.report(JSError.make(behaviorName, PolymerPassErrors.POLYMER_UNQUALIFIED_BEHAVIOR)); } @@ -197,20 +194,13 @@ static final class BehaviorDefinition { */ final boolean isGlobalDeclaration; - /** - * Language features to carry over to the extraction destination. - */ - final FeatureSet features; - BehaviorDefinition( List props, List functionsToCopy, - List nonPropertyMembersToCopy, boolean isGlobalDeclaration, - FeatureSet features) { + List nonPropertyMembersToCopy, boolean isGlobalDeclaration) { this.props = props; this.functionsToCopy = functionsToCopy; this.nonPropertyMembersToCopy = nonPropertyMembersToCopy; this.isGlobalDeclaration = isGlobalDeclaration; - this.features = features; } } } diff --git a/src/com/google/javascript/jscomp/PolymerClassDefinition.java b/src/com/google/javascript/jscomp/PolymerClassDefinition.java index 036a1308f57..b94af5d95e1 100644 --- a/src/com/google/javascript/jscomp/PolymerClassDefinition.java +++ b/src/com/google/javascript/jscomp/PolymerClassDefinition.java @@ -20,7 +20,6 @@ import com.google.common.collect.ImmutableList; import com.google.javascript.jscomp.PolymerBehaviorExtractor.BehaviorDefinition; import com.google.javascript.jscomp.PolymerPass.MemberDefinition; -import com.google.javascript.jscomp.parsing.parser.FeatureSet; import com.google.javascript.rhino.IR; import com.google.javascript.rhino.JSDocInfo; import com.google.javascript.rhino.Node; @@ -54,9 +53,6 @@ final class PolymerClassDefinition { /** Flattened list of behavior definitions used by this element. */ final ImmutableList behaviors; - /** Language features that should be carried over to the extraction destination. */ - final FeatureSet features; - PolymerClassDefinition( Node target, Node descriptor, @@ -64,8 +60,7 @@ final class PolymerClassDefinition { MemberDefinition constructor, String nativeBaseElement, List props, - ImmutableList behaviors, - FeatureSet features) { + ImmutableList behaviors) { this.target = target; Preconditions.checkState(descriptor.isObjectLit()); this.descriptor = descriptor; @@ -73,7 +68,6 @@ final class PolymerClassDefinition { this.nativeBaseElement = nativeBaseElement; this.props = props; this.behaviors = behaviors; - this.features = features; } /** @@ -138,14 +132,6 @@ final class PolymerClassDefinition { } overwriteMembersIfPresent(allProperties, PolymerPassStaticUtils.extractProperties(descriptor)); - FeatureSet newFeatures = null; - if (!behaviors.isEmpty()) { - newFeatures = behaviors.get(0).features; - for (int i = 1; i < behaviors.size(); i++) { - newFeatures = newFeatures.union(behaviors.get(i).features); - } - } - return new PolymerClassDefinition( target, descriptor, @@ -153,8 +139,7 @@ final class PolymerClassDefinition { new MemberDefinition(ctorInfo, null, constructor), nativeBaseElement, allProperties, - behaviors, - newFeatures); + behaviors); } /** diff --git a/src/com/google/javascript/jscomp/PolymerClassRewriter.java b/src/com/google/javascript/jscomp/PolymerClassRewriter.java index 2fe993d0141..f0bb6323e38 100644 --- a/src/com/google/javascript/jscomp/PolymerClassRewriter.java +++ b/src/com/google/javascript/jscomp/PolymerClassRewriter.java @@ -19,7 +19,6 @@ import com.google.common.collect.ImmutableList; import com.google.javascript.jscomp.PolymerBehaviorExtractor.BehaviorDefinition; import com.google.javascript.jscomp.PolymerPass.MemberDefinition; -import com.google.javascript.jscomp.parsing.parser.FeatureSet; import com.google.javascript.rhino.IR; import com.google.javascript.rhino.JSDocInfoBuilder; import com.google.javascript.rhino.JSTypeExpression; @@ -127,7 +126,7 @@ void rewritePolymerClass( // types declared inside IIFEs or any non-global scope. We should revisit this decision after // moving to the new type inference system which should be able to infer these types better. if (!isInGlobalScope && !cls.target.isGetProp()) { - Node scriptNode = NodeUtil.getEnclosingScript(parent); + Node scriptNode = NodeUtil.getEnclosingScript(exprRoot); scriptNode.addChildrenToFront(statements); } else { Node beforeRoot = exprRoot.getPrevious(); @@ -138,15 +137,6 @@ void rewritePolymerClass( } } - // Since behavior files might contain more language features than the class file, we need to - // update the feature sets. - FeatureSet newFeatures = cls.features; - if (newFeatures != null) { - Node scriptNode = NodeUtil.getEnclosingScript(parent); - FeatureSet oldFeatures = (FeatureSet) scriptNode.getProp(Node.FEATURE_SET); - scriptNode.putProp(Node.FEATURE_SET, oldFeatures.union(newFeatures)); - } - if (NodeUtil.isNameDeclaration(exprRoot)) { Node assignExpr = varToAssign(exprRoot); parent.replaceChild(exprRoot, assignExpr); diff --git a/src/com/google/javascript/jscomp/ReferenceCollectingCallback.java b/src/com/google/javascript/jscomp/ReferenceCollectingCallback.java index 08bc47b875c..83088bb6a0b 100644 --- a/src/com/google/javascript/jscomp/ReferenceCollectingCallback.java +++ b/src/com/google/javascript/jscomp/ReferenceCollectingCallback.java @@ -28,6 +28,7 @@ import com.google.javascript.rhino.StaticSourceFile; import com.google.javascript.rhino.StaticSymbolTable; import com.google.javascript.rhino.Token; + import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; @@ -114,10 +115,6 @@ public void process(Node externs, Node root) { NodeTraversal.traverseRoots(compiler, this, externs, root); } - public void process(Node root) { - NodeTraversal.traverse(compiler, root, this); - } - /** * Targets reference collection to a particular scope. */ diff --git a/src/com/google/javascript/jscomp/Result.java b/src/com/google/javascript/jscomp/Result.java index db0f76061ea..a191320c706 100644 --- a/src/com/google/javascript/jscomp/Result.java +++ b/src/com/google/javascript/jscomp/Result.java @@ -16,9 +16,7 @@ package com.google.javascript.jscomp; -import com.google.common.annotations.VisibleForTesting; import java.util.Map; -import java.util.Set; /** * Compilation results @@ -37,7 +35,6 @@ public class Result { public final Map cssNames; public final String externExport; public final String idGeneratorMap; - public final Set transpiledFiles; Result(JSError[] errors, JSError[] warnings, String debugLog, VariableMap variableMap, VariableMap propertyMap, @@ -45,10 +42,9 @@ public class Result { VariableMap stringMap, FunctionInformationMap functionInformationMap, SourceMap sourceMap, String externExport, - Map cssNames, String idGeneratorMap, - Set transpiledFiles) { + Map cssNames, String idGeneratorMap) { this.success = errors.length == 0; - this.errors = errors; + this.errors = errors; this.warnings = warnings; this.debugLog = debugLog; this.variableMap = variableMap; @@ -60,10 +56,9 @@ public class Result { this.externExport = externExport; this.cssNames = cssNames; this.idGeneratorMap = idGeneratorMap; - this.transpiledFiles = transpiledFiles; } - @VisibleForTesting + // Visible for testing only. public Result(JSError[] errors, JSError[] warnings, String debugLog, VariableMap variableMap, VariableMap propertyMap, VariableMap namedAnonFunctionMap, @@ -71,6 +66,6 @@ public Result(JSError[] errors, JSError[] warnings, String debugLog, SourceMap sourceMap, String externExport) { this(errors, warnings, debugLog, variableMap, propertyMap, namedAnonFunctionMap, null, functionInformationMap, sourceMap, - externExport, null, null, null); + externExport, null, null); } } diff --git a/src/com/google/javascript/jscomp/TranspilationPasses.java b/src/com/google/javascript/jscomp/TranspilationPasses.java index 1459d49024a..edde6d3b85e 100644 --- a/src/com/google/javascript/jscomp/TranspilationPasses.java +++ b/src/com/google/javascript/jscomp/TranspilationPasses.java @@ -16,10 +16,8 @@ package com.google.javascript.jscomp; -import com.google.javascript.jscomp.NodeTraversal.Callback; import com.google.javascript.jscomp.PassFactory.HotSwapPassFactory; -import com.google.javascript.jscomp.parsing.parser.FeatureSet; -import com.google.javascript.rhino.Node; + import java.util.List; /** @@ -152,91 +150,4 @@ protected HotSwapCompilerPass create(final AbstractCompiler compiler) { return new Es6RewriteGenerators(compiler); } }; - - /** - * @param script The SCRIPT node representing a JS file - * @return If the file has at least ES6 features currently implemented in modern browsers. - */ - static boolean isScriptEs6ImplOrHigher(Node script) { - FeatureSet features = (FeatureSet) script.getProp(Node.FEATURE_SET); - return features != null && features.isEs6ImplOrHigher(); - } - - /** - * Process ES6 checks if the input language is set to at least ES6 and a JS file has ES6 features. - * - * @param compiler An AbstractCompiler - * @param combinedRoot The combined root for all JS files. - * @param callbacks The callbacks that should be invoked if a file has ES6 features. - */ - static void processCheck(AbstractCompiler compiler, Node combinedRoot, Callback... callbacks) { - if (compiler.getOptions().getLanguageIn().isEs6OrHigher()) { - for (Node singleRoot : combinedRoot.children()) { - if (isScriptEs6ImplOrHigher(singleRoot)) { - for (Callback callback : callbacks) { - NodeTraversal.traverseEs6(compiler, singleRoot, callback); - } - } - } - } - } - - /** - * Hot-swap ES6 checks if the input language is set to at least ES6 and a JS file has ES6 - * features. - * - * @param compiler An AbstractCompiler - * @param scriptRoot The SCRIPT root for the JS file. - * @param callbacks The callbacks that should be invoked if the file has ES6 features. - */ - static void hotSwapCheck(AbstractCompiler compiler, Node scriptRoot, Callback... callbacks) { - if (compiler.getOptions().getLanguageIn().isEs6OrHigher()) { - if (isScriptEs6ImplOrHigher(scriptRoot)) { - for (Callback callback : callbacks) { - NodeTraversal.traverseEs6(compiler, scriptRoot, callback); - } - } - } - } - - /** - * Process ES6 transpilations if the input language is set to at least ES6 and the JS file has ES6 - * features. - * - * @param compiler An AbstractCompiler - * @param combinedRoot The combined root for all JS files. - * @param callbacks The callbacks that should be invoked if a file has ES6 features. - */ - static void processTranspile( - AbstractCompiler compiler, Node combinedRoot, Callback... callbacks) { - if (compiler.getOptions().lowerFromEs6()) { - for (Node singleRoot : combinedRoot.children()) { - if (isScriptEs6ImplOrHigher(singleRoot)) { - for (Callback callback : callbacks) { - singleRoot.putBooleanProp(Node.TRANSPILED, true); - NodeTraversal.traverseEs6(compiler, singleRoot, callback); - } - } - } - } - } - - /** - * Hot-swap ES6 transpilations if the input language is set to at least ES6 and the JS file has - * ES6 features. - * - * @param compiler An AbstractCompiler - * @param scriptRoot The SCRIPT root for the JS file. - * @param callbacks The callbacks that should be invoked if the file has ES6 features. - */ - static void hotSwapTranspile(AbstractCompiler compiler, Node scriptRoot, Callback... callbacks) { - if (compiler.getOptions().lowerFromEs6()) { - if (isScriptEs6ImplOrHigher(scriptRoot)) { - for (Callback callback : callbacks) { - scriptRoot.putBooleanProp(Node.TRANSPILED, true); - NodeTraversal.traverseEs6(compiler, scriptRoot, callback); - } - } - } - } } diff --git a/src/com/google/javascript/jscomp/VariableReferenceCheck.java b/src/com/google/javascript/jscomp/VariableReferenceCheck.java index 049a4a5aa30..c18347ab61a 100644 --- a/src/com/google/javascript/jscomp/VariableReferenceCheck.java +++ b/src/com/google/javascript/jscomp/VariableReferenceCheck.java @@ -23,6 +23,7 @@ import com.google.javascript.jscomp.ReferenceCollectingCallback.ReferenceCollection; import com.google.javascript.jscomp.ReferenceCollectingCallback.ReferenceMap; import com.google.javascript.rhino.Node; + import java.util.HashSet; import java.util.LinkedHashSet; import java.util.List; @@ -70,54 +71,26 @@ class VariableReferenceCheck implements HotSwapCompilerPass { private final AbstractCompiler compiler; - // If true, the pass will only check code that is at least ES6. Certain errors in block-scoped - // variable declarations will prevent correct transpilation, so this pass must be run. - private final boolean forTranspileOnly; - // NOTE(nicksantos): It's a lot faster to use a shared Set that // we clear after each method call, because the Set never gets too big. private final Set blocksWithDeclarations = new HashSet<>(); public VariableReferenceCheck(AbstractCompiler compiler) { - this(compiler, false); - } - - VariableReferenceCheck(AbstractCompiler compiler, boolean forTranspileOnly) { this.compiler = compiler; - this.forTranspileOnly = forTranspileOnly; } @Override public void process(Node externs, Node root) { - if (forTranspileOnly) { - if (!compiler.getOptions().getLanguageIn().isEs6OrHigher()) { - return; - } - for (Node singleRoot : root.children()) { - if (TranspilationPasses.isScriptEs6ImplOrHigher(singleRoot)) { - new ReferenceCollectingCallback(compiler, new ReferenceCheckingBehavior()) - .process(externs, root); - return; - } - } - } else { - new ReferenceCollectingCallback(compiler, new ReferenceCheckingBehavior()) - .process(externs, root); - } + ReferenceCollectingCallback callback = + new ReferenceCollectingCallback(compiler, new ReferenceCheckingBehavior()); + callback.process(externs, root); } @Override public void hotSwapScript(Node scriptRoot, Node originalRoot) { - if (forTranspileOnly) { - if (compiler.getOptions().getLanguageIn().isEs6OrHigher() - && TranspilationPasses.isScriptEs6ImplOrHigher(scriptRoot)) { - new ReferenceCollectingCallback(compiler, new ReferenceCheckingBehavior()) - .hotSwapScript(scriptRoot, originalRoot); - } - } else { - new ReferenceCollectingCallback(compiler, new ReferenceCheckingBehavior()) - .hotSwapScript(scriptRoot, originalRoot); - } + ReferenceCollectingCallback callback = + new ReferenceCollectingCallback(compiler, new ReferenceCheckingBehavior()); + callback.hotSwapScript(scriptRoot, originalRoot); } /** @@ -346,14 +319,9 @@ private void checkVar(Var v, List references) { if (!referenceNode.isFromExterns()) { // Special case to deal with var goog = goog || {}. Note that // let x = x || {} is illegal, just like var y = x || {}; let x = y; - if (v.isVar()) { - Node curr = reference.getParent(); - while (curr.isOr() && curr.getParent().getFirstChild() == curr) { - curr = curr.getParent(); - } - if (curr.isName() && curr.getString().equals(v.name)) { - continue; - } + Node grandparent = reference.getGrandparent(); + if ((v.isVar() && grandparent.isName() && grandparent.getString().equals(v.name))) { + continue; } // Only generate warnings if the scopes do not match in order diff --git a/src/com/google/javascript/jscomp/deps/TranspilingClosureBundler.java b/src/com/google/javascript/jscomp/deps/TranspilingClosureBundler.java index 59a6a227946..ac420455b1f 100644 --- a/src/com/google/javascript/jscomp/deps/TranspilingClosureBundler.java +++ b/src/com/google/javascript/jscomp/deps/TranspilingClosureBundler.java @@ -29,7 +29,6 @@ import com.google.javascript.jscomp.CompilerOptions; import com.google.javascript.jscomp.CompilerOptions.LanguageMode; import com.google.javascript.jscomp.PropertyRenamingPolicy; -import com.google.javascript.jscomp.Result; import com.google.javascript.jscomp.SourceFile; import com.google.javascript.jscomp.VariableRenamingPolicy; import java.io.ByteArrayOutputStream; @@ -127,8 +126,10 @@ public String call() throws IOException, UnsupportedEncodingException { // saved as instance state. ByteArrayOutputStream baos = new ByteArrayOutputStream(); Compiler compiler = new Compiler(new PrintStream(baos)); + // Threads can't be used in small unit tests. + compiler.disableThreads(); SourceFile sourceFile = SourceFile.fromCode(path, js); - Result result = compiler.compile( + compiler.compile( ImmutableList.of(), ImmutableList.of(sourceFile), getOptions()); @@ -141,9 +142,6 @@ public String call() throws IOException, UnsupportedEncodingException { } throw new IllegalStateException(message); } - if (!result.transpiledFiles.contains(sourceFile)) { - return js; - } StringBuilder source = new StringBuilder().append(compiler.toSource()); StringBuilder sourceMap = new StringBuilder(); compiler.getSourceMap().appendTo(sourceMap, path); @@ -172,6 +170,8 @@ private static String getEs6Runtime() { options.setLanguageOut(LanguageMode.ECMASCRIPT3); // change .delete to ['delete'] options.setForceLibraryInjection(ImmutableList.of("es6_runtime")); Compiler compiler = new Compiler(); + // Threads can't be used in small unit tests. + compiler.disableThreads(); SourceFile sourceFile = SourceFile.fromCode("source", ""); compiler.compile( ImmutableList.of(), ImmutableList.of(sourceFile), options); diff --git a/src/com/google/javascript/jscomp/parsing/parser/FeatureSet.java b/src/com/google/javascript/jscomp/parsing/parser/FeatureSet.java index c2bf5b2fddc..c89070fa307 100644 --- a/src/com/google/javascript/jscomp/parsing/parser/FeatureSet.java +++ b/src/com/google/javascript/jscomp/parsing/parser/FeatureSet.java @@ -265,9 +265,4 @@ public static FeatureSet valueOf(String name) { throw new IllegalArgumentException("No such FeatureSet: " + name); } } - - public boolean isEs6ImplOrHigher() { - String version = version(); - return !version.equals("es3") && !version.equals("es5"); - } } diff --git a/src/com/google/javascript/rhino/Node.java b/src/com/google/javascript/rhino/Node.java index af6c9207d74..a6497205ef0 100644 --- a/src/com/google/javascript/rhino/Node.java +++ b/src/com/google/javascript/rhino/Node.java @@ -148,8 +148,7 @@ public class Node implements Serializable { // goog.module() or goog.require() call. WAS_PREVIOUSLY_PROVIDED = 91, // Indicates a namespace that was provided at some point in the // past. - IS_ES6_CLASS = 92, // Indicates that a FUNCTION node is converted from an ES6 class - TRANSPILED = 93; // Indicates that a SCRIPT represents a transpiled file + IS_ES6_CLASS = 92; // Indicates that a FUNCTION node is converted from an ES6 class private static final String propToString(int propType) { switch (propType) { @@ -208,7 +207,6 @@ private static final String propToString(int propType) { case IS_MODULE_NAME: return "is_module_name"; case WAS_PREVIOUSLY_PROVIDED: return "was_previously_provided"; case IS_ES6_CLASS: return "is_es6_class"; - case TRANSPILED: return "transpiled"; default: throw new IllegalStateException("unexpected prop id " + propType); } diff --git a/test/com/google/javascript/jscomp/Es6ExtractClassesTest.java b/test/com/google/javascript/jscomp/Es6ExtractClassesTest.java index 9b5f1d01c69..9a3d58c2314 100644 --- a/test/com/google/javascript/jscomp/Es6ExtractClassesTest.java +++ b/test/com/google/javascript/jscomp/Es6ExtractClassesTest.java @@ -31,7 +31,6 @@ protected CompilerPass getProcessor(Compiler compiler) { @Override protected void setUp() { setAcceptedLanguage(LanguageMode.ECMASCRIPT6); - setLanguageOut(LanguageMode.ECMASCRIPT3); disableTypeCheck(); runTypeCheckAfterProcessing = true; } diff --git a/test/com/google/javascript/jscomp/Es6ToEs3ConverterTest.java b/test/com/google/javascript/jscomp/Es6ToEs3ConverterTest.java index 06483a5c99f..4c31531293d 100644 --- a/test/com/google/javascript/jscomp/Es6ToEs3ConverterTest.java +++ b/test/com/google/javascript/jscomp/Es6ToEs3ConverterTest.java @@ -1378,14 +1378,16 @@ public void testStaticSetter() { } public void testInitSymbol() { - test("let a = alert(Symbol.thimble);", "$jscomp.initSymbol(); var a = alert(Symbol.thimble)"); + test( + "alert(Symbol.thimble);", + "$jscomp.initSymbol(); alert(Symbol.thimble)"); assertThat(getLastCompiler().injected).containsExactly("es6/symbol"); test( LINE_JOINER.join( "function f() {", - " let x = 1;", - " let y = Symbol('nimble');", + " var x = 1;", + " var y = Symbol('nimble');", "}"), LINE_JOINER.join( "function f() {", @@ -1542,8 +1544,8 @@ public void testClassComputedPropGetterAndSetter() { * ES5 getters and setters should report an error if the languageOut is ES3. */ public void testEs5GettersAndSetters_es3() { - testError("let x = { get y() {} };", CANNOT_CONVERT); - testError("let x = { set y(value) {} };", CANNOT_CONVERT); + testError("var x = { get y() {} };", CANNOT_CONVERT); + testError("var x = { set y(value) {} };", CANNOT_CONVERT); } /** diff --git a/test/com/google/javascript/jscomp/PolymerPassStaticUtilsTest.java b/test/com/google/javascript/jscomp/PolymerPassStaticUtilsTest.java index f6cb0a1e32a..b601cadf4f8 100644 --- a/test/com/google/javascript/jscomp/PolymerPassStaticUtilsTest.java +++ b/test/com/google/javascript/jscomp/PolymerPassStaticUtilsTest.java @@ -25,13 +25,13 @@ public final class PolymerPassStaticUtilsTest extends TestCase { public void testGetPolymerElementType_noNativeElement() { PolymerClassDefinition def = new PolymerClassDefinition( - null, IR.objectlit(), null, null, null, null, null, null); + null, IR.objectlit(), null, null, null, null, null); assertThat(PolymerPassStaticUtils.getPolymerElementType(def)).isEqualTo("PolymerElement"); } public void testGetPolymerElementType_inputBaseElement() { PolymerClassDefinition def = new PolymerClassDefinition( - null, IR.objectlit(), null, null, "input", null, null, null); + null, IR.objectlit(), null, null, "input", null, null); assertThat(PolymerPassStaticUtils.getPolymerElementType(def)).isEqualTo("PolymerInputElement"); } diff --git a/test/com/google/javascript/jscomp/TypeCheckTest.java b/test/com/google/javascript/jscomp/TypeCheckTest.java index 3336f60cdef..24b7a5a208d 100644 --- a/test/com/google/javascript/jscomp/TypeCheckTest.java +++ b/test/com/google/javascript/jscomp/TypeCheckTest.java @@ -17021,7 +17021,6 @@ public void testModuloNullUndefThatWorkedWithoutSpecialSubtypingRules() { public void testEs5ClassExtendingEs6Class() { compiler.getOptions().setLanguageIn(LanguageMode.ECMASCRIPT6); - compiler.getOptions().setLanguageOut(LanguageMode.ECMASCRIPT5); testTypes( LINE_JOINER.join( "class Foo {}", @@ -17031,7 +17030,6 @@ public void testEs5ClassExtendingEs6Class() { public void testEs5ClassExtendingEs6Class_noWarning() { compiler.getOptions().setLanguageIn(LanguageMode.ECMASCRIPT6); - compiler.getOptions().setLanguageOut(LanguageMode.ECMASCRIPT5); testTypes( LINE_JOINER.join( "class A {}", @@ -17244,7 +17242,7 @@ private TypeCheckResult parseAndTypeCheckWithScope( TranspilationPasses.addEs6LatePasses(passes); PhaseOptimizer phaseopt = new PhaseOptimizer(compiler, null, null); phaseopt.consume(passes); - phaseopt.process(externsNode, externAndJsRoot); + phaseopt.process(externsNode, n); } TypedScope s = makeTypeCheck().processForTesting(externsNode, n); diff --git a/test/com/google/javascript/jscomp/VariableReferenceCheckTest.java b/test/com/google/javascript/jscomp/VariableReferenceCheckTest.java index facd463d58c..b1948767e0e 100644 --- a/test/com/google/javascript/jscomp/VariableReferenceCheckTest.java +++ b/test/com/google/javascript/jscomp/VariableReferenceCheckTest.java @@ -88,7 +88,6 @@ public void testEarlyReference() { public void testCorrectEarlyReference() { assertNoWarning("var goog = goog || {}"); - assertNoWarning("var google = google || window['google'] || {}"); assertNoWarning("function f() { a = 2; } var a = 2;"); }