Skip to content

Commit

Permalink
Make parsing.Config immutable again.
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=123752380
  • Loading branch information
blickly committed Jun 1, 2016
1 parent aa41f1d commit 78d0445
Show file tree
Hide file tree
Showing 10 changed files with 169 additions and 105 deletions.
16 changes: 11 additions & 5 deletions src/com/google/javascript/jscomp/Compiler.java
Expand Up @@ -2153,11 +2153,17 @@ Config getParserConfig(ConfigContext context) {
}

protected Config createConfig(Config.LanguageMode mode) {
Config config = ParserRunner.createConfig(mode, options.extraAnnotationNames);
config.setPreserveDetailedSourceInfo(options.preservesDetailedSourceInfo());
config.setKeepGoing(options.canContinueAfterErrors());
config.setParseJsDocDocumentation(options.isParseJsDocDocumentation());
config.setPreserveJsDocWhitespace(options.isPreserveJsDocWhitespace());
Config config =
ParserRunner.createConfig(
mode,
options.isParseJsDocDocumentation(),
options.preservesDetailedSourceInfo()
? Config.SourceLocationInformation.PRESERVE
: Config.SourceLocationInformation.DISCARD,
options.canContinueAfterErrors()
? Config.RunMode.KEEP_GOING
: Config.RunMode.STOP_AFTER_ERROR,
options.extraAnnotationNames);
return config;
}

Expand Down
52 changes: 21 additions & 31 deletions src/com/google/javascript/jscomp/CompilerOptions.java
Expand Up @@ -23,6 +23,7 @@
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Multimap;
import com.google.javascript.jscomp.parsing.Config;
import com.google.javascript.rhino.IR;
import com.google.javascript.rhino.Node;
import com.google.javascript.rhino.SourcePosition;
Expand Down Expand Up @@ -126,8 +127,7 @@ public boolean shouldGenerateTypedExterns() {
return generateTypedExterns;
}

private boolean parseJsDocDocumentation = false;
private boolean preserveJsDocWhitespace = false;
private Config.JsDocParsing parseJsDocDocumentation = Config.JsDocParsing.TYPES_ONLY;

