diff --git a/lib/6to5/transformation/helpers/define-map.js b/lib/6to5/transformation/helpers/define-map.js new file mode 100644 index 0000000000..0019351782 --- /dev/null +++ b/lib/6to5/transformation/helpers/define-map.js @@ -0,0 +1,71 @@ +var clone = require("lodash/lang/clone"); +var each = require("lodash/collection/each"); +var has = require("lodash/object/has"); +var t = require("../../types"); + +exports.push = function (mutatorMap, key, kind, computed, value) { + var alias; + + if (t.isIdentifier(key)) { + alias = key.name; + if (computed) alias = "computed:" + alias; + } else if (t.isLiteral(key)) { + alias = String(key.value); + } else { + alias = JSON.stringify(traverse.removeProperties(cloneDeep(key))); + } + + var map; + if (has(mutatorMap, alias)) { + map = mutatorMap[alias]; + } else { + map = {}; + } + mutatorMap[alias] = map; + + map._key = key; + if (computed) { + map._computed = true; + } + + map[kind] = value; +}; + +exports.build = function (mutatorMap) { + var objExpr = t.objectExpression([]); + + each(mutatorMap, function (map) { + var mapNode = t.objectExpression([]); + + var propNode = t.property("init", map._key, mapNode, map._computed); + + if (!map.get && !map.set) { + map.writable = t.literal(true); + } + + if (map.enumerable === false) { + delete map.enumerable; + } else { + map.enumerable = t.literal(true); + } + + map.configurable = t.literal(true); + + each(map, function (node, key) { + if (key[0] === "_") return; + + node = clone(node); + var inheritNode = node; + if (t.isMethodDefinition(node)) node = node.value; + + var prop = t.property("init", t.identifier(key), node); + t.inheritsComments(prop, inheritNode); + t.removeComments(inheritNode); + mapNode.properties.push(prop); + }); + + objExpr.properties.push(propNode); + }); + + return objExpr; +}; diff --git a/lib/6to5/transformation/transformers/es5/properties.mutators.js b/lib/6to5/transformation/transformers/es5/properties.mutators.js index ede62a061d..515ed5fd01 100644 --- a/lib/6to5/transformation/transformers/es5/properties.mutators.js +++ b/lib/6to5/transformation/transformers/es5/properties.mutators.js @@ -1,7 +1,8 @@ "use strict"; -var util = require("../../../util"); -var t = require("../../../types"); +var defineMap = require("../../helpers/define-map"); +var util = require("../../../util"); +var t = require("../../../types"); exports.ObjectExpression = function (node) { var mutatorMap = {}; @@ -10,7 +11,7 @@ exports.ObjectExpression = function (node) { node.properties = node.properties.filter(function (prop) { if (prop.kind === "get" || prop.kind === "set") { hasAny = true; - util.pushMutatorMap(mutatorMap, prop.key, prop.kind, prop.computed, prop.value); + defineMap.push(mutatorMap, prop.key, prop.kind, prop.computed, prop.value); return false; } else { return true; @@ -21,6 +22,6 @@ exports.ObjectExpression = function (node) { return t.callExpression( t.memberExpression(t.identifier("Object"), t.identifier("defineProperties")), - [node, util.buildDefineProperties(mutatorMap)] + [node, defineMap.build(mutatorMap)] ); }; diff --git a/lib/6to5/transformation/transformers/es6/classes.js b/lib/6to5/transformation/transformers/es6/classes.js index acd0af6087..1b025d72f6 100644 --- a/lib/6to5/transformation/transformers/es6/classes.js +++ b/lib/6to5/transformation/transformers/es6/classes.js @@ -2,6 +2,7 @@ var ReplaceSupers = require("../../helpers/replace-supers"); var nameMethod = require("../../helpers/name-method"); +var defineMap = require("../../helpers/define-map"); var util = require("../../../util"); var t = require("../../../types"); @@ -173,11 +174,11 @@ Class.prototype.buildBody = function () { var staticProps; if (this.hasInstanceMutators) { - instanceProps = util.buildDefineProperties(this.instanceMutatorMap); + instanceProps = defineMap.build(this.instanceMutatorMap); } if (this.hasStaticMutators) { - staticProps = util.buildDefineProperties(this.staticMutatorMap); + staticProps = defineMap.build(this.staticMutatorMap); } if (instanceProps || staticProps) { @@ -230,8 +231,8 @@ Class.prototype.pushMethod = function (node) { this.hasInstanceMutators = true; } - util.pushMutatorMap(mutatorMap, methodName, kind, node.computed, node); - util.pushMutatorMap(mutatorMap, methodName, "enumerable", node.computed, false); + defineMap.push(mutatorMap, methodName, kind, node.computed, node); + defineMap.push(mutatorMap, methodName, "enumerable", node.computed, false); }; /** diff --git a/lib/6to5/util.js b/lib/6to5/util.js index bf541e1a8a..17f120f288 100644 --- a/lib/6to5/util.js +++ b/lib/6to5/util.js @@ -81,73 +81,6 @@ exports.sourceMapToComment = function (map) { return "//# sourceMappingURL=data:application/json;base64," + base64; }; -exports.pushMutatorMap = function (mutatorMap, key, kind, computed, value) { - var alias; - - if (t.isIdentifier(key)) { - alias = key.name; - if (computed) alias = "computed:" + alias; - } else if (t.isLiteral(key)) { - alias = String(key.value); - } else { - alias = JSON.stringify(traverse.removeProperties(cloneDeep(key))); - } - - var map; - if (has(mutatorMap, alias)) { - map = mutatorMap[alias]; - } else { - map = {}; - } - mutatorMap[alias] = map; - - map._key = key; - if (computed) { - map._computed = true; - } - - map[kind] = value; -}; - -exports.buildDefineProperties = function (mutatorMap) { - var objExpr = t.objectExpression([]); - - each(mutatorMap, function (map) { - var mapNode = t.objectExpression([]); - - var propNode = t.property("init", map._key, mapNode, map._computed); - - if (!map.get && !map.set) { - map.writable = t.literal(true); - } - - if (map.enumerable === false) { - delete map.enumerable; - } else { - map.enumerable = t.literal(true); - } - - map.configurable = t.literal(true); - - each(map, function (node, key) { - if (key[0] === "_") return; - - node = clone(node); - var inheritNode = node; - if (t.isMethodDefinition(node)) node = node.value; - - var prop = t.property("init", t.identifier(key), node); - t.inheritsComments(prop, inheritNode); - t.removeComments(inheritNode); - mapNode.properties.push(prop); - }); - - objExpr.properties.push(propNode); - }); - - return objExpr; -}; - var templateVisitor = { enter: function (node, parent, scope, context, nodes) { if (t.isIdentifier(node) && has(nodes, node.name)) {