UNPKG

13.6 kBJavaScriptView Raw
1var _Object$defineProperty = require("core-js/library/fn/object/define-property.js");
2var _Symbol$toStringTag = require("core-js/library/fn/symbol/to-string-tag.js");
3var _Object$assign = require("core-js/library/fn/object/assign.js");
4var toArray = require("./toArray.js");
5var toPropertyKey = require("./toPropertyKey.js");
6function _decorate(decorators, factory, superClass, mixins) {
7 var api = _getDecoratorsApi();
8 if (mixins) {
9 for (var i = 0; i < mixins.length; i++) {
10 api = mixins[i](api);
11 }
12 }
13 var r = factory(function initialize(O) {
14 api.initializeInstanceElements(O, decorated.elements);
15 }, superClass);
16 var decorated = api.decorateClass(_coalesceClassElements(r.d.map(_createElementDescriptor)), decorators);
17 api.initializeClassElements(r.F, decorated.elements);
18 return api.runClassFinishers(r.F, decorated.finishers);
19}
20function _getDecoratorsApi() {
21 _getDecoratorsApi = function _getDecoratorsApi() {
22 return api;
23 };
24 var api = {
25 elementsDefinitionOrder: [["method"], ["field"]],
26 initializeInstanceElements: function initializeInstanceElements(O, elements) {
27 ["method", "field"].forEach(function (kind) {
28 elements.forEach(function (element) {
29 if (element.kind === kind && element.placement === "own") {
30 this.defineClassElement(O, element);
31 }
32 }, this);
33 }, this);
34 },
35 initializeClassElements: function initializeClassElements(F, elements) {
36 var proto = F.prototype;
37 ["method", "field"].forEach(function (kind) {
38 elements.forEach(function (element) {
39 var placement = element.placement;
40 if (element.kind === kind && (placement === "static" || placement === "prototype")) {
41 var receiver = placement === "static" ? F : proto;
42 this.defineClassElement(receiver, element);
43 }
44 }, this);
45 }, this);
46 },
47 defineClassElement: function defineClassElement(receiver, element) {
48 var descriptor = element.descriptor;
49 if (element.kind === "field") {
50 var initializer = element.initializer;
51 descriptor = {
52 enumerable: descriptor.enumerable,
53 writable: descriptor.writable,
54 configurable: descriptor.configurable,
55 value: initializer === void 0 ? void 0 : initializer.call(receiver)
56 };
57 }
58 _Object$defineProperty(receiver, element.key, descriptor);
59 },
60 decorateClass: function decorateClass(elements, decorators) {
61 var newElements = [];
62 var finishers = [];
63 var placements = {
64 "static": [],
65 prototype: [],
66 own: []
67 };
68 elements.forEach(function (element) {
69 this.addElementPlacement(element, placements);
70 }, this);
71 elements.forEach(function (element) {
72 if (!_hasDecorators(element)) return newElements.push(element);
73 var elementFinishersExtras = this.decorateElement(element, placements);
74 newElements.push(elementFinishersExtras.element);
75 newElements.push.apply(newElements, elementFinishersExtras.extras);
76 finishers.push.apply(finishers, elementFinishersExtras.finishers);
77 }, this);
78 if (!decorators) {
79 return {
80 elements: newElements,
81 finishers: finishers
82 };
83 }
84 var result = this.decorateConstructor(newElements, decorators);
85 finishers.push.apply(finishers, result.finishers);
86 result.finishers = finishers;
87 return result;
88 },
89 addElementPlacement: function addElementPlacement(element, placements, silent) {
90 var keys = placements[element.placement];
91 if (!silent && keys.indexOf(element.key) !== -1) {
92 throw new TypeError("Duplicated element (" + element.key + ")");
93 }
94 keys.push(element.key);
95 },
96 decorateElement: function decorateElement(element, placements) {
97 var extras = [];
98 var finishers = [];
99 for (var decorators = element.decorators, i = decorators.length - 1; i >= 0; i--) {
100 var keys = placements[element.placement];
101 keys.splice(keys.indexOf(element.key), 1);
102 var elementObject = this.fromElementDescriptor(element);
103 var elementFinisherExtras = this.toElementFinisherExtras((0, decorators[i])(elementObject) || elementObject);
104 element = elementFinisherExtras.element;
105 this.addElementPlacement(element, placements);
106 if (elementFinisherExtras.finisher) {
107 finishers.push(elementFinisherExtras.finisher);
108 }
109 var newExtras = elementFinisherExtras.extras;
110 if (newExtras) {
111 for (var j = 0; j < newExtras.length; j++) {
112 this.addElementPlacement(newExtras[j], placements);
113 }
114 extras.push.apply(extras, newExtras);
115 }
116 }
117 return {
118 element: element,
119 finishers: finishers,
120 extras: extras
121 };
122 },
123 decorateConstructor: function decorateConstructor(elements, decorators) {
124 var finishers = [];
125 for (var i = decorators.length - 1; i >= 0; i--) {
126 var obj = this.fromClassDescriptor(elements);
127 var elementsAndFinisher = this.toClassDescriptor((0, decorators[i])(obj) || obj);
128 if (elementsAndFinisher.finisher !== undefined) {
129 finishers.push(elementsAndFinisher.finisher);
130 }
131 if (elementsAndFinisher.elements !== undefined) {
132 elements = elementsAndFinisher.elements;
133 for (var j = 0; j < elements.length - 1; j++) {
134 for (var k = j + 1; k < elements.length; k++) {
135 if (elements[j].key === elements[k].key && elements[j].placement === elements[k].placement) {
136 throw new TypeError("Duplicated element (" + elements[j].key + ")");
137 }
138 }
139 }
140 }
141 }
142 return {
143 elements: elements,
144 finishers: finishers
145 };
146 },
147 fromElementDescriptor: function fromElementDescriptor(element) {
148 var obj = {
149 kind: element.kind,
150 key: element.key,
151 placement: element.placement,
152 descriptor: element.descriptor
153 };
154 var desc = {
155 value: "Descriptor",
156 configurable: true
157 };
158 _Object$defineProperty(obj, _Symbol$toStringTag, desc);
159 if (element.kind === "field") obj.initializer = element.initializer;
160 return obj;
161 },
162 toElementDescriptors: function toElementDescriptors(elementObjects) {
163 if (elementObjects === undefined) return;
164 return toArray(elementObjects).map(function (elementObject) {
165 var element = this.toElementDescriptor(elementObject);
166 this.disallowProperty(elementObject, "finisher", "An element descriptor");
167 this.disallowProperty(elementObject, "extras", "An element descriptor");
168 return element;
169 }, this);
170 },
171 toElementDescriptor: function toElementDescriptor(elementObject) {
172 var kind = String(elementObject.kind);
173 if (kind !== "method" && kind !== "field") {
174 throw new TypeError('An element descriptor\'s .kind property must be either "method" or' + ' "field", but a decorator created an element descriptor with' + ' .kind "' + kind + '"');
175 }
176 var key = toPropertyKey(elementObject.key);
177 var placement = String(elementObject.placement);
178 if (placement !== "static" && placement !== "prototype" && placement !== "own") {
179 throw new TypeError('An element descriptor\'s .placement property must be one of "static",' + ' "prototype" or "own", but a decorator created an element descriptor' + ' with .placement "' + placement + '"');
180 }
181 var descriptor = elementObject.descriptor;
182 this.disallowProperty(elementObject, "elements", "An element descriptor");
183 var element = {
184 kind: kind,
185 key: key,
186 placement: placement,
187 descriptor: _Object$assign({}, descriptor)
188 };
189 if (kind !== "field") {
190 this.disallowProperty(elementObject, "initializer", "A method descriptor");
191 } else {
192 this.disallowProperty(descriptor, "get", "The property descriptor of a field descriptor");
193 this.disallowProperty(descriptor, "set", "The property descriptor of a field descriptor");
194 this.disallowProperty(descriptor, "value", "The property descriptor of a field descriptor");
195 element.initializer = elementObject.initializer;
196 }
197 return element;
198 },
199 toElementFinisherExtras: function toElementFinisherExtras(elementObject) {
200 var element = this.toElementDescriptor(elementObject);
201 var finisher = _optionalCallableProperty(elementObject, "finisher");
202 var extras = this.toElementDescriptors(elementObject.extras);
203 return {
204 element: element,
205 finisher: finisher,
206 extras: extras
207 };
208 },
209 fromClassDescriptor: function fromClassDescriptor(elements) {
210 var obj = {
211 kind: "class",
212 elements: elements.map(this.fromElementDescriptor, this)
213 };
214 var desc = {
215 value: "Descriptor",
216 configurable: true
217 };
218 _Object$defineProperty(obj, _Symbol$toStringTag, desc);
219 return obj;
220 },
221 toClassDescriptor: function toClassDescriptor(obj) {
222 var kind = String(obj.kind);
223 if (kind !== "class") {
224 throw new TypeError('A class descriptor\'s .kind property must be "class", but a decorator' + ' created a class descriptor with .kind "' + kind + '"');
225 }
226 this.disallowProperty(obj, "key", "A class descriptor");
227 this.disallowProperty(obj, "placement", "A class descriptor");
228 this.disallowProperty(obj, "descriptor", "A class descriptor");
229 this.disallowProperty(obj, "initializer", "A class descriptor");
230 this.disallowProperty(obj, "extras", "A class descriptor");
231 var finisher = _optionalCallableProperty(obj, "finisher");
232 var elements = this.toElementDescriptors(obj.elements);
233 return {
234 elements: elements,
235 finisher: finisher
236 };
237 },
238 runClassFinishers: function runClassFinishers(constructor, finishers) {
239 for (var i = 0; i < finishers.length; i++) {
240 var newConstructor = (0, finishers[i])(constructor);
241 if (newConstructor !== undefined) {
242 if (typeof newConstructor !== "function") {
243 throw new TypeError("Finishers must return a constructor.");
244 }
245 constructor = newConstructor;
246 }
247 }
248 return constructor;
249 },
250 disallowProperty: function disallowProperty(obj, name, objectType) {
251 if (obj[name] !== undefined) {
252 throw new TypeError(objectType + " can't have a ." + name + " property.");
253 }
254 }
255 };
256 return api;
257}
258function _createElementDescriptor(def) {
259 var key = toPropertyKey(def.key);
260 var descriptor;
261 if (def.kind === "method") {
262 descriptor = {
263 value: def.value,
264 writable: true,
265 configurable: true,
266 enumerable: false
267 };
268 } else if (def.kind === "get") {
269 descriptor = {
270 get: def.value,
271 configurable: true,
272 enumerable: false
273 };
274 } else if (def.kind === "set") {
275 descriptor = {
276 set: def.value,
277 configurable: true,
278 enumerable: false
279 };
280 } else if (def.kind === "field") {
281 descriptor = {
282 configurable: true,
283 writable: true,
284 enumerable: true
285 };
286 }
287 var element = {
288 kind: def.kind === "field" ? "field" : "method",
289 key: key,
290 placement: def["static"] ? "static" : def.kind === "field" ? "own" : "prototype",
291 descriptor: descriptor
292 };
293 if (def.decorators) element.decorators = def.decorators;
294 if (def.kind === "field") element.initializer = def.value;
295 return element;
296}
297function _coalesceGetterSetter(element, other) {
298 if (element.descriptor.get !== undefined) {
299 other.descriptor.get = element.descriptor.get;
300 } else {
301 other.descriptor.set = element.descriptor.set;
302 }
303}
304function _coalesceClassElements(elements) {
305 var newElements = [];
306 var isSameElement = function isSameElement(other) {
307 return other.kind === "method" && other.key === element.key && other.placement === element.placement;
308 };
309 for (var i = 0; i < elements.length; i++) {
310 var element = elements[i];
311 var other;
312 if (element.kind === "method" && (other = newElements.find(isSameElement))) {
313 if (_isDataDescriptor(element.descriptor) || _isDataDescriptor(other.descriptor)) {
314 if (_hasDecorators(element) || _hasDecorators(other)) {
315 throw new ReferenceError("Duplicated methods (" + element.key + ") can't be decorated.");
316 }
317 other.descriptor = element.descriptor;
318 } else {
319 if (_hasDecorators(element)) {
320 if (_hasDecorators(other)) {
321 throw new ReferenceError("Decorators can't be placed on different accessors with for " + "the same property (" + element.key + ").");
322 }
323 other.decorators = element.decorators;
324 }
325 _coalesceGetterSetter(element, other);
326 }
327 } else {
328 newElements.push(element);
329 }
330 }
331 return newElements;
332}
333function _hasDecorators(element) {
334 return element.decorators && element.decorators.length;
335}
336function _isDataDescriptor(desc) {
337 return desc !== undefined && !(desc.value === undefined && desc.writable === undefined);
338}
339function _optionalCallableProperty(obj, name) {
340 var value = obj[name];
341 if (value !== undefined && typeof value !== "function") {
342 throw new TypeError("Expected '" + name + "' to be a function");
343 }
344 return value;
345}
346module.exports = _decorate, module.exports.__esModule = true, module.exports["default"] = module.exports;
\No newline at end of file