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 849 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
860 case "yield": | 860 case "yield": |
861 return true; | 861 return true; |
862 } | 862 } |
863 return false; | 863 return false; |
864 } | 864 } |
865 | 865 |
866 visitAccess(PropertyAccess access) { | 866 visitAccess(PropertyAccess access) { |
867 visitNestedExpression(access.receiver, CALL, | 867 visitNestedExpression(access.receiver, CALL, |
868 newInForInit: inForInit, | 868 newInForInit: inForInit, |
869 newAtStatementBegin: atStatementBegin); | 869 newAtStatementBegin: atStatementBegin); |
870 Node selector = access.selector; | 870 propertyNameOut(access.selector, inAccess: true); |
871 if (selector is LiteralString) { | |
872 LiteralString selectorString = selector; | |
873 String fieldWithQuotes = selectorString.value; | |
874 if (isValidJavaScriptId(fieldWithQuotes)) { | |
875 if (access.receiver is LiteralNumber) out(" "); | |
876 out("."); | |
877 out(fieldWithQuotes.substring(1, fieldWithQuotes.length - 1)); | |
878 return; | |
879 } | |
880 } | |
881 out("["); | |
882 visitNestedExpression(selector, EXPRESSION, | |
883 newInForInit: false, newAtStatementBegin: false); | |
884 out("]"); | |
885 } | 871 } |
886 | 872 |
887 visitNamedFunction(NamedFunction namedFunction) { | 873 visitNamedFunction(NamedFunction namedFunction) { |
888 VarCollector vars = new VarCollector(); | 874 VarCollector vars = new VarCollector(); |
889 vars.visitNamedFunction(namedFunction); | 875 vars.visitNamedFunction(namedFunction); |
890 functionOut(namedFunction.function, namedFunction.name, vars); | 876 functionOut(namedFunction.function, namedFunction.name, vars); |
891 } | 877 } |
892 | 878 |
893 visitFun(Fun fun) { | 879 visitFun(Fun fun) { |
894 VarCollector vars = new VarCollector(); | 880 VarCollector vars = new VarCollector(); |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1077 spaceOut(); | 1063 spaceOut(); |
1078 out("{}"); | 1064 out("{}"); |
1079 } else { | 1065 } else { |
1080 blockBody(fun.body, needsSeparation: false, needsNewline: false); | 1066 blockBody(fun.body, needsSeparation: false, needsNewline: false); |
1081 } | 1067 } |
1082 localNamer.leaveScope(); | 1068 localNamer.leaveScope(); |
1083 } | 1069 } |
1084 | 1070 |
1085 visitPropertyName(PropertyName node) => propertyNameOut(node); | 1071 visitPropertyName(PropertyName node) => propertyNameOut(node); |
1086 | 1072 |
1087 void propertyNameOut(Expression node, {bool inMethod: false}) { | 1073 void propertyNameOut(Expression node, {bool inMethod: false, |
1088 inForInit = false; | 1074 bool inAccess: false}) { |
1089 atStatementBegin = false; | |
1090 | 1075 |
1091 if (node is LiteralNumber) { | 1076 if (node is LiteralNumber) { |
1092 LiteralNumber nameNumber = node; | 1077 LiteralNumber nameNumber = node; |
| 1078 if (inAccess) out('['); |
1093 out(nameNumber.value); | 1079 out(nameNumber.value); |
| 1080 if (inAccess) out(']'); |
1094 } else { | 1081 } else { |
1095 String quotedName; | 1082 String quotedName; |
1096 if (node is PropertyName) { | 1083 if (node is PropertyName) { |
1097 quotedName = "'${node.name}'"; | 1084 quotedName = "'${node.name}'"; |
1098 } else if (node is LiteralString) { | 1085 } else if (node is LiteralString) { |
1099 quotedName = node.value; | 1086 quotedName = node.value; |
1100 } | 1087 } |
1101 if (quotedName != null) { | 1088 if (quotedName != null) { |
1102 if (isValidJavaScriptId(quotedName)) { | 1089 if (isValidJavaScriptId(quotedName)) { |
| 1090 if (inAccess) out('.'); |
1103 out(quotedName.substring(1, quotedName.length - 1)); | 1091 out(quotedName.substring(1, quotedName.length - 1)); |
1104 } else { | 1092 } else { |
1105 if (inMethod) out("["); | 1093 if (inMethod || inAccess) out("["); |
1106 out(quotedName); | 1094 out(quotedName); |
1107 if (inMethod) out("]"); | 1095 if (inMethod || inAccess) out("]"); |
1108 } | 1096 } |
1109 } else { | 1097 } else { |
1110 // ComputedPropertyName | 1098 // ComputedPropertyName |
1111 out("["); | 1099 out("["); |
1112 visit(node); | 1100 visitNestedExpression(node, EXPRESSION, |
| 1101 newInForInit: false, newAtStatementBegin: false); |
1113 out("]"); | 1102 out("]"); |
1114 } | 1103 } |
1115 } | 1104 } |
1116 } | 1105 } |
1117 | 1106 |
1118 visitLiteralExpression(LiteralExpression node) { | 1107 visitLiteralExpression(LiteralExpression node) { |
1119 String template = node.template; | 1108 String template = node.template; |
1120 List<Expression> inputs = node.inputs; | 1109 List<Expression> inputs = node.inputs; |
1121 | 1110 |
1122 List<String> parts = template.split('#'); | 1111 List<String> parts = template.split('#'); |
(...skipping 335 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1458 codes.add(nthLetter((n ~/ nameSpaceSize) % LETTERS)); | 1447 codes.add(nthLetter((n ~/ nameSpaceSize) % LETTERS)); |
1459 } | 1448 } |
1460 codes.add(charCodes.$0 + digit); | 1449 codes.add(charCodes.$0 + digit); |
1461 newName = new String.fromCharCodes(codes); | 1450 newName = new String.fromCharCodes(codes); |
1462 } | 1451 } |
1463 assert(new RegExp(r'[a-zA-Z][a-zA-Z0-9]*').hasMatch(newName)); | 1452 assert(new RegExp(r'[a-zA-Z][a-zA-Z0-9]*').hasMatch(newName)); |
1464 maps.last[oldName] = newName; | 1453 maps.last[oldName] = newName; |
1465 return newName; | 1454 return newName; |
1466 } | 1455 } |
1467 } | 1456 } |
OLD | NEW |