Skip to content

Commit 442cf33

Browse files
author
Brian Slesinsky
committed
Reduce sourcemap size by including newlines in statements
For pretty-printed statement blocks, include the following newline within each statement range. Also added more tests. This reduces sourcemap size by about 20% uncompressed and 40% compressed. (Combined with [1], the total is 76% reduction uncompressed and 72% compressed.) [1] https://gwt-review.googlesource.com/#/c/7174/ Change-Id: I71651e83b8eee2355190b7b5034d7c9643a13781
1 parent 1460a04 commit 442cf33

File tree

2 files changed

+75
-8
lines changed

2 files changed

+75
-8
lines changed

dev/core/src/com/google/gwt/dev/js/JsToStringGenerationVisitor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,7 @@ protected void printJsBlock(JsBlock x, boolean truncate, boolean finalNewline) {
935935
_semi();
936936
}
937937
_newlineOpt();
938+
billChildToHere();
938939
}
939940
}
940941
if (shouldRecordPositions) {

dev/core/test/com/google/gwt/dev/js/JsReportGenerationVisitorTest.java

Lines changed: 74 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import com.google.gwt.dev.js.ast.JsProgram;
2424
import com.google.gwt.dev.js.ast.JsStatement;
2525
import com.google.gwt.dev.util.DefaultTextOutput;
26-
import com.google.gwt.thirdparty.guava.common.base.Joiner;
2726
import com.google.gwt.thirdparty.guava.common.collect.Lists;
2827

2928
import junit.framework.TestCase;
@@ -38,6 +37,7 @@
3837
* JavaScript. (Doesn't check that they point to the right place.)
3938
*/
4039
public class JsReportGenerationVisitorTest extends TestCase {
40+
boolean compact = false;
4141
JsProgram program;
4242

4343
// TODO(skybrian) don't generate ranges for all of these nodes. Just do executable code.
@@ -51,13 +51,14 @@ public void testEmpty() throws Exception {
5151
public void testOneStatement() throws Exception {
5252
program = parseJs("x = 1");
5353
checkMappings(
54-
"x=1;",
54+
"x = 1;\n",
5555
"x",
5656
"1"
5757
);
5858
}
5959

60-
public void testTwoStatements() throws Exception {
60+
public void testTwoStatementsCompact() throws Exception {
61+
compact = true;
6162
program = parseJs("x = 1; y = 2");
6263
checkMappings(
6364
"x=1;y=2;",
@@ -70,7 +71,21 @@ public void testTwoStatements() throws Exception {
7071
);
7172
}
7273

73-
public void testIfStatement() throws Exception {
74+
public void testTwoStatementsPretty() throws Exception {
75+
program = parseJs("x = 1; y = 2");
76+
checkMappings(
77+
"x = 1;\ny = 2;\n",
78+
"x = 1;\n",
79+
"x",
80+
"1",
81+
"y = 2;\n",
82+
"y",
83+
"2"
84+
);
85+
}
86+
87+
public void testIfStatementCompact() throws Exception {
88+
compact = true;
7489
program = parseJs("if(true) { x=1 } else { y=2 }");
7590
checkMappings(
7691
"if(true){x=1}else{y=2}",
@@ -86,6 +101,44 @@ public void testIfStatement() throws Exception {
86101
);
87102
}
88103

104+
public void testIfStatementPretty() throws Exception {
105+
program = parseJs("if(true) { x=1 } else { y=2 }");
106+
checkMappings(
107+
"if (true) {\n x = 1;\n}\n else {\n y = 2;\n}\n",
108+
"true",
109+
"{\n x = 1;\n}\n",
110+
" x = 1;\n",
111+
" x",
112+
"1",
113+
"{\n y = 2;\n}\n",
114+
" y = 2;\n",
115+
" y",
116+
"2"
117+
);
118+
}
119+
120+
public void testFunctionCompact() throws Exception {
121+
compact = true;
122+
program = parseJs("function f() { return 42; }");
123+
checkMappings("function f(){return 42}\n",
124+
"function f(){return 42}",
125+
"{return 42}",
126+
"return 42",
127+
"42"
128+
);
129+
}
130+
131+
public void testFunctionPretty() throws Exception {
132+
program = parseJs("function f() { return 42; }");
133+
checkMappings(
134+
"function f(){\n return 42;\n}\n\n",
135+
"function f(){\n return 42;\n}\n",
136+
"{\n return 42;\n}\n",
137+
" return 42;\n",
138+
"42"
139+
);
140+
}
141+
89142
private JsProgram parseJs(String js) throws IOException, JsParserException {
90143
JsProgram program = new JsProgram();
91144
SourceInfo info = SourceOrigin.create(0, js.length(), 123, "test.js");
@@ -97,14 +150,20 @@ private JsProgram parseJs(String js) throws IOException, JsParserException {
97150

98151
private void checkMappings(String ...expectedLines)
99152
throws IOException, JsParserException {
100-
DefaultTextOutput text = new DefaultTextOutput(true);
153+
DefaultTextOutput text = new DefaultTextOutput(compact);
101154
JsReportGenerationVisitor generator = new JsReportGenerationVisitor(text,
102155
JavaToJavaScriptMap.EMPTY);
103156
generator.accept(program);
104157
String actual = dumpMappings(text.toString(), generator.getSourceInfoMap());
105158

106-
String expected = expectedLines.length == 0 ? "" : Joiner.on("\n").join(expectedLines) + "\n";
107-
assertEquals("Mappings:\n" + expected, actual);
159+
StringBuilder expected = new StringBuilder();
160+
expected.append("Mappings:\n");
161+
for (String line : expectedLines) {
162+
expected.append(escape(line));
163+
expected.append("\n");
164+
}
165+
166+
assertEquals(expected.toString(), actual);
108167
}
109168

110169
private String dumpMappings(String javascript, JsSourceMap mappings) {
@@ -115,9 +174,16 @@ private String dumpMappings(String javascript, JsSourceMap mappings) {
115174
out.append("Mappings:\n");
116175
for (Range r : ranges) {
117176
String js = javascript.substring(r.getStart(), r.getEnd());
118-
out.append(js);
177+
out.append(escape(js));
119178
out.append("\n");
120179
}
121180
return out.toString();
122181
}
182+
183+
/**
184+
* Escape newlines in a readable way so that each range is on one line for comparison.
185+
*/
186+
private String escape(String js) {
187+
return js.replace("\\n", "\\\\n").replace("\n", "\\n");
188+
}
123189
}

0 commit comments

Comments
 (0)