Skip to content

Commit

Permalink
Fix FeatureSet.without* methods by making FeatureSet act more like a …
Browse files Browse the repository at this point in the history
…set of features.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=157535798
  • Loading branch information
tbreisacher authored and brad4d committed May 31, 2017
1 parent e19dfa6 commit 02c1410
Show file tree
Hide file tree
Showing 17 changed files with 327 additions and 233 deletions.
1 change: 1 addition & 0 deletions pom-main.xml
Expand Up @@ -193,6 +193,7 @@
<sources>
<source>src/com/google/javascript/rhino/testing</source>
<source>src/com/google/javascript/jscomp/testing</source>
<source>src/com/google/javascript/jscomp/parsing/parser/testing</source>
<source>src/com/google/javascript/refactoring/testing</source>
</sources>
</configuration>
Expand Down
2 changes: 1 addition & 1 deletion src/com/google/javascript/jscomp/AstValidator.java
Expand Up @@ -1462,7 +1462,7 @@ private void validateMaximumChildCount(Node n, int i) {
}

private void validateFeature(Feature feature, Node n) {
if (!compiler.getFeatureSet().contains(feature)) {
if (!compiler.getFeatureSet().has(feature)) {
violation("AST should not contain " + feature, n);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/com/google/javascript/jscomp/Compiler.java
Expand Up @@ -38,6 +38,7 @@
import com.google.javascript.jscomp.parsing.Config;
import com.google.javascript.jscomp.parsing.ParserRunner;
import com.google.javascript.jscomp.parsing.parser.FeatureSet;
import com.google.javascript.jscomp.parsing.parser.FeatureSet.Feature;
import com.google.javascript.jscomp.parsing.parser.trees.Comment;
import com.google.javascript.jscomp.type.ChainableReverseAbstractInterpreter;
import com.google.javascript.jscomp.type.ClosureReverseAbstractInterpreter;
Expand Down Expand Up @@ -2089,7 +2090,7 @@ void processEs6Modules(List<CompilerInput> inputsToProcess, boolean forceRewrite
new Es6RewriteModules(this).processFile(root, forceRewrite);
}

setFeatureSet(featureSet.withoutModules());
setFeatureSet(featureSet.without(Feature.MODULES));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/com/google/javascript/jscomp/DefaultPassConfig.java
Expand Up @@ -1403,7 +1403,7 @@ protected CompilerPass create(AbstractCompiler compiler) {
};

private final PassFactory setFeatureSet(final FeatureSet featureSet) {
return new PassFactory("setFeatureSet:" + featureSet.toLanguageModeString(), true) {
return new PassFactory("setFeatureSet:" + featureSet.version(), true) {
@Override
protected CompilerPass create(final AbstractCompiler compiler) {
return new CompilerPass() {
Expand Down
Expand Up @@ -21,6 +21,7 @@
import com.google.javascript.jscomp.deps.DependencyInfo;
import com.google.javascript.jscomp.deps.ModuleLoader;
import com.google.javascript.jscomp.parsing.parser.FeatureSet;
import com.google.javascript.jscomp.parsing.parser.FeatureSet.Feature;
import java.util.Collection;
import java.util.Map;
import java.util.TreeMap;
Expand Down Expand Up @@ -48,7 +49,7 @@ public ImmutableMap<String, String> getLoadFlags() {
Map<String, String> loadFlagsBuilder = new TreeMap<>();
loadFlagsBuilder.putAll(delegate.getLoadFlags());
FeatureSet features = ast.getFeatures(compiler);
if (features.hasEs6Modules()) {
if (features.has(Feature.MODULES)) {
String previousModule = loadFlagsBuilder.get("module");
if (previousModule != null && !previousModule.equals("es6")) {
compiler.report(JSError.make(ModuleLoader.MODULE_CONFLICT, getName()));
Expand Down
4 changes: 2 additions & 2 deletions src/com/google/javascript/jscomp/NodeUtil.java
Expand Up @@ -3537,7 +3537,7 @@ static boolean isValidPropertyName(FeatureSet mode, String name) {
if (isValidSimpleName(name)) {
return true;
} else {
return mode.contains(Feature.KEYWORDS_AS_PROPERTIES) && TokenStream.isKeyword(name);
return mode.has(Feature.KEYWORDS_AS_PROPERTIES) && TokenStream.isKeyword(name);
}
}

Expand Down Expand Up @@ -3974,7 +3974,7 @@ public static boolean has(Node node,
}

/**
* @return The number of times the the predicate is true for the node
* @return The number of times the predicate is true for the node
* or any of its descendants.
*/
public static int getCount(
Expand Down
5 changes: 2 additions & 3 deletions src/com/google/javascript/jscomp/RewritePolyfills.java
Expand Up @@ -37,7 +37,7 @@ public class RewritePolyfills implements HotSwapCompilerPass {

static final DiagnosticType INSUFFICIENT_OUTPUT_VERSION_ERROR = DiagnosticType.disabled(
"JSC_INSUFFICIENT_OUTPUT_VERSION",
"Built-in ''{0}'' not supported in output version {1}: set --language_out to at least {2}");
"Built-in ''{0}'' not supported in output version {1}");

/**
* Represents a single polyfill: specifically, for a native symbol
Expand Down Expand Up @@ -245,8 +245,7 @@ public void visitGuarded(NodeTraversal traversal, Node node, Node parent) {
node,
INSUFFICIENT_OUTPUT_VERSION_ERROR,
name,
compiler.getOptions().getLanguageOut().toString(),
polyfill.polyfillVersion.toLanguageModeString());
compiler.getOptions().getLanguageOut().toString());
}
inject(polyfill);

Expand Down
4 changes: 2 additions & 2 deletions src/com/google/javascript/jscomp/TranspilationPasses.java
Expand Up @@ -190,11 +190,11 @@ protected HotSwapCompilerPass create(final AbstractCompiler compiler) {

/**
* @param script The SCRIPT node representing a JS file
* @return If the file has at least ES6 features currently implemented in modern browsers.
* @return If the file has any features which are part of ES6 but not part of ES5.
*/
static boolean isScriptEs6OrHigher(Node script) {
FeatureSet features = (FeatureSet) script.getProp(Node.FEATURE_SET);
return features != null && features.contains(FeatureSet.ES6);
return features != null && !FeatureSet.ES5.contains(features);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/com/google/javascript/jscomp/parsing/Config.java
Expand Up @@ -61,7 +61,7 @@ public enum LanguageMode {
public static LanguageMode minimumRequiredFor(FeatureSet.Feature feature) {
// relies on the LanguageMode enums being in the right order
for (LanguageMode mode : LanguageMode.values()) {
if (mode.featureSet.contains(feature)) {
if (mode.featureSet.has(feature)) {
return mode;
}
}
Expand Down
28 changes: 14 additions & 14 deletions src/com/google/javascript/jscomp/parsing/IRFactory.java
Expand Up @@ -575,7 +575,7 @@ private void validateParameters(Node n) {
JSDocInfo recordJsDoc(SourceRange location, JSDocInfo info) {
if (info != null && info.hasTypeInformation()) {
hasJsDocTypeAnnotations = true;
if (features.isTypeScript()) {
if (features.version().equals("ts")) {
errorReporter.error("Can only have JSDoc or inline type annotations, not both",
sourceName, lineno(location.start), charno(location.start));
}
Expand Down Expand Up @@ -1500,7 +1500,7 @@ Node processNameWithInlineJSDoc(IdentifierToken identifierToken) {

private void maybeWarnKeywordProperty(Node node) {
if (TokenStream.isKeyword(node.getString())) {
features = features.require(Feature.KEYWORDS_AS_PROPERTIES);
features = features.with(Feature.KEYWORDS_AS_PROPERTIES);
if (config.languageMode == LanguageMode.ECMASCRIPT3) {
errorReporter.warning(INVALID_ES3_PROP_NAME, sourceName,
node.getLineno(), node.getCharno());
Expand All @@ -1512,11 +1512,11 @@ private void maybeWarnReservedKeyword(IdentifierToken token) {
String identifier = token.value;
boolean isIdentifier = false;
if (TokenStream.isKeyword(identifier)) {
features = features.require(Feature.ES3_KEYWORDS_AS_IDENTIFIERS);
features = features.with(Feature.ES3_KEYWORDS_AS_IDENTIFIERS);
isIdentifier = config.languageMode == LanguageMode.ECMASCRIPT3;
}
if (reservedKeywords != null && reservedKeywords.contains(identifier)) {
features = features.require(Feature.KEYWORDS_AS_PROPERTIES);
features = features.with(Feature.KEYWORDS_AS_PROPERTIES);
isIdentifier = config.languageMode == LanguageMode.ECMASCRIPT3;
}
if (isIdentifier) {
Expand Down Expand Up @@ -2033,7 +2033,7 @@ Node processIllegalToken(ParseTree node) {

/** Reports an illegal getter and returns true if the language mode is too low. */
boolean maybeReportGetter(ParseTree node) {
features = features.require(Feature.GETTER);
features = features.with(Feature.GETTER);
if (config.languageMode == LanguageMode.ECMASCRIPT3) {
errorReporter.error(
GETTER_ERROR_MESSAGE,
Expand All @@ -2046,7 +2046,7 @@ boolean maybeReportGetter(ParseTree node) {

/** Reports an illegal setter and returns true if the language mode is too low. */
boolean maybeReportSetter(ParseTree node) {
features = features.require(Feature.SETTER);
features = features.with(Feature.SETTER);
if (config.languageMode == LanguageMode.ECMASCRIPT3) {
errorReporter.error(
SETTER_ERROR_MESSAGE,
Expand Down Expand Up @@ -2108,8 +2108,8 @@ Node processClassDeclaration(ClassDeclarationTree tree) {
Node body = newNode(Token.CLASS_MEMBERS);
setSourceInfo(body, tree);
for (ParseTree child : tree.elements) {
if (child.type == ParseTreeType.MEMBER_VARIABLE ||
child.type == ParseTreeType.COMPUTED_PROPERTY_MEMBER_VARIABLE) {
if (child.type == ParseTreeType.MEMBER_VARIABLE
|| child.type == ParseTreeType.COMPUTED_PROPERTY_MEMBER_VARIABLE) {
maybeWarnTypeSyntax(child, Feature.MEMBER_VARIABLE_IN_CLASS);
}
body.addChildToBack(transform(child));
Expand Down Expand Up @@ -2560,7 +2560,7 @@ private Node transformListOrEmpty(
}

void maybeWarnForFeature(ParseTree node, Feature feature) {
features = features.require(feature);
features = features.with(feature);
if (!isSupportedForInputLanguageMode(feature)) {

errorReporter.warning(
Expand Down Expand Up @@ -2601,7 +2601,7 @@ void maybeWarnTypeSyntax(ParseTree node, Feature feature) {
lineno(node),
charno(node));
}
features = features.require(feature);
features = features.with(feature);
recordTypeSyntax(node.location);
}

Expand Down Expand Up @@ -2893,7 +2893,7 @@ String normalizeString(LiteralToken token, boolean templateLiteral) {
result.append('\u000B');
break;
case '\n':
features = features.require(Feature.STRING_CONTINUATION);
features = features.with(Feature.STRING_CONTINUATION);
if (isEs5OrBetterMode()) {
errorReporter.warning(STRING_CONTINUATION_WARNING,
sourceName,
Expand Down Expand Up @@ -2975,7 +2975,7 @@ String normalizeString(LiteralToken token, boolean templateLiteral) {
}

boolean isSupportedForInputLanguageMode(Feature feature) {
return config.languageMode.featureSet.contains(feature);
return config.languageMode.featureSet.has(feature);
}

boolean isEs5OrBetterMode() {
Expand Down Expand Up @@ -3005,7 +3005,7 @@ private boolean inStrictContext() {
return Double.valueOf(value);
case 'b':
case 'B': {
features = features.require(Feature.BINARY_LITERALS);
features = features.with(Feature.BINARY_LITERALS);
if (!isSupportedForInputLanguageMode(Feature.BINARY_LITERALS)) {
errorReporter.warning(BINARY_NUMBER_LITERAL_WARNING,
sourceName,
Expand All @@ -3020,7 +3020,7 @@ private boolean inStrictContext() {
}
case 'o':
case 'O': {
features = features.require(Feature.OCTAL_LITERALS);
features = features.with(Feature.OCTAL_LITERALS);
if (!isSupportedForInputLanguageMode(Feature.OCTAL_LITERALS)) {
errorReporter.warning(OCTAL_NUMBER_LITERAL_WARNING,
sourceName,
Expand Down
4 changes: 2 additions & 2 deletions src/com/google/javascript/jscomp/parsing/ParserRunner.java
Expand Up @@ -130,7 +130,7 @@ public static ParseResult parse(
IRFactory factory =
IRFactory.transformTree(tree, sourceFile, sourceString, config, errorReporter);
root = factory.getResultNode();
features = features.require(factory.getFeatures());
features = features.union(factory.getFeatures());
root.putProp(Node.FEATURE_SET, features);

if (config.parseJsDocDocumentation.shouldParseDescriptions()) {
Expand Down Expand Up @@ -180,7 +180,7 @@ public static FeatureSet detectFeatures(String sourcePath, String sourceString)
Parser p = new Parser(config, new Es6ErrorReporter(reporter, false), file);
ProgramTree tree = p.parseProgram();
StaticSourceFile simpleSourceFile = new SimpleSourceFile(sourcePath, false);
return IRFactory.detectFeatures(tree, simpleSourceFile, sourceString).require(p.getFeatures());
return IRFactory.detectFeatures(tree, simpleSourceFile, sourceString).union(p.getFeatures());
}

private static class Es6ErrorReporter
Expand Down

2 comments on commit 02c1410

@MatrixFrog
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This appears to break the travis build. I am investigating.

@MatrixFrog
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be fixed on the next MOE push.

Please sign in to comment.