diff --git a/lib/babel/transformation/helpers/name-method.js b/lib/babel/transformation/helpers/name-method.js deleted file mode 100644 index 05cbcf1764..0000000000 --- a/lib/babel/transformation/helpers/name-method.js +++ /dev/null @@ -1,53 +0,0 @@ -"use strict"; - -var util = require("../../util"); -var t = require("../../types"); - -var visitor = { - enter: function (node, parent, scope, state) { - // check if this node is an identifier that matches the same as our function id - if (!t.isIdentifier(node, { name: state.id })) return; - - // check if this node is the one referenced - if (!t.isReferenced(node, parent)) return; - - // check that we don't have a local variable declared as that removes the need - // for the wrapper - var localDeclar = scope.getBindingIdentifier(state.id); - if (localDeclar !== state.outerDeclar) return; - - state.selfReference = true; - this.stop(); - } -}; - -exports.property = function (node, file, scope) { - var key = t.toComputedKey(node, node.key); - if (!t.isLiteral(key)) return node; // we can't set a function id with this - - var id = t.toIdentifier(key.value); - key = t.identifier(id); - - var state = { - id: id, - selfReference: false, - outerDeclar: scope.getBindingIdentifier(id), - }; - - scope.traverse(node, visitor, state); - - var method = node.value; - - if (state.selfReference) { - var templateName = "property-method-assignment-wrapper"; - if (method.generator) templateName += "-generator"; - node.value = util.template(templateName, { - FUNCTION: method, - FUNCTION_ID: key, - FUNCTION_KEY: scope.generateUidIdentifier(id), - WRAPPER_KEY: scope.generateUidIdentifier(id + "Wrapper") - }); - } else { - method.id = key; - } -}; diff --git a/lib/babel/transformation/transformers/es6/classes.js b/lib/babel/transformation/transformers/es6/classes.js index 6d1705bffa..2dfcf42b56 100644 --- a/lib/babel/transformation/transformers/es6/classes.js +++ b/lib/babel/transformation/transformers/es6/classes.js @@ -1,7 +1,6 @@ "use strict"; var ReplaceSupers = require("../../helpers/replace-supers"); -var nameMethod = require("../../helpers/name-method"); var defineMap = require("../../helpers/define-map"); var messages = require("../../../messages"); var util = require("../../../util"); @@ -217,8 +216,6 @@ ClassTransformer.prototype.pushMethod = function (node) { var kind = node.kind; if (kind === "") { - nameMethod.property(node, this.file, this.scope); - if (this.isLoose) { // use assignments instead of define properties for loose classes diff --git a/lib/babel/transformation/transformers/es6/properties.shorthand.js b/lib/babel/transformation/transformers/es6/properties.shorthand.js index fff70cef38..b5e934268c 100644 --- a/lib/babel/transformation/transformers/es6/properties.shorthand.js +++ b/lib/babel/transformation/transformers/es6/properties.shorthand.js @@ -1,8 +1,7 @@ "use strict"; -var nameMethod = require("../../helpers/name-method"); -var t = require("../../../types"); -var clone = require("lodash/lang/clone"); +var clone = require("lodash/lang/clone"); +var t = require("../../../types"); exports.check = function (node) { return t.isProperty(node) && (node.method || node.shorthand); @@ -11,7 +10,6 @@ exports.check = function (node) { exports.Property = function (node, parent, scope, file) { if (node.method) { node.method = false; - nameMethod.property(node, file, scope); } if (node.shorthand) { diff --git a/lib/babel/transformation/transformers/index.js b/lib/babel/transformation/transformers/index.js index 0b2eaad9f3..ae429df0e3 100644 --- a/lib/babel/transformation/transformers/index.js +++ b/lib/babel/transformation/transformers/index.js @@ -1,6 +1,9 @@ module.exports = { useStrict: require("./other/use-strict"), + // this goes at the start so we only transform the original user code + "spec.functionName": require("./spec/function-name"), + "validation.undeclaredVariableCheck": require("./validation/undeclared-variable-check"), "validation.noForInOfAssignment": require("./validation/no-for-in-of-assignment"), "validation.setters": require("./validation/setters"), @@ -91,7 +94,6 @@ module.exports = { "spec.typeofSymbol": require("./spec/typeof-symbol"), "spec.undefinedToVoid": require("./spec/undefined-to-void"), - "spec.functionName": require("./spec/function-name"), _moduleFormatter: require("./internal/module-formatter"), diff --git a/lib/babel/transformation/transformers/spec/function-name.js b/lib/babel/transformation/transformers/spec/function-name.js index 6100d99431..40d57407da 100644 --- a/lib/babel/transformation/transformers/spec/function-name.js +++ b/lib/babel/transformation/transformers/spec/function-name.js @@ -1,53 +1,54 @@ -"use strict"; - -var util = require("../../../util"); -var t = require("../../../types"); - -var propertyFunctionVisitor = { - enter: function (node, parent, scope, state) { - if (t.isReferencedIdentifier(node, parent, { name: state.name }) && scope.getBindingIdentifier(node.name) === state.binding) { - return state.getOuter(); - } - } -}; - -exports.FunctionExpression = function (node, parent, scope, file) { - if (node.id) return; - var id; - if (t.isProperty(parent)) { - id = parent.key; - } else if (t.isVariableDeclarator(parent)) { - id = parent.id; - } else { - return; - } - var binding = scope.getBindingIdentifier(id.name); - var outerId, selfGlobalId; - scope.traverse(node, propertyFunctionVisitor, { - name: id.name, - binding: binding, - - getOuter: function () { - if (!binding) { - return t.memberExpression( - selfGlobalId || (selfGlobalId = file.addHelper("self-global")), - id - ); - } - return t.callExpression( - outerId || (outerId = scope.generateUidIdentifier("getOuter")), - [] - ); - } - }); - node.id = id; - if (outerId) { - return util.template("named-func", { - GET_OUTER_ID: outerId, - ID: id, - FUNCTION: node - }); - } -}; - -exports.optional = true; +"use strict"; + +var t = require("../../../types"); +var util = require("../../../util"); + +var propertyFunctionVisitor = { + enter: function (node, parent, scope, state, file) { + if (t.isReferencedIdentifier(node, parent, { name: state.name }) && scope.getBindingIdentifier(node.name) === state.binding) { + return state.getOuter(); + } + } +}; + +//exports.ArrowFunctionExpression = +exports.FunctionExpression = function (node, parent, scope, file) { + // has an `id` so we don't need to infer one + if (node.id) return; + + var id; + if (t.isProperty(parent) && parent.kind === "init" && !parent.computed) { + // { foo: function () {} }; + id = parent.key; + } else if (t.isVariableDeclarator(parent)) { + // var foo = function () {}; + id = parent.id; + } else { + return; + } + + var binding = scope.getBindingIdentifier(id.name); + var outerId, selfGlobalId; + + scope.traverse(node, propertyFunctionVisitor, { + name: id.name, + binding: binding, + + getOuter: function () { + return t.callExpression( + outerId || (outerId = scope.generateUidIdentifier("getOuter")), + [] + ); + } + }); + + node.id = id; + + if (outerId) { + return util.template("named-func", { + GET_OUTER_ID: outerId, + FUNCTION: node, + ID: id + }); + } +}; diff --git a/test/fixtures/transformation/es6-arrow-functions/arguments/expected.js b/test/fixtures/transformation/es6-arrow-functions/arguments/expected.js index e10b8aad52..99d0d2a083 100644 --- a/test/fixtures/transformation/es6-arrow-functions/arguments/expected.js +++ b/test/fixtures/transformation/es6-arrow-functions/arguments/expected.js @@ -15,7 +15,7 @@ function two() { return _arguments; }; - var another = function () { + var another = function another() { var _arguments2 = arguments; var inner2 = function () { return _arguments2; @@ -54,7 +54,7 @@ five({ arguments: ["foo"] }); function six(obj) { var fn = function () { - var fn2 = function () { + var fn2 = function fn2() { return arguments[0]; }; return fn2("foobar"); diff --git a/test/fixtures/transformation/es6-arrow-functions/nested/expected.js b/test/fixtures/transformation/es6-arrow-functions/nested/expected.js index 022b06abd0..1e6a139baa 100644 --- a/test/fixtures/transformation/es6-arrow-functions/nested/expected.js +++ b/test/fixtures/transformation/es6-arrow-functions/nested/expected.js @@ -1,7 +1,7 @@ "use strict"; module.exports = { - init: function () { + init: function init() { var _this = this; return new Promise(function (resolve, reject) { MongoClient.connect(config.mongodb, function (err, db) { diff --git a/test/fixtures/transformation/es6-destructuring/parameters/expected.js b/test/fixtures/transformation/es6-destructuring/parameters/expected.js index 9608e1e16e..c61ec13955 100644 --- a/test/fixtures/transformation/es6-destructuring/parameters/expected.js +++ b/test/fixtures/transformation/es6-destructuring/parameters/expected.js @@ -19,7 +19,7 @@ function unpackObject(_ref) { console.log(unpackObject({ title: "title", author: "author" })); -var unpackArray = function (_ref, _ref3) { +var unpackArray = function unpackArray(_ref, _ref3) { var _ref2 = _slicedToArray(_ref, 3); var a = _ref2[0]; diff --git a/test/fixtures/transformation/es6-modules-amd/exports-variable/expected.js b/test/fixtures/transformation/es6-modules-amd/exports-variable/expected.js index 7d2314001f..04bcc7cdb6 100644 --- a/test/fixtures/transformation/es6-modules-amd/exports-variable/expected.js +++ b/test/fixtures/transformation/es6-modules-amd/exports-variable/expected.js @@ -7,7 +7,7 @@ define(["exports"], function (exports) { var foo = exports.foo = 1; var foo = exports.foo = 1; var bar = exports.bar = 2; - var foo2 = exports.foo2 = function () {}; + var foo2 = exports.foo2 = function foo2() {}; var foo3 = exports.foo3 = undefined; var foo4 = exports.foo4 = 2; var foo5 = exports.foo5 = undefined; @@ -20,4 +20,4 @@ define(["exports"], function (exports) { Object.defineProperty(exports, "__esModule", { value: true }); -}); \ No newline at end of file +}); diff --git a/test/fixtures/transformation/es6-modules-common/exports-variable/expected.js b/test/fixtures/transformation/es6-modules-common/exports-variable/expected.js index 157b333520..cb2657cfde 100644 --- a/test/fixtures/transformation/es6-modules-common/exports-variable/expected.js +++ b/test/fixtures/transformation/es6-modules-common/exports-variable/expected.js @@ -6,7 +6,7 @@ exports.foo7 = foo7; var foo = exports.foo = 1; var foo = exports.foo = 1; var bar = exports.bar = 2; -var foo2 = exports.foo2 = function () {}; +var foo2 = exports.foo2 = function foo2() {}; var foo3 = exports.foo3 = undefined; var foo4 = exports.foo4 = 2; var foo5 = exports.foo5 = undefined; @@ -18,4 +18,4 @@ var foo8 = exports.foo8 = function foo8() { Object.defineProperty(exports, "__esModule", { value: true -}); \ No newline at end of file +}); diff --git a/test/fixtures/transformation/es6-modules-ignore/exports-variable/expected.js b/test/fixtures/transformation/es6-modules-ignore/exports-variable/expected.js index bff6026c5a..3d7c5adad2 100644 --- a/test/fixtures/transformation/es6-modules-ignore/exports-variable/expected.js +++ b/test/fixtures/transformation/es6-modules-ignore/exports-variable/expected.js @@ -5,7 +5,7 @@ var _classCallCheck = function (instance, Constructor) { if (!(instance instance var foo = 1; var foo = 1, bar = 2; -var foo2 = function () {}; +var foo2 = function foo2() {}; var foo3 = undefined; var foo4 = 2; var foo5 = undefined; @@ -13,4 +13,4 @@ var foo6 = 3; function foo7() {} var foo8 = function foo8() { _classCallCheck(this, foo8); -}; \ No newline at end of file +}; diff --git a/test/fixtures/transformation/es6-modules-system/exports-variable/expected.js b/test/fixtures/transformation/es6-modules-system/exports-variable/expected.js index 770cb08145..969d94b8dc 100644 --- a/test/fixtures/transformation/es6-modules-system/exports-variable/expected.js +++ b/test/fixtures/transformation/es6-modules-system/exports-variable/expected.js @@ -11,7 +11,7 @@ System.register([], function (_export) { _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; foo = _export("foo", 1); - foo2 = _export("foo2", function () {}); + foo2 = _export("foo2", function foo2() {}); foo3 = _export("foo3", undefined); foo4 = _export("foo4", 2); foo5 = _export("foo5", undefined); @@ -22,4 +22,4 @@ System.register([], function (_export) { _export("foo3", foo3 = 5); } }; -}); \ No newline at end of file +}); diff --git a/test/fixtures/transformation/es6-modules-umd/exports-variable/expected.js b/test/fixtures/transformation/es6-modules-umd/exports-variable/expected.js index e2f8a293c8..55c06d85b3 100644 --- a/test/fixtures/transformation/es6-modules-umd/exports-variable/expected.js +++ b/test/fixtures/transformation/es6-modules-umd/exports-variable/expected.js @@ -13,7 +13,7 @@ var foo = exports.foo = 1; var foo = exports.foo = 1; var bar = exports.bar = 2; - var foo2 = exports.foo2 = function () {}; + var foo2 = exports.foo2 = function foo2() {}; var foo3 = exports.foo3 = undefined; var foo4 = exports.foo4 = 2; var foo5 = exports.foo5 = undefined; @@ -26,4 +26,4 @@ Object.defineProperty(exports, "__esModule", { value: true }); -}); \ No newline at end of file +}); diff --git a/test/fixtures/transformation/es6-parameters.rest/multiple/expected.js b/test/fixtures/transformation/es6-parameters.rest/multiple/expected.js index 0efb583e37..470cd17396 100644 --- a/test/fixtures/transformation/es6-parameters.rest/multiple/expected.js +++ b/test/fixtures/transformation/es6-parameters.rest/multiple/expected.js @@ -1,6 +1,6 @@ "use strict"; -var t = function (f) { +var t = function t(f) { for (var _len = arguments.length, items = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { items[_key - 1] = arguments[_key]; } diff --git a/test/fixtures/transformation/es6-parameters.rest/pattern/expected.js b/test/fixtures/transformation/es6-parameters.rest/pattern/expected.js index 56ad12447e..821f0dfae6 100644 --- a/test/fixtures/transformation/es6-parameters.rest/pattern/expected.js +++ b/test/fixtures/transformation/es6-parameters.rest/pattern/expected.js @@ -1,6 +1,6 @@ "use strict"; -var foo = function () { +var foo = function foo() { for (var _len = arguments.length, _ref = Array(_len), _key = 0; _key < _len; _key++) { _ref[_key] = arguments[_key]; } diff --git a/test/fixtures/transformation/es6-parameters.rest/single/expected.js b/test/fixtures/transformation/es6-parameters.rest/single/expected.js index f9229070eb..101e2f256c 100644 --- a/test/fixtures/transformation/es6-parameters.rest/single/expected.js +++ b/test/fixtures/transformation/es6-parameters.rest/single/expected.js @@ -1,6 +1,6 @@ "use strict"; -var t = function () { +var t = function t() { for (var _len = arguments.length, items = Array(_len), _key = 0; _key < _len; _key++) { items[_key] = arguments[_key]; } diff --git a/test/fixtures/transformation/playground/method-binding/expected.js b/test/fixtures/transformation/playground/method-binding/expected.js index 156bcfffd5..135ef9247d 100644 --- a/test/fixtures/transformation/playground/method-binding/expected.js +++ b/test/fixtures/transformation/playground/method-binding/expected.js @@ -14,7 +14,7 @@ var fn = (_obj$foo3 = obj[foo()], _obj$foo3.method.bind(_obj$foo3)); return _val2.toFixed(_args2[0]); })); -var get = function () { +var get = function get() { return 2; }; [1.1234, 23.53245, 3].map((_args3 = [get()], function (_val3) { diff --git a/test/fixtures/transformation/react/arrow-functions/expected.js b/test/fixtures/transformation/react/arrow-functions/expected.js index 0a2f07f0c8..0b69bab154 100644 --- a/test/fixtures/transformation/react/arrow-functions/expected.js +++ b/test/fixtures/transformation/react/arrow-functions/expected.js @@ -1,11 +1,11 @@ -var foo = function () { +var foo = function foo() { var _this = this; return function () { return React.createElement(_this, null); }; }; -var bar = function () { +var bar = function bar() { var _this = this; return function () { return React.createElement(_this.foo, null); diff --git a/test/fixtures/transformation/react/display-name-assignment-expression/actual.js b/test/fixtures/transformation/react/display-name-assignment-expression/actual.js index 7a91ae99d3..5b06299bc0 100644 --- a/test/fixtures/transformation/react/display-name-assignment-expression/actual.js +++ b/test/fixtures/transformation/react/display-name-assignment-expression/actual.js @@ -1,6 +1,6 @@ var Component; Component = React.createClass({ - render: function () { + render: function render() { return null; } }); diff --git a/test/fixtures/transformation/react/display-name-assignment-expression/expected.js b/test/fixtures/transformation/react/display-name-assignment-expression/expected.js index 86a1009408..12fa356793 100644 --- a/test/fixtures/transformation/react/display-name-assignment-expression/expected.js +++ b/test/fixtures/transformation/react/display-name-assignment-expression/expected.js @@ -1,7 +1,7 @@ var Component; Component = React.createClass({ displayName: "Component", - render: function () { + render: function render() { return null; } }); diff --git a/test/fixtures/transformation/react/display-name-export-default/actual.js b/test/fixtures/transformation/react/display-name-export-default/actual.js index 370f77173c..4331da16ef 100644 --- a/test/fixtures/transformation/react/display-name-export-default/actual.js +++ b/test/fixtures/transformation/react/display-name-export-default/actual.js @@ -1,5 +1,5 @@ export default React.createClass({ - render: function () { + render: function render() { return null; } }); diff --git a/test/fixtures/transformation/react/display-name-export-default/expected.js b/test/fixtures/transformation/react/display-name-export-default/expected.js index 182f4fe315..b97066d1e2 100644 --- a/test/fixtures/transformation/react/display-name-export-default/expected.js +++ b/test/fixtures/transformation/react/display-name-export-default/expected.js @@ -1,6 +1,6 @@ module.exports = React.createClass({ displayName: "actual", - render: function () { + render: function render() { return null; } }); diff --git a/test/fixtures/transformation/react/display-name-if-missing/actual.js b/test/fixtures/transformation/react/display-name-if-missing/actual.js index 501a447782..0ecab87f02 100644 --- a/test/fixtures/transformation/react/display-name-if-missing/actual.js +++ b/test/fixtures/transformation/react/display-name-if-missing/actual.js @@ -1,6 +1,6 @@ var Whateva = React.createClass({ displayName: "Whatever", - render: function () { + render: function render() { return null; } }); diff --git a/test/fixtures/transformation/react/display-name-if-missing/expected.js b/test/fixtures/transformation/react/display-name-if-missing/expected.js index 64f8f0b30d..dfbc9790ce 100644 --- a/test/fixtures/transformation/react/display-name-if-missing/expected.js +++ b/test/fixtures/transformation/react/display-name-if-missing/expected.js @@ -1,6 +1,6 @@ var Whateva = React.createClass({ displayName: "Whatever", - render: function () { + render: function render() { return null; } }); diff --git a/test/fixtures/transformation/react/display-name-object-declaration/actual.js b/test/fixtures/transformation/react/display-name-object-declaration/actual.js index 27aa16fb2f..904383aab8 100644 --- a/test/fixtures/transformation/react/display-name-object-declaration/actual.js +++ b/test/fixtures/transformation/react/display-name-object-declaration/actual.js @@ -1,6 +1,6 @@ exports = { Component: React.createClass({ - render: function () { + render: function render() { return null; } }) diff --git a/test/fixtures/transformation/react/display-name-object-declaration/expected.js b/test/fixtures/transformation/react/display-name-object-declaration/expected.js index 5203f7a7c5..e6565826f5 100644 --- a/test/fixtures/transformation/react/display-name-object-declaration/expected.js +++ b/test/fixtures/transformation/react/display-name-object-declaration/expected.js @@ -1,7 +1,7 @@ exports = { Component: React.createClass({ displayName: "Component", - render: function () { + render: function render() { return null; } }) diff --git a/test/fixtures/transformation/react/display-name-property-assignment/actual.js b/test/fixtures/transformation/react/display-name-property-assignment/actual.js index 88f7a1df11..d9d2c1ad66 100644 --- a/test/fixtures/transformation/react/display-name-property-assignment/actual.js +++ b/test/fixtures/transformation/react/display-name-property-assignment/actual.js @@ -1,5 +1,5 @@ exports.Component = React.createClass({ - render: function () { + render: function render() { return null; } }); diff --git a/test/fixtures/transformation/react/display-name-property-assignment/expected.js b/test/fixtures/transformation/react/display-name-property-assignment/expected.js index 2aacec1437..e08bf5d35d 100644 --- a/test/fixtures/transformation/react/display-name-property-assignment/expected.js +++ b/test/fixtures/transformation/react/display-name-property-assignment/expected.js @@ -1,6 +1,6 @@ exports.Component = React.createClass({ displayName: "Component", - render: function () { + render: function render() { return null; } }); diff --git a/test/fixtures/transformation/react/display-name-variable-declaration/actual.js b/test/fixtures/transformation/react/display-name-variable-declaration/actual.js index 2ce6d608db..1d32784d13 100644 --- a/test/fixtures/transformation/react/display-name-variable-declaration/actual.js +++ b/test/fixtures/transformation/react/display-name-variable-declaration/actual.js @@ -1,5 +1,5 @@ var Component = React.createClass({ - render: function () { + render: function render() { return null; } }); diff --git a/test/fixtures/transformation/react/display-name-variable-declaration/expected.js b/test/fixtures/transformation/react/display-name-variable-declaration/expected.js index d52d1f31d6..d6393b0105 100644 --- a/test/fixtures/transformation/react/display-name-variable-declaration/expected.js +++ b/test/fixtures/transformation/react/display-name-variable-declaration/expected.js @@ -1,6 +1,6 @@ var Component = React.createClass({ displayName: "Component", - render: function () { + render: function render() { return null; } });