Skip to content

Commit

Permalink
Always respect the --strict_mode_input flag.
Browse files Browse the repository at this point in the history
Previously, it was silently ignored in ES3, ES5, ES6, ES5_STRICT, ES6_STRICT,
and ES6_TYPED language modes.

Similar to my recent change for about the --emit_use_strict flag.

Note that this means that users of CommandLineRunner who want to avoid strict
mode checks will now need to pass --strict_mode_input=false.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=156799045
  • Loading branch information
blickly committed May 22, 2017
1 parent cc08013 commit 0266cab
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 44 deletions.
3 changes: 1 addition & 2 deletions src/com/google/javascript/jscomp/CommandLineRunner.java
Expand Up @@ -173,8 +173,7 @@ private static class Flags {
@Option( @Option(
name = "--strict_mode_input", name = "--strict_mode_input",
handler = BooleanOptionHandler.class, handler = BooleanOptionHandler.class,
usage = "Assume input sources are to run in strict mode." usage = "Assume input sources are to run in strict mode.")
+ " Ignored for language modes earlier than ECMASCRIPT_2016.")
private boolean strictModeInput = true; private boolean strictModeInput = true;


// Turn on (very slow) extra sanity checks for use when modifying the // Turn on (very slow) extra sanity checks for use when modifying the
Expand Down
19 changes: 2 additions & 17 deletions src/com/google/javascript/jscomp/Compiler.java
Expand Up @@ -473,7 +473,7 @@ protected void reconcileOptionsWithGuards() {
options.checkGlobalThisLevel); options.checkGlobalThisLevel);
} }


if (expectStrictModeInput()) { if (options.expectStrictModeInput()) {
options.setWarningLevel( options.setWarningLevel(
DiagnosticGroups.ES5_STRICT, DiagnosticGroups.ES5_STRICT,
CheckLevel.ERROR); CheckLevel.ERROR);
Expand All @@ -491,21 +491,6 @@ protected void reconcileOptionsWithGuards() {
} }
} }


private boolean expectStrictModeInput() {
switch (options.getLanguageIn()) {
case ECMASCRIPT3:
case ECMASCRIPT5:
case ECMASCRIPT6:
return false;
case ECMASCRIPT5_STRICT:
case ECMASCRIPT6_STRICT:
case ECMASCRIPT6_TYPED:
return true;
default:
return options.isStrictModeInput();
}
}

/** /**
* Initializes the instance state needed for a compile job. * Initializes the instance state needed for a compile job.
*/ */
Expand Down Expand Up @@ -2681,7 +2666,7 @@ Config getParserConfig(ConfigContext context) {
Config.LanguageMode configLanguageMode = getParserConfigLanguageMode( Config.LanguageMode configLanguageMode = getParserConfigLanguageMode(
options.getLanguageIn()); options.getLanguageIn());
Config.StrictMode strictMode = Config.StrictMode strictMode =
expectStrictModeInput() ? Config.StrictMode.STRICT : Config.StrictMode.SLOPPY; options.expectStrictModeInput() ? Config.StrictMode.STRICT : Config.StrictMode.SLOPPY;
parserConfig = createConfig(configLanguageMode, strictMode); parserConfig = createConfig(configLanguageMode, strictMode);
// Externs must always be parsed with at least ES5 language mode. // Externs must always be parsed with at least ES5 language mode.
externsParserConfig = externsParserConfig =
Expand Down
36 changes: 18 additions & 18 deletions src/com/google/javascript/jscomp/CompilerOptions.java
Expand Up @@ -1151,10 +1151,8 @@ public void setWrapGoogModulesForWhitespaceOnly(boolean enable) {


/** /**
* Are the input files written for strict mode? * Are the input files written for strict mode?
*
* <p>Ignored for language modes that do not support strict mode.
*/ */
private boolean isStrictModeInput = true; private Optional<Boolean> isStrictModeInput = Optional.absent();


/** Which algorithm to use for locating ES6 and CommonJS modules */ /** Which algorithm to use for locating ES6 and CommonJS modules */
ModuleLoader.ResolutionMode moduleResolutionMode; ModuleLoader.ResolutionMode moduleResolutionMode;
Expand Down Expand Up @@ -2721,18 +2719,7 @@ public void setConformanceConfigs(List<ConformanceConfig> configs) {
} }


public boolean shouldEmitUseStrict() { public boolean shouldEmitUseStrict() {
return this.emitUseStrict.or(getDefaultEmitUseStrict()); return this.emitUseStrict.or(getLanguageOut().isDefaultStrict());
}

boolean getDefaultEmitUseStrict() {
switch (getLanguageOut()) {
case ECMASCRIPT3:
case ECMASCRIPT5:
case ECMASCRIPT6:
return false;
default:
return true;
}
} }


public CompilerOptions setEmitUseStrict(boolean emitUseStrict) { public CompilerOptions setEmitUseStrict(boolean emitUseStrict) {
Expand Down Expand Up @@ -3031,6 +3018,19 @@ public static enum LanguageMode {
*/ */
NO_TRANSPILE; NO_TRANSPILE;



/** Whether this language mode defaults to strict mode */
boolean isDefaultStrict() {
switch (this) {
case ECMASCRIPT3:
case ECMASCRIPT5:
case ECMASCRIPT6:
return false;
default:
return true;
}
}

/** Whether this is ECMAScript 5 or higher. */ /** Whether this is ECMAScript 5 or higher. */
public boolean isEs5OrHigher() { public boolean isEs5OrHigher() {
Preconditions.checkState(this != NO_TRANSPILE); Preconditions.checkState(this != NO_TRANSPILE);
Expand Down Expand Up @@ -3310,12 +3310,12 @@ boolean isExplicitlyOn() {
} }
} }


public boolean isStrictModeInput() { public boolean expectStrictModeInput() {
return isStrictModeInput; return isStrictModeInput.or(getLanguageIn().isDefaultStrict());
} }


public CompilerOptions setStrictModeInput(boolean isStrictModeInput) { public CompilerOptions setStrictModeInput(boolean isStrictModeInput) {
this.isStrictModeInput = isStrictModeInput; this.isStrictModeInput = Optional.of(isStrictModeInput);
return this; return this;
} }


Expand Down
2 changes: 1 addition & 1 deletion src/com/google/javascript/jscomp/DefaultPassConfig.java
Expand Up @@ -2357,7 +2357,7 @@ protected CompilerPass create(AbstractCompiler compiler) {
options.inlineFunctions, options.inlineFunctions,
options.inlineLocalFunctions, options.inlineLocalFunctions,
true, true,
options.assumeStrictThis() || options.isStrictModeInput(), options.assumeStrictThis() || options.expectStrictModeInput(),
options.assumeClosuresOnlyCaptureReferences, options.assumeClosuresOnlyCaptureReferences,
options.maxFunctionSizeAfterInlining); options.maxFunctionSizeAfterInlining);
} }
Expand Down
3 changes: 0 additions & 3 deletions src/com/google/javascript/jscomp/parsing/Config.java
Expand Up @@ -16,8 +16,6 @@


