From e7ef64a5ade81dd656250ffdb60ec63f137d286a Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Thu, 9 Oct 2014 18:56:24 +1100 Subject: [PATCH] fix es6 visitor keys --- lib/6to5/transformers/block-binding.js | 6 +- lib/6to5/transformers/classes.js | 2 +- lib/6to5/traverse/visitor-keys.json | 117 +++++++++++------- .../modules/exports-default/expected.js | 18 ++- .../modules/exports-variable/actual.js | 14 +-- .../modules/exports-variable/expected.js | 41 +++--- 6 files changed, 126 insertions(+), 72 deletions(-) diff --git a/lib/6to5/transformers/block-binding.js b/lib/6to5/transformers/block-binding.js index 7a6c44b724..16e836dfdc 100644 --- a/lib/6to5/transformers/block-binding.js +++ b/lib/6to5/transformers/block-binding.js @@ -92,12 +92,14 @@ var buildNode = function (node) { nodes.push(b.variableDeclaration("var", declars)); - return node.declarations.map(function (declar) { + return _.compact(node.declarations.map(function (declar) { + if (!declar.init) return; + return util.template("assign", { VALUE: declar.init, KEY: declar.id }, true); - }); + })); } else if (node.type === "ForInStatement" && node.left.type === "VariableDeclaration" && !node.left._ignoreBlockBindingHoist) { var id = node.left.declarations[0].id; node.left = id; diff --git a/lib/6to5/transformers/classes.js b/lib/6to5/transformers/classes.js index dc78cbe2e7..9ac9aa3ced 100644 --- a/lib/6to5/transformers/classes.js +++ b/lib/6to5/transformers/classes.js @@ -22,7 +22,7 @@ var getMemberExpressionObject = function (node) { var buildClass = function (node) { var superName = node.superClass; - var className = node.id; + var className = node.id || b.identifier("Anonymous"); var superClassReference = node.superClass; diff --git a/lib/6to5/traverse/visitor-keys.json b/lib/6to5/traverse/visitor-keys.json index 85c0609647..924b011c60 100644 --- a/lib/6to5/traverse/visitor-keys.json +++ b/lib/6to5/traverse/visitor-keys.json @@ -1,46 +1,75 @@ { - "AssignmentExpression": ["left", "right"], - "ArrayExpression": ["elements"], - "ArrayPattern": ["elements"], - "ArrowFunctionExpression": ["params", "defaults", "rest", "body"], - "BlockStatement": ["body"], - "BinaryExpression": ["left", "right"], - "BreakStatement": ["label"], - "CallExpression": ["callee", "arguments"], - "CatchClause": ["param", "body"], - "ClassBody": ["body"], - "ClassDeclaration": ["id", "body", "superClass"], - "ClassExpression": ["id", "body", "superClass"], - "ConditionalExpression": ["test", "consequent", "alternate"], - "ContinueStatement": ["label"], - "DoWhileStatement": ["body", "test"], - "ExpressionStatement": ["expression"], - "ForStatement": ["init", "test", "update", "body"], - "ForInStatement": ["left", "right", "body"], - "ForOfStatement": ["left", "right", "body"], - "FunctionDeclaration": ["id", "params", "defaults", "rest", "body"], - "FunctionExpression": ["id", "params", "defaults", "rest", "body"], - "IfStatement": ["test", "consequent", "alternate"], - "LabeledStatement": ["label", "body"], - "LogicalExpression": ["left", "right"], - "MemberExpression": ["object", "property"], - "MethodDefinition": ["key", "value"], - "NewExpression": ["callee", "arguments"], - "ObjectExpression": ["properties"], - "ObjectPattern": ["properties"], - "Program": ["body"], - "Property": ["key", "value"], - "ReturnStatement": ["argument"], - "SequenceExpression": ["expressions"], - "SwitchStatement": ["discriminant", "cases"], - "SwitchCase": ["test", "consequent"], - "ThrowStatement": ["argument"], - "TryStatement": ["block", "handlers", "handler", "guardedHandlers", "finalizer"], - "UnaryExpression": ["argument"], - "UpdateExpression": ["argument"], - "VariableDeclaration": ["declarations"], - "VariableDeclarator": ["id", "init"], - "WhileStatement": ["test", "body"], - "WithStatement": ["object", "body"], - "YieldExpression": ["argument"] + "ArrayExpression": ["elements"], + "ArrayPattern": ["elements"], + "ArrowFunctionExpression": ["params", "defaults", "rest", "body"], + "AssignmentExpression": ["left", "right"], + "BinaryExpression": ["left", "right"], + "BlockStatement": ["body"], + "BreakStatement": ["label"], + "CallExpression": ["callee", "arguments"], + "CatchClause": ["param", "body"], + "ClassBody": ["body"], + "ClassDeclaration": ["id", "body", "superClass"], + "ClassExpression": ["id", "body", "superClass"], + "ClassProperty": ["key", "value"], + "ComprehensionBlock": ["left", "right", "body"], + "ComprehensionExpression": ["filter", "blocks", "body"], + "ConditionalExpression": ["test", "consequent", "alternate"], + "ContinueStatement": ["label"], + "DebuggerStatement": [], + "DoWhileStatement": ["body", "test"], + "EmptyStatement": [], + "ExportDeclaration": ["declaration", "specifiers", "source"], + "ExportBatchSpecifier": [], + "ExportSpecifier": ["id", "name"], + "ExpressionStatement": ["expression"], + "ForInStatement": ["left", "right", "body"], + "ForOfStatement": ["left", "right", "body"], + "ForStatement": ["init", "test", "update", "body"], + "FunctionDeclaration": ["id", "params", "defaults", "rest", "body"], + "FunctionExpression": ["id", "params", "defaults", "rest", "body"], + "Identifier": [], + "IfStatement": ["test", "consequent", "alternate"], + "ImportDeclaration": ["specifiers", "source"], + "ImportDefaultSpecifier": ["id"], + "ImportNamespaceSpecifier": ["id"], + "ImportSpecifier": ["id", "name"], + "LabeledStatement": ["label", "body"], + "Literal": [], + "LogicalExpression": ["left", "right"], + "MemberExpression": ["object", "property"], + "MethodDefinition": ["key", "value"], + "ModuleSpecifier": [], + "NewExpression": ["callee", "arguments"], + "ObjectExpression": ["properties"], + "ObjectPattern": ["properties"], + "ObjectTypeAnnotation": ["properties"], + "OptionalParameter": ["id"], + "ParametricTypeAnnotation": ["params"], + "ParametricallyTypedIdentifier": [], + "Program": ["body"], + "Property": ["key", "value"], + "ReturnStatement": ["argument"], + "SequenceExpression": ["expressions"], + "SpreadElement": ["argument"], + "SpreadProperty": ["argument"], + "SwitchCase": ["test", "consequent"], + "SwitchStatement": ["discriminant", "cases"], + "TaggedTemplateExpression": ["tag", "quasi"], + "TemplateElement": [], + "TemplateLiteral": ["quasis", "expressions"], + "ThisExpression": [], + "ThrowStatement": ["argument"], + "TryStatement": ["block", "handlers", "handler", "guardedHandlers", "finalizer"], + "TypeAnnotatedIdentifier": ["id", "annotation"], + "TypeAnnotation": ["id", "params", "returnType", "parametricType"], + "UnaryExpression": ["argument"], + "UpdateExpression": ["argument"], + "VariableDeclaration": ["declarations"], + "VariableDeclarator": ["id", "init"], + "VoidTypeAnnotation": [], + "WhileStatement": ["test", "body"], + "WithStatement": ["object", "body"], + "YieldExpression": ["argument"], + "AwaitExpression": ["argument"] } diff --git a/test/fixtures/modules/exports-default/expected.js b/test/fixtures/modules/exports-default/expected.js index cb9e564485..0e03dc1f79 100644 --- a/test/fixtures/modules/exports-default/expected.js +++ b/test/fixtures/modules/exports-default/expected.js @@ -1,8 +1,22 @@ module.exports = exports = 42; module.exports = exports = {}; + module.exports = exports = []; + module.exports = exports = foo; + module.exports = exports = function () {} -module.exports = exports = class {} + +module.exports = exports = function () { + function Anonymous() { + } + return Anonymous; +}(); + module.exports = exports = function foo () {} -module.exports = exports = class foo {} + +module.exports = exports = function () { + function foo() { + } + return foo; +}(); diff --git a/test/fixtures/modules/exports-variable/actual.js b/test/fixtures/modules/exports-variable/actual.js index 9ea2ab508d..b4629cc731 100644 --- a/test/fixtures/modules/exports-variable/actual.js +++ b/test/fixtures/modules/exports-variable/actual.js @@ -1,8 +1,8 @@ export var foo = 1; -export var foo = function () {}; -export var bar; -export let foo = 2; -export let bar; -export const foo = 3; -export function foo () {} -export class foo {} +export var foo2 = function () {}; +export var foo3; +export let foo4 = 2; +export let foo5; +export const foo6 = 3; +export function foo7 () {} +export class foo8 {} diff --git a/test/fixtures/modules/exports-variable/expected.js b/test/fixtures/modules/exports-variable/expected.js index d4fb035500..5b005e5383 100644 --- a/test/fixtures/modules/exports-variable/expected.js +++ b/test/fixtures/modules/exports-variable/expected.js @@ -1,23 +1,32 @@ -var foo = 1; -exports.foo = foo; +var foo; +var foo2; +var foo3; +var foo8; +(function () { + foo = 1; + exports.foo = foo; -var foo = function () {}; -exports.foo = foo; + foo2 = function () {}; + exports.foo2 = foo2; -var bar; -exports.bar = bar; + exports.foo3 = foo3; -let foo = 2; -exports.foo = foo; + var foo4 = 2; + exports.foo4 = foo4; -let bar; -exports.bar = bar; + var foo5; + exports.foo5 = foo5; -const foo = 3; -exports.foo = foo; + var foo6 = 3; + exports.foo6 = foo6; -function foo () {} -exports.foo = foo; + function foo7 () {} + exports.foo7 = foo7; -class foo {} -exports.foo = foo; + foo8 = function () { + function foo8() { + } + return foo8; + }(); + exports.foo8 = foo8; +}());