blob: 8851aa6ce2239c94a56f9f9653bf1fe3fbeea0ae [file] [log] [blame]
// 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 '../io/code_output.dart' show BufferedCodeOutput;
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();
void visitNodeWithChildren(Node node, String type, [Map params]) {
openNode(node, type, params);
void visitNode(Node node) {
visitNodeWithChildren(node, '${node.runtimeType}');
void visitName(Name node) {
openAndCloseNode(node, '${node.runtimeType}', {'name':});
void visitBinary(Binary node) {
visitNodeWithChildren(node, '${node.runtimeType}', {'op': node.op});
void visitLiteralString(LiteralString node) {
openAndCloseNode(node, '${node.runtimeType}', {'value': node.value});
* Pretty-prints given node tree into string.
static String prettyPrint(Node node) {
var p = new DebugPrinter();
/// Simple printing context that doesn't throw on errors.
class LenientPrintingContext extends SimpleJavaScriptPrintingContext
implements BufferedCodeOutput {
void error(String message) {