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 } else if (selector is Name) { | 937 } else if (selector is Name) { |
931 if (access.receiver is LiteralNumber) out(" ", isWhitespace: true); | 938 if (access.receiver is LiteralNumber) out(" ", isWhitespace: true); |
932 out("."); | 939 out("."); |
| 940 startNode(selector); |
933 out(selector.name); | 941 out(selector.name); |
| 942 endNode(selector); |
934 return; | 943 return; |
935 } | 944 } |
936 out("["); | 945 out("["); |
937 visitNestedExpression(selector, EXPRESSION, | 946 visitNestedExpression(selector, EXPRESSION, |
938 newInForInit: false, newAtStatementBegin: false); | 947 newInForInit: false, newAtStatementBegin: false); |
939 out("]"); | 948 out("]"); |
940 } | 949 } |
941 | 950 |
942 @override | 951 @override |
943 void visitNamedFunction(NamedFunction namedFunction) { | 952 void visitNamedFunction(NamedFunction namedFunction) { |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1015 void visitArrayInitializer(ArrayInitializer node) { | 1024 void visitArrayInitializer(ArrayInitializer node) { |
1016 out("["); | 1025 out("["); |
1017 List<Expression> elements = node.elements; | 1026 List<Expression> elements = node.elements; |
1018 for (int i = 0; i < elements.length; i++) { | 1027 for (int i = 0; i < elements.length; i++) { |
1019 Expression element = elements[i]; | 1028 Expression element = elements[i]; |
1020 if (element is ArrayHole) { | 1029 if (element is ArrayHole) { |
1021 // Note that array holes must have a trailing "," even if they are | 1030 // Note that array holes must have a trailing "," even if they are |
1022 // in last position. Otherwise `[,]` (having length 1) would become | 1031 // in last position. Otherwise `[,]` (having length 1) would become |
1023 // equal to `[]` (the empty array) | 1032 // equal to `[]` (the empty array) |
1024 // and [1,,] (array with 1 and a hole) would become [1,] = [1]. | 1033 // and [1,,] (array with 1 and a hole) would become [1,] = [1]. |
| 1034 startNode(element); |
1025 out(","); | 1035 out(","); |
| 1036 endNode(element); |
1026 continue; | 1037 continue; |
1027 } | 1038 } |
1028 if (i != 0) spaceOut(); | 1039 if (i != 0) spaceOut(); |
1029 visitNestedExpression(element, ASSIGNMENT, | 1040 visitNestedExpression(element, ASSIGNMENT, |
1030 newInForInit: false, newAtStatementBegin: false); | 1041 newInForInit: false, newAtStatementBegin: false); |
1031 // We can skip the trailing "," for the last element (since it's not | 1042 // We can skip the trailing "," for the last element (since it's not |
1032 // an array hole). | 1043 // an array hole). |
1033 if (i != elements.length - 1) out(","); | 1044 if (i != elements.length - 1) out(","); |
1034 } | 1045 } |
1035 out("]"); | 1046 out("]"); |
(...skipping 26 matching lines...) Expand all Loading... |
1062 indentLess(); | 1073 indentLess(); |
1063 if (!node.isOneLiner && !properties.isEmpty) { | 1074 if (!node.isOneLiner && !properties.isEmpty) { |
1064 lineOut(); | 1075 lineOut(); |
1065 indent(); | 1076 indent(); |
1066 } | 1077 } |
1067 out("}"); | 1078 out("}"); |
1068 } | 1079 } |
1069 | 1080 |
1070 @override | 1081 @override |
1071 void visitProperty(Property node) { | 1082 void visitProperty(Property node) { |
| 1083 startNode(node.name); |
1072 if (node.name is LiteralString) { | 1084 if (node.name is LiteralString) { |
1073 LiteralString nameString = node.name; | 1085 LiteralString nameString = node.name; |
1074 String name = nameString.value; | 1086 String name = nameString.value; |
1075 if (isValidJavaScriptId(name)) { | 1087 if (isValidJavaScriptId(name)) { |
1076 out(name.substring(1, name.length - 1)); | 1088 out(name.substring(1, name.length - 1)); |
1077 } else { | 1089 } else { |
1078 out(name); | 1090 out(name); |
1079 } | 1091 } |
1080 } else if (node.name is Name) { | 1092 } else if (node.name is Name) { |
1081 node.name.accept(this); | 1093 node.name.accept(this); |
1082 } else { | 1094 } else { |
1083 assert(node.name is LiteralNumber); | 1095 assert(node.name is LiteralNumber); |
1084 LiteralNumber nameNumber = node.name; | 1096 LiteralNumber nameNumber = node.name; |
1085 out(nameNumber.value); | 1097 out(nameNumber.value); |
1086 } | 1098 } |
| 1099 endNode(node.name); |
1087 out(":"); | 1100 out(":"); |
1088 spaceOut(); | 1101 spaceOut(); |
1089 visitNestedExpression(node.value, ASSIGNMENT, | 1102 visitNestedExpression(node.value, ASSIGNMENT, |
1090 newInForInit: false, newAtStatementBegin: false); | 1103 newInForInit: false, newAtStatementBegin: false); |
1091 } | 1104 } |
1092 | 1105 |
1093 @override | 1106 @override |
1094 void visitRegExpLiteral(RegExpLiteral node) { | 1107 void visitRegExpLiteral(RegExpLiteral node) { |
1095 out(node.pattern); | 1108 out(node.pattern); |
1096 } | 1109 } |
(...skipping 365 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1462 } | 1475 } |
1463 } | 1476 } |
1464 | 1477 |
1465 EnterExitNode exitNode(JavaScriptPrintingContext context, int position) { | 1478 EnterExitNode exitNode(JavaScriptPrintingContext context, int position) { |
1466 // Enter must happen before exit. | 1479 // Enter must happen before exit. |
1467 addToNode(context, position); | 1480 addToNode(context, position); |
1468 context.exitNode(node, startPosition, position, closingPosition); | 1481 context.exitNode(node, startPosition, position, closingPosition); |
1469 return parent; | 1482 return parent; |
1470 } | 1483 } |
1471 } | 1484 } |
OLD | NEW |