From 1376f2f18f66f688eceaf5c131f077970f4f18e2 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Mon, 23 Mar 2015 17:26:54 +1100 Subject: [PATCH] move helper templates to a separate namespace --- src/babel/transformation/file/index.js | 3 +- .../templates/apply-constructor.js | 5 --- .../templates/create-decorated-class.js | 30 --------------- ...erator.js => helper-async-to-generator.js} | 0 .../templates/{bind.js => helper-bind.js} | 0 ...ll-check.js => helper-class-call-check.js} | 0 ...create-class.js => helper-create-class.js} | 9 +++-- .../helper-create-decorated-class.js | 37 +++++++++++++++++++ .../{defaults.js => helper-defaults.js} | 0 ...-property.js => helper-define-property.js} | 0 .../{extends.js => helper-extends.js} | 0 .../templates/{get.js => helper-get.js} | 0 .../{has-own.js => helper-has-own.js} | 0 .../{inherits.js => helper-inherits.js} | 0 ....js => helper-interop-require-wildcard.js} | 0 ...p-require.js => helper-interop-require.js} | 0 ...s => helper-object-destructuring-empty.js} | 0 ...js => helper-object-without-properties.js} | 0 .../{self-global.js => helper-self-global.js} | 0 .../templates/{set.js => helper-set.js} | 0 ...ose.js => helper-sliced-to-array-loose.js} | 0 ...-to-array.js => helper-sliced-to-array.js} | 0 ...> helper-tagged-template-literal-loose.js} | 0 ...l.js => helper-tagged-template-literal.js} | 0 ...d.js => helper-temporal-assert-defined.js} | 0 ...efined.js => helper-temporal-undefined.js} | 0 .../{to-array.js => helper-to-array.js} | 0 ...array.js => helper-to-consumable-array.js} | 0 .../templates/{typeof.js => helper-typeof.js} | 0 29 files changed, 43 insertions(+), 41 deletions(-) delete mode 100644 src/babel/transformation/templates/apply-constructor.js delete mode 100644 src/babel/transformation/templates/create-decorated-class.js rename src/babel/transformation/templates/{async-to-generator.js => helper-async-to-generator.js} (100%) rename src/babel/transformation/templates/{bind.js => helper-bind.js} (100%) rename src/babel/transformation/templates/{class-call-check.js => helper-class-call-check.js} (100%) rename src/babel/transformation/templates/{create-class.js => helper-create-class.js} (57%) create mode 100644 src/babel/transformation/templates/helper-create-decorated-class.js rename src/babel/transformation/templates/{defaults.js => helper-defaults.js} (100%) rename src/babel/transformation/templates/{define-property.js => helper-define-property.js} (100%) rename src/babel/transformation/templates/{extends.js => helper-extends.js} (100%) rename src/babel/transformation/templates/{get.js => helper-get.js} (100%) rename src/babel/transformation/templates/{has-own.js => helper-has-own.js} (100%) rename src/babel/transformation/templates/{inherits.js => helper-inherits.js} (100%) rename src/babel/transformation/templates/{interop-require-wildcard.js => helper-interop-require-wildcard.js} (100%) rename src/babel/transformation/templates/{interop-require.js => helper-interop-require.js} (100%) rename src/babel/transformation/templates/{object-destructuring-empty.js => helper-object-destructuring-empty.js} (100%) rename src/babel/transformation/templates/{object-without-properties.js => helper-object-without-properties.js} (100%) rename src/babel/transformation/templates/{self-global.js => helper-self-global.js} (100%) rename src/babel/transformation/templates/{set.js => helper-set.js} (100%) rename src/babel/transformation/templates/{sliced-to-array-loose.js => helper-sliced-to-array-loose.js} (100%) rename src/babel/transformation/templates/{sliced-to-array.js => helper-sliced-to-array.js} (100%) rename src/babel/transformation/templates/{tagged-template-literal-loose.js => helper-tagged-template-literal-loose.js} (100%) rename src/babel/transformation/templates/{tagged-template-literal.js => helper-tagged-template-literal.js} (100%) rename src/babel/transformation/templates/{temporal-assert-defined.js => helper-temporal-assert-defined.js} (100%) rename src/babel/transformation/templates/{temporal-undefined.js => helper-temporal-undefined.js} (100%) rename src/babel/transformation/templates/{to-array.js => helper-to-array.js} (100%) rename src/babel/transformation/templates/{to-consumable-array.js => helper-to-consumable-array.js} (100%) rename src/babel/transformation/templates/{typeof.js => helper-typeof.js} (100%) diff --git a/src/babel/transformation/file/index.js b/src/babel/transformation/file/index.js index cd4417b4df..accbe5f09a 100644 --- a/src/babel/transformation/file/index.js +++ b/src/babel/transformation/file/index.js @@ -58,7 +58,6 @@ export default class File { "defaults", "create-class", "create-decorated-class", - "apply-constructor", "tagged-template-literal", "tagged-template-literal-loose", "interop-require", @@ -324,7 +323,7 @@ export default class File { var id = t.identifier(t.toIdentifier(name)); return t.memberExpression(runtime, id); } else { - var ref = util.template(name); + var ref = util.template("helper-" + name); ref._compact = true; var uid = this.scope.generateUidIdentifier(name); this.scope.push({ diff --git a/src/babel/transformation/templates/apply-constructor.js b/src/babel/transformation/templates/apply-constructor.js deleted file mode 100644 index db337be303..0000000000 --- a/src/babel/transformation/templates/apply-constructor.js +++ /dev/null @@ -1,5 +0,0 @@ -(function (Constructor, args) { - var instance = Object.create(Constructor.prototype); - var result = Constructor.apply(instance, args); - return result != null && (typeof result == "object" || typeof result == "function") ? result : instance; -}); diff --git a/src/babel/transformation/templates/create-decorated-class.js b/src/babel/transformation/templates/create-decorated-class.js deleted file mode 100644 index 2a16ebf5b9..0000000000 --- a/src/babel/transformation/templates/create-decorated-class.js +++ /dev/null @@ -1,30 +0,0 @@ -(function() { - function defineProperties(target, props) { - for (var i = 0; i < props.length; i ++) { - var descriptor = props[i]; - - descriptor.enumerable = false; - descriptor.configurable = true; - if (descriptor.value) descriptor.writable = true; - - if (descriptor.decorators) { - for (var i = 0; i < descriptor.decorators.length; i++) { - var decorator = descriptor.decorators[i]; - if (typeof decorator === "function") { - descriptor = decorator(target, descriptor.key, descriptor) || descriptor; - } else { - throw new TypeError("The decorator for method " + descriptor.key + " is of the invalid type " + typeof decorator); - } - } - } - - Object.defineProperty(target, descriptor.key, descriptor); - } - } - - return function (Constructor, protoProps, staticProps) { - if (protoProps) defineProperties(Constructor.prototype, protoProps); - if (staticProps) defineProperties(Constructor, staticProps); - return Constructor; - }; -})() diff --git a/src/babel/transformation/templates/async-to-generator.js b/src/babel/transformation/templates/helper-async-to-generator.js similarity index 100% rename from src/babel/transformation/templates/async-to-generator.js rename to src/babel/transformation/templates/helper-async-to-generator.js diff --git a/src/babel/transformation/templates/bind.js b/src/babel/transformation/templates/helper-bind.js similarity index 100% rename from src/babel/transformation/templates/bind.js rename to src/babel/transformation/templates/helper-bind.js diff --git a/src/babel/transformation/templates/class-call-check.js b/src/babel/transformation/templates/helper-class-call-check.js similarity index 100% rename from src/babel/transformation/templates/class-call-check.js rename to src/babel/transformation/templates/helper-class-call-check.js diff --git a/src/babel/transformation/templates/create-class.js b/src/babel/transformation/templates/helper-create-class.js similarity index 57% rename from src/babel/transformation/templates/create-class.js rename to src/babel/transformation/templates/helper-create-class.js index 4ebbc7e61e..09df13e546 100644 --- a/src/babel/transformation/templates/create-class.js +++ b/src/babel/transformation/templates/helper-create-class.js @@ -1,10 +1,11 @@ (function() { function defineProperties(target, props) { for (var i = 0; i < props.length; i ++) { - var prop = props[i]; - prop.configurable = true; - if (prop.value) prop.writable = true; - Object.defineProperty(target, prop.key, prop); + var descriptor = props[i]; + descriptor.enumerable = descriptor.enumerable || false; + descriptor.configurable = true; + if (descriptor.value) descriptor.writable = true; + Object.defineProperty(target, descriptor.key, descriptor); } } diff --git a/src/babel/transformation/templates/helper-create-decorated-class.js b/src/babel/transformation/templates/helper-create-decorated-class.js new file mode 100644 index 0000000000..af5b6ae992 --- /dev/null +++ b/src/babel/transformation/templates/helper-create-decorated-class.js @@ -0,0 +1,37 @@ +(function() { + function defineProperties(target, descriptors) { + for (var i = 0; i < descriptors.length; i ++) { + var descriptor = descriptors[i]; + var decorators = descriptor.decorators; + var key = descriptor.key; + + // don't want to expose these to userland since i know people will rely on them + // and think it's spec behaviour + delete descriptor.key; + delete descriptor.decorators; + + descriptor.enumerable = descriptor.enumerable || false; + descriptor.configurable = true; + if ("value" in descriptor || descriptor.initializer) descriptor.writable = true; + + if (decorators) { + for (var f = 0; f < decorators.length; f++) { + var decorator = decorators[f]; + if (typeof decorator === "function") { + descriptor = decorator(target, key, descriptor) || descriptor; + } else { + throw new TypeError("The decorator for method " + descriptor.key + " is of the invalid type " + typeof decorator); + } + } + } + + Object.defineProperty(target, key, descriptor); + } + } + + return function (Constructor, protoProps, staticProps) { + if (protoProps) defineProperties(Constructor.prototype, protoProps); + if (staticProps) defineProperties(Constructor, staticProps); + return Constructor; + }; +})() diff --git a/src/babel/transformation/templates/defaults.js b/src/babel/transformation/templates/helper-defaults.js similarity index 100% rename from src/babel/transformation/templates/defaults.js rename to src/babel/transformation/templates/helper-defaults.js diff --git a/src/babel/transformation/templates/define-property.js b/src/babel/transformation/templates/helper-define-property.js similarity index 100% rename from src/babel/transformation/templates/define-property.js rename to src/babel/transformation/templates/helper-define-property.js diff --git a/src/babel/transformation/templates/extends.js b/src/babel/transformation/templates/helper-extends.js similarity index 100% rename from src/babel/transformation/templates/extends.js rename to src/babel/transformation/templates/helper-extends.js diff --git a/src/babel/transformation/templates/get.js b/src/babel/transformation/templates/helper-get.js similarity index 100% rename from src/babel/transformation/templates/get.js rename to src/babel/transformation/templates/helper-get.js diff --git a/src/babel/transformation/templates/has-own.js b/src/babel/transformation/templates/helper-has-own.js similarity index 100% rename from src/babel/transformation/templates/has-own.js rename to src/babel/transformation/templates/helper-has-own.js diff --git a/src/babel/transformation/templates/inherits.js b/src/babel/transformation/templates/helper-inherits.js similarity index 100% rename from src/babel/transformation/templates/inherits.js rename to src/babel/transformation/templates/helper-inherits.js diff --git a/src/babel/transformation/templates/interop-require-wildcard.js b/src/babel/transformation/templates/helper-interop-require-wildcard.js similarity index 100% rename from src/babel/transformation/templates/interop-require-wildcard.js rename to src/babel/transformation/templates/helper-interop-require-wildcard.js diff --git a/src/babel/transformation/templates/interop-require.js b/src/babel/transformation/templates/helper-interop-require.js similarity index 100% rename from src/babel/transformation/templates/interop-require.js rename to src/babel/transformation/templates/helper-interop-require.js diff --git a/src/babel/transformation/templates/object-destructuring-empty.js b/src/babel/transformation/templates/helper-object-destructuring-empty.js similarity index 100% rename from src/babel/transformation/templates/object-destructuring-empty.js rename to src/babel/transformation/templates/helper-object-destructuring-empty.js diff --git a/src/babel/transformation/templates/object-without-properties.js b/src/babel/transformation/templates/helper-object-without-properties.js similarity index 100% rename from src/babel/transformation/templates/object-without-properties.js rename to src/babel/transformation/templates/helper-object-without-properties.js diff --git a/src/babel/transformation/templates/self-global.js b/src/babel/transformation/templates/helper-self-global.js similarity index 100% rename from src/babel/transformation/templates/self-global.js rename to src/babel/transformation/templates/helper-self-global.js diff --git a/src/babel/transformation/templates/set.js b/src/babel/transformation/templates/helper-set.js similarity index 100% rename from src/babel/transformation/templates/set.js rename to src/babel/transformation/templates/helper-set.js diff --git a/src/babel/transformation/templates/sliced-to-array-loose.js b/src/babel/transformation/templates/helper-sliced-to-array-loose.js similarity index 100% rename from src/babel/transformation/templates/sliced-to-array-loose.js rename to src/babel/transformation/templates/helper-sliced-to-array-loose.js diff --git a/src/babel/transformation/templates/sliced-to-array.js b/src/babel/transformation/templates/helper-sliced-to-array.js similarity index 100% rename from src/babel/transformation/templates/sliced-to-array.js rename to src/babel/transformation/templates/helper-sliced-to-array.js diff --git a/src/babel/transformation/templates/tagged-template-literal-loose.js b/src/babel/transformation/templates/helper-tagged-template-literal-loose.js similarity index 100% rename from src/babel/transformation/templates/tagged-template-literal-loose.js rename to src/babel/transformation/templates/helper-tagged-template-literal-loose.js diff --git a/src/babel/transformation/templates/tagged-template-literal.js b/src/babel/transformation/templates/helper-tagged-template-literal.js similarity index 100% rename from src/babel/transformation/templates/tagged-template-literal.js rename to src/babel/transformation/templates/helper-tagged-template-literal.js diff --git a/src/babel/transformation/templates/temporal-assert-defined.js b/src/babel/transformation/templates/helper-temporal-assert-defined.js similarity index 100% rename from src/babel/transformation/templates/temporal-assert-defined.js rename to src/babel/transformation/templates/helper-temporal-assert-defined.js diff --git a/src/babel/transformation/templates/temporal-undefined.js b/src/babel/transformation/templates/helper-temporal-undefined.js similarity index 100% rename from src/babel/transformation/templates/temporal-undefined.js rename to src/babel/transformation/templates/helper-temporal-undefined.js diff --git a/src/babel/transformation/templates/to-array.js b/src/babel/transformation/templates/helper-to-array.js similarity index 100% rename from src/babel/transformation/templates/to-array.js rename to src/babel/transformation/templates/helper-to-array.js diff --git a/src/babel/transformation/templates/to-consumable-array.js b/src/babel/transformation/templates/helper-to-consumable-array.js similarity index 100% rename from src/babel/transformation/templates/to-consumable-array.js rename to src/babel/transformation/templates/helper-to-consumable-array.js diff --git a/src/babel/transformation/templates/typeof.js b/src/babel/transformation/templates/helper-typeof.js similarity index 100% rename from src/babel/transformation/templates/typeof.js rename to src/babel/transformation/templates/helper-typeof.js