Skip to content

Commit

Permalink
Make sure STATIC_SOURCE_FILE property node is shared everywhere.
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=165027727
  • Loading branch information
rluble authored and blickly committed Aug 11, 2017
1 parent a403772 commit 55d6e92
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 27 deletions.
7 changes: 4 additions & 3 deletions src/com/google/javascript/jscomp/CheckSideEffects.java
Expand Up @@ -192,9 +192,10 @@ private void addExtern() {
builder.recordNoAlias(); builder.recordNoAlias();
var.setJSDocInfo(builder.build()); var.setJSDocInfo(builder.build());
CompilerInput input = compiler.getSynthesizedExternsInput(); CompilerInput input = compiler.getSynthesizedExternsInput();
name.setStaticSourceFile(input.getSourceFile()); Node root = input.getAstRoot(compiler);
var.setStaticSourceFileFrom(name); name.setStaticSourceFileFrom(root);
input.getAstRoot(compiler).addChildToBack(var); var.setStaticSourceFileFrom(root);
root.addChildToBack(var);
compiler.reportChangeToEnclosingScope(var); compiler.reportChangeToEnclosingScope(var);
} }


Expand Down
9 changes: 5 additions & 4 deletions src/com/google/javascript/jscomp/parsing/IRFactory.java
Expand Up @@ -292,6 +292,9 @@ 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;
// The template node properties are applied to all nodes in this transform.
this.templateNode = createTemplateNode();

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


Expand All @@ -310,8 +313,6 @@ private IRFactory(String sourceString,
this.config = config; this.config = config;
this.errorReporter = errorReporter; this.errorReporter = errorReporter;
this.transformDispatcher = new TransformDispatcher(); this.transformDispatcher = new TransformDispatcher();
// The template node properties are applied to all nodes in this transform.
this.templateNode = createTemplateNode();


if (config.strictMode == StrictMode.STRICT) { if (config.strictMode == StrictMode.STRICT) {
reservedKeywords = ES5_STRICT_RESERVED_KEYWORDS; reservedKeywords = ES5_STRICT_RESERVED_KEYWORDS;
Expand Down Expand Up @@ -903,7 +904,7 @@ private JsDocInfoParser createJsDocInfoParser(Comment node) {
charno + numOpeningChars), charno + numOpeningChars),
comment, comment,
position, position,
sourceFile, templateNode,
config, config,
errorReporter); errorReporter);
jsdocParser.setFileLevelJsDocBuilder(fileLevelJsDocBuilder); jsdocParser.setFileLevelJsDocBuilder(fileLevelJsDocBuilder);
Expand Down Expand Up @@ -934,7 +935,7 @@ private JSDocInfo parseInlineTypeDoc(Comment node) {
charno + numOpeningChars), charno + numOpeningChars),
comment, comment,
node.location.start.offset, node.location.start.offset,
sourceFile, templateNode,
config, config,
errorReporter); errorReporter);
return parser.parseInlineTypeDoc(); return parser.parseInlineTypeDoc();
Expand Down
26 changes: 9 additions & 17 deletions src/com/google/javascript/jscomp/parsing/JsDocInfoParser.java
Expand Up @@ -58,7 +58,6 @@ public final class JsDocInfoParser {


private final JsDocTokenStream stream; private final JsDocTokenStream stream;
private final JSDocInfoBuilder jsdocBuilder; private final JSDocInfoBuilder jsdocBuilder;
private final StaticSourceFile sourceFile;
private final ErrorReporter errorReporter; private final ErrorReporter errorReporter;


// Use a template node for properties set on all nodes to minimize the // Use a template node for properties set on all nodes to minimize the
Expand Down Expand Up @@ -148,6 +147,10 @@ void setFileOverviewJSDocInfo(JSDocInfo fileOverviewJSDocInfo) {
this.fileOverviewJSDocInfo = fileOverviewJSDocInfo; this.fileOverviewJSDocInfo = fileOverviewJSDocInfo;
} }


public StaticSourceFile getSourceFile() {
return templateNode.getStaticSourceFile();
}

