Comparing version 0.2.4 to 0.3.0
@@ -57,2 +57,4 @@ var assert = require("assert"), | ||
var types = require("./types").namedTypes; | ||
function genericPrint(n, options, print) { | ||
@@ -65,3 +67,3 @@ if (!n) | ||
assert.ok(n instanceof Object); | ||
types.Node.assert(n); | ||
@@ -71,3 +73,3 @@ switch (n.type) { | ||
n = n.program; | ||
assert.strictEqual(n.type, Syntax.Program); | ||
types.Program.assert(n); | ||
@@ -105,5 +107,11 @@ // intentionally fall through... | ||
case Syntax.Path: | ||
return fromString(".").join(n.body); | ||
case Syntax.Identifier: | ||
return fromString(n.name, options.tabWidth); | ||
case Syntax.SpreadElement: | ||
return concat(["...", print(n.argument)]); | ||
case Syntax.FunctionDeclaration: | ||
@@ -113,2 +121,5 @@ case Syntax.FunctionExpression: | ||
if (n.generator) | ||
parts.push("*"); | ||
if (n.id) | ||
@@ -125,2 +136,67 @@ parts.push(" ", print(n.id)); | ||
case Syntax.ArrowFunctionExpression: | ||
var parts = []; | ||
if (n.params.length === 1) { | ||
parts.push(print(n.params[0])); | ||
} else { | ||
parts.push( | ||
"(", | ||
fromString(", ").join(n.params.map(print)), | ||
")" | ||
); | ||
} | ||
parts.push(" => ", print(n.body)); | ||
return concat(parts); | ||
case Syntax.MethodDefinition: | ||
var parts = []; | ||
if (!n.kind || n.kind === "init") { | ||
if (n.value.generator) | ||
parts.push("*"); | ||
} else { | ||
assert.ok( | ||
n.kind === "get" || | ||
n.kind === "set"); | ||
parts.push(n.kind, " "); | ||
} | ||
parts.push( | ||
print(n.key), | ||
"(", | ||
fromString(", ").join(n.value.params.map(print)), | ||
") ", | ||
print(n.value.body) | ||
); | ||
return concat(parts); | ||
case Syntax.YieldExpression: | ||
var parts = ["yield"]; | ||
if (n.delegate) | ||
parts.push("*"); | ||
if (n.argument) | ||
parts.push(" ", print(n.argument)); | ||
return concat(parts); | ||
case Syntax.ModuleDeclaration: | ||
var parts = ["module", print(n.id)]; | ||
if (n.from) { | ||
assert.ok(!n.body); | ||
parts.push("=", print(n.from)); | ||
} else { | ||
parts.push(print(n.body)); | ||
} | ||
return fromString(" ").join(parts); | ||
case Syntax.BlockStatement: | ||
@@ -154,2 +230,3 @@ var naked = printNakedBlockStatement(n, print); | ||
case Syntax.ObjectExpression: | ||
case Syntax.ObjectPattern: | ||
var allowBreak = false, | ||
@@ -160,4 +237,11 @@ len = n.properties.length, | ||
n.properties.map(function(prop, i) { | ||
// Esprima uses this non-standard AST node type. | ||
prop.type = Syntax.Property; | ||
// Esprima uses these non-standard AST node types. | ||
if (!/^Property/.test(prop.type)) { | ||
if (prop.hasOwnProperty("kind")) { | ||
prop.type = Syntax.Property; | ||
} else { | ||
prop.type = Syntax.PropertyPattern || Syntax.Property; | ||
} | ||
} | ||
prop = print(prop).indent(options.tabWidth); | ||
@@ -185,2 +269,9 @@ | ||
case Syntax.PropertyPattern: | ||
return concat([ | ||
print(n.key), | ||
": ", | ||
print(n.pattern) | ||
]); | ||
case Syntax.Property: // Non-standard AST node type. | ||
@@ -193,3 +284,3 @@ var key = print(n.key), | ||
assert.strictEqual(val.type, Syntax.FunctionExpression); | ||
types.FunctionExpression.assert(val); | ||
assert.ok(val.id); | ||
@@ -210,2 +301,3 @@ assert.ok(n.kind === "get" || | ||
case Syntax.ArrayExpression: | ||
case Syntax.ArrayPattern: | ||
var elems = n.elements, | ||
@@ -364,2 +456,12 @@ len = elems.length, | ||
case Syntax.ForOfStatement: | ||
return concat([ | ||
"for (", | ||
print(n.left), | ||
" of ", | ||
print(n.right), | ||
")", | ||
adjustClause(print(n.body), options) | ||
]); | ||
case Syntax.DoWhileStatement: | ||
@@ -460,5 +562,75 @@ var doBody = concat([ | ||
// XJS extensions below. | ||
case Syntax.XJSAttribute: | ||
var parts = [print(n.name)]; | ||
if (n.value) | ||
// TODO Use {braces} when necessary. | ||
parts.push("=", print(n.value)); | ||
return concat(parts); | ||
case Syntax.XJSIdentifier: | ||
var str = n.name; | ||
if (typeof n.namespace === "string") | ||
str = n.namespace + ":" + str; | ||
return fromString(str, options.tabWidth); | ||
case Syntax.XJSExpression: | ||
return print(n.value); | ||
case Syntax.XJSElement: | ||
var parts = [print(n.openingElement)]; | ||
if (!n.selfClosing) { | ||
parts.push( | ||
concat(n.children.map(function(child) { | ||
if (child.type === Syntax.Literal) | ||
child.type = Syntax.XJSText; | ||
return print(child); | ||
})), | ||
print(n.closingElement) | ||
); | ||
} | ||
return concat(parts); | ||
case Syntax.XJSOpeningElement: | ||
var parts = ["<", print(n.name)]; | ||
n.attributes.forEach(function(attr) { | ||
parts.push(" ", print(attr)); | ||
}); | ||
parts.push(n.selfClosing ? " />" : ">"); | ||
return concat(parts); | ||
case Syntax.XJSClosingElement: | ||
return concat(["</", print(n.name), ">"]); | ||
case Syntax.XJSText: | ||
return fromString(n.value, options.tabWidth); | ||
// Unhandled types below. If encountered, nodes of these types should | ||
// be either left alone or desugared into AST types that are fully | ||
// supported by the pretty-printer. | ||
case Syntax.ClassBody: // TODO | ||
case Syntax.ClassDeclaration: // TODO | ||
case Syntax.ClassExpression: // TODO | ||
case Syntax.ClassHeritage: // TODO | ||
case Syntax.ComprehensionBlock: // TODO | ||
case Syntax.ComprehensionExpression: // TODO | ||
case Syntax.ExportDeclaration: // TODO | ||
case Syntax.ExportSpecifier: // TODO | ||
case Syntax.ExportSpecifierSet: // TODO | ||
case Syntax.Glob: // TODO | ||
case Syntax.ImportDeclaration: // TODO | ||
case Syntax.ImportSpecifier: // TODO | ||
case Syntax.TaggedTemplateExpression: // TODO | ||
case Syntax.TemplateElement: // TODO | ||
case Syntax.TemplateLiteral: // TODO | ||
default: | ||
debugger; | ||
throw new Error("unknown type: " + JSON.stringify(n)); | ||
throw new Error("unknown type: " + JSON.stringify(n.type)); | ||
} | ||
@@ -470,3 +642,3 @@ | ||
function printNakedBlockStatement(node, print) { | ||
assert.strictEqual(node.type, Syntax.BlockStatement); | ||
types.BlockStatement.assert(node); | ||
@@ -473,0 +645,0 @@ var allowBreak = false, |
@@ -10,2 +10,4 @@ var assert = require("assert"), | ||
assert.strictEqual(typeof type, "string"); | ||
if (exports.hasOwnProperty(type)) | ||
assert.strictEqual(exports[type], type); | ||
exports[type] = type; | ||
@@ -15,1 +17,9 @@ } | ||
addNonStandardType("File"); | ||
addNonStandardType("PropertyPattern"); | ||
addNonStandardType("XJSAttribute"); | ||
addNonStandardType("XJSIdentifier"); | ||
addNonStandardType("XJSExpression"); | ||
addNonStandardType("XJSElement"); | ||
addNonStandardType("XJSOpeningElement"); | ||
addNonStandardType("XJSClosingElement"); | ||
addNonStandardType("XJSText"); |
@@ -15,3 +15,3 @@ { | ||
], | ||
"version": "0.2.4", | ||
"version": "0.3.0", | ||
"homepage": "http://github.com/benjamn/recast", | ||
@@ -29,3 +29,3 @@ "repository": { | ||
"private": ">= 0.0.3", | ||
"ast-types": ">= 0.1.2" | ||
"ast-types": ">= 0.2.6" | ||
}, | ||
@@ -32,0 +32,0 @@ "devDependencies": {}, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
357385
10605
Updatedast-types@>= 0.2.6