package com.google.javascript.jscomp.parsing; package com.google.javascript.jscomp.parsing;


import static com.google.common.base.Preconditions.checkArgument;

import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.javascript.jscomp.parsing.parser.FeatureSet; import com.google.javascript.jscomp.parsing.parser.FeatureSet;
Expand Down Expand Up @@ -140,7 +138,6 @@ public enum RunMode {
LanguageMode languageMode, LanguageMode languageMode,
boolean parseInlineSourceMaps, boolean parseInlineSourceMaps,
StrictMode strictMode) { StrictMode strictMode) {
checkArgument(!(languageMode == LanguageMode.ECMASCRIPT3 && strictMode == StrictMode.STRICT));
this.parseInlineSourceMaps = parseInlineSourceMaps; this.parseInlineSourceMaps = parseInlineSourceMaps;
this.annotationNames = buildAnnotationNames(annotationWhitelist); this.annotationNames = buildAnnotationNames(annotationWhitelist);
this.parseJsDocDocumentation = parseJsDocDocumentation; this.parseJsDocDocumentation = parseJsDocDocumentation;
Expand Down
3 changes: 0 additions & 3 deletions src/com/google/javascript/jscomp/parsing/parser/Parser.java
Expand Up @@ -16,8 +16,6 @@


package com.google.javascript.jscomp.parsing.parser; package com.google.javascript.jscomp.parsing.parser;


import static com.google.common.base.Preconditions.checkArgument;

import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.io.BaseEncoding; import com.google.common.io.BaseEncoding;
import com.google.errorprone.annotations.FormatMethod; import com.google.errorprone.annotations.FormatMethod;
Expand Down Expand Up @@ -234,7 +232,6 @@ public static enum Mode {
private final boolean warnTrailingCommas; private final boolean warnTrailingCommas;


public Config(Mode mode, boolean isStrictMode) { public Config(Mode mode, boolean isStrictMode) {
checkArgument(!(mode == Mode.ES3 && isStrictMode));
parseTypeSyntax = mode == Mode.TYPESCRIPT; parseTypeSyntax = mode == Mode.TYPESCRIPT;
atLeast6 = !(mode == Mode.ES3 || mode == Mode.ES5); atLeast6 = !(mode == Mode.ES3 || mode == Mode.ES5);
this.isStrictMode = isStrictMode; this.isStrictMode = isStrictMode;
Expand Down
15 changes: 15 additions & 0 deletions test/com/google/javascript/jscomp/CommandLineRunnerTest.java
Expand Up @@ -1493,11 +1493,25 @@ public void testTwoParseErrors() {
public void testES3() { public void testES3() {
args.add("--language_in=ECMASCRIPT3"); args.add("--language_in=ECMASCRIPT3");
args.add("--language_out=ECMASCRIPT3"); args.add("--language_out=ECMASCRIPT3");
args.add("--strict_mode_input=false");
useStringComparison = true; useStringComparison = true;
test( test(
"var x = f.function", "var x = f.function",
"var x=f[\"function\"];", "var x=f[\"function\"];",
RhinoErrorReporter.INVALID_ES3_PROP_NAME); RhinoErrorReporter.INVALID_ES3_PROP_NAME);
testSame("var let;");
}

public void testES3plusStrictModeChecks() {
args.add("--language_in=ECMASCRIPT3");
args.add("--language_out=ECMASCRIPT3");
useStringComparison = true;
test(
"var x = f.function",
"var x=f[\"function\"];",
RhinoErrorReporter.INVALID_ES3_PROP_NAME);
test("var let", RhinoErrorReporter.PARSE_ERROR);
test("function f(x) { delete x; }", StrictModeCheck.DELETE_VARIABLE);
} }


public void testES6TranspiledByDefault() { public void testES6TranspiledByDefault() {
Expand All @@ -1515,6 +1529,7 @@ public void testES5ChecksInVerbose() {


public void testES5() { public void testES5() {
args.add("--language_in=ECMASCRIPT5"); args.add("--language_in=ECMASCRIPT5");
args.add("--strict_mode_input=false");
test("var x = f.function", "var x = f.function"); test("var x = f.function", "var x = f.function");
test("var let", "var let"); test("var let", "var let");
} }
Expand Down

0 comments on commit 0266cab

Please sign in to comment.