private enum State { private enum State {
SEARCHING_ANNOTATION, SEARCHING_ANNOTATION,
SEARCHING_NEWLINE, SEARCHING_NEWLINE,
Expand All @@ -158,13 +161,11 @@ private enum State {
JsDocInfoParser(JsDocTokenStream stream, JsDocInfoParser(JsDocTokenStream stream,
String comment, String comment,
int commentPosition, int commentPosition,
StaticSourceFile sourceFile, Node templateNode,
Config config, Config config,
ErrorReporter errorReporter) { ErrorReporter errorReporter) {
this.stream = stream; this.stream = stream;


this.sourceFile = sourceFile;

boolean parseDocumentation = config.parseJsDocDocumentation.shouldParseDescriptions(); boolean parseDocumentation = config.parseJsDocDocumentation.shouldParseDescriptions();
this.jsdocBuilder = new JSDocInfoBuilder(parseDocumentation); this.jsdocBuilder = new JSDocInfoBuilder(parseDocumentation);
if (comment != null) { if (comment != null) {
Expand All @@ -177,10 +178,11 @@ private enum State {
config.parseJsDocDocumentation == Config.JsDocParsing.INCLUDE_DESCRIPTIONS_WITH_WHITESPACE; config.parseJsDocDocumentation == Config.JsDocParsing.INCLUDE_DESCRIPTIONS_WITH_WHITESPACE;


this.errorReporter = errorReporter; this.errorReporter = errorReporter;
this.templateNode = this.createTemplateNode(); this.templateNode = templateNode == null ? IR.script() : templateNode;
} }


private String getSourceName() { private String getSourceName() {
StaticSourceFile sourceFile = getSourceFile();
return sourceFile == null ? null : sourceFile.getName(); return sourceFile == null ? null : sourceFile.getName();
} }


Expand Down Expand Up @@ -892,7 +894,7 @@ private JsDocToken parseAnnotation(JsDocToken token,
return token; return token;
} }


jsdocBuilder.markName(name, sourceFile, lineno, charno); jsdocBuilder.markName(name, templateNode, lineno, charno);


// Find the parameter's description (if applicable). // Find the parameter's description (if applicable).
if (jsdocBuilder.shouldParseDocumentation() if (jsdocBuilder.shouldParseDocumentation()
Expand Down Expand Up @@ -1040,7 +1042,7 @@ private JsDocToken parseAnnotation(JsDocToken token,
if (validTypeTransformation) { if (validTypeTransformation) {
TypeTransformationParser ttlParser = TypeTransformationParser ttlParser =
new TypeTransformationParser(typeTransformationExpr, new TypeTransformationParser(typeTransformationExpr,
sourceFile, errorReporter, templateLineno, templateCharno); getSourceFile(), errorReporter, templateLineno, templateCharno);
// If the parsing was successful store the type transformation // If the parsing was successful store the type transformation
if (ttlParser.parseTypeTransformation() if (ttlParser.parseTypeTransformation()
&& !jsdocBuilder.recordTypeTransformation( && !jsdocBuilder.recordTypeTransformation(
Expand Down Expand Up @@ -2589,16 +2591,6 @@ private Node newStringNode(String s, int lineno, int charno) {
return n; return n;
} }


// This is similar to IRFactory.createTemplateNode to share common props
// e.g., source-name, between all nodes.
private Node createTemplateNode() {
// The Node type choice is arbitrary.
Node templateNode = IR.script();
templateNode.setStaticSourceFile(
this.sourceFile);
return templateNode;
}

private Node reportTypeSyntaxWarning(String warning) { private Node reportTypeSyntaxWarning(String warning) {
addTypeWarning(warning, stream.getLineno(), stream.getCharno()); addTypeWarning(warning, stream.getLineno(), stream.getCharno());
return null; return null;
Expand Down
6 changes: 4 additions & 2 deletions src/com/google/javascript/rhino/JSDocInfoBuilder.java
Expand Up @@ -259,7 +259,7 @@ public void markTypeNode(Node typeNode, int lineno, int startCharno,
/** /**
* Adds a name declaration to the current marker. * Adds a name declaration to the current marker.
*/ */
public void markName(String name, StaticSourceFile file, public void markName(String name, Node templateNode,
int lineno, int charno) { int lineno, int charno) {
if (currentMarker != null) { if (currentMarker != null) {
// Record the name as both a SourcePosition<String> and a // Record the name as both a SourcePosition<String> and a
Expand All @@ -278,7 +278,9 @@ public void markName(String name, StaticSourceFile file,
JSDocInfo.NamePosition nodePos = new JSDocInfo.NamePosition(); JSDocInfo.NamePosition nodePos = new JSDocInfo.NamePosition();
Node node = Node.newString(Token.NAME, name, lineno, charno); Node node = Node.newString(Token.NAME, name, lineno, charno);
node.setLength(name.length()); node.setLength(name.length());
node.setStaticSourceFile(file); if (templateNode != null) {
node.setStaticSourceFileFrom(templateNode);
}
nodePos.setItem(node); nodePos.setItem(node);
nodePos.setPositionInformation(lineno, charno, nodePos.setPositionInformation(lineno, charno,
lineno, charno + name.length()); lineno, charno + name.length());
Expand Down
Expand Up @@ -29,6 +29,7 @@
import com.google.javascript.jscomp.parsing.Config.RunMode; import com.google.javascript.jscomp.parsing.Config.RunMode;
import com.google.javascript.jscomp.parsing.Config.StrictMode; import com.google.javascript.jscomp.parsing.Config.StrictMode;
import com.google.javascript.jscomp.parsing.ParserRunner.ParseResult; import com.google.javascript.jscomp.parsing.ParserRunner.ParseResult;
import com.google.javascript.rhino.IR;
import com.google.javascript.rhino.JSDocInfo; import com.google.javascript.rhino.JSDocInfo;
import com.google.javascript.rhino.JSDocInfo.Marker; import com.google.javascript.rhino.JSDocInfo.Marker;
import com.google.javascript.rhino.JSDocInfo.Visibility; import com.google.javascript.rhino.JSDocInfo.Visibility;
Expand Down Expand Up @@ -4770,12 +4771,14 @@ private JSDocInfo parse(String comment, JsDocParsing parseDocumentation,
Config.StrictMode.SLOPPY); Config.StrictMode.SLOPPY);


StaticSourceFile file = new SimpleSourceFile("testcode", false); StaticSourceFile file = new SimpleSourceFile("testcode", false);
Node templateNode = IR.script();
templateNode.setStaticSourceFile(file);


JsDocInfoParser jsdocParser = new JsDocInfoParser( JsDocInfoParser jsdocParser = new JsDocInfoParser(
stream(comment), stream(comment),
comment, comment,
0, 0,
file, templateNode,
config, config,
errorReporter); errorReporter);


Expand Down

0 comments on commit 55d6e92

Please sign in to comment.