Skip to content

Commit

Permalink
Change parsing.Config to use @AutoValue.Builder.
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=178463919
  • Loading branch information
shicks authored and blickly committed Dec 11, 2017
1 parent f9ac91d commit 0265efa
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 138 deletions.
145 changes: 77 additions & 68 deletions src/com/google/javascript/jscomp/parsing/Config.java
Expand Up @@ -16,24 +16,29 @@

package com.google.javascript.jscomp.parsing;

import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.javascript.jscomp.parsing.parser.FeatureSet;
import java.util.Set;

/**
* Configuration for the AST factory. Should be shared across AST creation
* for all files of a compilation process.
* Configuration for the AST factory. Should be shared across AST creation for all files of a
* compilation process.
*
* @author nicksantos@google.com (Nick Santos)
*/
public final class Config {
@AutoValue
public abstract class Config {

/**
* Level of language strictness required for the input source code.
*/
public enum StrictMode {
STRICT, SLOPPY;

public boolean isStrict() {
return this == STRICT;
}
}

/** JavaScript mode */
Expand Down Expand Up @@ -95,8 +100,11 @@ public enum JsDocParsing {
boolean shouldParseDescriptions() {
return this != TYPES_ONLY;
}

boolean shouldPreserveWhitespace() {
return this == INCLUDE_DESCRIPTIONS_WITH_WHITESPACE;
}
}
final JsDocParsing parseJsDocDocumentation;

/**
* Whether to keep going after encountering a parse error.
Expand All @@ -105,80 +113,81 @@ public enum RunMode {
STOP_AFTER_ERROR,
KEEP_GOING,
}
final RunMode keepGoing;

/**
* Recognized JSDoc annotations, mapped from their name to their internal
* representation.
*/
final ImmutableMap<String, Annotation> annotationNames;
/** Language level to accept. */
abstract LanguageMode languageMode();

/**
* Recognized names in a {@code @suppress} tag.
*/
final ImmutableSet<String> suppressionNames;
/** Whether to assume input is strict mode compliant. */
abstract StrictMode strictMode();

/**
* Accept ECMAScript5 syntax, such as getter/setter.
*/
final LanguageMode languageMode;
/** How to parse the descriptions of JsDoc comments. */
abstract JsDocParsing jsDocParsingMode();

final StrictMode strictMode;
/** Whether to keep going after encountering a parse error. */
abstract RunMode runMode();

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

Config(
Set<String> annotationWhitelist,
Set<String> suppressionNames,
LanguageMode languageMode,
StrictMode strictMode) {
this(
annotationWhitelist,
JsDocParsing.TYPES_ONLY,
RunMode.STOP_AFTER_ERROR,
suppressionNames,
languageMode,
false,
strictMode);
/** Recognized JSDoc annotations, mapped from their name to their internal representation. */
abstract ImmutableMap<String, Annotation> annotations();

/** Set of recognized names in a {@code @suppress} tag. */
abstract ImmutableSet<String> suppressionNames();

/** Whether to parse inline source maps (//# sourceMappingURL=data:...). */
abstract boolean parseInlineSourceMaps();

final ImmutableSet<String> annotationNames() {
return annotations().keySet();
}

Config(
Set<String> annotationWhitelist,
JsDocParsing parseJsDocDocumentation,
RunMode keepGoing,
Set<String> suppressionNames,
LanguageMode languageMode,
boolean parseInlineSourceMaps,
StrictMode strictMode) {
this.parseInlineSourceMaps = parseInlineSourceMaps;
this.annotationNames = buildAnnotationNames(annotationWhitelist);
this.parseJsDocDocumentation = parseJsDocDocumentation;
this.keepGoing = keepGoing;
this.suppressionNames = ImmutableSet.copyOf(suppressionNames);
this.languageMode = languageMode;
this.strictMode = strictMode;
static Builder builder() {
return new AutoValue_Config.Builder()
.setLanguageMode(LanguageMode.TYPESCRIPT)
.setStrictMode(StrictMode.STRICT)
.setJsDocParsingMode(JsDocParsing.TYPES_ONLY)
.setRunMode(RunMode.STOP_AFTER_ERROR)
.setExtraAnnotationNames(ImmutableSet.<String>of())
.setSuppressionNames(ImmutableSet.<String>of())
.setParseInlineSourceMaps(false);
}

/**
* Create the annotation names from the user-specified
* annotation whitelist.
*/
private static ImmutableMap<String, Annotation> buildAnnotationNames(
Set<String> annotationWhitelist) {
ImmutableMap.Builder<String, Annotation> annotationBuilder =
ImmutableMap.builder();
annotationBuilder.putAll(Annotation.recognizedAnnotations);
for (String unrecognizedAnnotation : annotationWhitelist) {
@AutoValue.Builder
abstract static class Builder {
abstract Builder setLanguageMode(LanguageMode mode);

abstract Builder setStrictMode(StrictMode mode);

abstract Builder setJsDocParsingMode(JsDocParsing mode);

abstract Builder setRunMode(RunMode mode);

abstract Builder setParseInlineSourceMaps(boolean parseInlineSourceMaps);

final Builder setSuppressionNames(Iterable<String> names) {
return setSuppressionNames(ImmutableSet.copyOf(names));
}

final Builder setExtraAnnotationNames(Iterable<String> names) {
return setAnnotations(buildAnnotations(names));
}

abstract Config build();

// The following are intended to be used internally only (but aren't private due to AutoValue).
abstract Builder setSuppressionNames(ImmutableSet<String> names);

abstract Builder setAnnotations(ImmutableMap<String, Annotation> names);
}

/** Create the annotation names from the user-specified annotation whitelist. */
private static ImmutableMap<String, Annotation> buildAnnotations(Iterable<String> whitelist) {
ImmutableMap.Builder<String, Annotation> annotationsBuilder = ImmutableMap.builder();
annotationsBuilder.putAll(Annotation.recognizedAnnotations);
for (String unrecognizedAnnotation : whitelist) {
if (!unrecognizedAnnotation.isEmpty()
&& !Annotation.recognizedAnnotations.containsKey(
unrecognizedAnnotation)) {
annotationBuilder.put(
unrecognizedAnnotation, Annotation.NOT_IMPLEMENTED);
&& !Annotation.recognizedAnnotations.containsKey(unrecognizedAnnotation)) {
annotationsBuilder.put(unrecognizedAnnotation, Annotation.NOT_IMPLEMENTED);
}
}
return annotationBuilder.build();
return annotationsBuilder.build();
}
}
34 changes: 14 additions & 20 deletions src/com/google/javascript/jscomp/parsing/IRFactory.java
Expand Up @@ -36,7 +36,6 @@
import com.google.common.collect.Lists;
import com.google.common.collect.UnmodifiableIterator;
import com.google.javascript.jscomp.parsing.Config.LanguageMode;
import com.google.javascript.jscomp.parsing.Config.StrictMode;
import com.google.javascript.jscomp.parsing.parser.FeatureSet;
import com.google.javascript.jscomp.parsing.parser.FeatureSet.Feature;
import com.google.javascript.jscomp.parsing.parser.IdentifierToken;
Expand Down Expand Up @@ -287,7 +286,7 @@ private IRFactory(String sourceString,
this.templateNode = createTemplateNode();

this.fileLevelJsDocBuilder =
new JSDocInfoBuilder(config.parseJsDocDocumentation.shouldParseDescriptions());
new JSDocInfoBuilder(config.jsDocParsingMode().shouldParseDescriptions());

// Pre-generate all the newlines in the file.
for (int charNo = 0; true; charNo++) {
Expand All @@ -305,9 +304,9 @@ private IRFactory(String sourceString,
this.errorReporter = errorReporter;
this.transformDispatcher = new TransformDispatcher();

if (config.strictMode == StrictMode.STRICT) {
if (config.strictMode().isStrict()) {
reservedKeywords = ES5_STRICT_RESERVED_KEYWORDS;
} else if (config.languageMode == LanguageMode.ECMASCRIPT3) {
} else if (config.languageMode() == LanguageMode.ECMASCRIPT3) {
reservedKeywords = null; // use TokenStream.isKeyword instead
} else {
reservedKeywords = ES5_RESERVED_KEYWORDS;
Expand Down Expand Up @@ -375,12 +374,7 @@ static FeatureSet detectFeatures(
return irFactory.features;
}

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

static final ErrorReporter NULL_REPORTER = new ErrorReporter() {
@Override
Expand Down Expand Up @@ -1337,7 +1331,7 @@ Node processFunction(FunctionDeclarationTree functionTree) {
if (!isArrow && !isSignature && !bodyNode.isNormalBlock()) {
// When in "keep going" mode the parser tries to parse some constructs the
// compiler doesn't support, repair it here.
checkState(config.keepGoing == Config.RunMode.KEEP_GOING);
checkState(config.runMode() == Config.RunMode.KEEP_GOING);
bodyNode = IR.block();
}
parseDirectives(bodyNode);
Expand Down Expand Up @@ -1555,7 +1549,7 @@ Node processNameWithInlineJSDoc(IdentifierToken identifierToken) {
private void maybeWarnKeywordProperty(Node node) {
if (TokenStream.isKeyword(node.getString())) {
features = features.with(Feature.KEYWORDS_AS_PROPERTIES);
if (config.languageMode == LanguageMode.ECMASCRIPT3) {
if (config.languageMode() == LanguageMode.ECMASCRIPT3) {
errorReporter.warning(INVALID_ES3_PROP_NAME, sourceName,
node.getLineno(), node.getCharno());
}
Expand All @@ -1567,11 +1561,11 @@ private void maybeWarnReservedKeyword(IdentifierToken token) {
boolean isIdentifier = false;
if (TokenStream.isKeyword(identifier)) {
features = features.with(Feature.ES3_KEYWORDS_AS_IDENTIFIERS);
isIdentifier = config.languageMode == LanguageMode.ECMASCRIPT3;
isIdentifier = config.languageMode() == LanguageMode.ECMASCRIPT3;
}
if (reservedKeywords != null && reservedKeywords.contains(identifier)) {
features = features.with(Feature.KEYWORDS_AS_PROPERTIES);
isIdentifier = config.languageMode == LanguageMode.ECMASCRIPT3;
isIdentifier = config.languageMode() == LanguageMode.ECMASCRIPT3;
}
if (isIdentifier) {
errorReporter.error(
Expand Down Expand Up @@ -2089,7 +2083,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.with(Feature.GETTER);
if (config.languageMode == LanguageMode.ECMASCRIPT3) {
if (config.languageMode() == LanguageMode.ECMASCRIPT3) {
errorReporter.error(
GETTER_ERROR_MESSAGE,
sourceName,
Expand All @@ -2102,7 +2096,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.with(Feature.SETTER);
if (config.languageMode == LanguageMode.ECMASCRIPT3) {
if (config.languageMode() == LanguageMode.ECMASCRIPT3) {
errorReporter.error(
SETTER_ERROR_MESSAGE,
sourceName,
Expand Down Expand Up @@ -2635,7 +2629,7 @@ void maybeProcessAccessibilityModifier(ParseTree parseTree, Node n, @Nullable To
}

void maybeWarnTypeSyntax(ParseTree node, Feature feature) {
if (config.languageMode != LanguageMode.TYPESCRIPT) {
if (config.languageMode() != LanguageMode.TYPESCRIPT) {
errorReporter.warning(
"type syntax is only supported in ES6 typed mode: " + feature,
sourceName,
Expand Down Expand Up @@ -3070,17 +3064,17 @@ String normalizeString(LiteralToken token, boolean templateLiteral) {
}

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

boolean isEs5OrBetterMode() {
return config.languageMode.featureSet.contains(FeatureSet.ES5);
return config.languageMode().featureSet.contains(FeatureSet.ES5);
}

private boolean inStrictContext() {
// 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.
return config.strictMode == StrictMode.STRICT;
return config.strictMode().isStrict();
}

double normalizeNumber(LiteralToken token) {
Expand Down
22 changes: 10 additions & 12 deletions src/com/google/javascript/jscomp/parsing/JsDocInfoParser.java
Expand Up @@ -117,7 +117,7 @@ private void addMissingTypeWarning(int lineno, int charno) {
private JSDocInfo fileOverviewJSDocInfo = null;
private State state;

private final Map<String, Annotation> annotationNames;
private final Map<String, Annotation> annotations;
private final Set<String> suppressionNames;
private final boolean preserveWhitespace;
private static final Set<String> modifiesAnnotationKeywords =
Expand Down Expand Up @@ -166,16 +166,15 @@ private enum State {
ErrorReporter errorReporter) {
this.stream = stream;

boolean parseDocumentation = config.parseJsDocDocumentation.shouldParseDescriptions();
boolean parseDocumentation = config.jsDocParsingMode().shouldParseDescriptions();
this.jsdocBuilder = new JSDocInfoBuilder(parseDocumentation);
if (comment != null) {
this.jsdocBuilder.recordOriginalCommentString(comment);
this.jsdocBuilder.recordOriginalCommentPosition(commentPosition);
}
this.annotationNames = config.annotationNames;
this.suppressionNames = config.suppressionNames;
this.preserveWhitespace =
config.parseJsDocDocumentation == Config.JsDocParsing.INCLUDE_DESCRIPTIONS_WITH_WHITESPACE;
this.annotations = config.annotations();
this.suppressionNames = config.suppressionNames();
this.preserveWhitespace = config.jsDocParsingMode().shouldPreserveWhitespace();

this.errorReporter = errorReporter;
this.templateNode = templateNode == null ? IR.script() : templateNode;
Expand Down Expand Up @@ -238,11 +237,10 @@ public static JSDocInfo parseJsdoc(String toParse) {

private static JsDocInfoParser getParser(String toParse) {
Config config =
new Config(
new HashSet<String>(),
new HashSet<String>(),
LanguageMode.ECMASCRIPT3,
Config.StrictMode.SLOPPY);
Config.builder()
.setLanguageMode(LanguageMode.ECMASCRIPT3)
.setStrictMode(Config.StrictMode.SLOPPY)
.build();
JsDocInfoParser parser = new JsDocInfoParser(
new JsDocTokenStream(toParse),
toParse,
Expand Down Expand Up @@ -384,7 +382,7 @@ private JsDocToken parseAnnotation(JsDocToken token,
int charno = stream.getCharno();

String annotationName = stream.getString();
Annotation annotation = annotationNames.get(annotationName);
Annotation annotation = annotations.get(annotationName);
if (annotation == null || annotationName.isEmpty()) {
addParserWarning("msg.bad.jsdoc.tag", annotationName);
} else {
Expand Down

0 comments on commit 0265efa

Please sign in to comment.