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 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 } | 206 } |
207 | 207 |
208 visitCommaSeparated(List<Node> nodes, int hasRequiredType, | 208 visitCommaSeparated(List<Node> nodes, int hasRequiredType, |
209 {bool newInForInit, bool newAtStatementBegin}) { | 209 {bool newInForInit, bool newAtStatementBegin}) { |
210 for (int i = 0; i < nodes.length; i++) { | 210 for (int i = 0; i < nodes.length; i++) { |
211 if (i != 0) { | 211 if (i != 0) { |
212 atStatementBegin = false; | 212 atStatementBegin = false; |
213 out(","); | 213 out(","); |
214 spaceOut(); | 214 spaceOut(); |
215 } | 215 } |
216 visitNestedExpression(nodes[i], hasRequiredType, | 216 var node = nodes[i]; |
217 newInForInit: newInForInit, | 217 if (node is Expression) { |
218 newAtStatementBegin: newAtStatementBegin); | 218 visitNestedExpression(node, hasRequiredType, |
| 219 newInForInit: newInForInit, |
| 220 newAtStatementBegin: newAtStatementBegin); |
| 221 } else { |
| 222 // TODO(ochafik): Precedence of Parameters? |
| 223 visit(node); |
| 224 } |
219 } | 225 } |
220 } | 226 } |
221 | 227 |
222 visitAll(List<Node> nodes) { | 228 visitAll(List<Node> nodes) { |
223 nodes.forEach(visit); | 229 nodes.forEach(visit); |
224 } | 230 } |
225 | 231 |
226 visitProgram(Program program) { | 232 visitProgram(Program program) { |
227 if (program.scriptTag != null) { | 233 if (program.scriptTag != null) { |
228 out('#!${program.scriptTag}\n'); | 234 out('#!${program.scriptTag}\n'); |
(...skipping 642 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
871 visitThis(This node) { | 877 visitThis(This node) { |
872 out("this"); | 878 out("this"); |
873 } | 879 } |
874 | 880 |
875 visitSuper(Super node) { | 881 visitSuper(Super node) { |
876 out("super"); | 882 out("super"); |
877 } | 883 } |
878 | 884 |
879 visitIdentifier(Identifier node) { | 885 visitIdentifier(Identifier node) { |
880 out(localNamer.getName(node)); | 886 out(localNamer.getName(node)); |
| 887 } |
| 888 |
| 889 visitParameter(Parameter node) { |
| 890 if (node.isRest) out('...'); |
| 891 visit(node.binding); |
881 outTypeAnnotation(node.type); | 892 outTypeAnnotation(node.type); |
882 } | 893 } |
883 | 894 |
884 visitRestParameter(RestParameter node) { | 895 visitTypeParameter(TypeParameter node) { |
885 out('...'); | 896 visit(node.name); |
886 visitIdentifier(node.parameter); | 897 if (node.bound != null && options.shouldEmitTypes) { |
| 898 out(" extends "); |
| 899 visit(node.bound); |
| 900 } |
887 } | 901 } |
888 | 902 |
889 bool isDigit(int charCode) { | 903 bool isDigit(int charCode) { |
890 return charCodes.$0 <= charCode && charCode <= charCodes.$9; | 904 return charCodes.$0 <= charCode && charCode <= charCodes.$9; |
891 } | 905 } |
892 | 906 |
893 bool isValidJavaScriptId(String field) { | 907 bool isValidJavaScriptId(String field) { |
894 if (field.length < 3) return false; | 908 if (field.length < 3) return false; |
895 // Ignore the leading and trailing string-delimiter. | 909 // Ignore the leading and trailing string-delimiter. |
896 for (int i = 1; i < field.length - 1; i++) { | 910 for (int i = 1; i < field.length - 1; i++) { |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
937 visitNamedFunction(NamedFunction namedFunction) { | 951 visitNamedFunction(NamedFunction namedFunction) { |
938 functionOut(namedFunction.function, namedFunction.name); | 952 functionOut(namedFunction.function, namedFunction.name); |
939 } | 953 } |
940 | 954 |
941 visitFun(Fun fun) { | 955 visitFun(Fun fun) { |
942 functionOut(fun, null); | 956 functionOut(fun, null); |
943 } | 957 } |
944 | 958 |
945 visitArrowFun(ArrowFun fun) { | 959 visitArrowFun(ArrowFun fun) { |
946 localNamer.enterScope(fun); | 960 localNamer.enterScope(fun); |
947 if (fun.params.length == 1 && | 961 if (fun.params.length == 1 && fun.params.single.binding is Expression && |
948 (fun.params.single.type == null || !options.shouldEmitTypes)) { | 962 (fun.params.single.type == null || !options.shouldEmitTypes)) { |
949 visitNestedExpression(fun.params.single, SPREAD, | 963 visit(fun.params.single.binding); |
950 newInForInit: false, newAtStatementBegin: false); | 964 // visitNestedExpression(fun.params.single, SPREAD, |
| 965 // newInForInit: false, newAtStatementBegin: false); |
951 } else { | 966 } else { |
952 out("("); | 967 out("("); |
953 visitCommaSeparated(fun.params, SPREAD, | 968 visitCommaSeparated(fun.params, SPREAD, |
954 newInForInit: false, newAtStatementBegin: false); | 969 newInForInit: false, newAtStatementBegin: false); |
955 out(")"); | 970 out(")"); |
956 } | 971 } |
957 outTypeAnnotation(fun.returnType); | 972 outTypeAnnotation(fun.returnType); |
958 spaceOut(); | 973 spaceOut(); |
959 out("=>"); | 974 out("=>"); |
960 if (fun.body is Expression) { | 975 if (fun.body is Expression) { |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1086 visit(node.tag); | 1101 visit(node.tag); |
1087 visit(node.template); | 1102 visit(node.template); |
1088 } | 1103 } |
1089 | 1104 |
1090 visitClassDeclaration(ClassDeclaration node) { | 1105 visitClassDeclaration(ClassDeclaration node) { |
1091 indent(); | 1106 indent(); |
1092 visit(node.classExpr); | 1107 visit(node.classExpr); |
1093 lineOut(); | 1108 lineOut(); |
1094 } | 1109 } |
1095 | 1110 |
1096 void outTypeParams(Iterable<Identifier> typeParams) { | 1111 void outTypeParams(Iterable<TypeParameter> typeParams) { |
1097 if (typeParams != null && options.shouldEmitTypes && typeParams.isNotEmpty)
{ | 1112 if (typeParams != null && options.shouldEmitTypes && typeParams.isNotEmpty)
{ |
1098 out("<"); | 1113 out("<"); |
1099 var first = true; | 1114 var first = true; |
1100 for (var typeParam in typeParams) { | 1115 for (var typeParam in typeParams) { |
1101 if (!first) out(", "); | 1116 if (!first) out(", "); |
1102 first = false; | 1117 first = false; |
1103 visit(typeParam); | 1118 visit(typeParam); |
1104 } | 1119 } |
1105 out(">"); | 1120 out(">"); |
1106 } | 1121 } |
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1384 params = new Set<String>(); | 1399 params = new Set<String>(); |
1385 | 1400 |
1386 void forEachVar(void fn(String v)) => vars.forEach(fn); | 1401 void forEachVar(void fn(String v)) => vars.forEach(fn); |
1387 void forEachParam(void fn(String p)) => params.forEach(fn); | 1402 void forEachParam(void fn(String p)) => params.forEach(fn); |
1388 | 1403 |
1389 void collectVarsInFunction(FunctionExpression fun) { | 1404 void collectVarsInFunction(FunctionExpression fun) { |
1390 if (!nested) { | 1405 if (!nested) { |
1391 nested = true; | 1406 nested = true; |
1392 if (fun.params != null) { | 1407 if (fun.params != null) { |
1393 for (var param in fun.params) { | 1408 for (var param in fun.params) { |
1394 params.add(param.name); | 1409 collectBinding(Node node) { |
| 1410 if (node is Identifier) { |
| 1411 // TODO(ochafik): collect destructured |
| 1412 params.add(node.name); |
| 1413 } else if (node is DestructuredVariable) { |
| 1414 collectBinding(node.name); |
| 1415 collectBinding(node.structure); |
| 1416 } else if (node is BindingPattern) { |
| 1417 node.variables.forEach(collectBinding); |
| 1418 } |
| 1419 } |
| 1420 collectBinding(param.binding); |
1395 } | 1421 } |
1396 } | 1422 } |
1397 fun.body.accept(this); | 1423 fun.body.accept(this); |
1398 nested = false; | 1424 nested = false; |
1399 } | 1425 } |
1400 } | 1426 } |
1401 | 1427 |
1402 void visitFunctionDeclaration(FunctionDeclaration declaration) { | 1428 void visitFunctionDeclaration(FunctionDeclaration declaration) { |
1403 // Note that we don't bother collecting the name of the function. | 1429 // Note that we don't bother collecting the name of the function. |
1404 collectVarsInFunction(declaration.function); | 1430 collectVarsInFunction(declaration.function); |
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1637 abstract class VariableDeclarationVisitor<T> extends BaseVisitor<T> { | 1663 abstract class VariableDeclarationVisitor<T> extends BaseVisitor<T> { |
1638 declare(Identifier node); | 1664 declare(Identifier node); |
1639 | 1665 |
1640 visitFunctionExpression(FunctionExpression node) { | 1666 visitFunctionExpression(FunctionExpression node) { |
1641 node.params.forEach(_scanVariableBinding); | 1667 node.params.forEach(_scanVariableBinding); |
1642 node.body.accept(this); | 1668 node.body.accept(this); |
1643 } | 1669 } |
1644 | 1670 |
1645 _scanVariableBinding(VariableBinding d) { | 1671 _scanVariableBinding(VariableBinding d) { |
1646 if (d is Identifier) declare(d); | 1672 if (d is Identifier) declare(d); |
| 1673 else if (d is Parameter) _scanVariableBinding(d.binding); |
1647 else d.accept(this); | 1674 else d.accept(this); |
1648 } | 1675 } |
1649 | 1676 |
1650 visitRestParameter(RestParameter node) { | 1677 visitParameter(Parameter node) { |
1651 _scanVariableBinding(node.parameter); | 1678 _scanVariableBinding(node.binding); |
1652 super.visitRestParameter(node); | 1679 super.visitParameter(node); |
1653 } | 1680 } |
1654 | 1681 |
1655 visitDestructuredVariable(DestructuredVariable node) { | 1682 visitDestructuredVariable(DestructuredVariable node) { |
1656 var name = node.name; | 1683 var name = node.name; |
1657 if (name is Identifier) _scanVariableBinding(name); | 1684 if (name is Identifier) _scanVariableBinding(name); |
1658 super.visitDestructuredVariable(node); | 1685 super.visitDestructuredVariable(node); |
1659 } | 1686 } |
1660 | 1687 |
1661 visitSimpleBindingPattern(SimpleBindingPattern node) { | 1688 visitSimpleBindingPattern(SimpleBindingPattern node) { |
1662 _scanVariableBinding(node.name); | 1689 _scanVariableBinding(node.name); |
(...skipping 19 matching lines...) Expand all Loading... |
1682 declare(node.name); | 1709 declare(node.name); |
1683 node.function.accept(this); | 1710 node.function.accept(this); |
1684 } | 1711 } |
1685 | 1712 |
1686 visitClassExpression(ClassExpression node) { | 1713 visitClassExpression(ClassExpression node) { |
1687 declare(node.name); | 1714 declare(node.name); |
1688 if (node.heritage != null) node.heritage.accept(this); | 1715 if (node.heritage != null) node.heritage.accept(this); |
1689 for (Method element in node.methods) element.accept(this); | 1716 for (Method element in node.methods) element.accept(this); |
1690 } | 1717 } |
1691 } | 1718 } |
OLD | NEW |