Skip to content

Commit

Permalink
Separate strict mode from language mode in parsing Config.
Browse files Browse the repository at this point in the history
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=136836551
  • Loading branch information
brad4d authored and blickly committed Oct 21, 2016
1 parent 11c7bef commit 9a2b74e
Show file tree
Hide file tree
Showing 10 changed files with 240 additions and 100 deletions.
25 changes: 14 additions & 11 deletions src/com/google/javascript/jscomp/Compiler.java
Expand Up @@ -2222,35 +2222,37 @@ Config getParserConfig(ConfigContext context) {
if (parserConfig == null) { if (parserConfig == null) {
switch (options.getLanguageIn()) { switch (options.getLanguageIn()) {
case ECMASCRIPT3: case ECMASCRIPT3:
parserConfig = createConfig(Config.LanguageMode.ECMASCRIPT3); parserConfig = createConfig(Config.LanguageMode.ECMASCRIPT3, Config.StrictMode.SLOPPY);
externsParserConfig = createConfig(Config.LanguageMode.ECMASCRIPT5); externsParserConfig =
createConfig(Config.LanguageMode.ECMASCRIPT5, Config.StrictMode.SLOPPY);
break; break;
case ECMASCRIPT5: case ECMASCRIPT5:
parserConfig = createConfig(Config.LanguageMode.ECMASCRIPT5); parserConfig = createConfig(Config.LanguageMode.ECMASCRIPT5, Config.StrictMode.SLOPPY);
externsParserConfig = parserConfig; externsParserConfig = parserConfig;
break; break;
case ECMASCRIPT5_STRICT: case ECMASCRIPT5_STRICT:
parserConfig = createConfig(Config.LanguageMode.ECMASCRIPT5_STRICT); parserConfig = createConfig(Config.LanguageMode.ECMASCRIPT5, Config.StrictMode.STRICT);
externsParserConfig = parserConfig; externsParserConfig = parserConfig;
break; break;
case ECMASCRIPT6: case ECMASCRIPT6:
parserConfig = createConfig(Config.LanguageMode.ECMASCRIPT6); parserConfig = createConfig(Config.LanguageMode.ECMASCRIPT6, Config.StrictMode.SLOPPY);
externsParserConfig = parserConfig; externsParserConfig = parserConfig;
break; break;
case ECMASCRIPT6_STRICT: case ECMASCRIPT6_STRICT:
parserConfig = createConfig(Config.LanguageMode.ECMASCRIPT6_STRICT); parserConfig = createConfig(Config.LanguageMode.ECMASCRIPT6, Config.StrictMode.STRICT);
externsParserConfig = parserConfig; externsParserConfig = parserConfig;
break; break;
case ECMASCRIPT6_TYPED: case ECMASCRIPT6_TYPED:
parserConfig = createConfig(Config.LanguageMode.ECMASCRIPT6_TYPED); parserConfig =
createConfig(Config.LanguageMode.TYPESCRIPT, Config.StrictMode.STRICT);
externsParserConfig = parserConfig; externsParserConfig = parserConfig;
break; break;
case ECMASCRIPT7: case ECMASCRIPT7:
parserConfig = createConfig(Config.LanguageMode.ECMASCRIPT7); parserConfig = createConfig(Config.LanguageMode.ECMASCRIPT7, Config.StrictMode.STRICT);
externsParserConfig = parserConfig; externsParserConfig = parserConfig;
break; break;
case ECMASCRIPT8: case ECMASCRIPT8:
parserConfig = createConfig(Config.LanguageMode.ECMASCRIPT8); parserConfig = createConfig(Config.LanguageMode.ECMASCRIPT8, Config.StrictMode.STRICT);
externsParserConfig = parserConfig; externsParserConfig = parserConfig;
break; break;
default: default:
Expand All @@ -2266,7 +2268,7 @@ Config getParserConfig(ConfigContext context) {
} }
} }


protected Config createConfig(Config.LanguageMode mode) { protected Config createConfig(Config.LanguageMode mode, Config.StrictMode strictMode) {
Config config = Config config =
ParserRunner.createConfig( ParserRunner.createConfig(
mode, mode,
Expand All @@ -2275,7 +2277,8 @@ protected Config createConfig(Config.LanguageMode mode) {
? Config.RunMode.KEEP_GOING ? Config.RunMode.KEEP_GOING
: Config.RunMode.STOP_AFTER_ERROR, : Config.RunMode.STOP_AFTER_ERROR,
options.extraAnnotationNames, options.extraAnnotationNames,
options.parseInlineSourceMaps); options.parseInlineSourceMaps,
strictMode);
return config; return config;
} }


Expand Down
5 changes: 3 additions & 2 deletions src/com/google/javascript/jscomp/gwt/client/JsfileParser.java
Expand Up @@ -210,12 +210,13 @@ private static FileInfo parse(String code, String filename, @Nullable Reporter r


Config config = Config config =
ParserRunner.createConfig( ParserRunner.createConfig(
// TODO(sdh): ES6_STRICT, with a non-strict fallback - then give warnings. // TODO(sdh): ES6 STRICT, with a non-strict fallback - then give warnings.
Config.LanguageMode.ECMASCRIPT6, Config.LanguageMode.ECMASCRIPT6,
Config.JsDocParsing.INCLUDE_DESCRIPTIONS_NO_WHITESPACE, Config.JsDocParsing.INCLUDE_DESCRIPTIONS_NO_WHITESPACE,
Config.RunMode.KEEP_GOING, Config.RunMode.KEEP_GOING,
/* extraAnnotationNames */ ImmutableSet.<String>of(), /* extraAnnotationNames */ ImmutableSet.<String>of(),
/* parseInlineSourceMaps */ true); /* parseInlineSourceMaps */ true,
Config.StrictMode.SLOPPY);


SourceFile source = SourceFile.fromCode(filename, code); SourceFile source = SourceFile.fromCode(filename, code);
FileInfo info = new FileInfo(errorReporter); FileInfo info = new FileInfo(errorReporter);
Expand Down
37 changes: 22 additions & 15 deletions src/com/google/javascript/jscomp/parsing/Config.java
Expand Up @@ -16,6 +16,8 @@


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 All @@ -41,23 +43,18 @@ public enum LanguageMode {


// Note that minimumRequiredFor() relies on these being defined in order from fewest features to // Note that minimumRequiredFor() relies on these being defined in order from fewest features to
// most features, and _STRICT versions should be supplied after unspecified strictness. // most features, and _STRICT versions should be supplied after unspecified strictness.
ECMASCRIPT3(FeatureSet.ES3, StrictMode.SLOPPY), ECMASCRIPT3(FeatureSet.ES3),
ECMASCRIPT5(FeatureSet.ES5, StrictMode.SLOPPY), ECMASCRIPT5(FeatureSet.ES5),
ECMASCRIPT5_STRICT(FeatureSet.ES5, StrictMode.STRICT), ECMASCRIPT6(FeatureSet.ES6_MODULES),
ECMASCRIPT6(FeatureSet.ES6_MODULES, StrictMode.SLOPPY), ECMASCRIPT7(FeatureSet.ES7_MODULES),
ECMASCRIPT6_STRICT(FeatureSet.ES6_MODULES, StrictMode.STRICT), ECMASCRIPT8(FeatureSet.ES8_MODULES),
ECMASCRIPT7(FeatureSet.ES7_MODULES, StrictMode.STRICT), TYPESCRIPT(FeatureSet.TYPESCRIPT),
ECMASCRIPT8(FeatureSet.ES8_MODULES, StrictMode.STRICT),
// TODO(bradfordcsmith): This should be renamed so it doesn't seem tied to es6
ECMASCRIPT6_TYPED(FeatureSet.TYPESCRIPT, StrictMode.STRICT),
; ;


public final FeatureSet featureSet; public final FeatureSet featureSet;
public final StrictMode strictMode;


LanguageMode(FeatureSet featureSet, StrictMode strictMode) { LanguageMode(FeatureSet featureSet) {
this.featureSet = featureSet; this.featureSet = featureSet;
this.strictMode = strictMode;
} }


/** /**
Expand Down Expand Up @@ -113,19 +110,26 @@ public enum RunMode {
*/ */
final LanguageMode languageMode; final LanguageMode languageMode;


final StrictMode strictMode;

/** /**
* Parse inline source maps (//# sourceMappingURL=data:...). * Parse inline source maps (//# sourceMappingURL=data:...).
*/ */
final boolean parseInlineSourceMaps; final boolean parseInlineSourceMaps;


Config(Set<String> annotationWhitelist, Set<String> suppressionNames, LanguageMode languageMode) { Config(
Set<String> annotationWhitelist,
Set<String> suppressionNames,
LanguageMode languageMode,
StrictMode strictMode) {
this( this(
annotationWhitelist, annotationWhitelist,
JsDocParsing.TYPES_ONLY, JsDocParsing.TYPES_ONLY,
RunMode.STOP_AFTER_ERROR, RunMode.STOP_AFTER_ERROR,
suppressionNames, suppressionNames,
languageMode, languageMode,
false); false,
strictMode);
} }


Config( Config(
Expand All @@ -134,13 +138,16 @@ public enum RunMode {
RunMode keepGoing, RunMode keepGoing,
Set<String> suppressionNames, Set<String> suppressionNames,
LanguageMode languageMode, LanguageMode languageMode,
boolean parseInlineSourceMaps) { boolean parseInlineSourceMaps,
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;
this.keepGoing = keepGoing; this.keepGoing = keepGoing;
this.suppressionNames = ImmutableSet.copyOf(suppressionNames); this.suppressionNames = ImmutableSet.copyOf(suppressionNames);
this.languageMode = languageMode; this.languageMode = languageMode;
this.strictMode = strictMode;
} }


/** /**
Expand Down
42 changes: 19 additions & 23 deletions src/com/google/javascript/jscomp/parsing/IRFactory.java
Expand Up @@ -161,6 +161,7 @@
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import javax.annotation.Nullable;


/** /**
* IRFactory transforms the external AST to the internal AST. * IRFactory transforms the external AST to the internal AST.
Expand Down Expand Up @@ -251,6 +252,10 @@ class IRFactory {
"implements", "interface", "let", "package", "private", "protected", "implements", "interface", "let", "package", "private", "protected",
"public", "static", "yield"); "public", "static", "yield");


/**
* If non-null, use this set of keywords instead of TokenStream.isKeyword().
*/
@Nullable
private final Set<String> reservedKeywords; private final Set<String> reservedKeywords;
private final Set<Comment> parsedComments = new HashSet<>(); private final Set<Comment> parsedComments = new HashSet<>();


Expand Down Expand Up @@ -304,23 +309,12 @@ private IRFactory(String sourceString,
// The template node properties are applied to all nodes in this transform. // The template node properties are applied to all nodes in this transform.
this.templateNode = createTemplateNode(); this.templateNode = createTemplateNode();


switch (config.languageMode) { if (config.strictMode == StrictMode.STRICT) {
case ECMASCRIPT3: reservedKeywords = ES5_STRICT_RESERVED_KEYWORDS;
reservedKeywords = null; // use TokenStream.isKeyword instead } else if (config.languageMode == LanguageMode.ECMASCRIPT3) {
break; reservedKeywords = null; // use TokenStream.isKeyword instead
case ECMASCRIPT5: } else {
case ECMASCRIPT6: reservedKeywords = ES5_RESERVED_KEYWORDS;
reservedKeywords = ES5_RESERVED_KEYWORDS;
break;
case ECMASCRIPT5_STRICT:
case ECMASCRIPT6_STRICT:
case ECMASCRIPT6_TYPED:
case ECMASCRIPT7:
case ECMASCRIPT8:
reservedKeywords = ES5_STRICT_RESERVED_KEYWORDS;
break;
default:
throw new IllegalStateException("unknown language mode: " + config.languageMode);
} }
} }


Expand Down Expand Up @@ -385,10 +379,12 @@ static FeatureSet detectFeatures(
return irFactory.features; return irFactory.features;
} }


static final Config NULL_CONFIG = new Config( static final Config NULL_CONFIG =
ImmutableSet.<String>of(), new Config(
ImmutableSet.<String>of(), ImmutableSet.<String>of(),
LanguageMode.ECMASCRIPT6_TYPED); ImmutableSet.<String>of(),
LanguageMode.TYPESCRIPT,
Config.StrictMode.STRICT);


static final ErrorReporter NULL_REPORTER = new ErrorReporter() { static final ErrorReporter NULL_REPORTER = new ErrorReporter() {
@Override @Override
Expand Down Expand Up @@ -2593,7 +2589,7 @@ void maybeProcessAccessibilityModifier(Node n, TokenType type) {
} }


void maybeWarnTypeSyntax(ParseTree node, Feature feature) { void maybeWarnTypeSyntax(ParseTree node, Feature feature) {
if (config.languageMode != LanguageMode.ECMASCRIPT6_TYPED) { if (config.languageMode != LanguageMode.TYPESCRIPT) {
errorReporter.warning( errorReporter.warning(
"type syntax is only supported in ES6 typed mode: " + feature, "type syntax is only supported in ES6 typed mode: " + feature,
sourceName, sourceName,
Expand Down Expand Up @@ -2984,7 +2980,7 @@ boolean isEs5OrBetterMode() {
private boolean inStrictContext() { private boolean inStrictContext() {
// TODO(johnlenz): in ECMASCRIPT5/6 is a "mixed" mode and we should track the context // TODO(johnlenz): in ECMASCRIPT5/6 is a "mixed" mode and we should track the context
// that we are in, if we want to support it. // that we are in, if we want to support it.
return config.languageMode.strictMode == StrictMode.STRICT; return config.strictMode == StrictMode.STRICT;
} }


double normalizeNumber(LiteralToken token) { double normalizeNumber(LiteralToken token) {
Expand Down
10 changes: 6 additions & 4 deletions src/com/google/javascript/jscomp/parsing/JsDocInfoParser.java
Expand Up @@ -232,10 +232,12 @@ public static JSDocInfo parseJsdoc(String toParse) {
} }


private static JsDocInfoParser getParser(String toParse) { private static JsDocInfoParser getParser(String toParse) {
Config config = new Config( Config config =
new HashSet<String>(), new Config(
new HashSet<String>(), new HashSet<String>(),
LanguageMode.ECMASCRIPT3); new HashSet<String>(),
LanguageMode.ECMASCRIPT3,
Config.StrictMode.SLOPPY);
JsDocInfoParser parser = new JsDocInfoParser( JsDocInfoParser parser = new JsDocInfoParser(
new JsDocTokenStream(toParse), new JsDocTokenStream(toParse),
toParse, toParse,
Expand Down
38 changes: 17 additions & 21 deletions src/com/google/javascript/jscomp/parsing/ParserRunner.java
Expand Up @@ -22,6 +22,7 @@
import com.google.javascript.jscomp.parsing.Config.JsDocParsing; import com.google.javascript.jscomp.parsing.Config.JsDocParsing;
import com.google.javascript.jscomp.parsing.Config.LanguageMode; import com.google.javascript.jscomp.parsing.Config.LanguageMode;
import com.google.javascript.jscomp.parsing.Config.RunMode; import com.google.javascript.jscomp.parsing.Config.RunMode;
import com.google.javascript.jscomp.parsing.Config.StrictMode;
import com.google.javascript.jscomp.parsing.parser.FeatureSet; import com.google.javascript.jscomp.parsing.parser.FeatureSet;
import com.google.javascript.jscomp.parsing.parser.Parser; import com.google.javascript.jscomp.parsing.parser.Parser;
import com.google.javascript.jscomp.parsing.parser.Parser.Config.Mode; import com.google.javascript.jscomp.parsing.parser.Parser.Config.Mode;
Expand Down Expand Up @@ -53,22 +54,30 @@ public final class ParserRunner {
// Should never need to instantiate class of static methods. // Should never need to instantiate class of static methods.
private ParserRunner() {} private ParserRunner() {}


public static Config createConfig(LanguageMode languageMode, // TODO(bradfordcsmith): Cleanup uses and remove this method.
Set<String> extraAnnotationNames) { @Deprecated
public static Config createConfig(LanguageMode languageMode, Set<String> extraAnnotationNames) {
return createConfig(languageMode, extraAnnotationNames, StrictMode.SLOPPY);
}

public static Config createConfig(
LanguageMode languageMode, Set<String> extraAnnotationNames, StrictMode strictMode) {
return createConfig( return createConfig(
languageMode, languageMode,
JsDocParsing.TYPES_ONLY, JsDocParsing.TYPES_ONLY,
RunMode.STOP_AFTER_ERROR, RunMode.STOP_AFTER_ERROR,
extraAnnotationNames, extraAnnotationNames,
true); true,
strictMode);
} }


public static Config createConfig( public static Config createConfig(
LanguageMode languageMode, LanguageMode languageMode,
JsDocParsing jsdocParsingMode, JsDocParsing jsdocParsingMode,
RunMode runMode, RunMode runMode,
Set<String> extraAnnotationNames, Set<String> extraAnnotationNames,
boolean parseInlineSourceMaps) { boolean parseInlineSourceMaps,
StrictMode strictMode) {


initResourceConfig(); initResourceConfig();
Set<String> effectiveAnnotationNames; Set<String> effectiveAnnotationNames;
Expand All @@ -84,7 +93,8 @@ public static Config createConfig(
runMode, runMode,
suppressionNames, suppressionNames,
languageMode, languageMode,
parseInlineSourceMaps); parseInlineSourceMaps,
strictMode);
} }


public static Set<String> getReservedVars() { public static Set<String> getReservedVars() {
Expand Down Expand Up @@ -140,39 +150,25 @@ public static ParseResult parse(
private static com.google.javascript.jscomp.parsing.parser.Parser.Config newParserConfig( private static com.google.javascript.jscomp.parsing.parser.Parser.Config newParserConfig(
Config config) { Config config) {
LanguageMode languageMode = config.languageMode; LanguageMode languageMode = config.languageMode;
boolean isStrictMode; boolean isStrictMode = config.strictMode == StrictMode.STRICT;
Mode parserConfigLanguageMode; Mode parserConfigLanguageMode;
switch (languageMode) { switch (languageMode) {
case ECMASCRIPT6_TYPED: case TYPESCRIPT:
parserConfigLanguageMode = Mode.TYPESCRIPT; parserConfigLanguageMode = Mode.TYPESCRIPT;
isStrictMode = true;
break; break;


case ECMASCRIPT3: case ECMASCRIPT3:
parserConfigLanguageMode = Mode.ES3; parserConfigLanguageMode = Mode.ES3;
isStrictMode = false;
break; break;


case ECMASCRIPT5: case ECMASCRIPT5:
parserConfigLanguageMode = Mode.ES5; parserConfigLanguageMode = Mode.ES5;
isStrictMode = false;
break;

case ECMASCRIPT5_STRICT:
parserConfigLanguageMode = Mode.ES5;
isStrictMode = true;
break; break;


case ECMASCRIPT6: case ECMASCRIPT6:
parserConfigLanguageMode = Mode.ES6_OR_GREATER;
isStrictMode = false;
break;

case ECMASCRIPT6_STRICT:
case ECMASCRIPT7: case ECMASCRIPT7:
case ECMASCRIPT8: case ECMASCRIPT8:
parserConfigLanguageMode = Mode.ES6_OR_GREATER; parserConfigLanguageMode = Mode.ES6_OR_GREATER;
isStrictMode = true;
break; break;


default: default:
Expand Down
Expand Up @@ -23,7 +23,6 @@
import com.google.javascript.rhino.Node; import com.google.javascript.rhino.Node;
import com.google.javascript.rhino.SimpleErrorReporter; import com.google.javascript.rhino.SimpleErrorReporter;
import com.google.javascript.rhino.StaticSourceFile; import com.google.javascript.rhino.StaticSourceFile;

import java.util.HashSet; import java.util.HashSet;


/** /**
Expand Down Expand Up @@ -249,7 +248,11 @@ private boolean checkParameterCount(Node expr, Keywords keyword) {
*/ */
public boolean parseTypeTransformation() { public boolean parseTypeTransformation() {
Config config = Config config =
new Config(new HashSet<String>(), new HashSet<String>(), LanguageMode.ECMASCRIPT6); new Config(
new HashSet<String>(),
new HashSet<String>(),
LanguageMode.ECMASCRIPT6,
Config.StrictMode.SLOPPY);
// TODO(lpino): ParserRunner reports errors if the expression is not // TODO(lpino): ParserRunner reports errors if the expression is not
// ES6 valid. We need to abort the validation of the type transformation // ES6 valid. We need to abort the validation of the type transformation
// whenever an error is reported. // whenever an error is reported.
Expand Down

0 comments on commit 9a2b74e

Please sign in to comment.