add Statement node type

This commit is contained in:
Sebastian McKenzie 2014-11-07 20:46:43 +11:00
parent 2a9af21e93
commit 795e38e4f4
5 changed files with 69 additions and 15 deletions

View File

@ -70,7 +70,7 @@ CommonJSFormatter.prototype.export = function (node, nodes) {
KEY: declar.id KEY: declar.id
}, true); }, true);
nodes.push(declar); nodes.push(t.toStatement(declar));
nodes.push(assign); nodes.push(assign);
if (t.isFunctionDeclaration(declar)) { if (t.isFunctionDeclaration(declar)) {

View File

@ -1,5 +1,7 @@
module.exports = IgnoreFormatter; module.exports = IgnoreFormatter;
var t = require("../types");
function IgnoreFormatter() { function IgnoreFormatter() {
} }
@ -13,7 +15,8 @@ IgnoreFormatter.prototype.importSpecifier = function () {
}; };
IgnoreFormatter.prototype.export = function (node, nodes) { IgnoreFormatter.prototype.export = function (node, nodes) {
nodes.push(node.declaration); var declar = t.toStatement(node.declaration, true);
if (declar) nodes.push(declar);
}; };
IgnoreFormatter.prototype.exportSpecifier = function () { IgnoreFormatter.prototype.exportSpecifier = function () {

View File

@ -20,13 +20,15 @@ exports.ForStatement = function (node, parent, file) {
_.each(node.body, function (child) { _.each(node.body, function (child) {
if (child && t.isVariableDeclaration(child) && child.kind === "const") { if (child && t.isVariableDeclaration(child) && child.kind === "const") {
_.each(child.declarations, function (declar) { _.each(child.declarations, function (declar) {
_.each(t.getIds(declar.id), function (name) { _.each(t.getIds(declar), function (name) {
check(declar, [name]); check(declar, [name]);
constants.push(name); constants.push(name);
}); });
declar._ignoreConstant = true; declar._ignoreConstant = true;
}); });
child._ignoreConstant = true;
child.kind = "let"; child.kind = "let";
} }
}); });
@ -36,7 +38,7 @@ exports.ForStatement = function (node, parent, file) {
traverse(node, function (child) { traverse(node, function (child) {
if (child._ignoreConstant) return; if (child._ignoreConstant) return;
if (t.isVariableDeclarator(child) || t.isFunctionDeclaration(child) || t.isClassDeclaration(child) || t.isAssignmentExpression(child)) { if (t.isVariableDeclarator(child) || t.isDeclaration(child) || t.isAssignmentExpression(child)) {
check(child, t.getIds(child)); check(child, t.getIds(child));
} }
}); });

View File

@ -1,9 +1,27 @@
{ {
"ExpressionStatement": ["Statement"],
"BreakStatement": ["Statement"],
"ContinueStatement": ["Statement"],
"DebuggerStatement": ["Statement"],
"DoWhileStatement": ["Statement"],
"IfStatement": ["Statement"],
"ReturnStatement": ["Statement"],
"SwitchStatement": ["Statement"],
"ThrowStatement": ["Statement"],
"TryStatement": ["Statement"],
"WhileStatement": ["Statement"],
"WithStatement": ["Statement"],
"EmptyStatement": ["Statement"],
"LabeledStatement": ["Statement"],
"VariableDeclaration": ["Statement", "Declaration"],
"ExportDeclaration": ["Statement", "Declaration"],
"ImportDeclaration": ["Statement", "Declaration"],
"ArrowFunctionExpression": ["Scope", "Function"], "ArrowFunctionExpression": ["Scope", "Function"],
"FunctionDeclaration": ["Scope", "Function"], "FunctionDeclaration": ["Statement", "Declaration", "Scope", "Function"],
"FunctionExpression": ["Scope", "Function"], "FunctionExpression": ["Scope", "Function"],
"BlockStatement": ["Scope"], "BlockStatement": ["Statement", "Scope"],
"Program": ["Scope"], "Program": ["Scope"],
"LogicalExpression": ["Binary"], "LogicalExpression": ["Binary"],
@ -13,12 +31,12 @@
"SpreadProperty": ["UnaryLike"], "SpreadProperty": ["UnaryLike"],
"SpreadElement": ["UnaryLike"], "SpreadElement": ["UnaryLike"],
"ClassDeclaration": ["Class"], "ClassDeclaration": ["Statement", "Declaration", "Class"],
"ClassExpression": ["Class"], "ClassExpression": ["Class"],
"ForOfStatement": ["For"], "ForOfStatement": ["Statement", "For"],
"ForInStatement": ["For"], "ForInStatement": ["Statement", "For"],
"ForStatement": ["For"], "ForStatement": ["Statement", "For"],
"ObjectPattern": ["Pattern"], "ObjectPattern": ["Pattern"],
"ArrayPattern": ["Pattern"], "ArrayPattern": ["Pattern"],

View File

@ -74,6 +74,37 @@ t.ensureBlock = function (node) {
node.body = t.toBlock(node.body, node); node.body = t.toBlock(node.body, node);
}; };
t.toStatement = function (node, ignore) {
var mustHaveId = false;
var newType;
if (t.isClass(node)) {
mustHaveId = true;
newType = "ClassDeclaration";
} else if (t.isFunction(node)) {
mustHaveId = true;
newType = "FunctionDeclaration";
} else if (t.isStatement(node)) {
newType = node.type;
}
if (mustHaveId && !node.id) {
newType = false;
}
if (!newType) {
if (ignore) {
return false;
} else {
throw new Error("cannot turn " + node.type + " to a statement");
}
}
node.type = newType;
return node;
};
t.toBlock = function (node, parent) { t.toBlock = function (node, parent) {
if (t.isBlockStatement(node)) { if (t.isBlockStatement(node)) {
return node; return node;
@ -107,8 +138,6 @@ t.getIds = function (node, map) {
_.each(id.elements, function (elem) { _.each(id.elements, function (elem) {
search.push(elem); search.push(elem);
}); });
} else if (t.isMemberExpression(id)) {
search.push(id.object);
} else if (t.isAssignmentExpression(id)) { } else if (t.isAssignmentExpression(id)) {
search.push(id.left); search.push(id.left);
} else if (t.isObjectPattern(id)) { } else if (t.isObjectPattern(id)) {
@ -117,12 +146,14 @@ t.getIds = function (node, map) {
}); });
} else if (t.isVariableDeclaration(id)) { } else if (t.isVariableDeclaration(id)) {
search = search.concat(id.declarations); search = search.concat(id.declarations);
} else if (t.isVariableDeclarator(id) || t.isFunctionDeclaration(id) || t.isClassDeclaration(id)) { } else if (t.isImportSpecifier(id) || t.isExportSpecifier(id) || t.isVariableDeclarator(id) || t.isFunctionDeclaration(id) || t.isClassDeclaration(id)) {
search.push(id.id); search.push(id.id);
} else if (t.isSpreadElement(id)) { } else if (t.isSpreadElement(id)) {
search.push(id.argument); search.push(id.argument);
} else if (id) { } else if (t.isExportDeclaration(id) || t.isImportDeclaration(id)) {
throw new Error("unknown node " + id.type); search = search.concat(id.specifiers);
} else if (t.isMemberExpression(id)) {
search.push(id.object);
} }
} }