Skip to content

Commit

Permalink
Remove more uses of the deprecated isEsXOrHigher methods.
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=157264681
  • Loading branch information
blickly committed May 26, 2017
1 parent fdf120f commit 2a490b0
Show file tree
Hide file tree
Showing 15 changed files with 49 additions and 48 deletions.
Expand Up @@ -17,6 +17,7 @@
package com.google.javascript.jscomp;

import com.google.common.base.Preconditions;
import com.google.javascript.jscomp.parsing.parser.FeatureSet;
import com.google.javascript.rhino.Node;

/**
Expand Down Expand Up @@ -134,7 +135,8 @@ boolean nodeTypeMayHaveSideEffects(Node n) {
* ignored when this is true.
*/
boolean isEcmaScript5OrGreater() {
return compiler != null && compiler.getOptions().getLanguageOut().isEs5OrHigher();
return compiler != null
&& compiler.getOptions().getLanguageOut().toFeatureSet().contains(FeatureSet.ES5);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/com/google/javascript/jscomp/CommandLineRunner.java
Expand Up @@ -31,6 +31,7 @@
import com.google.javascript.jscomp.CompilerOptions.IsolationMode;
import com.google.javascript.jscomp.SourceMap.LocationMapping;
import com.google.javascript.jscomp.deps.ModuleLoader;
import com.google.javascript.jscomp.parsing.parser.FeatureSet;
import com.google.javascript.rhino.TokenStream;
import com.google.protobuf.TextFormat;
import java.io.BufferedReader;
Expand Down Expand Up @@ -1688,7 +1689,8 @@ protected CompilerOptions createOptions() {
options.setForceLibraryInjection(flags.forceInjectLibraries);
}

options.rewritePolyfills = flags.rewritePolyfills && options.getLanguageIn().isEs6OrHigher();
options.rewritePolyfills =
flags.rewritePolyfills && options.getLanguageIn().toFeatureSet().contains(FeatureSet.ES6);

if (!flags.translationsFile.isEmpty()) {
try {
Expand Down
17 changes: 1 addition & 16 deletions src/com/google/javascript/jscomp/CompilerOptions.java
Expand Up @@ -3015,25 +3015,10 @@ boolean isDefaultStrict() {
}
}

/** Whether this is ECMAScript 5 or higher. */
@Deprecated
public boolean isEs5OrHigher() {
Preconditions.checkState(this != NO_TRANSPILE);
return this != LanguageMode.ECMASCRIPT3;
}

/** Whether this is ECMAScript 6 or higher. */
@Deprecated
public boolean isEs6OrHigher() {
Preconditions.checkState(this != NO_TRANSPILE);
switch (this) {
case ECMASCRIPT3:
case ECMASCRIPT5:
case ECMASCRIPT5_STRICT:
return false;
default:
return true;
}
return this.toFeatureSet().contains(FeatureSet.ES6);
}

public static LanguageMode fromString(String value) {
Expand Down
Expand Up @@ -17,6 +17,7 @@

import static com.google.common.base.Strings.isNullOrEmpty;

import com.google.javascript.jscomp.parsing.parser.FeatureSet;
import com.google.javascript.jscomp.parsing.parser.util.format.SimpleFormat;

/**
Expand Down Expand Up @@ -47,8 +48,9 @@ static void preprocess(CompilerOptions options) {
+ "remove_unused_prototype_props to be turned on.");
}

if (options.getLanguageOut().isEs6OrHigher()
&& !options.skipNonTranspilationPasses && !options.skipTranspilationAndCrash) {
if (options.getLanguageOut().toFeatureSet().contains(FeatureSet.ES6)
&& !options.skipNonTranspilationPasses
&& !options.skipTranspilationAndCrash) {
throw new InvalidOptionsException(
"ES6 is only supported for transpilation to a lower ECMAScript"
+ " version. Set --language_out to ES3, ES5, or ES5_STRICT.");
Expand All @@ -71,7 +73,7 @@ static void preprocess(CompilerOptions options) {
}

if (options.dartPass) {
if (!options.getLanguageOut().isEs5OrHigher()) {
if (!options.getLanguageOut().toFeatureSet().contains(FeatureSet.ES5)) {
throw new InvalidOptionsException("Dart requires --language_out=ES5 or higher.");
}
// --dart_pass does not support type-aware property renaming yet.
Expand Down
Expand Up @@ -16,9 +16,8 @@

package com.google.javascript.jscomp;


import com.google.javascript.jscomp.CompilerOptions.LanguageMode;
import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback;
import com.google.javascript.jscomp.parsing.parser.FeatureSet;
import com.google.javascript.rhino.IR;
import com.google.javascript.rhino.Node;

Expand Down Expand Up @@ -48,7 +47,7 @@ public void visit(NodeTraversal t, Node n, Node parent) {
case GETTER_DEF:
case SETTER_DEF:
case STRING_KEY:
if (NodeUtil.isValidPropertyName(LanguageMode.ECMASCRIPT3, n.getString())) {
if (NodeUtil.isValidPropertyName(FeatureSet.ES3, n.getString())) {
if (n.getBooleanProp(Node.QUOTED_PROP)) {
n.putBooleanProp(Node.QUOTED_PROP, false);
compiler.reportChangeToEnclosingScope(n);
Expand All @@ -59,8 +58,7 @@ public void visit(NodeTraversal t, Node n, Node parent) {
case GETELEM:
Node left = n.getFirstChild();
Node right = left.getNext();
if (right.isString()
&& NodeUtil.isValidPropertyName(LanguageMode.ECMASCRIPT3, right.getString())) {
if (right.isString() && NodeUtil.isValidPropertyName(FeatureSet.ES3, right.getString())) {
n.removeChild(left);
n.removeChild(right);
Node newGetProp = IR.getprop(left, right);
Expand Down
4 changes: 3 additions & 1 deletion src/com/google/javascript/jscomp/DartSuperAccessorsPass.java
Expand Up @@ -16,6 +16,7 @@
package com.google.javascript.jscomp;

import com.google.common.base.Preconditions;
import com.google.javascript.jscomp.parsing.parser.FeatureSet;
import com.google.javascript.rhino.IR;
import com.google.javascript.rhino.Node;

Expand Down Expand Up @@ -50,7 +51,8 @@ public DartSuperAccessorsPass(AbstractCompiler compiler) {

this.renameProperties = options.propertyRenaming == PropertyRenamingPolicy.ALL_UNQUOTED;

Preconditions.checkState(options.getLanguageOut().isEs5OrHigher(),
Preconditions.checkState(
options.getLanguageOut().toFeatureSet().contains(FeatureSet.ES5),
"Dart super accessors pass requires ES5+ output");

// We currently rely on JSCompiler_renameProperty, which is not type-aware.
Expand Down
4 changes: 2 additions & 2 deletions src/com/google/javascript/jscomp/DefaultPassConfig.java
Expand Up @@ -368,7 +368,7 @@ protected List<PassFactory> getChecks() {

// 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()) {
if (options.dartPass && !options.getLanguageOut().toFeatureSet().contains(FeatureSet.ES6)) {
checks.add(dartSuperAccessorsPass);
}

Expand Down Expand Up @@ -911,7 +911,7 @@ protected List<PassFactory> getOptimizations() {
passes.add(sanityCheckVars);

// Raise to ES6, if allowed
if (options.getLanguageOut().isEs6OrHigher()) {
if (options.getLanguageOut().toFeatureSet().contains(FeatureSet.ES6)) {
passes.add(optimizeToEs6);
}

Expand Down
19 changes: 12 additions & 7 deletions src/com/google/javascript/jscomp/NodeUtil.java
Expand Up @@ -22,6 +22,8 @@
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableSet;
import com.google.javascript.jscomp.CompilerOptions.LanguageMode;
import com.google.javascript.jscomp.parsing.parser.FeatureSet;
import com.google.javascript.jscomp.parsing.parser.FeatureSet.Feature;
import com.google.javascript.rhino.IR;
import com.google.javascript.rhino.InputId;
import com.google.javascript.rhino.JSDocInfo;
Expand Down Expand Up @@ -3507,10 +3509,13 @@ static boolean isValidSimpleName(String name) {
&& isLatin(name);
}

/**
* Determines whether the given name is a valid qualified name.
*/
@Deprecated
public static boolean isValidQualifiedName(LanguageMode mode, String name) {
return isValidQualifiedName(mode.toFeatureSet(), name);
}

/** Determines whether the given name is a valid qualified name. */
public static boolean isValidQualifiedName(FeatureSet mode, String name) {
if (name.endsWith(".") || name.startsWith(".")) {
return false;
}
Expand All @@ -3525,14 +3530,14 @@ public static boolean isValidQualifiedName(LanguageMode mode, String name) {
}

/**
* Determines whether the given name can appear on the right side of
* the dot operator. Many properties (like reserved words) cannot, in ES3.
* Determines whether the given name can appear on the right side of the dot operator. Many
* properties (like reserved words) cannot, in ES3.
*/
static boolean isValidPropertyName(LanguageMode mode, String name) {
static boolean isValidPropertyName(FeatureSet mode, String name) {
if (isValidSimpleName(name)) {
return true;
} else {
return mode.isEs5OrHigher() && TokenStream.isKeyword(name);
return mode.contains(Feature.KEYWORDS_AS_PROPERTIES) && TokenStream.isKeyword(name);
}
}

Expand Down
Expand Up @@ -1043,7 +1043,8 @@ private boolean verifyProvide(NodeTraversal t, Node methodName, Node arg) {
return false;
}

if (!NodeUtil.isValidQualifiedName(compiler.getOptions().getLanguageIn(), arg.getString())) {
if (!NodeUtil.isValidQualifiedName(
compiler.getOptions().getLanguageIn().toFeatureSet(), arg.getString())) {
compiler.report(t.makeError(arg, INVALID_PROVIDE_ERROR,
arg.getString(), compiler.getOptions().getLanguageIn().toString()));
return false;
Expand Down Expand Up @@ -1077,7 +1078,8 @@ private boolean verifyDefine(NodeTraversal t,
}

String name = args.getString();
if (!NodeUtil.isValidQualifiedName(compiler.getOptions().getLanguageIn(), name)) {
if (!NodeUtil.isValidQualifiedName(
compiler.getOptions().getLanguageIn().toFeatureSet(), name)) {
compiler.report(t.makeError(args, INVALID_DEFINE_NAME_ERROR, name));
return false;
}
Expand Down
Expand Up @@ -1150,7 +1150,7 @@ private String getExportedName(NodeTraversal t, Node n, Var var) {
if (key.isStringKey()
&& !key.isQuotedString()
&& NodeUtil.isValidPropertyName(
compiler.getOptions().getLanguageIn(), key.getString())) {
compiler.getOptions().getLanguageIn().toFeatureSet(), key.getString())) {
if (key.hasChildren()) {
if (key.getFirstChild().isQualifiedName()) {
if (key.getFirstChild() == n) {
Expand Down
4 changes: 2 additions & 2 deletions src/com/google/javascript/jscomp/TranspilationPasses.java
Expand Up @@ -205,7 +205,7 @@ static boolean isScriptEs6ImplOrHigher(Node script) {
* @param callbacks The callbacks that should be invoked if a file has ES6 features.
*/
static void processCheck(AbstractCompiler compiler, Node combinedRoot, Callback... callbacks) {
if (compiler.getOptions().getLanguageIn().isEs6OrHigher()) {
if (compiler.getOptions().getLanguageIn().toFeatureSet().contains(FeatureSet.ES6)) {
for (Node singleRoot : combinedRoot.children()) {
if (isScriptEs6ImplOrHigher(singleRoot)) {
for (Callback callback : callbacks) {
Expand All @@ -225,7 +225,7 @@ static void processCheck(AbstractCompiler compiler, Node combinedRoot, Callback.
* @param callbacks The callbacks that should be invoked if the file has ES6 features.
*/
static void hotSwapCheck(AbstractCompiler compiler, Node scriptRoot, Callback... callbacks) {
if (compiler.getOptions().getLanguageIn().isEs6OrHigher()) {
if (compiler.getOptions().getLanguageIn().toFeatureSet().contains(FeatureSet.ES6)) {
if (isScriptEs6ImplOrHigher(scriptRoot)) {
for (Callback callback : callbacks) {
NodeTraversal.traverseEs6(compiler, scriptRoot, callback);
Expand Down
5 changes: 3 additions & 2 deletions src/com/google/javascript/jscomp/VariableReferenceCheck.java
Expand Up @@ -20,6 +20,7 @@
import com.google.common.collect.Sets;
import com.google.javascript.jscomp.NodeTraversal.AbstractShallowCallback;
import com.google.javascript.jscomp.ReferenceCollectingCallback.Behavior;
import com.google.javascript.jscomp.parsing.parser.FeatureSet;
import com.google.javascript.rhino.JSDocInfo;
import com.google.javascript.rhino.Node;
import com.google.javascript.rhino.Token;
Expand Down Expand Up @@ -95,7 +96,7 @@ private boolean shouldProcess(Node root) {
if (!forTranspileOnly) {
return true;
}
if (compiler.getOptions().getLanguageIn().isEs6OrHigher()) {
if (compiler.getOptions().getLanguageIn().toFeatureSet().contains(FeatureSet.ES6)) {
for (Node singleRoot : root.children()) {
if (TranspilationPasses.isScriptEs6ImplOrHigher(singleRoot)) {
return true;
Expand All @@ -117,7 +118,7 @@ compiler, new ReferenceCheckingBehavior(), new Es6SyntacticScopeCreator(compiler
@Override
public void hotSwapScript(Node scriptRoot, Node originalRoot) {
if (!forTranspileOnly
|| (compiler.getOptions().getLanguageIn().isEs6OrHigher()
|| (compiler.getOptions().getLanguageIn().toFeatureSet().contains(FeatureSet.ES6)
&& TranspilationPasses.isScriptEs6ImplOrHigher(scriptRoot))) {
new ReferenceCollectingCallback(
compiler, new ReferenceCheckingBehavior(), new Es6SyntacticScopeCreator(compiler))
Expand Down
Expand Up @@ -22,6 +22,7 @@

import com.google.common.collect.ImmutableList;
import com.google.javascript.jscomp.CompilerOptions.LanguageMode;
import com.google.javascript.jscomp.parsing.parser.FeatureSet;
import com.google.javascript.rhino.IR;
import com.google.javascript.rhino.InputId;
import com.google.javascript.rhino.Node;
Expand Down Expand Up @@ -222,8 +223,7 @@ private final void parseAndTypeCheck(String externs, String js) {
passes.add(makePassFactory("convertEs6TypedToEs6",
new Es6TypedToEs6Converter(compiler)));
}
if (compilerOptions.getLanguageIn().isEs6OrHigher()
&& !compilerOptions.getLanguageOut().isEs6OrHigher()) {
if (compilerOptions.needsTranspilationFrom(FeatureSet.ES6)) {
TranspilationPasses.addEs2017Passes(passes);
TranspilationPasses.addEs6EarlyPasses(passes);
TranspilationPasses.addEs6LatePasses(passes);
Expand Down
5 changes: 3 additions & 2 deletions test/com/google/javascript/jscomp/NodeUtilTest.java
Expand Up @@ -25,6 +25,7 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.javascript.jscomp.CompilerOptions.LanguageMode;
import com.google.javascript.jscomp.parsing.parser.FeatureSet;
import com.google.javascript.rhino.IR;
import com.google.javascript.rhino.JSTypeExpression;
import com.google.javascript.rhino.Node;
Expand Down Expand Up @@ -2587,10 +2588,10 @@ private static Node getNameNode(Node n, String name) {
}

private static boolean isValidPropertyName(String s) {
return NodeUtil.isValidPropertyName(LanguageMode.ECMASCRIPT3, s);
return NodeUtil.isValidPropertyName(FeatureSet.ES3, s);
}

private static boolean isValidQualifiedName(String s) {
return NodeUtil.isValidQualifiedName(LanguageMode.ECMASCRIPT3, s);
return NodeUtil.isValidQualifiedName(FeatureSet.ES3, s);
}
}
3 changes: 2 additions & 1 deletion test/com/google/javascript/jscomp/TypeCheckTest.java
Expand Up @@ -24,6 +24,7 @@
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.javascript.jscomp.CompilerOptions.LanguageMode;
import com.google.javascript.jscomp.parsing.parser.FeatureSet;
import com.google.javascript.jscomp.type.ClosureReverseAbstractInterpreter;
import com.google.javascript.jscomp.type.SemanticReverseAbstractInterpreter;
import com.google.javascript.rhino.IR;
Expand Down Expand Up @@ -17849,7 +17850,7 @@ private TypeCheckResult parseAndTypeCheckWithScope(
Joiner.on(", ").join(compiler.getErrors()),
0, compiler.getErrorCount());

if (compiler.getOptions().getLanguageIn().isEs6OrHigher()) {
if (compiler.getOptions().getLanguageIn().toFeatureSet().contains(FeatureSet.ES6)) {
List<PassFactory> passes = new ArrayList<>();
TranspilationPasses.addEs2017Passes(passes);
TranspilationPasses.addEs6EarlyPasses(passes);
Expand Down

0 comments on commit 2a490b0

Please sign in to comment.