Chromium Code Reviews| Index: pkg/compiler/lib/src/js/js_debug.dart |
| diff --git a/pkg/compiler/lib/src/js/js_debug.dart b/pkg/compiler/lib/src/js/js_debug.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..58c9104a1f53f70388558b0205eba9d7c33b460d |
| --- /dev/null |
| +++ b/pkg/compiler/lib/src/js/js_debug.dart |
| @@ -0,0 +1,55 @@ |
| +// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +/// Helper for debug JS nodes. |
| + |
| +library js.debug; |
| + |
| +import 'package:js_ast/js_ast.dart'; |
| +import '../util/util.dart' show Indentation, Tagging; |
| + |
| +/// Unparse the JavaScript [node]. |
| +String nodeToString(Node node) { |
| + JavaScriptPrintingOptions options = new JavaScriptPrintingOptions( |
| + shouldCompressOutput: true, |
| + preferSemicolonToNewlineInMinifiedOutput: true); |
| + LenientPrintingContext printingContext = |
| + new LenientPrintingContext(); |
| + new Printer(options, printingContext).visit(node); |
| + return printingContext.getText(); |
| +} |
| + |
| +/// Visitor that creates an XML-like representation of the structure of a |
| +/// JavaScript [Node]. |
| +class DebugPrinter extends BaseVisitor with Indentation, Tagging<Node> { |
| + StringBuffer sb = new StringBuffer(); |
| + |
| + visitNodeWithChildren(Node node, String type) { |
|
floitsch
2015/06/29 08:55:52
add return type
Johnni Winther
2015/06/29 12:36:29
Done.
|
| + openNode(node, type); |
| + node.visitChildren(this); |
| + closeNode(); |
| + } |
| + |
| + @override |
| + visitNode(Node node) { |
| + visitNodeWithChildren(node, '${node.runtimeType}'); |
| + } |
| + |
| + /** |
| + * Pretty-prints given node tree into string. |
| + */ |
| + static String prettyPrint(Node node) { |
| + var p = new DebugPrinter(); |
| + node.accept(p); |
| + return p.sb.toString(); |
| + } |
| +} |
| + |
| +/// Simple printing context that doesn't throw on errors. |
| +class LenientPrintingContext extends SimpleJavaScriptPrintingContext { |
| + @override |
| + void error(String message) { |
| + buffer.write('>>$message<<'); |
| + } |
| +} |