Chromium Code Reviews| 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 visitNodeWithChildren(Node node, String type) { | |
|
floitsch
2015/06/29 08:55:52
add return type
Johnni Winther
2015/06/29 12:36:29
Done.
| |
| 29 openNode(node, type); | |
| 30 node.visitChildren(this); | |
| 31 closeNode(); | |
| 32 } | |
| 33 | |
| 34 @override | |
| 35 visitNode(Node node) { | |
| 36 visitNodeWithChildren(node, '${node.runtimeType}'); | |
| 37 } | |
| 38 | |
| 39 /** | |
| 40 * Pretty-prints given node tree into string. | |
| 41 */ | |
| 42 static String prettyPrint(Node node) { | |
| 43 var p = new DebugPrinter(); | |
| 44 node.accept(p); | |
| 45 return p.sb.toString(); | |
| 46 } | |
| 47 } | |
| 48 | |
| 49 /// Simple printing context that doesn't throw on errors. | |
| 50 class LenientPrintingContext extends SimpleJavaScriptPrintingContext { | |
| 51 @override | |
| 52 void error(String message) { | |
| 53 buffer.write('>>$message<<'); | |
| 54 } | |
| 55 } | |
| OLD | NEW |