diff --git a/src/com/google/javascript/jscomp/CheckSideEffects.java b/src/com/google/javascript/jscomp/CheckSideEffects.java index 81ae78b46f9..f27cb5449fa 100644 --- a/src/com/google/javascript/jscomp/CheckSideEffects.java +++ b/src/com/google/javascript/jscomp/CheckSideEffects.java @@ -193,7 +193,7 @@ private void addExtern() { var.setJSDocInfo(builder.build()); CompilerInput input = compiler.getSynthesizedExternsInput(); name.setStaticSourceFile(input.getSourceFile()); - var.setStaticSourceFile(input.getSourceFile()); + var.setStaticSourceFileFrom(name); input.getAstRoot(compiler).addChildToBack(var); compiler.reportChangeToEnclosingScope(var); } diff --git a/src/com/google/javascript/rhino/Node.java b/src/com/google/javascript/rhino/Node.java index 1fa782012ca..ca9239fa7a8 100644 --- a/src/com/google/javascript/rhino/Node.java +++ b/src/com/google/javascript/rhino/Node.java @@ -1266,6 +1266,25 @@ private static void toStringTreeHelper(Node n, int level, Appendable sb) //========================================================================== // Source position management + public void setStaticSourceFileFrom(Node other) { + // Make sure source file prop nodes are not duplicated. + if (other.propListHead != null + && (this.propListHead == null + || (this.propListHead.propType == STATIC_SOURCE_FILE + && this.propListHead.next == null))) { + // Either the node has only STATIC_SOURCE_FILE as a property or has not properties. + PropListItem tail = other.propListHead; + while (tail.next != null) { + tail = tail.next; + } + if (tail.propType == STATIC_SOURCE_FILE) { + propListHead = tail; + return; + } + } + setStaticSourceFile(other.getStaticSourceFile()); + } + public void setStaticSourceFile(@Nullable StaticSourceFile file) { this.putProp(STATIC_SOURCE_FILE, file); } @@ -2225,15 +2244,15 @@ public Node cloneTree(boolean cloneTypeExprs) { // TODO(nicksantos): The semantics of this method are ill-defined. Delete it. @Deprecated public Node useSourceInfoWithoutLengthIfMissingFrom(Node other) { - if (getProp(ORIGINALNAME_PROP) == null) { - putProp(ORIGINALNAME_PROP, other.getProp(ORIGINALNAME_PROP)); - } - if (getStaticSourceFile() == null) { - setStaticSourceFile(other.getStaticSourceFile()); + setStaticSourceFileFrom(other); sourcePosition = other.sourcePosition; } + if (getProp(ORIGINALNAME_PROP) == null) { + putProp(ORIGINALNAME_PROP, other.getProp(ORIGINALNAME_PROP)); + } + return this; } @@ -2258,8 +2277,8 @@ public Node useSourceInfoWithoutLengthIfMissingFromForTree(Node other) { * that of {@code other}. */ public Node useSourceInfoFrom(Node other) { + setStaticSourceFileFrom(other); putProp(ORIGINALNAME_PROP, other.getProp(ORIGINALNAME_PROP)); - setStaticSourceFile(other.getStaticSourceFile()); sourcePosition = other.sourcePosition; length = other.length; return this; @@ -2291,16 +2310,16 @@ public Node srcrefTree(Node other) { * that of {@code other} iff the source info is missing. */ public Node useSourceInfoIfMissingFrom(Node other) { - if (getProp(ORIGINALNAME_PROP) == null) { - putProp(ORIGINALNAME_PROP, other.getProp(ORIGINALNAME_PROP)); - } - if (getStaticSourceFile() == null) { - setStaticSourceFile(other.getStaticSourceFile()); + setStaticSourceFileFrom(other); sourcePosition = other.sourcePosition; length = other.length; } + if (getProp(ORIGINALNAME_PROP) == null) { + putProp(ORIGINALNAME_PROP, other.getProp(ORIGINALNAME_PROP)); + } + return this; }