Skip to content

Commit

Permalink
Merge pull request mozilla#64 from anba/bug-688023
Browse files Browse the repository at this point in the history
Patch for Bug 688023
  • Loading branch information
hns committed Aug 3, 2012
2 parents 797afee + e3b7240 commit 31062fd
Show file tree
Hide file tree
Showing 8 changed files with 253 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/org/mozilla/javascript/ast/ForInLoop.java
Expand Up @@ -131,7 +131,7 @@ public String toSource(int depth) {
sb.append(" in ");
sb.append(iteratedObject.toSource(0));
sb.append(") ");
if (body instanceof Block) {
if (body.getType() == Token.BLOCK) {
sb.append(body.toSource(depth).trim()).append("\n");
} else {
sb.append("\n").append(body.toSource(depth+1));
Expand Down
2 changes: 1 addition & 1 deletion src/org/mozilla/javascript/ast/ForLoop.java
Expand Up @@ -110,7 +110,7 @@ public String toSource(int depth) {
sb.append("; ");
sb.append(increment.toSource(0));
sb.append(") ");
if (body instanceof Block) {
if (body.getType() == Token.BLOCK) {
sb.append(body.toSource(depth).trim()).append("\n");
} else {
sb.append("\n").append(body.toSource(depth+1));
Expand Down
19 changes: 12 additions & 7 deletions src/org/mozilla/javascript/ast/IfStatement.java
Expand Up @@ -149,15 +149,20 @@ public String toSource(int depth) {
sb.append("if (");
sb.append(condition.toSource(0));
sb.append(") ");
if (!(thenPart instanceof Block)) {
sb.append("\n").append(makeIndent(depth));
if (thenPart.getType() != Token.BLOCK) {
sb.append("\n").append(makeIndent(depth + 1));
}
sb.append(thenPart.toSource(depth).trim());
if (elsePart instanceof IfStatement) {
sb.append(" else ");
sb.append(elsePart.toSource(depth).trim());
} else if (elsePart != null) {
sb.append(" else ");
if (elsePart != null) {
if (thenPart.getType() != Token.BLOCK) {
sb.append("\n").append(pad).append("else ");
} else {
sb.append(" else ");
}
if (elsePart.getType() != Token.BLOCK
&& elsePart.getType() != Token.IF) {
sb.append("\n").append(makeIndent(depth + 1));
}
sb.append(elsePart.toSource(depth).trim());
}
sb.append("\n");
Expand Down
2 changes: 1 addition & 1 deletion src/org/mozilla/javascript/ast/WhileLoop.java
Expand Up @@ -57,7 +57,7 @@ public String toSource(int depth) {
sb.append("while (");
sb.append(condition.toSource(0));
sb.append(") ");
if (body instanceof Block) {
if (body.getType() == Token.BLOCK) {
sb.append(body.toSource(depth).trim());
sb.append("\n");
} else {
Expand Down
8 changes: 5 additions & 3 deletions src/org/mozilla/javascript/ast/WithStatement.java
Expand Up @@ -113,9 +113,11 @@ public String toSource(int depth) {
sb.append("with (");
sb.append(expression.toSource(0));
sb.append(") ");
sb.append(statement.toSource(depth+1));
if (!(statement instanceof Block)) {
sb.append(";\n");
if (statement.getType() == Token.BLOCK) {
sb.append(statement.toSource(depth).trim());
sb.append("\n");
} else {
sb.append("\n").append(statement.toSource(depth + 1));
}
return sb.toString();
}
Expand Down
14 changes: 7 additions & 7 deletions testsrc/org/mozilla/javascript/tests/Bug491621Test.java
Expand Up @@ -44,13 +44,13 @@ public void testVarDeclarationToSource()
assertSource("for(var i=0;i<10;i++)x[i]=i;a++;",
"for (var i = 0; i < 10; i++) \n x[i] = i;\na++;\n");
assertSource("var a;if(true)a=1;",
"var a;\nif (true) \na = 1;\n");
"var a;\nif (true) \n a = 1;\n");
assertSource("switch(x){case 1:var y;z++}",
"switch (x) {\n case 1:\n var y;\n z++;\n}\n");
assertSource("for(var p in o)s+=o[p]",
"for (var p in o) \n s += o[p];\n");
assertSource("if(c)var a=0;else a=1",
"if (c) \nvar a = 0; else a = 1;\n");
"if (c) \n var a = 0;\nelse \n a = 1;\n");
assertSource("for(var i=0;i<10;i++)var x=i;x++;",
"for (var i = 0; i < 10; i++) \n var x = i;\nx++;\n");
assertSource("function f(){var i=2;for(var j=0;j<i;++j)print(j);}",
Expand All @@ -68,15 +68,15 @@ public void testLetDeclarationToSource()
assertSource("for(let i=0;i<10;i++)x[i]=i;a++;",
"for (let i = 0; i < 10; i++) \n x[i] = i;\na++;\n");
assertSource("let a;if(true)a=1;",
"let a;\nif (true) \na = 1;\n");
"let a;\nif (true) \n a = 1;\n");
assertSource("switch(x){case 1:let y;z++}",
"switch (x) {\n case 1:\n let y;\n z++;\n}\n");
assertSource("for(let p in o)s+=o[p]",
"for (let p in o) \n s += o[p];\n");
assertSource("if(c)let a=0;else a=1",
"if (c) \nlet a = 0; else a = 1;\n");
"if (c) \n let a = 0;\nelse \n a = 1;\n");
assertSource("for(let i=0;i<10;i++){let x=i;}x++;",
"for (let i = 0; i < 10; i++) \n {\n let x = i;\n }\nx++;\n");
"for (let i = 0; i < 10; i++) {\n let x = i;\n}\nx++;\n");
assertSource("function f(){let i=2;for(let j=0;j<i;++j)print(j);}",
"function f() {\n let i = 2;\n for (let j = 0; j < i; ++j) \n print(j);\n}\n");
}
Expand All @@ -90,10 +90,10 @@ public void testConstDeclarationToSource()
assertSource("const x=0;x++;",
"const x = 0;\nx++;\n");
assertSource("const a;if(true)a=1;",
"const a;\nif (true) \na = 1;\n");
"const a;\nif (true) \n a = 1;\n");
assertSource("switch(x){case 1:const y;z++}",
"switch (x) {\n case 1:\n const y;\n z++;\n}\n");
assertSource("if(c)const a=0;else a=1",
"if (c) \nconst a = 0; else a = 1;\n");
"if (c) \n const a = 0;\nelse \n a = 1;\n");
}
}
10 changes: 5 additions & 5 deletions testsrc/org/mozilla/javascript/tests/Bug687669Test.java
Expand Up @@ -70,13 +70,13 @@ public void testToSource() {
assertEquals("L1:\n ;\n", toSource("L1:;"));
assertEquals("L1:\n ;\na = 1;\n", toSource("L1:; a=1;"));

assertEquals("if (1) \n;\n", toSource("if(1);"));
assertEquals("if (1) \n;\na = 1;\n", toSource("if(1); a=1;"));
assertEquals("if (1) \n ;\n", toSource("if(1);"));
assertEquals("if (1) \n ;\na = 1;\n", toSource("if(1); a=1;"));

assertEquals("if (1) \na = 1;\n", toSource("if(1)a=1;"));
assertEquals("if (1) \na = 1;\na = 1;\n", toSource("if(1)a=1; a=1;"));
assertEquals("if (1) \n a = 1;\n", toSource("if(1)a=1;"));
assertEquals("if (1) \n a = 1;\na = 1;\n", toSource("if(1)a=1; a=1;"));

assertEquals("if (1) \n;\n;\n;\n;\n", toSource("if(1);;;;"));
assertEquals("if (1) \n ;\n;\n;\n;\n", toSource("if(1);;;;"));
}

}
221 changes: 221 additions & 0 deletions testsrc/org/mozilla/javascript/tests/Bug688023Test.java
@@ -0,0 +1,221 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.javascript.tests;

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mozilla.javascript.CompilerEnvirons;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.ErrorReporter;
import org.mozilla.javascript.Parser;
import org.mozilla.javascript.ast.AstRoot;

/**
* @author André Bargull
*
*/
public class Bug688023Test {
private Context cx;

@Before
public void setUp() {
cx = Context.enter();
}

@After
public void tearDown() {
Context.exit();
}

private AstRoot parse(CharSequence cs) {
CompilerEnvirons compilerEnv = new CompilerEnvirons();
compilerEnv.initFromContext(cx);
ErrorReporter compilationErrorReporter = compilerEnv.getErrorReporter();
Parser p = new Parser(compilerEnv, compilationErrorReporter);
return p.parse(cs.toString(), "<eval>", 1);
}

private String toSource(CharSequence cs) {
return parse(cs).toSource();
}

private static String lines(String... lines) {
StringBuilder sb = new StringBuilder();
for (String line : lines) {
sb.append(line).append('\n');
}
return sb.toString();
}

@Test
public void toSourceForInLoop() {
assertEquals(lines(
"for (x in y) ",
" b = 1;"
), toSource("for(x in y) b=1;"));
assertEquals(lines(
"for (x in y) {",
" b = 1;",
"}"
), toSource("for(x in y) {b=1;}"));
}

@Test
public void toSourceForLoop() {
assertEquals(lines(
"for (; ; ) ",
" b = 1;"
), toSource("for(;;) b=1;"));
assertEquals(lines(
"for (; ; ) {",
" b = 1;",
"}"
), toSource("for(;;) {b=1;}"));
}

@Test
public void toSourceWhileLoop() {
assertEquals(lines(
"while (a) ",
" b = 1;"
), toSource("while(a) b=1;"));
assertEquals(lines(
"while (a) {",
" b = 1;",
"}"
), toSource("while(a) {b=1;}"));
}

@Test
public void toSourceWithStatement() {
assertEquals(lines(
"with (a) ",
" b = 1;"
), toSource("with(a) b=1;"));
assertEquals(lines(
"with (a) {",
" b = 1;",
"}"
), toSource("with(a) {b=1;}"));
}

@Test
public void toSourceIfStatement() {
assertEquals(lines(
"if (a) ",
" b = 1;"
), toSource("if(a) b=1;"));
assertEquals(lines(
"if (a) {",
" b = 1;",
"}"
), toSource("if(a) {b=1;}"));
}

@Test
public void toSourceIfElseStatement() {
assertEquals(lines(
"if (a) ",
" b = 1;",
"else ",
" b = 2;"
), toSource("if(a) b=1; else b=2;"));
assertEquals(lines(
"if (a) {",
" b = 1;",
"} else ",
" b = 2;"
), toSource("if(a) { b=1; } else b=2;"));
assertEquals(lines(
"if (a) ",
" b = 1;",
"else {",
" b = 2;",
"}"
), toSource("if(a) b=1; else { b=2; }"));
assertEquals(lines(
"if (a) {",
" b = 1;",
"} else {",
" b = 2;",
"}"
), toSource("if(a) { b=1; } else { b=2; }"));
}

@Test
public void toSourceIfElseIfElseStatement() {
assertEquals(lines(
"if (a) ",
" b = 1;",
"else if (a) ",
" b = 2;",
"else ",
" b = 3;"
), toSource("if(a) b=1; else if (a) b=2; else b=3;"));
assertEquals(lines(
"if (a) {",
" b = 1;",
"} else if (a) ",
" b = 2;",
"else ",
" b = 3;"
), toSource("if(a) { b=1; } else if (a) b=2; else b=3;"));
assertEquals(lines(
"if (a) ",
" b = 1;",
"else if (a) {",
" b = 2;",
"} else ",
" b = 3;"
), toSource("if(a) b=1; else if (a) { b=2; } else b=3;"));
assertEquals(lines(
"if (a) {",
" b = 1;",
"} else if (a) {",
" b = 2;",
"} else ",
" b = 3;"
), toSource("if(a) { b=1; } else if (a) { b=2; } else b=3;"));
assertEquals(lines(
"if (a) ",
" b = 1;",
"else if (a) ",
" b = 2;",
"else {",
" b = 3;",
"}"
), toSource("if(a) b=1; else if (a) b=2; else {b=3;}"));
assertEquals(lines(
"if (a) {",
" b = 1;",
"} else if (a) ",
" b = 2;",
"else {",
" b = 3;",
"}"
), toSource("if(a) { b=1; } else if (a) b=2; else {b=3;}"));
assertEquals(lines(
"if (a) ",
" b = 1;",
"else if (a) {",
" b = 2;",
"} else {",
" b = 3;",
"}"
), toSource("if(a) b=1; else if (a) { b=2; } else {b=3;}"));
assertEquals(lines(
"if (a) {",
" b = 1;",
"} else if (a) {",
" b = 2;",
"} else {",
" b = 3;",
"}"
), toSource("if(a) { b=1; } else if (a) { b=2; } else {b=3;}"));
}
}

0 comments on commit 31062fd

Please sign in to comment.