Skip to content

Commit

Permalink
Store source locations for nonJSDoc Comments
Browse files Browse the repository at this point in the history
Preserving begin and end offsets for regular comments would make applying fixes to source easier.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=260631458
  • Loading branch information
rishipal committed Jul 30, 2019
1 parent 3ec3029 commit 9366e75
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 6 deletions.
25 changes: 22 additions & 3 deletions src/com/google/javascript/jscomp/parsing/IRFactory.java
Expand Up @@ -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;
Expand Down Expand Up @@ -699,12 +700,27 @@ private boolean shouldAttachJSDocHere(ParseTree tree) {
}
}

private static String combineCommentsIntoSingleString(ArrayList<Comment> 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<Comment> comments) {
String result = "";
Iterator<Comment> 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++;
Expand All @@ -714,7 +730,10 @@ private static String combineCommentsIntoSingleString(ArrayList<Comment> comment
prevCommentEndLine = currComment.location.end.line;
}
}
return result;

NonJSDocComment nonJSDocComment =
new NonJSDocComment(completeCommentBegin, completeCommentEnd, result);
return nonJSDocComment;
}

private static Comment skipJsDocComments(UnmodifiableIterator<Comment> comments) {
Expand Down Expand Up @@ -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);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/com/google/javascript/rhino/Node.java
Expand Up @@ -214,19 +214,19 @@ 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
*/
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;
}
Expand Down
68 changes: 68 additions & 0 deletions 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;
}
}

0 comments on commit 9366e75

Please sign in to comment.