add Statement node type
This commit is contained in:
parent
2a9af21e93
commit
795e38e4f4
@ -70,7 +70,7 @@ CommonJSFormatter.prototype.export = function (node, nodes) {
|
||||
KEY: declar.id
|
||||
}, true);
|
||||
|
||||
nodes.push(declar);
|
||||
nodes.push(t.toStatement(declar));
|
||||
nodes.push(assign);
|
||||
|
||||
if (t.isFunctionDeclaration(declar)) {
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
module.exports = IgnoreFormatter;
|
||||
|
||||
var t = require("../types");
|
||||
|
||||
function IgnoreFormatter() {
|
||||
|
||||
}
|
||||
@ -13,7 +15,8 @@ IgnoreFormatter.prototype.importSpecifier = function () {
|
||||
};
|
||||
|
||||
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 () {
|
||||
|
||||
@ -20,13 +20,15 @@ exports.ForStatement = function (node, parent, file) {
|
||||
_.each(node.body, function (child) {
|
||||
if (child && t.isVariableDeclaration(child) && child.kind === "const") {
|
||||
_.each(child.declarations, function (declar) {
|
||||
_.each(t.getIds(declar.id), function (name) {
|
||||
_.each(t.getIds(declar), function (name) {
|
||||
check(declar, [name]);
|
||||
constants.push(name);
|
||||
});
|
||||
|
||||
declar._ignoreConstant = true;
|
||||
});
|
||||
|
||||
child._ignoreConstant = true;
|
||||
child.kind = "let";
|
||||
}
|
||||
});
|
||||
@ -36,7 +38,7 @@ exports.ForStatement = function (node, parent, file) {
|
||||
traverse(node, function (child) {
|
||||
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));
|
||||
}
|
||||
});
|
||||
|
||||
@ -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"],
|
||||
"FunctionDeclaration": ["Scope", "Function"],
|
||||
"FunctionDeclaration": ["Statement", "Declaration", "Scope", "Function"],
|
||||
"FunctionExpression": ["Scope", "Function"],
|
||||
|
||||
"BlockStatement": ["Scope"],
|
||||
"BlockStatement": ["Statement", "Scope"],
|
||||
"Program": ["Scope"],
|
||||
|
||||
"LogicalExpression": ["Binary"],
|
||||
@ -13,12 +31,12 @@
|
||||
"SpreadProperty": ["UnaryLike"],
|
||||
"SpreadElement": ["UnaryLike"],
|
||||
|
||||
"ClassDeclaration": ["Class"],
|
||||
"ClassDeclaration": ["Statement", "Declaration", "Class"],
|
||||
"ClassExpression": ["Class"],
|
||||
|
||||
"ForOfStatement": ["For"],
|
||||
"ForInStatement": ["For"],
|
||||
"ForStatement": ["For"],
|
||||
"ForOfStatement": ["Statement", "For"],
|
||||
"ForInStatement": ["Statement", "For"],
|
||||
"ForStatement": ["Statement", "For"],
|
||||
|
||||
"ObjectPattern": ["Pattern"],
|
||||
"ArrayPattern": ["Pattern"],
|
||||
|
||||
@ -74,6 +74,37 @@ t.ensureBlock = function (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) {
|
||||
if (t.isBlockStatement(node)) {
|
||||
return node;
|
||||
@ -107,8 +138,6 @@ t.getIds = function (node, map) {
|
||||
_.each(id.elements, function (elem) {
|
||||
search.push(elem);
|
||||
});
|
||||
} else if (t.isMemberExpression(id)) {
|
||||
search.push(id.object);
|
||||
} else if (t.isAssignmentExpression(id)) {
|
||||
search.push(id.left);
|
||||
} else if (t.isObjectPattern(id)) {
|
||||
@ -117,12 +146,14 @@ t.getIds = function (node, map) {
|
||||
});
|
||||
} else if (t.isVariableDeclaration(id)) {
|
||||
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);
|
||||
} else if (t.isSpreadElement(id)) {
|
||||
search.push(id.argument);
|
||||
} else if (id) {
|
||||
throw new Error("unknown node " + id.type);
|
||||
} else if (t.isExportDeclaration(id) || t.isImportDeclaration(id)) {
|
||||
search = search.concat(id.specifiers);
|
||||
} else if (t.isMemberExpression(id)) {
|
||||
search.push(id.object);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user