From 8f9d02d6891dd9f8a829b91ec362476e650944dc Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Fri, 8 May 2015 22:53:31 +0100 Subject: [PATCH] further work splitting up transformers into their own "category" in order of what they need and what they actually do --- .../es3/member-expression-literals.js | 2 +- .../transformers/es3/property-literals.js | 2 +- .../transformers/es5/properties.mutators.js | 38 ++++++++--------- .../transformers/es6/destructuring.js | 4 ++ .../transformers/es6/properties.computed.js | 4 +- .../transformers/es6/tail-call.js | 4 ++ .../transformation/transformers/index.js | 41 ++++++++++++------- .../transformers/internal/block-hoist.js | 2 +- .../transformers/internal/modules.js | 4 ++ .../transformers/internal/shadow-functions.js | 2 +- .../transformers/internal/validation.js | 4 ++ .../minification/dead-code-elimination.js | 3 +- .../member-expression-literals.js | 2 +- .../minification/property-literals.js | 2 +- .../transformers/other/regenerator.js | 10 ++--- .../transformers/other/runtime/index.js | 4 +- .../transformers/spec/function-name.js | 4 ++ .../utility/inline-environment-variables.js | 3 +- .../utility/inline-expressions.js | 3 +- .../transformers/utility/remove-console.js | 3 +- .../transformers/utility/remove-debugger.js | 3 +- 21 files changed, 88 insertions(+), 56 deletions(-) diff --git a/src/babel/transformation/transformers/es3/member-expression-literals.js b/src/babel/transformation/transformers/es3/member-expression-literals.js index d4465fde70..e80f5c62d1 100644 --- a/src/babel/transformation/transformers/es3/member-expression-literals.js +++ b/src/babel/transformation/transformers/es3/member-expression-literals.js @@ -1,7 +1,7 @@ import * as t from "../../../types"; export var metadata = { - category: "builtin-cleanup" + category: "builtin-trailing" }; export var MemberExpression = { diff --git a/src/babel/transformation/transformers/es3/property-literals.js b/src/babel/transformation/transformers/es3/property-literals.js index ecfd63c8dd..9fa015e82b 100644 --- a/src/babel/transformation/transformers/es3/property-literals.js +++ b/src/babel/transformation/transformers/es3/property-literals.js @@ -1,7 +1,7 @@ import * as t from "../../../types"; export var metadata = { - category: "builtin-cleanup" + category: "builtin-trailing" }; export var Property = { diff --git a/src/babel/transformation/transformers/es5/properties.mutators.js b/src/babel/transformation/transformers/es5/properties.mutators.js index 7fe77c7f41..a3161efae8 100644 --- a/src/babel/transformation/transformers/es5/properties.mutators.js +++ b/src/babel/transformation/transformers/es5/properties.mutators.js @@ -1,26 +1,24 @@ import * as defineMap from "../../helpers/define-map"; import * as t from "../../../types"; -export var ObjectExpression = { - exit(node, parent, scope, file) { - var mutatorMap = {}; - var hasAny = false; +export function ObjectExpression(node, parent, scope, file) { + var mutatorMap = {}; + var hasAny = false; - node.properties = node.properties.filter(function (prop) { - if (prop.kind === "get" || prop.kind === "set") { - hasAny = true; - defineMap.push(mutatorMap, prop, prop.kind, file); - return false; - } else { - return true; - } - }); + node.properties = node.properties.filter(function (prop) { + if (prop.kind === "get" || prop.kind === "set") { + hasAny = true; + defineMap.push(mutatorMap, prop, prop.kind, file); + return false; + } else { + return true; + } + }); - if (!hasAny) return; + if (!hasAny) return; - return t.callExpression( - t.memberExpression(t.identifier("Object"), t.identifier("defineProperties")), - [node, defineMap.toDefineObject(mutatorMap)] - ); - } -}; + return t.callExpression( + t.memberExpression(t.identifier("Object"), t.identifier("defineProperties")), + [node, defineMap.toDefineObject(mutatorMap)] + ); +} diff --git a/src/babel/transformation/transformers/es6/destructuring.js b/src/babel/transformation/transformers/es6/destructuring.js index 5fb2df518b..53e7b2d22b 100644 --- a/src/babel/transformation/transformers/es6/destructuring.js +++ b/src/babel/transformation/transformers/es6/destructuring.js @@ -1,6 +1,10 @@ import * as messages from "../../../messages"; import * as t from "../../../types"; +export var metadata = { + category: "builtin-advanced" +}; + export function ForOfStatement(node, parent, scope, file) { var left = node.left; diff --git a/src/babel/transformation/transformers/es6/properties.computed.js b/src/babel/transformation/transformers/es6/properties.computed.js index d944844ca0..9c477f8348 100644 --- a/src/babel/transformation/transformers/es6/properties.computed.js +++ b/src/babel/transformation/transformers/es6/properties.computed.js @@ -67,8 +67,8 @@ export var ObjectExpression = { exit(node, parent, scope, file) { var hasComputed = false; - for (var i = 0; i < node.properties.length; i++) { - hasComputed = t.isProperty(node.properties[i], { computed: true, kind: "init" }); + for (var prop of (node.properties: Array)) { + hasComputed = t.isProperty(prop, { computed: true, kind: "init" }); if (hasComputed) break; } diff --git a/src/babel/transformation/transformers/es6/tail-call.js b/src/babel/transformation/transformers/es6/tail-call.js index 12fb5a8a8d..e3a9482067 100644 --- a/src/babel/transformation/transformers/es6/tail-call.js +++ b/src/babel/transformation/transformers/es6/tail-call.js @@ -6,6 +6,10 @@ import * as util from "../../../util"; import map from "lodash/collection/map"; import * as t from "../../../types"; +export var metadata = { + category: "builtin-trailing" +}; + exports.Function = function (node, parent, scope, file) { if (node.generator || node.async) return; var tailCall = new TailCallTransformer(this, scope, file); diff --git a/src/babel/transformation/transformers/index.js b/src/babel/transformation/transformers/index.js index a4e0317b84..7977ee9e9f 100644 --- a/src/babel/transformation/transformers/index.js +++ b/src/babel/transformation/transformers/index.js @@ -1,20 +1,24 @@ export default { - // builtin-basic + //- builtin-setup + _validation: require("./internal/validation"), "utility.removeDebugger": require("./utility/remove-debugger"), "utility.removeConsole": require("./utility/remove-console"), "utility.inlineEnvironmentVariables": require("./utility/inline-environment-variables"), "utility.inlineExpressions": require("./utility/inline-expressions"), "minification.deadCodeElimination": require("./minification/dead-code-elimination"), _modules: require("./internal/modules"), + "spec.functionName": require("./spec/function-name"), + + //- builtin-basic + // this is where the bulk of the ES6 transformations take place, none of them require traversal state + // so they can all be concatenated together for performance "es7.classProperties": require("./es7/class-properties"), "es7.trailingFunctionCommas": require("./es7/trailing-function-commas"), "es7.asyncFunctions": require("./es7/async-functions"), "es7.decorators": require("./es7/decorators"), strict: require("./other/strict"), - _validation: require("./internal/validation"), "validation.undeclaredVariableCheck": require("./validation/undeclared-variable-check"), "validation.react": require("./validation/react"), - "spec.functionName": require("./spec/function-name"), "es6.arrowFunctions": require("./es6/arrow-functions"), "spec.blockScopedFunctions": require("./spec/block-scoped-functions"), "optimisation.react.constantElements": require("./optimisation/react.constant-elements"), @@ -41,30 +45,37 @@ export default { "es6.parameters.rest": require("./es6/parameters.rest"), "es6.spread": require("./es6/spread"), "es6.parameters.default": require("./es6/parameters.default"), - "es6.destructuring": require("./es6/destructuring"), - "es6.tailCall": require("./es6/tail-call"), "es7.exportExtensions": require("./es7/export-extensions"), "spec.protoToAssign": require("./spec/proto-to-assign"), - _shadowFunctions: require("./internal/shadow-functions"), "es7.doExpressions": require("./es7/do-expressions"), "es6.spec.symbols": require("./es6/spec.symbols"), - ludicrous: require("./other/ludicrous"), "spec.undefinedToVoid": require("./spec/undefined-to-void"), jscript: require("./other/jscript"), flow: require("./other/flow"), _hoistDirectives: require("./internal/hoist-directives"), - // builtin-modules - "es6.modules": require("./es6/modules"), - regenerator: require("./other/regenerator"), - runtime: require("./other/runtime"), - _moduleFormatter: require("./internal/module-formatter"), - - // builtin-advanced + //- builtin-advanced + "es6.destructuring": require("./es6/destructuring"), "es6.blockScoping": require("./es6/block-scoping"), "es6.spec.blockScoping": require("./es6/spec.block-scoping"), - // builtin-cleanup + // es6 syntax transformation is **forbidden** past this point since regenerator will chuck a massive + // hissy fit + + //- regenerator + regenerator: require("./other/regenerator"), + + //- builtin-modules + runtime: require("./other/runtime"), + "es6.modules": require("./es6/modules"), + _moduleFormatter: require("./internal/module-formatter"), + + //- builtin-trailing + // these clean up the output and do finishing up transformations, it's important to note that by this + // stage you can't import any new modules or insert new ES6 as all those transformers have already + // been ran + "es6.tailCall": require("./es6/tail-call"), + _shadowFunctions: require("./internal/shadow-functions"), "es3.propertyLiterals": require("./es3/property-literals"), "es3.memberExpressionLiterals": require("./es3/member-expression-literals"), "minification.memberExpressionLiterals": require("./minification/member-expression-literals"), diff --git a/src/babel/transformation/transformers/internal/block-hoist.js b/src/babel/transformation/transformers/internal/block-hoist.js index 700d91b2a9..2ab60a0755 100644 --- a/src/babel/transformation/transformers/internal/block-hoist.js +++ b/src/babel/transformation/transformers/internal/block-hoist.js @@ -1,7 +1,7 @@ import sortBy from "lodash/collection/sortBy"; export var metadata = { - category: "builtin-cleanup" + category: "builtin-trailing" }; // Priority: diff --git a/src/babel/transformation/transformers/internal/modules.js b/src/babel/transformation/transformers/internal/modules.js index 6d3dfe83c8..71d51e7a25 100644 --- a/src/babel/transformation/transformers/internal/modules.js +++ b/src/babel/transformation/transformers/internal/modules.js @@ -6,6 +6,10 @@ import * as t from "../../../types"; +export var metadata = { + category: "builtin-setup" +}; + export function ImportDeclaration(node, parent, scope, file) { if (node.source) { node.source.value = file.resolveModuleSource(node.source.value); diff --git a/src/babel/transformation/transformers/internal/shadow-functions.js b/src/babel/transformation/transformers/internal/shadow-functions.js index decec95409..d082fd1ba8 100644 --- a/src/babel/transformation/transformers/internal/shadow-functions.js +++ b/src/babel/transformation/transformers/internal/shadow-functions.js @@ -86,7 +86,7 @@ function aliasFunction(getBody, path, scope) { // crawling the entire function tree export var metadata = { - category: "builtin-cleanup" + category: "builtin-trailing" }; export var Program = { diff --git a/src/babel/transformation/transformers/internal/validation.js b/src/babel/transformation/transformers/internal/validation.js index da830eabef..31dfabc78e 100644 --- a/src/babel/transformation/transformers/internal/validation.js +++ b/src/babel/transformation/transformers/internal/validation.js @@ -1,6 +1,10 @@ import * as messages from "../../../messages"; import * as t from "../../../types"; +export var metadata = { + category: "builtin-setup" +}; + export function ForOfStatement(node, parent, scope, file) { var left = node.left; if (t.isVariableDeclaration(left)) { diff --git a/src/babel/transformation/transformers/minification/dead-code-elimination.js b/src/babel/transformation/transformers/minification/dead-code-elimination.js index 2d840f991e..58524530bb 100644 --- a/src/babel/transformation/transformers/minification/dead-code-elimination.js +++ b/src/babel/transformation/transformers/minification/dead-code-elimination.js @@ -18,7 +18,8 @@ function toStatements(node) { } export var metadata = { - optional: true + optional: true, + category: "builtin-setup" }; export function Identifier(node, parent, scope) { diff --git a/src/babel/transformation/transformers/minification/member-expression-literals.js b/src/babel/transformation/transformers/minification/member-expression-literals.js index a7d9823535..333bd02201 100644 --- a/src/babel/transformation/transformers/minification/member-expression-literals.js +++ b/src/babel/transformation/transformers/minification/member-expression-literals.js @@ -2,7 +2,7 @@ import * as t from "../../../types"; export var metadata = { optional: true, - category: "builtin-cleanup" + category: "builtin-trailing" }; export var MemberExpression = { diff --git a/src/babel/transformation/transformers/minification/property-literals.js b/src/babel/transformation/transformers/minification/property-literals.js index d1d682fbdb..4bdc31ae59 100644 --- a/src/babel/transformation/transformers/minification/property-literals.js +++ b/src/babel/transformation/transformers/minification/property-literals.js @@ -2,7 +2,7 @@ import * as t from "../../../types"; export var metadata = { optional: true, - category: "builtin-cleanup" + category: "builtin-trailing" }; export var Property = { diff --git a/src/babel/transformation/transformers/other/regenerator.js b/src/babel/transformation/transformers/other/regenerator.js index 9d1b17e675..d276ec9908 100644 --- a/src/babel/transformation/transformers/other/regenerator.js +++ b/src/babel/transformation/transformers/other/regenerator.js @@ -2,11 +2,9 @@ import regenerator from "regenerator"; import * as t from "../../../types"; export var metadata = { - category: "builtin-modules" + category: "regenerator" }; -export var Program = { - exit(ast) { - regenerator.transform(ast); - } -}; +export function Program(ast) { + regenerator.transform(ast); +} diff --git a/src/babel/transformation/transformers/other/runtime/index.js b/src/babel/transformation/transformers/other/runtime/index.js index 3fa283268d..5d9a0dbc44 100644 --- a/src/babel/transformation/transformers/other/runtime/index.js +++ b/src/babel/transformation/transformers/other/runtime/index.js @@ -11,10 +11,10 @@ const RUNTIME_MODULE_NAME = "babel-runtime"; export var metadata = { optional: true, - category: "builtin-modules" + category: "builtin-post-modules" }; -export function Program(node, parent, scope, file) { +export function pre(file) { file.set("helperGenerator", function (name) { return file.addImport(`${RUNTIME_MODULE_NAME}/helpers/${name}`, name, "absoluteDefault"); }); diff --git a/src/babel/transformation/transformers/spec/function-name.js b/src/babel/transformation/transformers/spec/function-name.js index 928036e0be..d45d2a4165 100644 --- a/src/babel/transformation/transformers/spec/function-name.js +++ b/src/babel/transformation/transformers/spec/function-name.js @@ -1,3 +1,7 @@ +export var metadata = { + category: "builtin-setup" +}; + export { bare as FunctionExpression, bare as ArrowFunctionExpression diff --git a/src/babel/transformation/transformers/utility/inline-environment-variables.js b/src/babel/transformation/transformers/utility/inline-environment-variables.js index 97725fbc51..5c00d13ccc 100644 --- a/src/babel/transformation/transformers/utility/inline-environment-variables.js +++ b/src/babel/transformation/transformers/utility/inline-environment-variables.js @@ -1,7 +1,8 @@ import * as t from "../../../types"; export var metadata = { - optional: true + optional: true, + category: "builtin-setup" }; var match = t.buildMatchMemberExpression("process.env"); diff --git a/src/babel/transformation/transformers/utility/inline-expressions.js b/src/babel/transformation/transformers/utility/inline-expressions.js index 437cce640d..5fbb8de75c 100644 --- a/src/babel/transformation/transformers/utility/inline-expressions.js +++ b/src/babel/transformation/transformers/utility/inline-expressions.js @@ -1,7 +1,8 @@ import * as t from "../../../types"; export var metadata = { - optional: true + optional: true, + category: "builtin-setup" }; export function Expression(node, parent, scope) { diff --git a/src/babel/transformation/transformers/utility/remove-console.js b/src/babel/transformation/transformers/utility/remove-console.js index 317840ce3e..88106ee69a 100644 --- a/src/babel/transformation/transformers/utility/remove-console.js +++ b/src/babel/transformation/transformers/utility/remove-console.js @@ -1,7 +1,8 @@ import * as t from "../../../types"; export var metadata = { - optional: true + optional: true, + category: "builtin-setup" }; export function CallExpression(node, parent) { diff --git a/src/babel/transformation/transformers/utility/remove-debugger.js b/src/babel/transformation/transformers/utility/remove-debugger.js index f7d84d0e81..b797c700cc 100644 --- a/src/babel/transformation/transformers/utility/remove-debugger.js +++ b/src/babel/transformation/transformers/utility/remove-debugger.js @@ -1,7 +1,8 @@ import * as t from "../../../types"; export var metadata = { - optional: true + optional: true, + category: "builtin-setup" }; export function ExpressionStatement(node) {