Skip to content

Commit

Permalink
Extract the transpilation passes in DefaultPassConfig
Browse files Browse the repository at this point in the history
These passes are run with --skip_non_transpilation_passes. Extract them so we
don't have to check for "options.skipNonTranspilationPasses" a million times.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=129781652
  • Loading branch information
Dominator008 authored and blickly committed Aug 9, 2016
1 parent f9ef2e6 commit de2ce7d
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 52 deletions.
27 changes: 19 additions & 8 deletions src/com/google/javascript/jscomp/Compiler.java
Expand Up @@ -735,17 +735,17 @@ private void compileInternal() {
if (options.skipNonTranspilationPasses) {
// i.e. whitespace-only mode, which will not work with goog.module without:
whitespaceOnlyPasses();
transpile();
return;
}

if (!options.skipNonTranspilationPasses || options.lowerFromEs6()) {
check();
if (hasErrors()) {
return;
}
check();
if (hasErrors()) {
return;
}

if (!options.checksOnly && !options.shouldGenerateTypedExterns()) {
optimize();
}
if (!options.checksOnly && !options.shouldGenerateTypedExterns()) {
optimize();
}

if (options.recordFunctionInformation) {
Expand Down Expand Up @@ -818,6 +818,17 @@ public void whitespaceOnlyPasses() {
}
}

public void transpile() {
Tracer t = newTracer("runTranspileOnlyPasses");
try {
for (PassFactory pf : getPassConfig().getTranspileOnlyPasses()) {
pf.create(this).process(externsRoot, jsRoot);
}
} finally {
stopTracer(t, "runTranspileOnlyPasses");
}
}

public void check() {
runCustomPasses(CustomPassExecutionTime.BEFORE_CHECKS);

Expand Down
105 changes: 61 additions & 44 deletions src/com/google/javascript/jscomp/DefaultPassConfig.java
Expand Up @@ -185,6 +185,43 @@ void maybeInitializeModuleRewriteState() {
}
}

@Override
protected List<PassFactory> getTranspileOnlyPasses() {
List<PassFactory> passes = new ArrayList<>();

if (options.getLanguageIn() == LanguageMode.ECMASCRIPT6_TYPED
&& options.getLanguageOut() != LanguageMode.ECMASCRIPT6_TYPED) {
passes.add(convertEs6TypedToEs6);
}

passes.add(checkMissingSuper);
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.
if (options.dartPass && !options.getLanguageOut().isEs6OrHigher()) {
passes.add(dartSuperAccessorsPass);
}

if (options.getLanguageIn().isEs6OrHigher() && !options.skipTranspilationAndCrash) {
TranspilationPasses.addEs6EarlyPasses(passes);
TranspilationPasses.addEs6LatePasses(passes);
passes.add(markTranspilationDone);
}

if (options.raiseToEs6Typed()) {
passes.add(convertToTypedES6);
}

if (!options.forceLibraryInjection.isEmpty()) {
passes.add(injectRuntimeLibraries);
}

assertAllOneTimePasses(passes);
assertValidOrder(passes);
return passes;
}

@Override
protected List<PassFactory> getWhitespaceOnlyPasses() {
List<PassFactory> passes = new ArrayList<>();
Expand Down Expand Up @@ -226,24 +263,22 @@ protected List<PassFactory> getChecks() {
checks.add(lintChecks);
}

if (!options.skipNonTranspilationPasses && options.closurePass
&& options.enables(DiagnosticGroups.LINT_CHECKS)) {
if (options.closurePass && options.enables(DiagnosticGroups.LINT_CHECKS)) {
checks.add(checkRequiresAndProvidesSorted);
}

if (!options.skipNonTranspilationPasses
&& (options.enables(DiagnosticGroups.MISSING_REQUIRE)
|| options.enables(DiagnosticGroups.STRICT_MISSING_REQUIRE)
|| options.enables(DiagnosticGroups.EXTRA_REQUIRE))) {
if (options.enables(DiagnosticGroups.MISSING_REQUIRE)
|| options.enables(DiagnosticGroups.STRICT_MISSING_REQUIRE)
|| options.enables(DiagnosticGroups.EXTRA_REQUIRE)) {
checks.add(checkRequires);
}

if (!options.skipNonTranspilationPasses && options.closurePass) {
if (options.closurePass) {
checks.add(closureCheckModule);
checks.add(closureRewriteModule);
}

if (!options.skipNonTranspilationPasses && options.declaredGlobalExternsOnWindow) {
if (options.declaredGlobalExternsOnWindow) {
checks.add(declaredGlobalExternsOnWindow);
}

Expand All @@ -255,78 +290,71 @@ protected List<PassFactory> getChecks() {
checks.add(checkMissingSuper);
checks.add(checkVariableReferences);

if (!options.skipNonTranspilationPasses && options.closurePass) {
if (options.closurePass) {
checks.add(closureGoogScopeAliases);
checks.add(closureRewriteClass);
}

if (!options.skipNonTranspilationPasses) {
checks.add(checkSideEffects);
}
checks.add(checkSideEffects);

if (options.enables(DiagnosticGroups.MISSING_PROVIDE)) {
checks.add(checkProvides);
}

if (options.jqueryPass && !options.skipNonTranspilationPasses) {
if (options.jqueryPass) {
checks.add(jqueryAliases);
}

if (options.angularPass && !options.skipNonTranspilationPasses) {
if (options.angularPass) {
checks.add(angularPass);
}

if (!options.generateExportsAfterTypeChecking
&& options.generateExports && !options.skipNonTranspilationPasses) {
if (!options.generateExportsAfterTypeChecking && options.generateExports) {
checks.add(generateExports);
}

if (options.exportTestFunctions && !options.skipNonTranspilationPasses) {
if (options.exportTestFunctions) {
checks.add(exportTestFunctions);
}

if (options.closurePass && !options.skipNonTranspilationPasses) {
if (options.closurePass) {
checks.add(closurePrimitives);
}

// It's important that the PolymerPass run *after* the ClosurePrimitives
// rewrite and *before* the suspicious code checks.
// This is enforced in the assertValidOrder method.
if (options.polymerPass && !options.skipNonTranspilationPasses) {
if (options.polymerPass) {
checks.add(polymerPass);
}

if ((options.checkSuspiciousCode
|| options.enables(DiagnosticGroups.GLOBAL_THIS)
|| options.enables(DiagnosticGroups.DEBUGGER_STATEMENT_PRESENT))
&& !options.skipNonTranspilationPasses) {
if (options.checkSuspiciousCode
|| options.enables(DiagnosticGroups.GLOBAL_THIS)
|| options.enables(DiagnosticGroups.DEBUGGER_STATEMENT_PRESENT)) {
checks.add(suspiciousCode);
}

if (options.closurePass && options.checkMissingGetCssNameLevel.isOn()
&& !options.skipNonTranspilationPasses) {
if (options.closurePass && options.checkMissingGetCssNameLevel.isOn()) {
checks.add(closureCheckGetCssName);
}

if (options.syntheticBlockStartMarker != null && !options.skipNonTranspilationPasses) {
if (options.syntheticBlockStartMarker != null) {
// This pass must run before the first fold constants pass.
checks.add(createSyntheticBlocks);
}

if (!options.skipNonTranspilationPasses) {
checks.add(checkVars);
}
checks.add(checkVars);

if (options.inferConsts && !options.skipNonTranspilationPasses) {
if (options.inferConsts) {
checks.add(inferConsts);
}

if (options.computeFunctionSideEffects && !options.skipNonTranspilationPasses) {
if (options.computeFunctionSideEffects) {
checks.add(checkRegExp);
}

// This pass should run before types are assigned.
if (options.processObjectPropertyString && !options.skipNonTranspilationPasses) {
if (options.processObjectPropertyString) {
checks.add(objectPropertyStringPreprocess);
}

Expand All @@ -337,10 +365,7 @@ protected List<PassFactory> getChecks() {
}

if (options.getLanguageIn().isEs6OrHigher() && !options.skipTranspilationAndCrash) {
if (!options.skipNonTranspilationPasses) {
checks.add(es6ExternsCheck);
}
checks.add(es6SuperCheck);
checks.add(es6ExternsCheck);
TranspilationPasses.addEs6EarlyPasses(checks);
}

Expand Down Expand Up @@ -1293,14 +1318,6 @@ protected CompilerPass create(final AbstractCompiler compiler) {
}
};

private final PassFactory es6SuperCheck =
new PassFactory("es6SuperCheck", true) {
@Override
protected CompilerPass create(final AbstractCompiler compiler) {
return new Es6SuperCheck(compiler);
}
};

/**
* Desugars ES6_TYPED features into ES6 code.
*/
Expand Down
5 changes: 5 additions & 0 deletions src/com/google/javascript/jscomp/PassConfig.java
Expand Up @@ -109,6 +109,11 @@ protected List<PassFactory> getWhitespaceOnlyPasses() {
return Collections.emptyList();
}

/** Gets the transpilation passes */
protected List<PassFactory> getTranspileOnlyPasses() {
return Collections.emptyList();
}

/**
* Gets the checking passes to run.
*
Expand Down
8 changes: 8 additions & 0 deletions src/com/google/javascript/jscomp/TranspilationPasses.java
Expand Up @@ -34,6 +34,7 @@ private TranspilationPasses() {}
* transpile them, even if the output language is also ES6.
*/
public static void addEs6EarlyPasses(List<PassFactory> passes) {
passes.add(es6SuperCheck);
passes.add(es6ConvertSuper);
passes.add(es6RewriteArrowFunction);
passes.add(es6RenameVariablesInParamLists);
Expand All @@ -57,6 +58,13 @@ public static void addEs6LatePasses(List<PassFactory> passes) {
passes.add(rewritePolyfills);
}

private static final PassFactory es6SuperCheck =
new PassFactory("es6SuperCheck", true) {
@Override
protected CompilerPass create(final AbstractCompiler compiler) {
return new Es6SuperCheck(compiler);
}
};

static final HotSwapPassFactory es6ExtractClasses =
new HotSwapPassFactory("Es6ExtractClasses", true) {
Expand Down

0 comments on commit de2ce7d

Please sign in to comment.