From 6f7da38957c389e53af7aaf480ef8e48d8c6a514 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Thu, 15 Jan 2015 20:07:32 +1100 Subject: [PATCH] abstract out named method helper --- .../transformation/helpers/name-method.js | 43 ++++++++++++++++ .../transformers/es6-classes.js | 3 +- .../es6-property-method-assignment.js | 49 ++----------------- 3 files changed, 50 insertions(+), 45 deletions(-) create mode 100644 lib/6to5/transformation/helpers/name-method.js diff --git a/lib/6to5/transformation/helpers/name-method.js b/lib/6to5/transformation/helpers/name-method.js new file mode 100644 index 0000000000..00d3c77a08 --- /dev/null +++ b/lib/6to5/transformation/helpers/name-method.js @@ -0,0 +1,43 @@ +var traverse = require("../../traverse"); +var util = require("../../util"); +var t = require("../../types"); + +module.exports = 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 selfReference = false; + var outerDeclar = scope.get(id, true); + + traverse(node, { + enter: function (node, parent, scope) { + // check if this node is an identifier that matches the same as our function id + if (!t.isIdentifier(node, { name: 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.get(id, true); + if (localDeclar !== outerDeclar) return; + + selfReference = true; + this.stop(); + } + }, scope); + + if (selfReference) { + node.value = util.template("property-method-assignment-wrapper", { + FUNCTION: node.value, + FUNCTION_ID: key, + FUNCTION_KEY: file.generateUidIdentifier(id, scope), + WRAPPER_KEY: file.generateUidIdentifier(id + "Wrapper", scope) + }); + } else { + node.value.id = key; + } +}; diff --git a/lib/6to5/transformation/transformers/es6-classes.js b/lib/6to5/transformation/transformers/es6-classes.js index 04be814120..1259e39f31 100644 --- a/lib/6to5/transformation/transformers/es6-classes.js +++ b/lib/6to5/transformation/transformers/es6-classes.js @@ -1,4 +1,5 @@ var propertyMethodAssignment = require("./es6-property-method-assignment"); +var nameMethod = require("../helpers/name-method"); var traverse = require("../../traverse"); var util = require("../../util"); var t = require("../../types"); @@ -194,7 +195,7 @@ Class.prototype.pushMethod = function (node) { var kind = node.kind; if (kind === "") { - propertyMethodAssignment._namedMethod(node, this.file, this.scope); + nameMethod(node, this.file, this.scope); if (this.isLoose) { // use assignments instead of define properties for loose classes diff --git a/lib/6to5/transformation/transformers/es6-property-method-assignment.js b/lib/6to5/transformation/transformers/es6-property-method-assignment.js index 5dbe72c55a..99399cb117 100644 --- a/lib/6to5/transformation/transformers/es6-property-method-assignment.js +++ b/lib/6to5/transformation/transformers/es6-property-method-assignment.js @@ -1,53 +1,14 @@ -var traverse = require("../../traverse"); -var util = require("../../util"); -var t = require("../../types"); - -exports._namedMethod = 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 selfReference = false; - var outerDeclar = scope.get(id, true); - - traverse(node, { - enter: function (node, parent, scope) { - // check if this node is an identifier that matches the same as our function id - if (!t.isIdentifier(node, { name: 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.get(id, true); - if (localDeclar !== outerDeclar) return; - - selfReference = true; - this.stop(); - } - }, scope); - - if (selfReference) { - node.value = util.template("property-method-assignment-wrapper", { - FUNCTION: node.value, - FUNCTION_ID: key, - FUNCTION_KEY: file.generateUidIdentifier(id, scope), - WRAPPER_KEY: file.generateUidIdentifier(id + "Wrapper", scope) - }); - } else { - node.value.id = key; - } -}; +var nameMethod = require("../helpers/name-method"); +var traverse = require("../../traverse"); +var util = require("../../util"); +var t = require("../../types"); exports.Property = function (node, parent, file, scope) { if (!node.method) return; node.method = false; - exports._namedMethod(node, file, scope); + nameMethod(node, file, scope); }; exports.ObjectExpression = function (node) {