Skip to content

Commit

Permalink
Update AST feature set in transpilation passes
Browse files Browse the repository at this point in the history
Before this change the AST feature set was updated in
DefaultPassConfig, which required it to have knowledge about which
passes transpiled which features.

This change will make it easier to move the transpilation passes around.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=188245670
  • Loading branch information
brad4d authored and lauraharker committed Mar 8, 2018
1 parent 18e41ee commit b13f1a3
Show file tree
Hide file tree
Showing 15 changed files with 114 additions and 46 deletions.
38 changes: 0 additions & 38 deletions src/com/google/javascript/jscomp/DefaultPassConfig.java
Expand Up @@ -165,17 +165,14 @@ protected List<PassFactory> getTranspileOnlyPasses() {

if (options.needsTranspilationFrom(ES2018)) {
TranspilationPasses.addEs2018Passes(passes);
passes.add(setFeatureSet(ES8));
}

if (options.needsTranspilationFrom(ES8)) {
TranspilationPasses.addEs2017Passes(passes);
passes.add(setFeatureSet(ES7));
}

if (options.needsTranspilationFrom(ES7)) {
TranspilationPasses.addEs2016Passes(passes);
passes.add(setFeatureSet(ES6));
}

// If the user has specified an input language of ES7 and an output language of ES6 or lower,
Expand All @@ -189,7 +186,6 @@ protected List<PassFactory> getTranspileOnlyPasses() {
if (options.rewritePolyfills) {
TranspilationPasses.addRewritePolyfillPass(passes);
}
passes.add(setFeatureSet(options.getLanguageOut().toFeatureSet()));
}

if (!options.forceLibraryInjection.isEmpty()) {
Expand Down Expand Up @@ -280,7 +276,6 @@ protected List<PassFactory> getChecks() {

if (options.needsTranspilationFrom(ES2018)) {
TranspilationPasses.addEs2018Passes(checks);
checks.add(setFeatureSet(ES8));
}

if (options.enables(DiagnosticGroups.LINT_CHECKS)) {
Expand Down Expand Up @@ -386,12 +381,10 @@ protected List<PassFactory> getChecks() {

if (options.needsTranspilationFrom(ES8)) {
TranspilationPasses.addEs2017Passes(checks);
checks.add(setFeatureSet(ES7));
}

if (options.needsTranspilationFrom(ES7) && !options.getTypeCheckEs6Natively()) {
TranspilationPasses.addEs2016Passes(checks);
checks.add(setFeatureSet(ES6));
}

if (options.needsTranspilationFrom(ES6)) {
Expand All @@ -411,16 +404,6 @@ protected List<PassFactory> getChecks() {
TranspilationPasses.addRewritePolyfillPass(checks);
}

if (options.needsTranspilationFrom(ES6)) {
if (options.getTypeCheckEs6Natively()) {
checks.add(setFeatureSet(FeatureSet.NTI_SUPPORTED));
} else {
// TODO(bradfordcsmith): This marking is really about how variable scoping is handled during
// type checking. It should really be handled in a more direct fashion.
checks.add(setFeatureSet(options.getLanguageOut().toFeatureSet()));
}
}

if (!options.forceLibraryInjection.isEmpty()) {
checks.add(injectRuntimeLibraries);
}
Expand All @@ -439,12 +422,10 @@ protected List<PassFactory> getChecks() {

if (options.needsTranspilationFrom(ES7)) {
TranspilationPasses.addEs2016Passes(checks);
checks.add(setFeatureSet(ES6));
}

if (options.needsTranspilationFrom(ES6)) {
TranspilationPasses.addEs6PassesAfterNTI(checks);
checks.add(setFeatureSet(options.getLanguageOut().toFeatureSet()));
}
}

Expand Down Expand Up @@ -1540,25 +1521,6 @@ protected FeatureSet featureSet() {
}
};

private final PassFactory setFeatureSet(final FeatureSet featureSet) {
return new PassFactory("setFeatureSet:" + featureSet.version(), true) {
@Override
protected CompilerPass create(final AbstractCompiler compiler) {
return new CompilerPass() {
@Override
public void process(Node externs, Node root) {
compiler.setFeatureSet(featureSet);
}
};
}

@Override
public FeatureSet featureSet() {
return FeatureSet.latest();
}
};
}

private final PassFactory declaredGlobalExternsOnWindow =
new PassFactory(PassNames.DECLARED_GLOBAL_EXTERNS_ON_WINDOW, true) {
@Override
Expand Down
16 changes: 12 additions & 4 deletions src/com/google/javascript/jscomp/EarlyEs6ToEs3Converter.java
Expand Up @@ -22,6 +22,7 @@
import com.google.javascript.jscomp.NodeTraversal.Callback;
import com.google.javascript.jscomp.NodeUtil.Visitor;
import com.google.javascript.jscomp.parsing.parser.FeatureSet;
import com.google.javascript.jscomp.parsing.parser.FeatureSet.Feature;
import com.google.javascript.rhino.IR;
import com.google.javascript.rhino.JSDocInfo;
import com.google.javascript.rhino.JSDocInfoBuilder;
Expand Down Expand Up @@ -57,21 +58,28 @@ public final class EarlyEs6ToEs3Converter implements Callback, HotSwapCompilerPa

private static final String FRESH_SPREAD_VAR = "$jscomp$spread$args";
// Since there's currently no Feature for Symbol, run this pass if the code has any ES6 features.
private static final FeatureSet transpiledFeatures = FeatureSet.ES6.without(FeatureSet.ES5);
private static final FeatureSet requiredForFeatures = FeatureSet.ES6.without(FeatureSet.ES5);
private static final FeatureSet featuresTranspiledAway =
FeatureSet.BARE_MINIMUM.with(
Feature.ARRAY_PATTERN_REST,
Feature.REST_PARAMETERS,
Feature.SPREAD_EXPRESSIONS);

public EarlyEs6ToEs3Converter(AbstractCompiler compiler) {
this.compiler = compiler;
}

@Override
public void process(Node externs, Node root) {
TranspilationPasses.processTranspile(compiler, externs, transpiledFeatures, this);
TranspilationPasses.processTranspile(compiler, root, transpiledFeatures, this);
TranspilationPasses.processTranspile(compiler, externs, requiredForFeatures, this);
TranspilationPasses.processTranspile(compiler, root, requiredForFeatures, this);
TranspilationPasses.markFeaturesAsTranspiledAway(compiler, featuresTranspiledAway);
}

@Override
public void hotSwapScript(Node scriptRoot, Node originalRoot) {
TranspilationPasses.hotSwapTranspile(compiler, scriptRoot, transpiledFeatures, this);
TranspilationPasses.hotSwapTranspile(compiler, scriptRoot, requiredForFeatures, this);
TranspilationPasses.markFeaturesAsTranspiledAway(compiler, featuresTranspiledAway);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/com/google/javascript/jscomp/Es6ConvertSuper.java
Expand Up @@ -267,10 +267,12 @@ public void process(Node externs, Node root) {
// Might need to synthesize constructors for ambient classes in .d.ts externs
TranspilationPasses.processTranspile(compiler, externs, transpiledFeatures, this);
TranspilationPasses.processTranspile(compiler, root, transpiledFeatures, this);
TranspilationPasses.markFeaturesAsTranspiledAway(compiler, transpiledFeatures);
}

@Override
public void hotSwapScript(Node scriptRoot, Node originalRoot) {
TranspilationPasses.hotSwapTranspile(compiler, scriptRoot, transpiledFeatures, this);
TranspilationPasses.markFeaturesAsTranspiledAway(compiler, transpiledFeatures);
}
}
2 changes: 2 additions & 0 deletions src/com/google/javascript/jscomp/Es6RewriteArrowFunction.java
Expand Up @@ -72,11 +72,13 @@ public Es6RewriteArrowFunction(AbstractCompiler compiler) {
public void process(Node externs, Node root) {
TranspilationPasses.processTranspile(compiler, externs, transpiledFeatures, this);
TranspilationPasses.processTranspile(compiler, root, transpiledFeatures, this);
TranspilationPasses.markFeaturesAsTranspiledAway(compiler, transpiledFeatures);
}

@Override
public void hotSwapScript(Node scriptRoot, Node originalRoot) {
TranspilationPasses.hotSwapTranspile(compiler, scriptRoot, transpiledFeatures, this);
TranspilationPasses.markFeaturesAsTranspiledAway(compiler, transpiledFeatures);
}

@Override
Expand Down
Expand Up @@ -122,6 +122,7 @@ public void process(Node externs, Node root) {
NodeTraversal.traverseEs6(compiler, root, transformer);
transformer.transformLoopClosure();
rewriteDeclsToVars();
TranspilationPasses.markFeaturesAsTranspiledAway(compiler, transpiledFeatures);
}

@Override
Expand All @@ -133,6 +134,7 @@ public void hotSwapScript(Node scriptRoot, Node originalRoot) {
NodeTraversal.traverseEs6(compiler, scriptRoot, transformer);
transformer.transformLoopClosure();
rewriteDeclsToVars();
TranspilationPasses.markFeaturesAsTranspiledAway(compiler, transpiledFeatures);
}

/**
Expand Down
Expand Up @@ -42,11 +42,13 @@ public Es6RewriteBlockScopedFunctionDeclaration(AbstractCompiler compiler) {
public void process(Node externs, Node root) {
TranspilationPasses.processTranspile(compiler, externs, transpiledFeatures, this);
TranspilationPasses.processTranspile(compiler, root, transpiledFeatures, this);
TranspilationPasses.markFeaturesAsTranspiledAway(compiler, transpiledFeatures);
}

@Override
public void hotSwapScript(Node scriptRoot, Node originalRoot) {
TranspilationPasses.hotSwapTranspile(compiler, scriptRoot, transpiledFeatures, this);
TranspilationPasses.markFeaturesAsTranspiledAway(compiler, transpiledFeatures);
}

@Override
Expand Down
15 changes: 14 additions & 1 deletion src/com/google/javascript/jscomp/Es6RewriteClass.java
Expand Up @@ -42,7 +42,8 @@
*/
public final class Es6RewriteClass implements NodeTraversal.Callback, HotSwapCompilerPass {
private final AbstractCompiler compiler;
private static final FeatureSet features = FeatureSet.BARE_MINIMUM.with(Feature.CLASSES);
private static final FeatureSet features =
FeatureSet.BARE_MINIMUM.with(Feature.CLASSES, Feature.NEW_TARGET);

// Whether to add $jscomp.inherits(Parent, Child) for each subclass.
private final boolean shouldAddInheritsPolyfill;
Expand Down Expand Up @@ -75,11 +76,23 @@ public Es6RewriteClass(AbstractCompiler compiler, boolean shouldAddInheritsPolyf
public void process(Node externs, Node root) {
TranspilationPasses.processTranspile(compiler, externs, features, this);
TranspilationPasses.processTranspile(compiler, root, features, this);
// Don't mark features as transpiled away if we had errors that prevented transpilation.
// We don't want a redundant error from the AstValidator complaining that the features are still
// there
if (!compiler.hasHaltingErrors()) {
TranspilationPasses.markFeaturesAsTranspiledAway(compiler, features);
}
}

@Override
public void hotSwapScript(Node scriptRoot, Node originalRoot) {
TranspilationPasses.hotSwapTranspile(compiler, scriptRoot, features, this);
// Don't mark features as transpiled away if we had errors that prevented transpilation.
// We don't want a redundant error from the AstValidator complaining that the features are still
// there
if (!compiler.hasHaltingErrors()) {
TranspilationPasses.markFeaturesAsTranspiledAway(compiler, features);
}
}

@Override
Expand Down
2 changes: 2 additions & 0 deletions src/com/google/javascript/jscomp/Es6RewriteDestructuring.java
Expand Up @@ -49,11 +49,13 @@ public Es6RewriteDestructuring(AbstractCompiler compiler) {
public void process(Node externs, Node root) {
TranspilationPasses.processTranspile(compiler, externs, transpiledFeatures, this);
TranspilationPasses.processTranspile(compiler, root, transpiledFeatures, this);
TranspilationPasses.markFeaturesAsTranspiledAway(compiler, transpiledFeatures);
}

@Override
public void hotSwapScript(Node scriptRoot, Node originalRoot) {
TranspilationPasses.hotSwapTranspile(compiler, scriptRoot, transpiledFeatures, this);
TranspilationPasses.markFeaturesAsTranspiledAway(compiler, transpiledFeatures);
}

@Override
Expand Down
2 changes: 2 additions & 0 deletions src/com/google/javascript/jscomp/Es6RewriteGenerators.java
Expand Up @@ -86,12 +86,14 @@ final class Es6RewriteGenerators implements HotSwapCompilerPass {
public void process(Node externs, Node root) {
TranspilationPasses.processTranspile(
compiler, root, transpiledFeatures, new GeneratorFunctionsTranspiler());
TranspilationPasses.markFeaturesAsTranspiledAway(compiler, transpiledFeatures);
}

@Override
public void hotSwapScript(Node scriptRoot, Node originalRoot) {
TranspilationPasses.hotSwapTranspile(
compiler, scriptRoot, transpiledFeatures, new GeneratorFunctionsTranspiler());
TranspilationPasses.markFeaturesAsTranspiledAway(compiler, transpiledFeatures);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/com/google/javascript/jscomp/Es7ToEs6Converter.java
Expand Up @@ -49,11 +49,13 @@ public Es7ToEs6Converter(AbstractCompiler compiler) {
public void process(Node externs, Node root) {
TranspilationPasses.processTranspile(compiler, externs, transpiledFeatures, this);
TranspilationPasses.processTranspile(compiler, root, transpiledFeatures, this);
TranspilationPasses.markFeaturesAsTranspiledAway(compiler, transpiledFeatures);
}

@Override
public void hotSwapScript(Node scriptRoot, Node originalRoot) {
TranspilationPasses.hotSwapTranspile(compiler, scriptRoot, transpiledFeatures, this);
TranspilationPasses.markFeaturesAsTranspiledAway(compiler, transpiledFeatures);
}

@Override
Expand Down
2 changes: 2 additions & 0 deletions src/com/google/javascript/jscomp/EsNextToEs8Converter.java
Expand Up @@ -59,11 +59,13 @@ public EsNextToEs8Converter(AbstractCompiler compiler) {
public void process(Node externs, Node root) {
TranspilationPasses.processTranspile(compiler, externs, transpiledFeatures, this);
TranspilationPasses.processTranspile(compiler, root, transpiledFeatures, this);
TranspilationPasses.markFeaturesAsTranspiledAway(compiler, transpiledFeatures);
}

@Override
public void hotSwapScript(Node scriptRoot, Node originalRoot) {
TranspilationPasses.hotSwapTranspile(compiler, scriptRoot, transpiledFeatures, this);
TranspilationPasses.markFeaturesAsTranspiledAway(compiler, transpiledFeatures);
}

@Override
Expand Down
11 changes: 10 additions & 1 deletion src/com/google/javascript/jscomp/LateEs6ToEs3Converter.java
Expand Up @@ -24,6 +24,7 @@
import com.google.javascript.jscomp.AbstractCompiler.MostRecentTypechecker;
import com.google.javascript.jscomp.CompilerOptions.LanguageMode;
import com.google.javascript.jscomp.parsing.parser.FeatureSet;
import com.google.javascript.jscomp.parsing.parser.FeatureSet.Feature;
import com.google.javascript.rhino.IR;
import com.google.javascript.rhino.JSDocInfo;
import com.google.javascript.rhino.Node;
Expand All @@ -49,7 +50,13 @@
// TODO(tbreisacher): This class does too many things. Break it into smaller passes.
public final class LateEs6ToEs3Converter implements NodeTraversal.Callback, HotSwapCompilerPass {
private final AbstractCompiler compiler;
private static final FeatureSet transpiledFeatures = FeatureSet.ES6.without(FeatureSet.ES5);
private static final FeatureSet transpiledFeatures =
FeatureSet.BARE_MINIMUM.with(
Feature.COMPUTED_PROPERTIES,
Feature.EXTENDED_OBJECT_LITERALS,
Feature.FOR_OF,
Feature.MEMBER_DECLARATIONS,
Feature.TEMPLATE_LITERALS);
// addTypes indicates whether we should add type information when transpiling.
private final boolean addTypes;
private final TypeIRegistry registry;
Expand Down Expand Up @@ -77,11 +84,13 @@ public LateEs6ToEs3Converter(AbstractCompiler compiler) {
public void process(Node externs, Node root) {
TranspilationPasses.processTranspile(compiler, externs, transpiledFeatures, this);
TranspilationPasses.processTranspile(compiler, root, transpiledFeatures, this);
TranspilationPasses.markFeaturesAsTranspiledAway(compiler, transpiledFeatures);
}

@Override
public void hotSwapScript(Node scriptRoot, Node originalRoot) {
TranspilationPasses.hotSwapTranspile(compiler, scriptRoot, transpiledFeatures, this);
TranspilationPasses.markFeaturesAsTranspiledAway(compiler, transpiledFeatures);
}

@Override
Expand Down
2 changes: 2 additions & 0 deletions src/com/google/javascript/jscomp/RewriteAsyncFunctions.java
Expand Up @@ -137,11 +137,13 @@ public RewriteAsyncFunctions(AbstractCompiler compiler) {
public void process(Node externs, Node root) {
TranspilationPasses.processTranspile(compiler, externs, transpiledFeatures, this);
TranspilationPasses.processTranspile(compiler, root, transpiledFeatures, this);
TranspilationPasses.markFeaturesAsTranspiledAway(compiler, transpiledFeatures);
}

@Override
public void hotSwapScript(Node scriptRoot, Node originalRoot) {
TranspilationPasses.hotSwapTranspile(compiler, scriptRoot, transpiledFeatures, this);
TranspilationPasses.markFeaturesAsTranspiledAway(compiler, transpiledFeatures);
}

@Override
Expand Down

0 comments on commit b13f1a3

Please sign in to comment.