From 795e38e4f44dba1e47f00c97e3e808dfbe1ffc84 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Fri, 7 Nov 2014 20:46:43 +1100 Subject: [PATCH] add Statement node type --- lib/6to5/modules/common.js | 2 +- lib/6to5/modules/ignore.js | 5 +++- lib/6to5/transformers/constants.js | 6 +++-- lib/6to5/types/alias-keys.json | 30 +++++++++++++++++----- lib/6to5/types/index.js | 41 ++++++++++++++++++++++++++---- 5 files changed, 69 insertions(+), 15 deletions(-) diff --git a/lib/6to5/modules/common.js b/lib/6to5/modules/common.js index 6ab851437d..deb9b64de3 100644 --- a/lib/6to5/modules/common.js +++ b/lib/6to5/modules/common.js @@ -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)) { diff --git a/lib/6to5/modules/ignore.js b/lib/6to5/modules/ignore.js index 53368ecfeb..e74fe8d83b 100644 --- a/lib/6to5/modules/ignore.js +++ b/lib/6to5/modules/ignore.js @@ -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 () { diff --git a/lib/6to5/transformers/constants.js b/lib/6to5/transformers/constants.js index 3150f86c62..e46534bee3 100644 --- a/lib/6to5/transformers/constants.js +++ b/lib/6to5/transformers/constants.js @@ -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)); } }); diff --git a/lib/6to5/types/alias-keys.json b/lib/6to5/types/alias-keys.json index 076ad3e898..90de64c757 100644 --- a/lib/6to5/types/alias-keys.json +++ b/lib/6to5/types/alias-keys.json @@ -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"], diff --git a/lib/6to5/types/index.js b/lib/6to5/types/index.js index ecb687e1ec..e1585fba3f 100644 --- a/lib/6to5/types/index.js +++ b/lib/6to5/types/index.js @@ -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); } }