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) { protected Config createConfig(Config.LanguageMode mode) {
Config config = ParserRunner.createConfig(mode, options.extraAnnotationNames); Config config =
config.setPreserveDetailedSourceInfo(options.preservesDetailedSourceInfo()); ParserRunner.createConfig(
config.setKeepGoing(options.canContinueAfterErrors()); mode,
config.setParseJsDocDocumentation(options.isParseJsDocDocumentation()); options.isParseJsDocDocumentation(),
config.setPreserveJsDocWhitespace(options.isPreserveJsDocWhitespace()); options.preservesDetailedSourceInfo()
? Config.SourceLocationInformation.PRESERVE
: Config.SourceLocationInformation.DISCARD,
options.canContinueAfterErrors()
? Config.RunMode.KEEP_GOING
: Config.RunMode.STOP_AFTER_ERROR,
options.extraAnnotationNames);
return config; 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.ImmutableSet;
import com.google.common.collect.LinkedHashMultimap; import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Multimap; import com.google.common.collect.Multimap;
import com.google.javascript.jscomp.parsing.Config;
import com.google.javascript.rhino.IR; import com.google.javascript.rhino.IR;
import com.google.javascript.rhino.Node; import com.google.javascript.rhino.Node;
import com.google.javascript.rhino.SourcePosition; import com.google.javascript.rhino.SourcePosition;
Expand Down Expand Up @@ -126,8 +127,7 @@ public boolean shouldGenerateTypedExterns() {
return generateTypedExterns; return generateTypedExterns;
} }


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


/** /**
* Even if checkTypes is disabled, clients such as IDEs might want to still infer types. * 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); setContinueAfterErrors(ideMode);
setAllowHotswapReplaceScript(ideMode); setAllowHotswapReplaceScript(ideMode);
setPreserveDetailedSourceInfo(ideMode); setPreserveDetailedSourceInfo(ideMode);
setParseJsDocDocumentation(ideMode); setParseJsDocDocumentation(
ideMode
? Config.JsDocParsing.INCLUDE_DESCRIPTIONS_NO_WHITESPACE
: Config.JsDocParsing.TYPES_ONLY);
} }


public void setAllowHotswapReplaceScript(boolean allowRecompilation) { public void setAllowHotswapReplaceScript(boolean allowRecompilation) {
Expand All @@ -1879,14 +1882,23 @@ boolean canContinueAfterErrors() {
return continueAfterErrors; 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 * Enables or disables the parsing of JSDoc documentation, and optionally also
* enabled then documentation is always parsed. * 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; 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. * @return True when JSDoc documentation will be parsed, false if not.
*/ */
public boolean isParseJsDocDocumentation() { public Config.JsDocParsing isParseJsDocDocumentation() {
return this.parseJsDocDocumentation; 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 * 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). * 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( 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.SourceLocationInformation.PRESERVE,
Config.RunMode.KEEP_GOING,
/* extraAnnotationNames */ ImmutableSet.<String>of()); /* extraAnnotationNames */ ImmutableSet.<String>of());
config.setPreserveDetailedSourceInfo(true);
config.setKeepGoing(true);
config.setParseJsDocDocumentation(true);


SourceFile source = SourceFile.fromCode(filename, code); SourceFile source = SourceFile.fromCode(filename, code);
FileInfo info = new FileInfo(errorReporter); 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. * Whether to parse the descriptions of JsDoc comments.
*/ */
boolean parseJsDocDocumentation; public enum JsDocParsing {
TYPES_ONLY,
INCLUDE_DESCRIPTIONS_NO_WHITESPACE,
INCLUDE_DESCRIPTIONS_WITH_WHITESPACE;


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


/** /**
* Whether to keep detailed source location information such as the exact length of every node. * 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. * 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 * Recognized JSDoc annotations, mapped from their name to their internal
Expand All @@ -76,7 +88,26 @@ public enum LanguageMode {
final LanguageMode languageMode; final LanguageMode languageMode;


Config(Set<String> annotationWhitelist, Set<String> suppressionNames, 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.annotationNames = buildAnnotationNames(annotationWhitelist);
this.parseJsDocDocumentation = parseJsDocDocumentation;
this.preserveDetailedSourceInfo = preserveDetailedSourceInfo;
this.keepGoing = keepGoing;
this.suppressionNames = suppressionNames; this.suppressionNames = suppressionNames;
this.languageMode = languageMode; this.languageMode = languageMode;
} }
Expand All @@ -100,20 +131,4 @@ private static Map<String, Annotation> buildAnnotationNames(
} }
return annotationBuilder.build(); 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.currentComment = skipNonJsDoc(nextCommentIter);
this.newlines = new ArrayList<>(); this.newlines = new ArrayList<>();
this.sourceFile = sourceFile; this.sourceFile = sourceFile;
this.fileLevelJsDocBuilder = new JSDocInfoBuilder( this.fileLevelJsDocBuilder =
config.parseJsDocDocumentation); new JSDocInfoBuilder(config.parseJsDocDocumentation.shouldParseDescriptions());


// Pre-generate all the newlines in the file. // Pre-generate all the newlines in the file.
for (int charNo = 0; true; charNo++) { 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. // Set the length on the node if we're in IDE mode.
void maybeSetLength( void maybeSetLength(
Node node, SourcePosition start, SourcePosition end) { Node node, SourcePosition start, SourcePosition end) {
if (config.preserveDetailedSourceInfo) { if (config.preserveDetailedSourceInfo == Config.SourceLocationInformation.PRESERVE) {
node.setLength(end.offset - start.offset); node.setLength(end.offset - start.offset);
} }
} }


void maybeSetLengthFrom(Node node, Node ref) { void maybeSetLengthFrom(Node node, Node ref) {
if (config.preserveDetailedSourceInfo) { if (config.preserveDetailedSourceInfo == Config.SourceLocationInformation.PRESERVE) {
node.setLength(ref.getLength()); node.setLength(ref.getLength());
} }
} }
Expand Down Expand Up @@ -1255,7 +1255,7 @@ Node processFunction(FunctionDeclarationTree functionTree) {
if (!isArrow && !isSignature && !bodyNode.isBlock()) { if (!isArrow && !isSignature && !bodyNode.isBlock()) {
// When in "keep going" mode the parser tries to parse some constructs the // When in "keep going" mode the parser tries to parse some constructs the
// compiler doesn't support, repair it here. // compiler doesn't support, repair it here.
Preconditions.checkState(config.keepGoing); Preconditions.checkState(config.keepGoing == Config.RunMode.KEEP_GOING);
bodyNode = IR.block(); bodyNode = IR.block();
} }
parseDirectives(bodyNode); 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.sourceFile = sourceFile;


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


this.errorReporter = errorReporter; this.errorReporter = errorReporter;
this.templateNode = this.createTemplateNode(); 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.base.Splitter;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet; 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.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.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,6 +56,21 @@ private ParserRunner() {}


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


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


if (config.preserveDetailedSourceInfo) { if (config.preserveDetailedSourceInfo == Config.SourceLocationInformation.PRESERVE) {
comments = p.getComments(); 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) { private Node parse(String source, String... warnings) {
TestErrorReporter testErrorReporter = new TestErrorReporter(null, warnings); TestErrorReporter testErrorReporter = new TestErrorReporter(null, warnings);
Config config = ParserRunner.createConfig(mode, null); Config config =
config.setPreserveDetailedSourceInfo(true); ParserRunner.createConfig(
config.setKeepGoing(true); mode,
config.setParseJsDocDocumentation(true); Config.JsDocParsing.INCLUDE_DESCRIPTIONS_NO_WHITESPACE,
Config.SourceLocationInformation.PRESERVE,
Config.RunMode.KEEP_GOING,
null);
Node script = ParserRunner.parse( Node script = ParserRunner.parse(
new SimpleSourceFile("input", false), new SimpleSourceFile("input", false),
source, source,
Expand Down

0 comments on commit 78d0445

Please sign in to comment.