Skip to content

Commit 6d34796

Browse files
rakudramaCommit Queue
authored andcommitted
[dart2js] Minify statement labels.
Also simplify use of JavaScriptPrintingOptions. Change-Id: Icdb603edd76b71dbc4a5d913407b96e5e9589265 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/452223 Reviewed-by: Mayank Patke <[email protected]> Commit-Queue: Stephen Adams <[email protected]>
1 parent 2482ee2 commit 6d34796

File tree

7 files changed

+73
-32
lines changed

7 files changed

+73
-32
lines changed

pkg/compiler/lib/src/js/js.dart

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,11 @@ export 'js_debug.dart';
2525
String prettyPrint(
2626
Node node, {
2727
bool enableMinification = false,
28-
bool allowVariableMinification = true,
2928
bool preferSemicolonToNewlineInMinifiedOutput = false,
3029
}) {
3130
// TODO(johnniwinther): Do we need all the options here?
3231
JavaScriptPrintingOptions options = JavaScriptPrintingOptions(
33-
shouldCompressOutput: enableMinification,
34-
minifyLocalVariables: allowVariableMinification,
32+
minify: enableMinification,
3533
preferSemicolonToNewlineInMinifiedOutput:
3634
preferSemicolonToNewlineInMinifiedOutput,
3735
);
@@ -48,13 +46,12 @@ CodeBuffer createCodeBuffer(
4846
DumpInfoJsAstRegistry? monitor,
4947
JavaScriptAnnotationMonitor annotationMonitor =
5048
const JavaScriptAnnotationMonitor(),
51-
bool allowVariableMinification = true,
5249
List<CodeOutputListener> listeners = const [],
5350
}) {
51+
bool enableMinification = compilerOptions.enableMinification;
5452
JavaScriptPrintingOptions options = JavaScriptPrintingOptions(
5553
utf8: compilerOptions.features.writeUtf8.isEnabled,
56-
shouldCompressOutput: compilerOptions.enableMinification,
57-
minifyLocalVariables: allowVariableMinification,
54+
minify: enableMinification,
5855
);
5956
CodeBuffer outBuffer = CodeBuffer(listeners);
6057
SourceInformationProcessor sourceInformationProcessor =

pkg/compiler/lib/src/js/js_debug.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ import 'package:kernel/text/indentation.dart' show Indentation, Tagging;
1212
/// Unparse the JavaScript [node].
1313
String nodeToString(Node node, {bool pretty = false}) {
1414
JavaScriptPrintingOptions options = JavaScriptPrintingOptions(
15-
shouldCompressOutput: !pretty,
16-
preferSemicolonToNewlineInMinifiedOutput: !pretty,
15+
minify: !pretty,
1716
);
1817
LenientPrintingContext printingContext = LenientPrintingContext();
1918
Printer(options, printingContext).visit(node);

pkg/compiler/test/js/js_parser_statements_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import 'package:compiler/src/js/js.dart' show js;
99

1010
testStatement(String statement, arguments, String expect) {
1111
jsAst.Node node = js.statement(statement, arguments);
12-
String jsText = jsAst.prettyPrint(node, allowVariableMinification: false);
12+
String jsText = jsAst.prettyPrint(node);
1313
Expect.stringEquals(
1414
expect.trim(),
1515
jsText.trim(),

pkg/compiler/test/js/js_parser_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import 'package:compiler/src/js/js.dart' show js;
88

99
testExpression(String expression, [String expect = ""]) {
1010
jsAst.Node node = js(expression);
11-
String jsText = jsAst.prettyPrint(node, allowVariableMinification: false);
11+
String jsText = jsAst.prettyPrint(node);
1212
if (expect == "") {
1313
Expect.stringEquals(expression, jsText);
1414
} else {

pkg/compiler/test/sourcemaps/helpers/sourcemap_helper.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@ class CodePointComputer extends TraceListener {
696696

697697
String nodeToString(js.Node node) {
698698
js.JavaScriptPrintingOptions options = js.JavaScriptPrintingOptions(
699-
shouldCompressOutput: true,
699+
minify: true,
700700
preferSemicolonToNewlineInMinifiedOutput: true,
701701
);
702702
LenientPrintingContext printingContext = LenientPrintingContext();

pkg/js_ast/lib/src/printer.dart

Lines changed: 65 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@ class JavaScriptPrintingOptions {
1111
final bool utf8;
1212
final bool shouldCompressOutput;
1313
final bool minifyLocalVariables;
14+
final bool minifyStatementLabels;
1415
final bool preferSemicolonToNewlineInMinifiedOutput;
1516

1617
const JavaScriptPrintingOptions({
1718
this.utf8 = false,
18-
this.shouldCompressOutput = false,
19-
this.minifyLocalVariables = false,
19+
bool minify = false,
2020
this.preferSemicolonToNewlineInMinifiedOutput = false,
21-
});
21+
}) : shouldCompressOutput = minify,
22+
minifyLocalVariables = minify,
23+
minifyStatementLabels = minify;
2224
}
2325

2426
/// An environment in which JavaScript printing is done. Provides emitting of
@@ -89,6 +91,7 @@ class Printer implements NodeVisitor<void> {
8991
final bool shouldCompressOutput;
9092
final DanglingElseVisitor danglingElseVisitor;
9193
final LocalNamer localNamer;
94+
final _LabelNamer _labelNamer;
9295
final bool isDebugContext;
9396

9497
int _charCount = 0;
@@ -140,10 +143,10 @@ class Printer implements NodeVisitor<void> {
140143
: isDebugContext = context.isDebugContext,
141144
shouldCompressOutput = options.shouldCompressOutput,
142145
danglingElseVisitor = DanglingElseVisitor(context),
143-
localNamer = determineRenamer(
144-
options.shouldCompressOutput,
145-
options.minifyLocalVariables,
146-
);
146+
localNamer = options.minifyLocalVariables
147+
? MinifyRenamer()
148+
: IdentityNamer(),
149+
_labelNamer = _LabelNamer(options.minifyStatementLabels);
147150

148151
static LocalNamer determineRenamer(
149152
bool shouldCompressOutput,
@@ -577,7 +580,7 @@ class Printer implements NodeVisitor<void> {
577580
if (node.targetLabel == null) {
578581
outIndent('continue');
579582
} else {
580-
outIndent('continue ${node.targetLabel}');
583+
outIndent('continue ${_labelNamer.mapLabelName(node.targetLabel!)}');
581584
}
582585
outSemicolonLn();
583586
}
@@ -587,7 +590,7 @@ class Printer implements NodeVisitor<void> {
587590
if (node.targetLabel == null) {
588591
outIndent('break');
589592
} else {
590-
outIndent('break ${node.targetLabel}');
593+
outIndent('break ${_labelNamer.mapLabelName(node.targetLabel!)}');
591594
}
592595
outSemicolonLn();
593596
}
@@ -724,7 +727,7 @@ class Printer implements NodeVisitor<void> {
724727

725728
@override
726729
void visitLabeledStatement(LabeledStatement node) {
727-
outIndent('${node.label}:');
730+
outIndent('${_labelNamer.mapLabelName(node.label)}:');
728731
blockBody(node.body, needsSeparation: false, needsNewline: true);
729732
}
730733

@@ -740,7 +743,7 @@ class Printer implements NodeVisitor<void> {
740743
newAtStatementBegin: false,
741744
);
742745
}
743-
localNamer.enterScope(vars);
746+
_enterFunctionScope(vars);
744747
out('(');
745748
visitCommaSeparated(
746749
fun.params,
@@ -771,10 +774,20 @@ class Printer implements NodeVisitor<void> {
771774
shouldIndent: false,
772775
needsNewline: false,
773776
);
774-
localNamer.leaveScope();
777+
_exitFunctionScope();
775778
return closingPosition;
776779
}
777780

781+
void _enterFunctionScope(VarCollector vars) {
782+
localNamer.enterScope(vars);
783+
_labelNamer.enterFunction();
784+
}
785+
786+
void _exitFunctionScope() {
787+
_labelNamer.exitFunction();
788+
localNamer.leaveScope();
789+
}
790+
778791
@override
779792
void visitFunctionDeclaration(FunctionDeclaration declaration) {
780793
VarCollector vars = VarCollector();
@@ -1393,7 +1406,7 @@ class Printer implements NodeVisitor<void> {
13931406

13941407
int arrowFunctionOut(ArrowFunction fun, VarCollector vars) {
13951408
// TODO: support static, get/set, async, and generators.
1396-
localNamer.enterScope(vars);
1409+
_enterFunctionScope(vars);
13971410
final List<Parameter> params = fun.params;
13981411
if (params.length == 1 && _isIdentifierParameter(params.first)) {
13991412
visitNestedExpression(
@@ -1438,7 +1451,7 @@ class Printer implements NodeVisitor<void> {
14381451
if (needsParens) out(')');
14391452
closingPosition = _charCount;
14401453
}
1441-
localNamer.leaveScope();
1454+
_exitFunctionScope();
14421455
return closingPosition;
14431456
}
14441457

@@ -1637,7 +1650,7 @@ class Printer implements NodeVisitor<void> {
16371650
int methodOut(MethodDefinition node, VarCollector vars) {
16381651
// TODO: support static, get/set, async, and generators.
16391652
Fun fun = node.function;
1640-
localNamer.enterScope(vars);
1653+
_enterFunctionScope(vars);
16411654
out('(');
16421655
visitCommaSeparated(
16431656
fun.params,
@@ -1652,7 +1665,7 @@ class Printer implements NodeVisitor<void> {
16521665
shouldIndent: false,
16531666
needsNewline: false,
16541667
);
1655-
localNamer.leaveScope();
1668+
_exitFunctionScope();
16561669
return closingPosition;
16571670
}
16581671

@@ -2139,6 +2152,42 @@ class MinifyRenamer implements LocalNamer {
21392152
}
21402153
}
21412154

2155+
class _LabelNamer {
2156+
final bool renameLabels;
2157+
2158+
Map<String, String> _renamings = {};
2159+
2160+
final List<Map<String, String>> _outerScopes = [];
2161+
2162+
_LabelNamer(this.renameLabels);
2163+
2164+
String mapLabelName(String name) {
2165+
if (!renameLabels) return name;
2166+
return _renamings[name] ??= _newLabelName(_renamings, name);
2167+
}
2168+
2169+
static String _newLabelName(Map<String, String> renamings, String name) {
2170+
assert(!renamings.containsKey(name));
2171+
int index = renamings.length;
2172+
if (index < 26) return String.fromCharCode(index + 'A'.codeUnitAt(0));
2173+
index -= 26;
2174+
if (index < 26) return String.fromCharCode(index + 'a'.codeUnitAt(0));
2175+
index -= 26;
2176+
return 'L$index';
2177+
}
2178+
2179+
void enterFunction() {
2180+
if (!renameLabels) return;
2181+
_outerScopes.add(_renamings);
2182+
_renamings = {};
2183+
}
2184+
2185+
void exitFunction() {
2186+
if (!renameLabels) return;
2187+
_renamings = _outerScopes.removeLast();
2188+
}
2189+
}
2190+
21422191
/// Information pertaining the enter and exit callbacks for [node].
21432192
class EnterExitNode {
21442193
final EnterExitNode? parent;

pkg/js_ast/test/print_helper.dart

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,7 @@ Node _test(
5656
}
5757

5858
String prettyPrint(Node node) {
59-
JavaScriptPrintingOptions options = JavaScriptPrintingOptions(
60-
shouldCompressOutput: false,
61-
minifyLocalVariables: false,
62-
preferSemicolonToNewlineInMinifiedOutput: false,
63-
);
59+
JavaScriptPrintingOptions options = JavaScriptPrintingOptions();
6460
SimpleJavaScriptPrintingContext context = SimpleJavaScriptPrintingContext();
6561
Printer printer = Printer(options, context);
6662
printer.visit(node);

0 commit comments

Comments
 (0)