Skip to content

Commit

Permalink
Add a compiler option to specify strict mode treatment of
Browse files Browse the repository at this point in the history
input javascript.

The option defaults to "on", but is ignored for language modes
that disallow or explictly require strict mode.

Also removed the isStrict() method from LanguageMode,
since we're moving to separate specification of this for
future language modes.

This is a no-op change.
It is a step toward adding a flag to control strict mode treatment of inputs
independent of input language mode.

Related to #2068

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=136870289
  • Loading branch information
brad4d authored and blickly committed Oct 21, 2016
1 parent 609e86b commit 73409ec
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/com/google/javascript/jscomp/CommandLineRunner.java
Expand Up @@ -1629,7 +1629,7 @@ protected CompilerOptions createOptions() {
}

options.setPrintSourceAfterEachPass(flags.printSourceAfterEachPass);
options.setEmitUseStrict(flags.emitUseStrict && options.getLanguageOut().isStrict());
options.setEmitUseStrict(flags.emitUseStrict);

return options;
}
Expand Down
31 changes: 28 additions & 3 deletions src/com/google/javascript/jscomp/Compiler.java
Expand Up @@ -409,7 +409,7 @@ protected void reconcileOptionsWithGuards() {
options.checkGlobalThisLevel);
}

if (options.getLanguageIn().isStrict()) {
if (expectStrictModeInput()) {
options.setWarningLevel(
DiagnosticGroups.ES5_STRICT,
CheckLevel.ERROR);
Expand All @@ -427,6 +427,21 @@ 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.
*/
Expand Down Expand Up @@ -1980,11 +1995,21 @@ private String toSource(Node n, SourceMap sourceMap, boolean firstOutput) {
builder.setCompilerOptions(options);
builder.setSourceMap(sourceMap);
builder.setTagAsExterns(firstOutput && options.shouldGenerateTypedExterns());
builder.setTagAsStrict(
firstOutput && options.isEmitUseStrict() && options.getLanguageOut().isStrict());
builder.setTagAsStrict(firstOutput && shouldEmitUseStrict());
return builder.build();
}

private boolean shouldEmitUseStrict() {
switch (options.getLanguageOut()) {
case ECMASCRIPT3:
case ECMASCRIPT5:
case ECMASCRIPT6:
return false;
default:
return options.isEmitUseStrict();
}
}

/**
* Stores a buffer of text to which more can be appended. This is just like a
* StringBuilder except that we also track the number of lines.
Expand Down
31 changes: 16 additions & 15 deletions src/com/google/javascript/jscomp/CompilerOptions.java
Expand Up @@ -1042,6 +1042,13 @@ public void setWrapGoogModulesForWhitespaceOnly(boolean enable) {
*/
boolean printConfig = false;

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

/**
* Should the compiler print its configuration options to stderr when they are initialized?
*
Expand Down Expand Up @@ -2864,21 +2871,6 @@ public static enum LanguageMode {
*/
NO_TRANSPILE;

/** Whether this is a "strict mode" language. */
public boolean isStrict() {
Preconditions.checkState(this != NO_TRANSPILE);
switch (this) {
case ECMASCRIPT7:
case ECMASCRIPT8:
case ECMASCRIPT5_STRICT:
case ECMASCRIPT6_STRICT:
case ECMASCRIPT6_TYPED:
return true;
default:
return false;
}
}

/** Whether this is ECMAScript 5 or higher. */
public boolean isEs5OrHigher() {
Preconditions.checkState(this != NO_TRANSPILE);
Expand Down Expand Up @@ -3157,4 +3149,13 @@ boolean isExplicitlyOn() {
return this == TRUE || this == ON;
}
}

public boolean isStrictModeInput() {
return isStrictModeInput;
}

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

0 comments on commit 73409ec

Please sign in to comment.