Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(421)

Unified Diff: lib/src/js/printer.dart

Issue 1524843002: JS: Format if statements with no else on a single line (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: rebased Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « lib/src/codegen/js_printer.dart ('k') | test/codegen/expect/DeltaBlue.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/js/printer.dart
diff --git a/lib/src/js/printer.dart b/lib/src/js/printer.dart
index 155ce854febd115c28cde134099c0378b465deba..e0e2c14c40219ca94d5000c450d11facf2d31c51 100644
--- a/lib/src/js/printer.dart
+++ b/lib/src/js/printer.dart
@@ -9,6 +9,7 @@ class JavaScriptPrintingOptions {
final bool shouldCompressOutput;
final bool minifyLocalVariables;
final bool preferSemicolonToNewlineInMinifiedOutput;
+ final bool allowSingleLineIfStatements;
/// True to allow keywords in properties, such as `obj.var` or `obj.function`
/// Modern JS engines support this.
@@ -22,6 +23,7 @@ class JavaScriptPrintingOptions {
this.minifyLocalVariables: false,
this.preferSemicolonToNewlineInMinifiedOutput: false,
this.allowKeywordsInProperties: false,
+ this.allowSingleLineIfStatements: false,
this.arrowFnBindThisWorkaround: false});
}
@@ -71,6 +73,8 @@ class Printer implements NodeVisitor {
int _indentLevel = 0;
// A cache of all indentation strings used so far.
List<String> _indentList = <String>[""];
+ /// Whether the next call to [indent] should just be a no-op.
+ bool _skipNextIndent = false;
static final identifierCharacterRegExp = new RegExp(r'^[a-zA-Z_0-9$]');
static final expressionContinuationRegExp = new RegExp(r'^[-+([]');
@@ -181,7 +185,16 @@ class Printer implements NodeVisitor {
void outIndent(String str) { indent(); out(str); }
void outIndentLn(String str) { indent(); outLn(str); }
+
+ void skipNextIndent() {
+ _skipNextIndent = true;
+ }
+
void indent() {
+ if (_skipNextIndent) {
+ _skipNextIndent = false;
+ return;
+ }
if (!shouldCompressOutput) {
out(indentation);
}
@@ -299,8 +312,16 @@ class Printer implements NodeVisitor {
visitNestedExpression(node.condition, EXPRESSION,
newInForInit: false, newAtStatementBegin: false);
out(")");
- bool thenWasBlock =
- blockBody(then, needsSeparation: false, needsNewline: !hasElse);
+ bool thenWasBlock;
+ if (options.allowSingleLineIfStatements && !hasElse && then is! Block) {
+ thenWasBlock = false;
+ spaceOut();
+ skipNextIndent();
+ visit(then);
+ } else {
+ thenWasBlock =
+ blockBody(then, needsSeparation: false, needsNewline: !hasElse);
+ }
if (hasElse) {
if (thenWasBlock) {
spaceOut();
« no previous file with comments | « lib/src/codegen/js_printer.dart ('k') | test/codegen/expect/DeltaBlue.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698