From 55d6e923fbf0380450504790a3e06477f3c89a1c Mon Sep 17 00:00:00 2001 From: rluble Date: Fri, 11 Aug 2017 14:46:54 -0700 Subject: [PATCH] Make sure STATIC_SOURCE_FILE property node is shared everywhere. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=165027727 --- .../javascript/jscomp/CheckSideEffects.java | 7 ++--- .../javascript/jscomp/parsing/IRFactory.java | 9 ++++--- .../jscomp/parsing/JsDocInfoParser.java | 26 +++++++------------ .../javascript/rhino/JSDocInfoBuilder.java | 6 +++-- .../jscomp/parsing/JsDocInfoParserTest.java | 5 +++- 5 files changed, 26 insertions(+), 27 deletions(-) diff --git a/src/com/google/javascript/jscomp/CheckSideEffects.java b/src/com/google/javascript/jscomp/CheckSideEffects.java index f27cb5449fa..5a73c3f3a81 100644 --- a/src/com/google/javascript/jscomp/CheckSideEffects.java +++ b/src/com/google/javascript/jscomp/CheckSideEffects.java @@ -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); } diff --git a/src/com/google/javascript/jscomp/parsing/IRFactory.java b/src/com/google/javascript/jscomp/parsing/IRFactory.java index a7ceea732fe..d16bb853873 100644 --- a/src/com/google/javascript/jscomp/parsing/IRFactory.java +++ b/src/com/google/javascript/jscomp/parsing/IRFactory.java @@ -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()); @@ -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; @@ -903,7 +904,7 @@ private JsDocInfoParser createJsDocInfoParser(Comment node) { charno + numOpeningChars), comment, position, - sourceFile, + templateNode, config, errorReporter); jsdocParser.setFileLevelJsDocBuilder(fileLevelJsDocBuilder); @@ -934,7 +935,7 @@ private JSDocInfo parseInlineTypeDoc(Comment node) { charno + numOpeningChars), comment, node.location.start.offset, - sourceFile, + templateNode, config, errorReporter); return parser.parseInlineTypeDoc(); diff --git a/src/com/google/javascript/jscomp/parsing/JsDocInfoParser.java b/src/com/google/javascript/jscomp/parsing/JsDocInfoParser.java index e46bf99bac8..91a1978ac3b 100644 --- a/src/com/google/javascript/jscomp/parsing/JsDocInfoParser.java +++ b/src/com/google/javascript/jscomp/parsing/JsDocInfoParser.java @@ -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 @@ -148,6 +147,10 @@ void setFileOverviewJSDocInfo(JSDocInfo fileOverviewJSDocInfo) { this.fileOverviewJSDocInfo = fileOverviewJSDocInfo; } + public StaticSourceFile getSourceFile() { + return templateNode.getStaticSourceFile(); + } + private enum State { SEARCHING_ANNOTATION, SEARCHING_NEWLINE, @@ -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) { @@ -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(); } @@ -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() @@ -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( @@ -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; diff --git a/src/com/google/javascript/rhino/JSDocInfoBuilder.java b/src/com/google/javascript/rhino/JSDocInfoBuilder.java index fec6ed4b5a5..1718484dd8b 100644 --- a/src/com/google/javascript/rhino/JSDocInfoBuilder.java +++ b/src/com/google/javascript/rhino/JSDocInfoBuilder.java @@ -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 and a @@ -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()); diff --git a/test/com/google/javascript/jscomp/parsing/JsDocInfoParserTest.java b/test/com/google/javascript/jscomp/parsing/JsDocInfoParserTest.java index a89cf506ff3..0441be25007 100644 --- a/test/com/google/javascript/jscomp/parsing/JsDocInfoParserTest.java +++ b/test/com/google/javascript/jscomp/parsing/JsDocInfoParserTest.java @@ -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; @@ -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);