From 41d60a85e9bbcabe2641bb89e2da527ad7030c56 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Fri, 2 Jan 2015 00:36:54 +1100 Subject: [PATCH] simplify define property by using a helper in playground object getter memoization --- .../playground-object-getter-memoization.js | 11 +-- .../object-getter-memoization/expected.js | 70 +++++-------------- 2 files changed, 23 insertions(+), 58 deletions(-) diff --git a/lib/6to5/transformation/transformers/playground-object-getter-memoization.js b/lib/6to5/transformation/transformers/playground-object-getter-memoization.js index 5d05abe82c..39f8b711d7 100644 --- a/lib/6to5/transformation/transformers/playground-object-getter-memoization.js +++ b/lib/6to5/transformation/transformers/playground-object-getter-memoization.js @@ -3,7 +3,7 @@ var util = require("../../util"); var t = require("../../types"); exports.Property = -exports.MethodDefinition = function (node) { +exports.MethodDefinition = function (node, parent, file) { if (node.kind !== "memo") return; node.kind = "get"; @@ -21,10 +21,11 @@ exports.MethodDefinition = function (node) { if (t.isFunction(node)) return; if (t.isReturnStatement(node) && node.argument) { - node.argument = t.memberExpression(util.template("object-getter-memoization", { - KEY: key, - VALUE: node.argument - }), key, true); + node.argument = t.memberExpression(t.callExpression(file.addDeclaration("define-property"), [ + t.thisExpression(), + key, + node.argument + ]), key, true); } } }); diff --git a/test/fixtures/transformation/playground/object-getter-memoization/expected.js b/test/fixtures/transformation/playground/object-getter-memoization/expected.js index f0318bbdf6..5478190460 100644 --- a/test/fixtures/transformation/playground/object-getter-memoization/expected.js +++ b/test/fixtures/transformation/playground/object-getter-memoization/expected.js @@ -5,60 +5,24 @@ var _prototypeProperties = function (child, staticProps, instanceProps) { if (instanceProps) Object.defineProperties(child.prototype, instanceProps); }; +var _defineProperty = function (obj, key, value) { + return Object.defineProperty(obj, key, { + value: value, + enumerable: true, + configurable: true, + writable: true + }); +}; + var Foo = function Foo() {}; -_prototypeProperties(Foo, null, (function (_ref) { - _ref[bar] = { - get: function () { - return Object.defineProperty(this, bar, { - enumerable: true, - configurable: true, - writable: true, - value: complex() - })[bar]; - }, - enumerable: true - }; - return _ref; -})({ - bar: { - get: function () { - return Object.defineProperty(this, "bar", { - enumerable: true, - configurable: true, - writable: true, - value: complex() - }).bar; - }, - enumerable: true - } +_prototypeProperties(Foo, null, _defineProperty({}, bar, { + get: function () { + return _defineProperty(this, bar, complex())[bar]; + }, + enumerable: true })); -var foo = (function (_foo) { - _foo[bar] = function () { - return Object.defineProperty(this, bar, { - enumerable: true, - configurable: true, - writable: true, - value: complex() - })[bar]; - }; - - return _foo; -})((function (_ref2) { - Object.defineProperties(_ref2, { - bar: { - get: function () { - return Object.defineProperty(this, "bar", { - enumerable: true, - configurable: true, - writable: true, - value: complex() - }).bar; - }, - enumerable: true - } - }); - - return _ref2; -})({})); +var foo = _defineProperty({}, bar, function () { + return _defineProperty(this, bar, complex())[bar]; +});