Skip to content

Commit b98bb8d

Browse files
committed
Use types.visit in test/syntax.js instead of Visitor.
1 parent b2a018d commit b98bb8d

2 files changed

Lines changed: 63 additions & 37 deletions

File tree

lib/printer.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,9 @@ function genericPrintNoParens(path, options, print) {
924924
parts.push(";");
925925
return concat(parts);
926926

927+
case "ClassProperty":
928+
return concat([print(path.get("id")), ";"]);
929+
927930
case "ClassDeclaration":
928931
case "ClassExpression":
929932
var parts = ["class"];
@@ -938,6 +941,20 @@ function genericPrintNoParens(path, options, print) {
938941

939942
return concat(parts);
940943

944+
// These types are unprintable because they serve as abstract
945+
// supertypes for other (printable) types.
946+
case "Node":
947+
case "SourceLocation":
948+
case "Position":
949+
case "Statement":
950+
case "Function":
951+
case "Pattern":
952+
case "Expression":
953+
case "Declaration":
954+
case "Specifier":
955+
case "NamedSpecifier":
956+
throw new Error("unprintable type: " + JSON.stringify(n.type));
957+
941958
// Unhandled types below. If encountered, nodes of these types should
942959
// be either left alone or desugared into AST types that are fully
943960
// supported by the pretty-printer.
@@ -955,6 +972,26 @@ function genericPrintNoParens(path, options, print) {
955972
case "GraphExpression": // TODO
956973
case "GraphIndexExpression": // TODO
957974
case "TypeAnnotation": // TODO
975+
// XML types that nobody cares about or needs to print.
976+
case "XMLDefaultDeclaration":
977+
case "XMLAnyName":
978+
case "XMLQualifiedIdentifier":
979+
case "XMLFunctionQualifiedIdentifier":
980+
case "XMLAttributeSelector":
981+
case "XMLFilterExpression":
982+
case "XML":
983+
case "XMLElement":
984+
case "XMLList":
985+
case "XMLEscape":
986+
case "XMLText":
987+
case "XMLStartTag":
988+
case "XMLEndTag":
989+
case "XMLPointTag":
990+
case "XMLName":
991+
case "XMLAttribute":
992+
case "XMLCdata":
993+
case "XMLComment":
994+
case "XMLProcessingInstruction":
958995
default:
959996
debugger;
960997
throw new Error("unknown type: " + JSON.stringify(n.type));

test/syntax.js

Lines changed: 26 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ var fs = require("fs");
33
var path = require("path");
44
var types = require("../lib/types");
55
var parse = require("../lib/parser").parse;
6-
var Visitor = require("../lib/visitor").Visitor;
76

87
describe("syntax", function() {
98
// Make sure we handle all possible node types in Syntax, and no additional
@@ -17,49 +16,39 @@ describe("syntax", function() {
1716
var ast = parse(data);
1817
assert.ok(ast);
1918

20-
var types = {};
21-
new GenericPrintVisitor(types).visit(ast);
19+
var typeNames = {};
20+
types.visit(ast, {
21+
visitFunctionDeclaration: function(path) {
22+
var decl = path.node;
23+
if (types.namedTypes.Identifier.check(decl.id) &&
24+
decl.id.name === "genericPrintNoParens") {
25+
this.traverse(path, {
26+
visitSwitchCase: function(path) {
27+
var test = path.node.test;
28+
if (test &&
29+
test.type === "Literal" &&
30+
typeof test.value === "string") {
31+
var name = test.value;
32+
typeNames[name] = name;
33+
}
34+
return false;
35+
}
36+
});
37+
} else {
38+
this.traverse(path);
39+
}
40+
}
41+
});
2242

2343
for (var name in types.namedTypes) {
2444
if (types.namedTypes.hasOwnProperty(name)) {
25-
assert.ok(types.hasOwnProperty(name), "unhandled type: " + name);
26-
assert.strictEqual(name, types[name]);
27-
delete types[name];
45+
assert.ok(typeNames.hasOwnProperty(name), "unhandled type: " + name);
46+
assert.strictEqual(name, typeNames[name]);
47+
delete typeNames[name];
2848
}
2949
}
3050

3151
done();
3252
});
3353
});
34-
35-
var GenericPrintVisitor = Visitor.extend({
36-
init: function(types) {
37-
this.types = types;
38-
},
39-
40-
visitFunctionDeclaration: function(decl) {
41-
if (types.namedTypes.Identifier.check(decl.id) &&
42-
decl.id.name === "genericPrintNoParens")
43-
{
44-
new CaseVisitor(this.types).visit(decl);
45-
}
46-
}
47-
})
48-
49-
var CaseVisitor = Visitor.extend({
50-
init: function(types) {
51-
this.types = types;
52-
},
53-
54-
visitSwitchCase: function(expr) {
55-
var test = expr.test;
56-
if (test &&
57-
test.type === "Literal" &&
58-
typeof test.value === "string")
59-
{
60-
var name = test.value;
61-
this.types[name] = name;
62-
}
63-
}
64-
});
6554
});

0 commit comments

Comments
 (0)