OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 /// Helper for debug JS nodes. |
| 6 |
| 7 library js.debug; |
| 8 |
| 9 import 'package:js_ast/js_ast.dart'; |
| 10 import '../util/util.dart' show Indentation, Tagging; |
| 11 |
| 12 /// Unparse the JavaScript [node]. |
| 13 String nodeToString(Node node) { |
| 14 JavaScriptPrintingOptions options = new JavaScriptPrintingOptions( |
| 15 shouldCompressOutput: true, |
| 16 preferSemicolonToNewlineInMinifiedOutput: true); |
| 17 LenientPrintingContext printingContext = |
| 18 new LenientPrintingContext(); |
| 19 new Printer(options, printingContext).visit(node); |
| 20 return printingContext.getText(); |
| 21 } |
| 22 |
| 23 /// Visitor that creates an XML-like representation of the structure of a |
| 24 /// JavaScript [Node]. |
| 25 class DebugPrinter extends BaseVisitor with Indentation, Tagging<Node> { |
| 26 StringBuffer sb = new StringBuffer(); |
| 27 |
| 28 void visitNodeWithChildren(Node node, String type) { |
| 29 openNode(node, type); |
| 30 node.visitChildren(this); |
| 31 closeNode(); |
| 32 } |
| 33 |
| 34 @override |
| 35 void visitNode(Node node) { |
| 36 visitNodeWithChildren(node, '${node.runtimeType}'); |
| 37 } |
| 38 |
| 39 @override |
| 40 void visitName(Name node) { |
| 41 openAndCloseNode(node, '${node.runtimeType}', {'name': node.name}); |
| 42 } |
| 43 |
| 44 @override |
| 45 void visitLiteralString(LiteralString node) { |
| 46 openAndCloseNode(node, '${node.runtimeType}', {'value': node.value}); |
| 47 } |
| 48 |
| 49 /** |
| 50 * Pretty-prints given node tree into string. |
| 51 */ |
| 52 static String prettyPrint(Node node) { |
| 53 var p = new DebugPrinter(); |
| 54 node.accept(p); |
| 55 return p.sb.toString(); |
| 56 } |
| 57 } |
| 58 |
| 59 /// Simple printing context that doesn't throw on errors. |
| 60 class LenientPrintingContext extends SimpleJavaScriptPrintingContext { |
| 61 @override |
| 62 void error(String message) { |
| 63 buffer.write('>>$message<<'); |
| 64 } |
| 65 } |
OLD | NEW |