Skip to content

Commit

Permalink
Modify when the runtime_type_check library is injected so that it hap…
Browse files Browse the repository at this point in the history
…pens before type checking.

This is in preparation for banning runtime library injection after type checking).

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=201261788
  • Loading branch information
concavelenz authored and brad4d committed Jun 20, 2018
1 parent 784b4af commit 28863f6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
8 changes: 2 additions & 6 deletions src/com/google/javascript/jscomp/DefaultPassConfig.java
Expand Up @@ -187,9 +187,7 @@ protected List<PassFactory> getTranspileOnlyPasses() {
}
}

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

assertAllOneTimePasses(passes);
assertValidOrderForChecks(passes);
Expand Down Expand Up @@ -377,9 +375,7 @@ protected List<PassFactory> getChecks() {
TranspilationPasses.addRewritePolyfillPass(checks);
}

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

if (options.needsTranspilationFrom(ES6)) {
checks.add(convertStaticInheritance);
Expand Down
7 changes: 6 additions & 1 deletion src/com/google/javascript/jscomp/InjectRuntimeLibraries.java
Expand Up @@ -31,7 +31,12 @@ public InjectRuntimeLibraries(AbstractCompiler compiler) {

@Override
public void process(Node externs, Node root) {
for (String forced : compiler.getOptions().forceLibraryInjection) {
CompilerOptions options = compiler.getOptions();
if (options.runtimeTypeCheck) {
compiler.ensureLibraryInjected("runtime_type_check", true);
}

for (String forced : options.forceLibraryInjection) {
compiler.ensureLibraryInjected(forced, true);
}
}
Expand Down
28 changes: 28 additions & 0 deletions test/com/google/javascript/jscomp/IntegrationTest.java
Expand Up @@ -5218,6 +5218,34 @@ public void testDestructuringRest() {
"}"));
}

private void addMinimalExternsForRuntimeTypeCheck() {
ImmutableList.Builder<SourceFile> externsList = ImmutableList.builder();
externsList.addAll(externs);
externsList.add(
SourceFile.fromCode(
"other_externs.js",
lines(
"Array.prototype.join;",
"var RegExp;",
"RegExp.prototype.exec;",
"Window.prototype.frames",
"Window.prototype.length",
"")));
externs = externsList.build();
}

public void testRuntimeTypeCheckInjection() {
CompilerOptions options = createCompilerOptions();
addMinimalExternsForRuntimeTypeCheck();
options.checkTypes = true;
options.enableRuntimeTypeCheck("callMe");

// Verify that no warning/errors or exceptions occure but otherwise ignore the output.
test(
options, new String[] { "function callMe(a) {}; /** @type {string} */ var x = y; " },
(String []) null);
}

/** Creates a CompilerOptions object with google coding conventions. */
@Override
protected CompilerOptions createCompilerOptions() {
Expand Down

0 comments on commit 28863f6

Please sign in to comment.