/**
* Even if checkTypes is disabled, clients such as IDEs might want to still infer types.
Expand Down Expand Up @@ -1852,7 +1852,10 @@ public void setIdeMode(boolean ideMode) {
setContinueAfterErrors(ideMode);
setAllowHotswapReplaceScript(ideMode);
setPreserveDetailedSourceInfo(ideMode);
setParseJsDocDocumentation(ideMode);
setParseJsDocDocumentation(
ideMode
? Config.JsDocParsing.INCLUDE_DESCRIPTIONS_NO_WHITESPACE
: Config.JsDocParsing.TYPES_ONLY);
}

public void setAllowHotswapReplaceScript(boolean allowRecompilation) {
Expand All @@ -1879,14 +1882,23 @@ boolean canContinueAfterErrors() {
return continueAfterErrors;
}


@Deprecated
public void setParseJsDocDocumentation(boolean parseJsDocDocumentation) {
setParseJsDocDocumentation(
parseJsDocDocumentation
? Config.JsDocParsing.INCLUDE_DESCRIPTIONS_NO_WHITESPACE
: Config.JsDocParsing.TYPES_ONLY);
}

/**
* Enables or disables the parsing of JSDoc documentation. When IDE mode is
* enabled then documentation is always parsed.
* Enables or disables the parsing of JSDoc documentation, and optionally also
* the preservation of all whitespace and formatting within a JSDoc comment.
* By default, whitespace is collapsed for all comments except {@literal @license} and
* {@literal @preserve} blocks,
*
* @param parseJsDocDocumentation
* True to enable JSDoc documentation parsing, false to disable it.
*/
public void setParseJsDocDocumentation(boolean parseJsDocDocumentation) {
public void setParseJsDocDocumentation(Config.JsDocParsing parseJsDocDocumentation) {
this.parseJsDocDocumentation = parseJsDocDocumentation;
}

Expand All @@ -1895,32 +1907,10 @@ public void setParseJsDocDocumentation(boolean parseJsDocDocumentation) {
*
* @return True when JSDoc documentation will be parsed, false if not.
*/
public boolean isParseJsDocDocumentation() {
public Config.JsDocParsing isParseJsDocDocumentation() {
return this.parseJsDocDocumentation;
}

/**
* Enables or disables the preservation of all whitespace and formatting within a JSDoc
* comment. By default, whitespace is collapsed for all comments except {@literal @license} and
* {@literal @preserve} blocks,
*
* <p>Setting this option has no effect if {@link #isParseJsDocDocumentation()}
* returns false.
*
* @param preserveJsDocWhitespace
* True to preserve whitespace in text extracted from JSDoc comments.
*/
public void setPreserveJsDocWhitespace(boolean preserveJsDocWhitespace) {
this.preserveJsDocWhitespace = preserveJsDocWhitespace;
}

/**
* @return Whether to preserve whitespace in all text extracted from JSDoc comments.
*/
public boolean isPreserveJsDocWhitespace() {
return preserveJsDocWhitespace;
}

/**
* Skip all passes (other than transpilation, if requested). Don't inject es6_runtime.js
* or do any checks/optimizations (this is useful for per-file transpilation).
Expand Down
6 changes: 3 additions & 3 deletions src/com/google/javascript/jscomp/gwt/client/JsfileParser.java
Expand Up @@ -215,10 +215,10 @@ private static FileInfo parse(String code, String filename, @Nullable Reporter r
ParserRunner.createConfig(
// TODO(sdh): ES6_STRICT, with a non-strict fallback - then give warnings.
Config.LanguageMode.ECMASCRIPT6,
Config.JsDocParsing.INCLUDE_DESCRIPTIONS_NO_WHITESPACE,
Config.SourceLocationInformation.PRESERVE,
Config.RunMode.KEEP_GOING,
/* extraAnnotationNames */ ImmutableSet.<String>of());
config.setPreserveDetailedSourceInfo(true);
config.setKeepGoing(true);
config.setParseJsDocDocumentation(true);

SourceFile source = SourceFile.fromCode(filename, code);
FileInfo info = new FileInfo(errorReporter);
Expand Down
61 changes: 38 additions & 23 deletions src/com/google/javascript/jscomp/parsing/Config.java
Expand Up @@ -42,22 +42,34 @@ public enum LanguageMode {
/**
* Whether to parse the descriptions of JsDoc comments.
*/
boolean parseJsDocDocumentation;
public enum JsDocParsing {
TYPES_ONLY,
INCLUDE_DESCRIPTIONS_NO_WHITESPACE,
INCLUDE_DESCRIPTIONS_WITH_WHITESPACE;

/**
* Whether to preserve whitespace when extracting text from JsDoc comments.
*/
boolean preserveJsDocWhitespace;
boolean shouldParseDescriptions() {
return this != TYPES_ONLY;
}
}
final JsDocParsing parseJsDocDocumentation;

/**
* Whether to keep detailed source location information such as the exact length of every node.
*/
boolean preserveDetailedSourceInfo;
public enum SourceLocationInformation {
DISCARD,
PRESERVE,
}
final SourceLocationInformation preserveDetailedSourceInfo;

/**
* Whether to keep going after encountering a parse error.
*/
boolean keepGoing;
public enum RunMode {
STOP_AFTER_ERROR,
KEEP_GOING,
}
final RunMode keepGoing;

/**
* Recognized JSDoc annotations, mapped from their name to their internal
Expand All @@ -76,7 +88,26 @@ public enum LanguageMode {
final LanguageMode languageMode;

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

Config(
Set<String> annotationWhitelist,
JsDocParsing parseJsDocDocumentation,
SourceLocationInformation preserveDetailedSourceInfo,
RunMode keepGoing,
Set<String> suppressionNames,
LanguageMode languageMode) {
this.annotationNames = buildAnnotationNames(annotationWhitelist);
this.parseJsDocDocumentation = parseJsDocDocumentation;
this.preserveDetailedSourceInfo = preserveDetailedSourceInfo;
this.keepGoing = keepGoing;
this.suppressionNames = suppressionNames;
this.languageMode = languageMode;
}
Expand All @@ -100,20 +131,4 @@ private static Map<String, Annotation> buildAnnotationNames(
}
return annotationBuilder.build();
}

public void setPreserveDetailedSourceInfo(boolean preserveDetailedSourceInfo) {
this.preserveDetailedSourceInfo = preserveDetailedSourceInfo;
}

public void setKeepGoing(boolean keepGoing) {
this.keepGoing = keepGoing;
}

public void setParseJsDocDocumentation(boolean parseJsDocDocumentation) {
this.parseJsDocDocumentation = parseJsDocDocumentation;
}

public void setPreserveJsDocWhitespace(boolean preserveJsDocWhitespace) {
this.preserveJsDocWhitespace = preserveJsDocWhitespace;
}
}
10 changes: 5 additions & 5 deletions src/com/google/javascript/jscomp/parsing/IRFactory.java
Expand Up @@ -281,8 +281,8 @@ private IRFactory(String sourceString,
this.currentComment = skipNonJsDoc(nextCommentIter);
this.newlines = new ArrayList<>();
this.sourceFile = sourceFile;
this.fileLevelJsDocBuilder = new JSDocInfoBuilder(
config.parseJsDocDocumentation);
this.fileLevelJsDocBuilder =
new JSDocInfoBuilder(config.parseJsDocDocumentation.shouldParseDescriptions());

// Pre-generate all the newlines in the file.
for (int charNo = 0; true; charNo++) {
Expand Down Expand Up @@ -931,13 +931,13 @@ private JSDocInfo parseInlineTypeDoc(Comment node) {
// Set the length on the node if we're in IDE mode.
void maybeSetLength(
Node node, SourcePosition start, SourcePosition end) {
if (config.preserveDetailedSourceInfo) {
if (config.preserveDetailedSourceInfo == Config.SourceLocationInformation.PRESERVE) {
node.setLength(end.offset - start.offset);
}
}

void maybeSetLengthFrom(Node node, Node ref) {
if (config.preserveDetailedSourceInfo) {
if (config.preserveDetailedSourceInfo == Config.SourceLocationInformation.PRESERVE) {
node.setLength(ref.getLength());
}
}
Expand Down Expand Up @@ -1255,7 +1255,7 @@ Node processFunction(FunctionDeclarationTree functionTree) {
if (!isArrow && !isSignature && !bodyNode.isBlock()) {
// When in "keep going" mode the parser tries to parse some constructs the
// compiler doesn't support, repair it here.
Preconditions.checkState(config.keepGoing);
Preconditions.checkState(config.keepGoing == Config.RunMode.KEEP_GOING);
bodyNode = IR.block();
}
parseDirectives(bodyNode);
Expand Down
6 changes: 4 additions & 2 deletions src/com/google/javascript/jscomp/parsing/JsDocInfoParser.java
Expand Up @@ -156,14 +156,16 @@ private enum State {

this.sourceFile = sourceFile;

this.jsdocBuilder = new JSDocInfoBuilder(config.parseJsDocDocumentation);
boolean parseDocumentation = config.parseJsDocDocumentation.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.preserveJsDocWhitespace;
this.preserveWhitespace =
config.parseJsDocDocumentation == Config.JsDocParsing.INCLUDE_DESCRIPTIONS_WITH_WHITESPACE;

this.errorReporter = errorReporter;
this.templateNode = this.createTemplateNode();
Expand Down
34 changes: 29 additions & 5 deletions src/com/google/javascript/jscomp/parsing/ParserRunner.java
Expand Up @@ -19,7 +19,10 @@
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.javascript.jscomp.parsing.Config.JsDocParsing;
import com.google.javascript.jscomp.parsing.Config.LanguageMode;
import com.google.javascript.jscomp.parsing.Config.RunMode;
import com.google.javascript.jscomp.parsing.Config.SourceLocationInformation;
import com.google.javascript.jscomp.parsing.parser.FeatureSet;
import com.google.javascript.jscomp.parsing.parser.Parser;
import com.google.javascript.jscomp.parsing.parser.Parser.Config.Mode;
Expand Down Expand Up @@ -53,6 +56,21 @@ private ParserRunner() {}

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

public static Config createConfig(
LanguageMode languageMode,
JsDocParsing jsdocParsingMode,
SourceLocationInformation sourceLocationInfo,
RunMode runMode,
Set<String> extraAnnotationNames) {

initResourceConfig();
Set<String> effectiveAnnotationNames;
if (extraAnnotationNames == null) {
Expand All @@ -61,7 +79,13 @@ public static Config createConfig(LanguageMode languageMode,
effectiveAnnotationNames = new HashSet<>(annotationNames);
effectiveAnnotationNames.addAll(extraAnnotationNames);
}
return new Config(effectiveAnnotationNames, suppressionNames, languageMode);
return new Config(
effectiveAnnotationNames,
jsdocParsingMode,
sourceLocationInfo,
runMode,
suppressionNames,
languageMode);
}

public static Set<String> getReservedVars() {
Expand Down Expand Up @@ -91,8 +115,8 @@ public static ParseResult parse(
ErrorReporter errorReporter) {
// TODO(johnlenz): unify "SourceFile", "Es6ErrorReporter" and "Config"
SourceFile file = new SourceFile(sourceFile.getName(), sourceString);
Es6ErrorReporter es6ErrorReporter =
new Es6ErrorReporter(errorReporter, config.keepGoing);
boolean keepGoing = config.keepGoing == Config.RunMode.KEEP_GOING;
Es6ErrorReporter es6ErrorReporter = new Es6ErrorReporter(errorReporter, keepGoing);
com.google.javascript.jscomp.parsing.parser.Parser.Config es6config =
new com.google.javascript.jscomp.parsing.parser.Parser.Config(mode(
config.languageMode));
Expand All @@ -101,15 +125,15 @@ public static ParseResult parse(
Node root = null;
List<Comment> comments = ImmutableList.of();
FeatureSet features = p.getFeatures();
if (tree != null && (!es6ErrorReporter.hadError() || config.keepGoing)) {
if (tree != null && (!es6ErrorReporter.hadError() || keepGoing)) {
IRFactory factory =
IRFactory.transformTree(tree, sourceFile, sourceString, config, errorReporter);
root = factory.getResultNode();
features = features.require(factory.getFeatures());
root.setIsSyntheticBlock(true);
root.putProp(Node.FEATURE_SET, features);

if (config.preserveDetailedSourceInfo) {
if (config.preserveDetailedSourceInfo == Config.SourceLocationInformation.PRESERVE) {
comments = p.getComments();
}
}
Expand Down
11 changes: 7 additions & 4 deletions test/com/google/javascript/jscomp/parsing/AttachJsdocsTest.java
Expand Up @@ -788,10 +788,13 @@ public void testInlineInExport() {

private Node parse(String source, String... warnings) {
TestErrorReporter testErrorReporter = new TestErrorReporter(null, warnings);
Config config = ParserRunner.createConfig(mode, null);
config.setPreserveDetailedSourceInfo(true);
config.setKeepGoing(true);
config.setParseJsDocDocumentation(true);
Config config =
ParserRunner.createConfig(
mode,
Config.JsDocParsing.INCLUDE_DESCRIPTIONS_NO_WHITESPACE,
Config.SourceLocationInformation.PRESERVE,
Config.RunMode.KEEP_GOING,
null);
Node script = ParserRunner.parse(
new SimpleSourceFile("input", false),
source,
Expand Down

0 comments on commit 78d0445

Please sign in to comment.