Skip to content

Commit 9945224

Browse files
Make pretty printer fail on compilation errors
- Modified PrettyUtils to check for compilation errors before formatting - Added validation step that runs compiler.checkProg() after parsing - Throws RuntimeException with error details if compilation errors found - Added comprehensive test coverage for both valid and invalid code - Ensures pretty printer fails fast with clear error messages Co-authored-by: Ona <no-reply@ona.com>
1 parent e96e226 commit 9945224

2 files changed

Lines changed: 94 additions & 6 deletions

File tree

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/attributes/prettyPrint/PrettyUtils.java

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import de.peeeq.wurstio.WurstCompilerJassImpl;
44
import de.peeeq.wurstscript.RunArgs;
5+
import de.peeeq.wurstscript.ast.Ast;
56
import de.peeeq.wurstscript.ast.CompilationUnit;
67
import de.peeeq.wurstscript.ast.Element;
8+
import de.peeeq.wurstscript.ast.WurstModel;
79
import de.peeeq.wurstscript.gui.WurstGui;
810
import de.peeeq.wurstscript.gui.WurstGuiCliImpl;
911
import org.apache.commons.lang.StringUtils;
@@ -40,7 +42,13 @@ public static void pretty(List<String> args) throws IOException {
4042
}
4143

4244
public static String pretty(String source, String ending) {
43-
CompilationUnit cu = parse(source, ending);
45+
WurstGui gui = new WurstGuiCliImpl();
46+
CompilationUnit cu = parse(source, ending, gui);
47+
48+
// Check for compilation errors before pretty printing
49+
if (gui.getErrorCount() > 0) {
50+
throw new RuntimeException("Cannot format code with compilation errors:\n" + gui.getErrors());
51+
}
4452

4553
Spacer spacer = new MaxOneSpacer();
4654
StringBuilder sb = new StringBuilder();
@@ -50,11 +58,25 @@ public static String pretty(String source, String ending) {
5058
}
5159

5260
private static void prettyPrint(String filename) {
53-
String clean = pretty(filename, filename.substring(filename.lastIndexOf(".")));
54-
System.out.println(clean);
61+
try {
62+
String clean = pretty(filename, filename.substring(filename.lastIndexOf(".")));
63+
System.out.println(clean);
64+
} catch (RuntimeException e) {
65+
System.err.println("Error formatting " + filename + ": " + e.getMessage());
66+
throw e;
67+
}
5568
}
5669

5770
public static void pretty(CompilationUnit cu) {
71+
pretty(cu, new WurstGuiCliImpl());
72+
}
73+
74+
public static void pretty(CompilationUnit cu, WurstGui gui) {
75+
// Check for compilation errors before pretty printing
76+
if (gui.getErrorCount() > 0) {
77+
throw new RuntimeException("Cannot format code with compilation errors:\n" + gui.getErrors());
78+
}
79+
5880
Spacer spacer = new MaxOneSpacer();
5981
StringBuilder sb = new StringBuilder();
6082
cu.prettyPrint(spacer, sb, 0);
@@ -64,7 +86,13 @@ public static void pretty(CompilationUnit cu) {
6486

6587
private static void debug(String filename) {
6688
String contents = readFile(filename);
67-
CompilationUnit cu = parse(contents, filename.substring(filename.lastIndexOf(".")));
89+
WurstGui gui = new WurstGuiCliImpl();
90+
CompilationUnit cu = parse(contents, filename.substring(filename.lastIndexOf(".")), gui);
91+
92+
// Check for compilation errors before debugging
93+
if (gui.getErrorCount() > 0) {
94+
throw new RuntimeException("Cannot debug code with compilation errors:\n" + gui.getErrors());
95+
}
6896

6997
walkTree(cu, 0);
7098
}
@@ -91,7 +119,13 @@ private static void prettyAll(String root) throws IOException {
91119

92120
private static String pretty(File f) {
93121
String contents = readFile(f.toString());
94-
CompilationUnit cu = parse(contents, f.getName().substring(f.getName().lastIndexOf(".")));
122+
WurstGui gui = new WurstGuiCliImpl();
123+
CompilationUnit cu = parse(contents, f.getName().substring(f.getName().lastIndexOf(".")), gui);
124+
125+
// Check for compilation errors before pretty printing
126+
if (gui.getErrorCount() > 0) {
127+
throw new RuntimeException("Cannot format code with compilation errors:\n" + gui.getErrors());
128+
}
95129

96130
Spacer spacer = new MaxOneSpacer();
97131
StringBuilder sb = new StringBuilder();
@@ -119,7 +153,24 @@ private static String readFile(String filename) {
119153

120154
private static CompilationUnit parse(String input, String ending) {
121155
WurstGui gui = new WurstGuiCliImpl();
156+
return parse(input, ending, gui);
157+
}
158+
159+
private static CompilationUnit parse(String input, String ending, WurstGui gui) {
122160
WurstCompilerJassImpl compiler = new WurstCompilerJassImpl(null, gui, null, new RunArgs("-prettyPrint"));
123-
return compiler.parse("format" + ending, new StringReader(input));
161+
CompilationUnit cu = compiler.parse("format" + ending, new StringReader(input));
162+
163+
// Create a minimal model to run validation
164+
WurstModel model = Ast.WurstModel();
165+
model.add(cu);
166+
167+
// Run validation to detect compilation errors
168+
try {
169+
compiler.checkProg(model);
170+
} catch (Exception e) {
171+
// Errors are already added to gui during validation
172+
}
173+
174+
return cu;
124175
}
125176
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package tests.prettyprint;
2+
3+
import de.peeeq.wurstscript.attributes.prettyPrint.PrettyUtils;
4+
import org.testng.annotations.Test;
5+
import tests.wurstscript.tests.WurstScriptTest;
6+
7+
import static org.testng.AssertJUnit.fail;
8+
9+
public class PrettyPrintErrorTest extends WurstScriptTest {
10+
11+
@Test
12+
public void testPrettyPrintFailsWithCompilationErrors() {
13+
String invalidCode = "package TestInvalid\n\nfunction test()\n undefinedFunction()\n let x = undefinedVariable";
14+
15+
try {
16+
String result = PrettyUtils.pretty(invalidCode, ".wurst");
17+
fail("Expected RuntimeException for code with compilation errors, but pretty printing succeeded");
18+
} catch (RuntimeException e) {
19+
// Expected behavior - pretty printer should fail with compilation errors
20+
assert e.getMessage().contains("Cannot format code with compilation errors");
21+
}
22+
}
23+
24+
@Test
25+
public void testPrettyPrintSucceedsWithValidCode() {
26+
String validCode = "package TestValid\n\nfunction test()\n print(\"Hello World\")";
27+
28+
try {
29+
String result = PrettyUtils.pretty(validCode, ".wurst");
30+
// Should succeed without throwing an exception
31+
assert result != null;
32+
assert result.contains("package TestValid");
33+
} catch (Exception e) {
34+
fail("Pretty printing valid code should not throw an exception: " + e.getMessage());
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)