diff --git a/src/com/google/javascript/jscomp/parsing/IRFactory.java b/src/com/google/javascript/jscomp/parsing/IRFactory.java index 3f34bf8f214..0373ed21fb7 100644 --- a/src/com/google/javascript/jscomp/parsing/IRFactory.java +++ b/src/com/google/javascript/jscomp/parsing/IRFactory.java @@ -154,6 +154,7 @@ import com.google.javascript.rhino.JSDocInfoBuilder; import com.google.javascript.rhino.Node; import com.google.javascript.rhino.Node.TypeDeclarationNode; +import com.google.javascript.rhino.NonJSDocComment; import com.google.javascript.rhino.StaticSourceFile; import com.google.javascript.rhino.Token; import com.google.javascript.rhino.TokenStream; @@ -699,12 +700,27 @@ private boolean shouldAttachJSDocHere(ParseTree tree) { } } - private static String combineCommentsIntoSingleString(ArrayList comments) { + /** + * Appends every comment associated with this node into one NonJSDocComment. It would be legal to + * replace all comments associated with this node with that one string. + * + * @param comments - list of line or block comments that are sequential in source code + * @return complete comment as NonJSDocComment + */ + private static NonJSDocComment combineCommentsIntoSingleComment(ArrayList comments) { String result = ""; Iterator itr = comments.iterator(); int prevCommentEndLine = Integer.MAX_VALUE; + int completeCommentBegin = 0; + int completeCommentEnd = 0; while (itr.hasNext()) { Comment currComment = itr.next(); + if (currComment.location.start.offset < completeCommentBegin) { + completeCommentBegin = currComment.location.start.offset; + } + if (currComment.location.end.offset > completeCommentEnd) { + completeCommentEnd = currComment.location.end.offset; + } while (prevCommentEndLine < currComment.location.start.line) { result += "\n"; prevCommentEndLine++; @@ -714,7 +730,10 @@ private static String combineCommentsIntoSingleString(ArrayList comment prevCommentEndLine = currComment.location.end.line; } } - return result; + + NonJSDocComment nonJSDocComment = + new NonJSDocComment(completeCommentBegin, completeCommentEnd, result); + return nonJSDocComment; } private static Comment skipJsDocComments(UnmodifiableIterator comments) { @@ -789,7 +808,7 @@ Node transform(ParseTree tree) { } if (this.config.jsDocParsingMode() == JsDocParsing.INCLUDE_ALL_COMMENTS) { if (!associatedComments.isEmpty()) { - String completeComment = combineCommentsIntoSingleString(associatedComments); + NonJSDocComment completeComment = combineCommentsIntoSingleComment(associatedComments); node.setNonJSDocComment(completeComment); } } diff --git a/src/com/google/javascript/rhino/Node.java b/src/com/google/javascript/rhino/Node.java index f620cd9d1fe..8eee8643212 100644 --- a/src/com/google/javascript/rhino/Node.java +++ b/src/com/google/javascript/rhino/Node.java @@ -214,7 +214,7 @@ private enum Prop { } /** - * Get the NonJSDoc comment attached to this node. + * Get the NonJSDoc comment string attached to this node. * * @return the information or empty string if no nonJSDoc is attached to this node */ @@ -222,11 +222,11 @@ public final String getNonJSDocCommentString() { if (getProp(Prop.NON_JSDOC_COMMENT) == null) { return ""; } - return (String) getProp(Prop.NON_JSDOC_COMMENT); + return ((NonJSDocComment) getProp(Prop.NON_JSDOC_COMMENT)).getCommentString(); } /** Sets the NonJSDoc comment attached to this node. */ - public final Node setNonJSDocComment(String comment) { + public final Node setNonJSDocComment(NonJSDocComment comment) { putProp(Prop.NON_JSDOC_COMMENT, comment); return this; } diff --git a/src/com/google/javascript/rhino/NonJSDocComment.java b/src/com/google/javascript/rhino/NonJSDocComment.java new file mode 100644 index 00000000000..1bb4cfa6dda --- /dev/null +++ b/src/com/google/javascript/rhino/NonJSDocComment.java @@ -0,0 +1,68 @@ +/* + * + * ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is Rhino code, released + * May 6, 1999. + * + * The Initial Developer of the Original Code is + * Netscape Communications Corporation. + * Portions created by the Initial Developer are Copyright (C) 1997-1999 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Bob Jervis + * Google Inc. + * + * Alternatively, the contents of this file may be used under the terms of + * the GNU General Public License Version 2 or later (the "GPL"), in which + * case the provisions of the GPL are applicable instead of those above. If + * you wish to allow use of your version of this file only under the terms of + * the GPL and not to allow others to use your version of this file under the + * MPL, indicate your decision by deleting the provisions above and replacing + * them with the notice and other provisions required by the GPL. If you do + * not delete the provisions above, a recipient may use your version of this + * file under either the MPL or the GPL. + * + * ***** END LICENSE BLOCK ***** */ + +package com.google.javascript.rhino; + +/** Minimal class holding information about a nonJSDoc comment's source location and contents */ +public class NonJSDocComment { + private final int beginOffset; + private final int endOffset; + private final String contents; + + public NonJSDocComment(int begin, int end, String s) { + beginOffset = begin; + endOffset = end; + contents = s; + } + + public String getCommentString() { + if (contents == null) { + return ""; + } + return contents; + } + + public int getBeginOffset() { + return beginOffset; + } + + public int getEndOffset() { + return endOffset; + } +}