Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retaining of comments and their position in the actual code after parsing. #465

Merged
merged 9 commits into from
Aug 10, 2018
316 changes: 207 additions & 109 deletions src/org/mozilla/javascript/Parser.java

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/org/mozilla/javascript/ScriptRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ public static String escapeString(String s)
*/
public static String escapeString(String s, char escapeQuote)
{
if (!(escapeQuote == '"' || escapeQuote == '\'')) Kit.codeBug();
if (!(escapeQuote == '"' || escapeQuote == '\'' || escapeQuote == '`')) Kit.codeBug();
StringBuilder sb = null;

for(int i = 0, L = s.length(); i != L; ++i) {
Expand Down
2 changes: 1 addition & 1 deletion src/org/mozilla/javascript/TokenStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,7 @@ final int getToken() throws IOException
}

// is it a string?
if (c == '"' || c == '\'') {
if (c == '"' || c == '\'' || c == '`') {
// We attempt to accumulate a string the fast way, by
// building it directly out of the reader. But if there
// are any escaped characters in the string, we revert to
Expand Down
24 changes: 24 additions & 0 deletions src/org/mozilla/javascript/ast/AstNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@ public abstract class AstNode extends Node implements Comparable<AstNode> {
protected int position = -1;
protected int length = 1;
protected AstNode parent;
/*
* Holds comments that are on same line as of actual statement e.g.
* For a for loop
* 1) for(var i=0; i<10; i++) //test comment { }
* 2) for(var i=0; i<10; i++)
* //test comment
* //test comment 2
* { }
* For If Statement
* 1) if (x == 2) //test if comment
* a = 3 + 4; //then comment
* and so on
*/
protected AstNode inlineComment;

private static Map<Integer,String> operatorNames =
new HashMap<Integer,String>();
Expand Down Expand Up @@ -570,6 +584,8 @@ public boolean visit(AstNode node) {
buffer.append(node.getLength());
if (tt == Token.NAME) {
buffer.append(" ").append(((Name)node).getIdentifier());
} else if(tt == Token.STRING) {
buffer.append(" ").append(((StringLiteral)node).getValue(true));
}
buffer.append("\n");
return true; // process kids
Expand Down Expand Up @@ -601,4 +617,12 @@ public String debugPrint() {
visit(dpv);
return dpv.toString();
}

public AstNode getInlineComment() {
return inlineComment;
}

public void setInlineComment(AstNode inlineComment) {
this.inlineComment = inlineComment;
}
}
3 changes: 3 additions & 0 deletions src/org/mozilla/javascript/ast/AstRoot.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ public String toSource(int depth) {
StringBuilder sb = new StringBuilder();
for (Node node : this) {
sb.append(((AstNode)node).toSource(depth));
if(node.getType() == Token.COMMENT) {
sb.append("\n");
}
}
return sb.toString();
}
Expand Down
12 changes: 10 additions & 2 deletions src/org/mozilla/javascript/ast/Block.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,18 @@ public String toSource(int depth) {
sb.append(makeIndent(depth));
sb.append("{\n");
for (Node kid : this) {
sb.append(((AstNode)kid).toSource(depth+1));
AstNode astNodeKid = (AstNode)kid;
sb.append(astNodeKid.toSource(depth+1));
if(astNodeKid.getType() == Token.COMMENT) {
sb.append("\n");
}
}
sb.append(makeIndent(depth));
sb.append("}\n");
sb.append("}");
if(this.getInlineComment() != null) {
sb.append(this.getInlineComment().toSource(depth));
}
sb.append("\n");
return sb.toString();
}

Expand Down
12 changes: 12 additions & 0 deletions src/org/mozilla/javascript/ast/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,23 @@ public String getValue() {
return value;
}

/**
* Set the comment Value with the new commentString. and updates the length with new Length.
* @param commentString
*/
public void setValue(String commentString) {
this.value = commentString;
this.setLength(this.value.length());
}

@Override
public String toSource(int depth) {
StringBuilder sb = new StringBuilder(getLength() + 10);
sb.append(makeIndent(depth));
sb.append(value);
if(Token.CommentType.BLOCK_COMMENT == this.getCommentType()) {
sb.append("\n");
}
return sb.toString();
}

Expand Down
3 changes: 3 additions & 0 deletions src/org/mozilla/javascript/ast/DoLoop.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ public String toSource(int depth) {
StringBuilder sb = new StringBuilder();
sb.append(makeIndent(depth));
sb.append("do ");
if(this.getInlineComment() != null) {
sb.append(this.getInlineComment().toSource(depth + 1)).append("\n");
}
sb.append(body.toSource(depth).trim());
sb.append(" while (");
sb.append(condition.toSource(0));
Expand Down
6 changes: 5 additions & 1 deletion src/org/mozilla/javascript/ast/ExpressionStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ public boolean hasSideEffects() {
public String toSource(int depth) {
StringBuilder sb = new StringBuilder();
sb.append(expr.toSource(depth));
sb.append(";\n");
sb.append(";");
if(this.getInlineComment() != null) {
sb.append(this.getInlineComment().toSource(depth));
}
sb.append("\n");
return sb.toString();
}

Expand Down
14 changes: 12 additions & 2 deletions src/org/mozilla/javascript/ast/ForLoop.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,20 @@ public String toSource(int depth) {
sb.append("; ");
sb.append(increment.toSource(0));
sb.append(") ");
if(this.getInlineComment() != null) {
sb.append(this.getInlineComment().toSource()).append("\n");
}
if (body.getType() == Token.BLOCK) {
sb.append(body.toSource(depth).trim()).append("\n");
String bodySource = body.toSource(depth);
if(this.getInlineComment() == null) {
bodySource = bodySource.trim();
}
sb.append(bodySource).append("\n");
} else {
sb.append("\n").append(body.toSource(depth+1));
if(this.getInlineComment() == null) {
sb.append("\n");
}
sb.append(body.toSource(depth+1));
}
return sb.toString();
}
Expand Down
3 changes: 3 additions & 0 deletions src/org/mozilla/javascript/ast/FunctionCall.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ public String toSource(int depth) {
printList(arguments, sb);
}
sb.append(")");
if(this.getInlineComment() != null) {
sb.append(this.getInlineComment().toSource(depth)).append("\n");
}
return sb.toString();
}

Expand Down
28 changes: 25 additions & 3 deletions src/org/mozilla/javascript/ast/IfStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class IfStatement extends AstNode {
private AstNode thenPart;
private int elsePosition = -1;
private AstNode elsePart;
private AstNode elseKeyWordInlineComment;
private int lp = -1;
private int rp = -1;

Expand Down Expand Up @@ -149,19 +150,31 @@ public String toSource(int depth) {
sb.append("if (");
sb.append(condition.toSource(0));
sb.append(") ");
if (thenPart.getType() != Token.BLOCK) {
sb.append("\n").append(makeIndent(depth + 1));
if(this.getInlineComment() != null) {
sb.append(" ").append(this.getInlineComment().toSource()).append("\n");
}
if (thenPart.getType() != Token.BLOCK) {
if(this.getInlineComment() == null) {
sb.append("\n");
}
sb.append(makeIndent(depth + 1));
}
sb.append(thenPart.toSource(depth).trim());
if (elsePart != null) {
if (thenPart.getType() != Token.BLOCK) {
sb.append("\n").append(pad).append("else ");
} else {
sb.append(" else ");
}
if(this.getElseKeyWordInlineComment() != null) {
sb.append(" ").append(this.getElseKeyWordInlineComment().toSource()).append("\n");
}
if (elsePart.getType() != Token.BLOCK
&& elsePart.getType() != Token.IF) {
sb.append("\n").append(makeIndent(depth + 1));
if(this.getElseKeyWordInlineComment() == null) {
sb.append("\n");
}
sb.append(makeIndent(depth + 1));
}
sb.append(elsePart.toSource(depth).trim());
}
Expand All @@ -183,4 +196,13 @@ public void visit(NodeVisitor v) {
}
}
}

public AstNode getElseKeyWordInlineComment() {
return elseKeyWordInlineComment;
}

public void setElseKeyWordInlineComment(AstNode elseKeyWordInlineComment) {
this.elseKeyWordInlineComment = elseKeyWordInlineComment;
}

}
6 changes: 5 additions & 1 deletion src/org/mozilla/javascript/ast/Scope.java
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,11 @@ public String toSource(int depth) {
sb.append(makeIndent(depth));
sb.append("{\n");
for (Node kid : this) {
sb.append(((AstNode)kid).toSource(depth+1));
AstNode astNodeKid = (AstNode) kid;
sb.append(astNodeKid.toSource(depth+1));
if(astNodeKid.getType() == Token.COMMENT) {
sb.append("\n");
}
}
sb.append(makeIndent(depth));
sb.append("}\n");
Expand Down
9 changes: 8 additions & 1 deletion src/org/mozilla/javascript/ast/SwitchCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,18 @@ public String toSource(int depth) {
} else {
sb.append("case ");
sb.append(expression.toSource(0));
sb.append(":\n");
sb.append(":");
if(this.getInlineComment() != null) {
sb.append(this.getInlineComment().toSource(depth + 1));
}
sb.append("\n");
}
if (statements != null) {
for (AstNode s : statements) {
sb.append(s.toSource(depth+1));
if(s.getType() == Token.COMMENT && ((Comment)s).getCommentType() == Token.CommentType.LINE) {
sb.append("\n");
}
}
}
return sb.toString();
Expand Down
3 changes: 3 additions & 0 deletions src/org/mozilla/javascript/ast/TryStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ public String toSource(int depth) {
StringBuilder sb = new StringBuilder(250);
sb.append(makeIndent(depth));
sb.append("try ");
if(this.getInlineComment() != null) {
sb.append(this.getInlineComment().toSource(depth + 1)).append("\n");
}
sb.append(tryBlock.toSource(depth).trim());
for (CatchClause cc : getCatchClauses()) {
sb.append(cc.toSource(depth));
Expand Down
7 changes: 6 additions & 1 deletion src/org/mozilla/javascript/ast/VariableDeclaration.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,12 @@ public String toSource(int depth) {
sb.append(" ");
printList(variables, sb);
if (isStatement()) {
sb.append(";\n");
sb.append(";");
}
if(this.getInlineComment() != null) {
sb.append(this.getInlineComment().toSource(depth)).append("\n");
} else if (isStatement()) {
sb.append("\n");
}
return sb.toString();
}
Expand Down
8 changes: 7 additions & 1 deletion src/org/mozilla/javascript/ast/WhileLoop.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,17 @@ public String toSource(int depth) {
sb.append("while (");
sb.append(condition.toSource(0));
sb.append(") ");
if(this.getInlineComment() != null) {
sb.append(this.getInlineComment().toSource(depth + 1)).append("\n");
}
if (body.getType() == Token.BLOCK) {
sb.append(body.toSource(depth).trim());
sb.append("\n");
} else {
sb.append("\n").append(body.toSource(depth+1));
if(this.getInlineComment() == null) {
sb.append("\n");
}
sb.append(body.toSource(depth+1));
}
return sb.toString();
}
Expand Down
6 changes: 6 additions & 0 deletions src/org/mozilla/javascript/ast/WithStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,13 @@ public String toSource(int depth) {
sb.append("with (");
sb.append(expression.toSource(0));
sb.append(") ");
if(this.getInlineComment() != null) {
sb.append(this.getInlineComment().toSource(depth + 1));
}
if (statement.getType() == Token.BLOCK) {
if(this.getInlineComment() != null) {
sb.append("\n");
}
sb.append(statement.toSource(depth).trim());
sb.append("\n");
} else {
Expand Down
22 changes: 16 additions & 6 deletions testsrc/org/mozilla/javascript/tests/ParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,23 @@ public void testAutoSemiColonBetweenNames() {
public void testParseAutoSemiColonBeforeNewlineAndComments() throws IOException {
AstRoot root = parseAsReader(
"var s = 3\n"
+ "/* */var t = 1;");
+ "/* */ /* test comment */ var t = 1;");
assertNotNull(root.getComments());
assertEquals(1, root.getComments().size());
assertEquals(2, root.getComments().size());

assertEquals("var s = 3;\nvar t = 1;\n", root.toSource());
}

public void testNewlineAndComments() throws IOException {
AstRoot root = parseAsReader(
"var s = 3;\n"
+ "/* */ /* txt */var t = 1");
assertNotNull(root.getComments());
assertEquals(2, root.getComments().size());

assertEquals("var s = 3;\n/* */\n\n/* txt */\n\nvar t = 1;\n", root.toSource());
}

public void testAutoSemiBeforeComment1() {
parse("var a = 1\n/** a */ var b = 2");
}
Expand Down Expand Up @@ -862,7 +872,7 @@ public void testJSDocAttachment1() {
assertEquals(1, root.getComments().size());
assertEquals("/** @type number */",
root.getComments().first().getValue());
assertNotNull(root.getFirstChild().getJsDoc());
assertNotNull(root.getFirstChild().getNext().getJsDoc());
}

public void testJSDocAttachment2() {
Expand All @@ -871,7 +881,7 @@ public void testJSDocAttachment2() {
assertEquals(1, root.getComments().size());
assertEquals("/** @type number */",
root.getComments().first().getValue());
ExpressionStatement st = (ExpressionStatement) root.getFirstChild();
ExpressionStatement st = (ExpressionStatement) root.getFirstChild().getNext();
assertNotNull(st.getExpression().getJsDoc());
}

Expand Down Expand Up @@ -1022,7 +1032,7 @@ public void testJSDocAttachment15() {
assertNotNull(root.getComments());
assertEquals(1, root.getComments().size());

ExpressionStatement st = (ExpressionStatement) root.getFirstChild();
ExpressionStatement st = (ExpressionStatement) root.getFirstChild().getNext();
assertNotNull(st.getExpression().getJsDoc());
}

Expand All @@ -1034,7 +1044,7 @@ public void testJSDocAttachment16() {
assertNotNull(root.getComments());
assertEquals(1, root.getComments().size());

WithStatement st = (WithStatement) root.getFirstChild();
WithStatement st = (WithStatement) root.getFirstChild().getNext();
assertNotNull(st.getJsDoc());
}

Expand Down
Loading