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();
var.setJSDocInfo(builder.build());
CompilerInput input = compiler.getSynthesizedExternsInput();
name.setStaticSourceFile(input.getSourceFile());
var.setStaticSourceFileFrom(name);
input.getAstRoot(compiler).addChildToBack(var);
Node root = input.getAstRoot(compiler);
name.setStaticSourceFileFrom(root);
var.setStaticSourceFileFrom(root);
root.addChildToBack(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.newlines = new ArrayList<>();
this.sourceFile = sourceFile;
// The template node properties are applied to all nodes in this transform.
this.templateNode = createTemplateNode();

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

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

if (config.strictMode == StrictMode.STRICT) {
reservedKeywords = ES5_STRICT_RESERVED_KEYWORDS;
Expand Down Expand Up @@ -903,7 +904,7 @@ private JsDocInfoParser createJsDocInfoParser(Comment node) {
charno + numOpeningChars),
comment,
position,
sourceFile,
templateNode,
config,
errorReporter);
jsdocParser.setFileLevelJsDocBuilder(fileLevelJsDocBuilder);
Expand Down Expand Up @@ -934,7 +935,7 @@ private JSDocInfo parseInlineTypeDoc(Comment node) {
charno + numOpeningChars),
comment,
node.location.start.offset,
sourceFile,
templateNode,
config,
errorReporter);
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 JSDocInfoBuilder jsdocBuilder;
private final StaticSourceFile sourceFile;
private final ErrorReporter errorReporter;

// 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;
}

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

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

this.sourceFile = sourceFile;

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

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

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

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

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

// Find the parameter's description (if applicable).
if (jsdocBuilder.shouldParseDocumentation()
Expand Down Expand Up @@ -1040,7 +1042,7 @@ private JsDocToken parseAnnotation(JsDocToken token,
if (validTypeTransformation) {
TypeTransformationParser ttlParser =
new TypeTransformationParser(typeTransformationExpr,
sourceFile, errorReporter, templateLineno, templateCharno);
getSourceFile(), errorReporter, templateLineno, templateCharno);
// If the parsing was successful store the type transformation
if (ttlParser.parseTypeTransformation()
&& !jsdocBuilder.recordTypeTransformation(
Expand Down Expand Up @@ -2589,16 +2591,6 @@ private Node newStringNode(String s, int lineno, int charno) {
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) {
addTypeWarning(warning, stream.getLineno(), stream.getCharno());
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.
*/
public void markName(String name, StaticSourceFile file,
public void markName(String name, Node templateNode,
int lineno, int charno) {
if (currentMarker != null) {
// 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();
Node node = Node.newString(Token.NAME, name, lineno, charno);
node.setLength(name.length());
node.setStaticSourceFile(file);
if (templateNode != null) {
node.setStaticSourceFileFrom(templateNode);
}
nodePos.setItem(node);
nodePos.setPositionInformation(lineno, charno,
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.StrictMode;
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.Marker;
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);

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

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

Expand Down

0 comments on commit 55d6e92

Please sign in to comment.