| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 part of js_ast; | 5 part of js_ast; |
| 6 | 6 |
| 7 | 7 |
| 8 class JavaScriptPrintingOptions { | 8 class JavaScriptPrintingOptions { |
| 9 final bool shouldCompressOutput; | 9 final bool shouldCompressOutput; |
| 10 final bool minifyLocalVariables; | 10 final bool minifyLocalVariables; |
| (...skipping 342 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 353 blockBody(then, needsSeparation: false, needsNewline: !hasElse); | 353 blockBody(then, needsSeparation: false, needsNewline: !hasElse); |
| 354 if (hasElse) { | 354 if (hasElse) { |
| 355 if (thenWasBlock) { | 355 if (thenWasBlock) { |
| 356 spaceOut(); | 356 spaceOut(); |
| 357 } else { | 357 } else { |
| 358 indent(); | 358 indent(); |
| 359 } | 359 } |
| 360 out("else"); | 360 out("else"); |
| 361 if (elsePart is If) { | 361 if (elsePart is If) { |
| 362 pendingSpace = true; | 362 pendingSpace = true; |
| 363 startNode(elsePart); |
| 363 ifOut(elsePart, false); | 364 ifOut(elsePart, false); |
| 365 endNode(elsePart); |
| 364 } else { | 366 } else { |
| 365 blockBody(unwrapBlockIfSingleStatement(elsePart), | 367 blockBody(unwrapBlockIfSingleStatement(elsePart), |
| 366 needsSeparation: true, needsNewline: true); | 368 needsSeparation: true, needsNewline: true); |
| 367 } | 369 } |
| 368 } | 370 } |
| 369 } | 371 } |
| 370 | 372 |
| 371 @override | 373 @override |
| 372 void visitIf(If node) { | 374 void visitIf(If node) { |
| 373 ifOut(node, true); | 375 ifOut(node, true); |
| (...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 621 localNamer.leaveScope(); | 623 localNamer.leaveScope(); |
| 622 return closingPosition; | 624 return closingPosition; |
| 623 | 625 |
| 624 } | 626 } |
| 625 | 627 |
| 626 @override | 628 @override |
| 627 visitFunctionDeclaration(FunctionDeclaration declaration) { | 629 visitFunctionDeclaration(FunctionDeclaration declaration) { |
| 628 VarCollector vars = new VarCollector(); | 630 VarCollector vars = new VarCollector(); |
| 629 vars.visitFunctionDeclaration(declaration); | 631 vars.visitFunctionDeclaration(declaration); |
| 630 indent(); | 632 indent(); |
| 631 functionOut(declaration.function, declaration.name, vars); | 633 startNode(declaration.function); |
| 634 currentNode.closingPosition = |
| 635 functionOut(declaration.function, declaration.name, vars); |
| 636 endNode(declaration.function); |
| 632 lineOut(); | 637 lineOut(); |
| 633 } | 638 } |
| 634 | 639 |
| 635 visitNestedExpression(Expression node, int requiredPrecedence, | 640 visitNestedExpression(Expression node, int requiredPrecedence, |
| 636 {bool newInForInit, bool newAtStatementBegin}) { | 641 {bool newInForInit, bool newAtStatementBegin}) { |
| 637 bool needsParentheses = | 642 bool needsParentheses = |
| 638 // a - (b + c). | 643 // a - (b + c). |
| 639 (requiredPrecedence != EXPRESSION && | 644 (requiredPrecedence != EXPRESSION && |
| 640 node.precedenceLevel < requiredPrecedence) || | 645 node.precedenceLevel < requiredPrecedence) || |
| 641 // for (a = (x in o); ... ; ... ) { ... } | 646 // for (a = (x in o); ... ; ... ) { ... } |
| (...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 917 visitNestedExpression(access.receiver, CALL, | 922 visitNestedExpression(access.receiver, CALL, |
| 918 newInForInit: inForInit, | 923 newInForInit: inForInit, |
| 919 newAtStatementBegin: atStatementBegin); | 924 newAtStatementBegin: atStatementBegin); |
| 920 Node selector = access.selector; | 925 Node selector = access.selector; |
| 921 if (selector is LiteralString) { | 926 if (selector is LiteralString) { |
| 922 LiteralString selectorString = selector; | 927 LiteralString selectorString = selector; |
| 923 String fieldWithQuotes = selectorString.value; | 928 String fieldWithQuotes = selectorString.value; |
| 924 if (isValidJavaScriptId(fieldWithQuotes)) { | 929 if (isValidJavaScriptId(fieldWithQuotes)) { |
| 925 if (access.receiver is LiteralNumber) out(" ", isWhitespace: true); | 930 if (access.receiver is LiteralNumber) out(" ", isWhitespace: true); |
| 926 out("."); | 931 out("."); |
| 932 startNode(selector); |
| 927 out(fieldWithQuotes.substring(1, fieldWithQuotes.length - 1)); | 933 out(fieldWithQuotes.substring(1, fieldWithQuotes.length - 1)); |
| 934 endNode(selector); |
| 928 return; | 935 return; |
| 929 } | 936 } |
| 930 } | 937 } |
| 931 out("["); | 938 out("["); |
| 932 visitNestedExpression(selector, EXPRESSION, | 939 visitNestedExpression(selector, EXPRESSION, |
| 933 newInForInit: false, newAtStatementBegin: false); | 940 newInForInit: false, newAtStatementBegin: false); |
| 934 out("]"); | 941 out("]"); |
| 935 } | 942 } |
| 936 | 943 |
| 937 @override | 944 @override |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1005 void visitArrayInitializer(ArrayInitializer node) { | 1012 void visitArrayInitializer(ArrayInitializer node) { |
| 1006 out("["); | 1013 out("["); |
| 1007 List<Expression> elements = node.elements; | 1014 List<Expression> elements = node.elements; |
| 1008 for (int i = 0; i < elements.length; i++) { | 1015 for (int i = 0; i < elements.length; i++) { |
| 1009 Expression element = elements[i]; | 1016 Expression element = elements[i]; |
| 1010 if (element is ArrayHole) { | 1017 if (element is ArrayHole) { |
| 1011 // Note that array holes must have a trailing "," even if they are | 1018 // Note that array holes must have a trailing "," even if they are |
| 1012 // in last position. Otherwise `[,]` (having length 1) would become | 1019 // in last position. Otherwise `[,]` (having length 1) would become |
| 1013 // equal to `[]` (the empty array) | 1020 // equal to `[]` (the empty array) |
| 1014 // and [1,,] (array with 1 and a hole) would become [1,] = [1]. | 1021 // and [1,,] (array with 1 and a hole) would become [1,] = [1]. |
| 1022 startNode(element); |
| 1015 out(","); | 1023 out(","); |
| 1024 endNode(element); |
| 1016 continue; | 1025 continue; |
| 1017 } | 1026 } |
| 1018 if (i != 0) spaceOut(); | 1027 if (i != 0) spaceOut(); |
| 1019 visitNestedExpression(element, ASSIGNMENT, | 1028 visitNestedExpression(element, ASSIGNMENT, |
| 1020 newInForInit: false, newAtStatementBegin: false); | 1029 newInForInit: false, newAtStatementBegin: false); |
| 1021 // We can skip the trailing "," for the last element (since it's not | 1030 // We can skip the trailing "," for the last element (since it's not |
| 1022 // an array hole). | 1031 // an array hole). |
| 1023 if (i != elements.length - 1) out(","); | 1032 if (i != elements.length - 1) out(","); |
| 1024 } | 1033 } |
| 1025 out("]"); | 1034 out("]"); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 1052 indentLess(); | 1061 indentLess(); |
| 1053 if (!node.isOneLiner && !properties.isEmpty) { | 1062 if (!node.isOneLiner && !properties.isEmpty) { |
| 1054 lineOut(); | 1063 lineOut(); |
| 1055 indent(); | 1064 indent(); |
| 1056 } | 1065 } |
| 1057 out("}"); | 1066 out("}"); |
| 1058 } | 1067 } |
| 1059 | 1068 |
| 1060 @override | 1069 @override |
| 1061 void visitProperty(Property node) { | 1070 void visitProperty(Property node) { |
| 1071 startNode(node.name); |
| 1062 if (node.name is LiteralString) { | 1072 if (node.name is LiteralString) { |
| 1063 LiteralString nameString = node.name; | 1073 LiteralString nameString = node.name; |
| 1064 String name = nameString.value; | 1074 String name = nameString.value; |
| 1065 if (isValidJavaScriptId(name)) { | 1075 if (isValidJavaScriptId(name)) { |
| 1066 out(name.substring(1, name.length - 1)); | 1076 out(name.substring(1, name.length - 1)); |
| 1067 } else { | 1077 } else { |
| 1068 out(name); | 1078 out(name); |
| 1069 } | 1079 } |
| 1070 } else { | 1080 } else { |
| 1071 assert(node.name is LiteralNumber); | 1081 assert(node.name is LiteralNumber); |
| 1072 LiteralNumber nameNumber = node.name; | 1082 LiteralNumber nameNumber = node.name; |
| 1073 out(nameNumber.value); | 1083 out(nameNumber.value); |
| 1074 } | 1084 } |
| 1085 endNode(node.name); |
| 1075 out(":"); | 1086 out(":"); |
| 1076 spaceOut(); | 1087 spaceOut(); |
| 1077 visitNestedExpression(node.value, ASSIGNMENT, | 1088 visitNestedExpression(node.value, ASSIGNMENT, |
| 1078 newInForInit: false, newAtStatementBegin: false); | 1089 newInForInit: false, newAtStatementBegin: false); |
| 1079 } | 1090 } |
| 1080 | 1091 |
| 1081 @override | 1092 @override |
| 1082 void visitRegExpLiteral(RegExpLiteral node) { | 1093 void visitRegExpLiteral(RegExpLiteral node) { |
| 1083 out(node.pattern); | 1094 out(node.pattern); |
| 1084 } | 1095 } |
| (...skipping 365 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1450 } | 1461 } |
| 1451 } | 1462 } |
| 1452 | 1463 |
| 1453 EnterExitNode exitNode(JavaScriptPrintingContext context, int position) { | 1464 EnterExitNode exitNode(JavaScriptPrintingContext context, int position) { |
| 1454 // Enter must happen before exit. | 1465 // Enter must happen before exit. |
| 1455 addToNode(context, position); | 1466 addToNode(context, position); |
| 1456 context.exitNode(node, startPosition, position, closingPosition); | 1467 context.exitNode(node, startPosition, position, closingPosition); |
| 1457 return parent; | 1468 return parent; |
| 1458 } | 1469 } |
| 1459 } | 1470 } |
| OLD | NEW |