| 1 | import template from "@babel/template"; |
| 2 | import type * as t from "@babel/types"; |
| 3 | |
| 4 | import generated from "./helpers-generated"; |
| 5 | |
| 6 | interface Helper { |
| 7 | minVersion: string; |
| 8 | ast: () => t.Program; |
| 9 | } |
| 10 | |
| 11 | const helpers: Record<string, Helper> = { __proto__: null, ...generated }; |
| 12 | export default helpers; |
| 13 | |
| 14 | const helper = (minVersion: string) => (tpl: TemplateStringsArray) => ({ |
| 15 | minVersion, |
| 16 | ast: () => template.program.ast(tpl), |
| 17 | }); |
| 18 | |
| 19 | if (!process.env.BABEL_8_BREAKING) { |
| 20 | helpers.AwaitValue = helper("7.0.0-beta.0")` |
| 21 | export default function _AwaitValue(value) { |
| 22 | this.wrapped = value; |
| 23 | } |
| 24 | `; |
| 25 | } |
| 26 | |
| 27 | helpers.wrapAsyncGenerator = helper("7.0.0-beta.0")` |
| 28 | import AsyncGenerator from "AsyncGenerator"; |
| 29 | |
| 30 | export default function _wrapAsyncGenerator(fn) { |
| 31 | return function () { |
| 32 | return new AsyncGenerator(fn.apply(this, arguments)); |
| 33 | }; |
| 34 | } |
| 35 | `; |
| 36 | |
| 37 | helpers.asyncToGenerator = helper("7.0.0-beta.0")` |
| 38 | function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { |
| 39 | try { |
| 40 | var info = gen[key](arg); |
| 41 | var value = info.value; |
| 42 | } catch (error) { |
| 43 | reject(error); |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | if (info.done) { |
| 48 | resolve(value); |
| 49 | } else { |
| 50 | Promise.resolve(value).then(_next, _throw); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | export default function _asyncToGenerator(fn) { |
| 55 | return function () { |
| 56 | var self = this, args = arguments; |
| 57 | return new Promise(function (resolve, reject) { |
| 58 | var gen = fn.apply(self, args); |
| 59 | function _next(value) { |
| 60 | asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); |
| 61 | } |
| 62 | function _throw(err) { |
| 63 | asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); |
| 64 | } |
| 65 | |
| 66 | _next(undefined); |
| 67 | }); |
| 68 | }; |
| 69 | } |
| 70 | `; |
| 71 | |
| 72 | helpers.classCallCheck = helper("7.0.0-beta.0")` |
| 73 | export default function _classCallCheck(instance, Constructor) { |
| 74 | if (!(instance instanceof Constructor)) { |
| 75 | throw new TypeError("Cannot call a class as a function"); |
| 76 | } |
| 77 | } |
| 78 | `; |
| 79 | |
| 80 | helpers.createClass = helper("7.0.0-beta.0")` |
| 81 | import toPropertyKey from "toPropertyKey"; |
| 82 | function _defineProperties(target, props) { |
| 83 | for (var i = 0; i < props.length; i ++) { |
| 84 | var descriptor = props[i]; |
| 85 | descriptor.enumerable = descriptor.enumerable || false; |
| 86 | descriptor.configurable = true; |
| 87 | if ("value" in descriptor) descriptor.writable = true; |
| 88 | Object.defineProperty(target, toPropertyKey(descriptor.key), descriptor); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | export default function _createClass(Constructor, protoProps, staticProps) { |
| 93 | if (protoProps) _defineProperties(Constructor.prototype, protoProps); |
| 94 | if (staticProps) _defineProperties(Constructor, staticProps); |
| 95 | Object.defineProperty(Constructor, "prototype", { writable: false }); |
| 96 | return Constructor; |
| 97 | } |
| 98 | `; |
| 99 | |
| 100 | helpers.defineEnumerableProperties = helper("7.0.0-beta.0")` |
| 101 | export default function _defineEnumerableProperties(obj, descs) { |
| 102 | for (var key in descs) { |
| 103 | var desc = descs[key]; |
| 104 | desc.configurable = desc.enumerable = true; |
| 105 | if ("value" in desc) desc.writable = true; |
| 106 | Object.defineProperty(obj, key, desc); |
| 107 | } |
| 108 | |
| 109 | // Symbols are not enumerated over by for-in loops. If native |
| 110 | // Symbols are available, fetch all of the descs object's own |
| 111 | // symbol properties and define them on our target object too. |
| 112 | if (Object.getOwnPropertySymbols) { |
| 113 | var objectSymbols = Object.getOwnPropertySymbols(descs); |
| 114 | for (var i = 0; i < objectSymbols.length; i++) { |
| 115 | var sym = objectSymbols[i]; |
| 116 | var desc = descs[sym]; |
| 117 | desc.configurable = desc.enumerable = true; |
| 118 | if ("value" in desc) desc.writable = true; |
| 119 | Object.defineProperty(obj, sym, desc); |
| 120 | } |
| 121 | } |
| 122 | return obj; |
| 123 | } |
| 124 | `; |
| 125 | |
| 126 | helpers.defaults = helper("7.0.0-beta.0")` |
| 127 | export default function _defaults(obj, defaults) { |
| 128 | var keys = Object.getOwnPropertyNames(defaults); |
| 129 | for (var i = 0; i < keys.length; i++) { |
| 130 | var key = keys[i]; |
| 131 | var value = Object.getOwnPropertyDescriptor(defaults, key); |
| 132 | if (value && value.configurable && obj[key] === undefined) { |
| 133 | Object.defineProperty(obj, key, value); |
| 134 | } |
| 135 | } |
| 136 | return obj; |
| 137 | } |
| 138 | `; |
| 139 | |
| 140 | helpers.defineProperty = helper("7.0.0-beta.0")` |
| 141 | import toPropertyKey from "toPropertyKey"; |
| 142 | export default function _defineProperty(obj, key, value) { |
| 143 | key = toPropertyKey(key); |
| 144 | // Shortcircuit the slow defineProperty path when possible. |
| 145 | // We are trying to avoid issues where setters defined on the |
| 146 | // prototype cause side effects under the fast path of simple |
| 147 | // assignment. By checking for existence of the property with |
| 148 | // the in operator, we can optimize most of this overhead away. |
| 149 | if (key in obj) { |
| 150 | Object.defineProperty(obj, key, { |
| 151 | value: value, |
| 152 | enumerable: true, |
| 153 | configurable: true, |
| 154 | writable: true |
| 155 | }); |
| 156 | } else { |
| 157 | obj[key] = value; |
| 158 | } |
| 159 | return obj; |
| 160 | } |
| 161 | `; |
| 162 | |
| 163 | // need a bind because https://github.com/babel/babel/issues/14527 |
| 164 | helpers.extends = helper("7.0.0-beta.0")` |
| 165 | export default function _extends() { |
| 166 | _extends = Object.assign ? Object.assign.bind() : function (target) { |
| 167 | for (var i = 1; i < arguments.length; i++) { |
| 168 | var source = arguments[i]; |
| 169 | for (var key in source) { |
| 170 | if (Object.prototype.hasOwnProperty.call(source, key)) { |
| 171 | target[key] = source[key]; |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | return target; |
| 176 | }; |
| 177 | |
| 178 | return _extends.apply(this, arguments); |
| 179 | } |
| 180 | `; |
| 181 | |
| 182 | // TODO(babel-8): This old helper can be removed in babel v8 |
| 183 | helpers.objectSpread = helper("7.0.0-beta.0")` |
| 184 | import defineProperty from "defineProperty"; |
| 185 | |
| 186 | export default function _objectSpread(target) { |
| 187 | for (var i = 1; i < arguments.length; i++) { |
| 188 | var source = (arguments[i] != null) ? Object(arguments[i]) : {}; |
| 189 | var ownKeys = Object.keys(source); |
| 190 | if (typeof Object.getOwnPropertySymbols === 'function') { |
| 191 | ownKeys.push.apply(ownKeys, Object.getOwnPropertySymbols(source).filter(function(sym) { |
| 192 | return Object.getOwnPropertyDescriptor(source, sym).enumerable; |
| 193 | })); |
| 194 | } |
| 195 | ownKeys.forEach(function(key) { |
| 196 | defineProperty(target, key, source[key]); |
| 197 | }); |
| 198 | } |
| 199 | return target; |
| 200 | } |
| 201 | `; |
| 202 | |
| 203 | helpers.inherits = helper("7.0.0-beta.0")` |
| 204 | import setPrototypeOf from "setPrototypeOf"; |
| 205 | |
| 206 | export default function _inherits(subClass, superClass) { |
| 207 | if (typeof superClass !== "function" && superClass !== null) { |
| 208 | throw new TypeError("Super expression must either be null or a function"); |
| 209 | } |
| 210 | // We can't use defineProperty to set the prototype in a single step because it |
| 211 | // doesn't work in Chrome <= 36. https://github.com/babel/babel/issues/14056 |
| 212 | // V8 bug: https://bugs.chromium.org/p/v8/issues/detail?id=3334 |
| 213 | subClass.prototype = Object.create(superClass && superClass.prototype, { |
| 214 | constructor: { |
| 215 | value: subClass, |
| 216 | writable: true, |
| 217 | configurable: true |
| 218 | } |
| 219 | }); |
| 220 | Object.defineProperty(subClass, "prototype", { writable: false }); |
| 221 | if (superClass) setPrototypeOf(subClass, superClass); |
| 222 | } |
| 223 | `; |
| 224 | |
| 225 | helpers.inheritsLoose = helper("7.0.0-beta.0")` |
| 226 | import setPrototypeOf from "setPrototypeOf"; |
| 227 | |
| 228 | export default function _inheritsLoose(subClass, superClass) { |
| 229 | subClass.prototype = Object.create(superClass.prototype); |
| 230 | subClass.prototype.constructor = subClass; |
| 231 | setPrototypeOf(subClass, superClass); |
| 232 | } |
| 233 | `; |
| 234 | |
| 235 | // need a bind because https://github.com/babel/babel/issues/14527 |
| 236 | helpers.getPrototypeOf = helper("7.0.0-beta.0")` |
| 237 | export default function _getPrototypeOf(o) { |
| 238 | _getPrototypeOf = Object.setPrototypeOf |
| 239 | ? Object.getPrototypeOf.bind() |
| 240 | : function _getPrototypeOf(o) { |
| 241 | return o.__proto__ || Object.getPrototypeOf(o); |
| 242 | }; |
| 243 | return _getPrototypeOf(o); |
| 244 | } |
| 245 | `; |
| 246 | |
| 247 | helpers.setPrototypeOf = helper("7.0.0-beta.0")` |
| 248 | export default function _setPrototypeOf(o, p) { |
| 249 | _setPrototypeOf = Object.setPrototypeOf |
| 250 | ? Object.setPrototypeOf.bind() |
| 251 | : function _setPrototypeOf(o, p) { |
| 252 | o.__proto__ = p; |
| 253 | return o; |
| 254 | }; |
| 255 | return _setPrototypeOf(o, p); |
| 256 | } |
| 257 | `; |
| 258 | |
| 259 | helpers.isNativeReflectConstruct = helper("7.9.0")` |
| 260 | export default function _isNativeReflectConstruct() { |
| 261 | if (typeof Reflect === "undefined" || !Reflect.construct) return false; |
| 262 | |
| 263 | // core-js@3 |
| 264 | if (Reflect.construct.sham) return false; |
| 265 | |
| 266 | // Proxy can't be polyfilled. Every browser implemented |
| 267 | // proxies before or at the same time as Reflect.construct, |
| 268 | // so if they support Proxy they also support Reflect.construct. |
| 269 | if (typeof Proxy === "function") return true; |
| 270 | |
| 271 | // Since Reflect.construct can't be properly polyfilled, some |
| 272 | // implementations (e.g. core-js@2) don't set the correct internal slots. |
| 273 | // Those polyfills don't allow us to subclass built-ins, so we need to |
| 274 | // use our fallback implementation. |
| 275 | try { |
| 276 | // If the internal slots aren't set, this throws an error similar to |
| 277 | // TypeError: this is not a Boolean object. |
| 278 | |
| 279 | Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {})); |
| 280 | return true; |
| 281 | } catch (e) { |
| 282 | return false; |
| 283 | } |
| 284 | } |
| 285 | `; |
| 286 | |
| 287 | // need a bind because https://github.com/babel/babel/issues/14527 |
| 288 | helpers.construct = helper("7.0.0-beta.0")` |
| 289 | import setPrototypeOf from "setPrototypeOf"; |
| 290 | import isNativeReflectConstruct from "isNativeReflectConstruct"; |
| 291 | |
| 292 | export default function _construct(Parent, args, Class) { |
| 293 | if (isNativeReflectConstruct()) { |
| 294 | _construct = Reflect.construct.bind(); |
| 295 | } else { |
| 296 | // NOTE: If Parent !== Class, the correct __proto__ is set *after* |
| 297 | // calling the constructor. |
| 298 | _construct = function _construct(Parent, args, Class) { |
| 299 | var a = [null]; |
| 300 | a.push.apply(a, args); |
| 301 | var Constructor = Function.bind.apply(Parent, a); |
| 302 | var instance = new Constructor(); |
| 303 | if (Class) setPrototypeOf(instance, Class.prototype); |
| 304 | return instance; |
| 305 | }; |
| 306 | } |
| 307 | // Avoid issues with Class being present but undefined when it wasn't |
| 308 | // present in the original call. |
| 309 | return _construct.apply(null, arguments); |
| 310 | } |
| 311 | `; |
| 312 | |
| 313 | helpers.isNativeFunction = helper("7.0.0-beta.0")` |
| 314 | export default function _isNativeFunction(fn) { |
| 315 | // Note: This function returns "true" for core-js functions. |
| 316 | return Function.toString.call(fn).indexOf("[native code]") !== -1; |
| 317 | } |
| 318 | `; |
| 319 | |
| 320 | // Based on https://github.com/WebReflection/babel-plugin-transform-builtin-classes |
| 321 | helpers.wrapNativeSuper = helper("7.0.0-beta.0")` |
| 322 | import getPrototypeOf from "getPrototypeOf"; |
| 323 | import setPrototypeOf from "setPrototypeOf"; |
| 324 | import isNativeFunction from "isNativeFunction"; |
| 325 | import construct from "construct"; |
| 326 | |
| 327 | export default function _wrapNativeSuper(Class) { |
| 328 | var _cache = typeof Map === "function" ? new Map() : undefined; |
| 329 | |
| 330 | _wrapNativeSuper = function _wrapNativeSuper(Class) { |
| 331 | if (Class === null || !isNativeFunction(Class)) return Class; |
| 332 | if (typeof Class !== "function") { |
| 333 | throw new TypeError("Super expression must either be null or a function"); |
| 334 | } |
| 335 | if (typeof _cache !== "undefined") { |
| 336 | if (_cache.has(Class)) return _cache.get(Class); |
| 337 | _cache.set(Class, Wrapper); |
| 338 | } |
| 339 | function Wrapper() { |
| 340 | return construct(Class, arguments, getPrototypeOf(this).constructor) |
| 341 | } |
| 342 | Wrapper.prototype = Object.create(Class.prototype, { |
| 343 | constructor: { |
| 344 | value: Wrapper, |
| 345 | enumerable: false, |
| 346 | writable: true, |
| 347 | configurable: true, |
| 348 | } |
| 349 | }); |
| 350 | |
| 351 | return setPrototypeOf(Wrapper, Class); |
| 352 | } |
| 353 | |
| 354 | return _wrapNativeSuper(Class) |
| 355 | } |
| 356 | `; |
| 357 | |
| 358 | helpers.instanceof = helper("7.0.0-beta.0")` |
| 359 | export default function _instanceof(left, right) { |
| 360 | if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) { |
| 361 | return !!right[Symbol.hasInstance](left); |
| 362 | } else { |
| 363 | return left instanceof right; |
| 364 | } |
| 365 | } |
| 366 | `; |
| 367 | |
| 368 | helpers.interopRequireDefault = helper("7.0.0-beta.0")` |
| 369 | export default function _interopRequireDefault(obj) { |
| 370 | return obj && obj.__esModule ? obj : { default: obj }; |
| 371 | } |
| 372 | `; |
| 373 | |
| 374 | helpers.interopRequireWildcard = helper("7.14.0")` |
| 375 | function _getRequireWildcardCache(nodeInterop) { |
| 376 | if (typeof WeakMap !== "function") return null; |
| 377 | |
| 378 | var cacheBabelInterop = new WeakMap(); |
| 379 | var cacheNodeInterop = new WeakMap(); |
| 380 | return (_getRequireWildcardCache = function (nodeInterop) { |
| 381 | return nodeInterop ? cacheNodeInterop : cacheBabelInterop; |
| 382 | })(nodeInterop); |
| 383 | } |
| 384 | |
| 385 | export default function _interopRequireWildcard(obj, nodeInterop) { |
| 386 | if (!nodeInterop && obj && obj.__esModule) { |
| 387 | return obj; |
| 388 | } |
| 389 | |
| 390 | if (obj === null || (typeof obj !== "object" && typeof obj !== "function")) { |
| 391 | return { default: obj } |
| 392 | } |
| 393 | |
| 394 | var cache = _getRequireWildcardCache(nodeInterop); |
| 395 | if (cache && cache.has(obj)) { |
| 396 | return cache.get(obj); |
| 397 | } |
| 398 | |
| 399 | var newObj = {}; |
| 400 | var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; |
| 401 | for (var key in obj) { |
| 402 | if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { |
| 403 | var desc = hasPropertyDescriptor |
| 404 | ? Object.getOwnPropertyDescriptor(obj, key) |
| 405 | : null; |
| 406 | if (desc && (desc.get || desc.set)) { |
| 407 | Object.defineProperty(newObj, key, desc); |
| 408 | } else { |
| 409 | newObj[key] = obj[key]; |
| 410 | } |
| 411 | } |
| 412 | } |
| 413 | newObj.default = obj; |
| 414 | if (cache) { |
| 415 | cache.set(obj, newObj); |
| 416 | } |
| 417 | return newObj; |
| 418 | } |
| 419 | `; |
| 420 | |
| 421 | helpers.newArrowCheck = helper("7.0.0-beta.0")` |
| 422 | export default function _newArrowCheck(innerThis, boundThis) { |
| 423 | if (innerThis !== boundThis) { |
| 424 | throw new TypeError("Cannot instantiate an arrow function"); |
| 425 | } |
| 426 | } |
| 427 | `; |
| 428 | |
| 429 | helpers.objectDestructuringEmpty = helper("7.0.0-beta.0")` |
| 430 | export default function _objectDestructuringEmpty(obj) { |
| 431 | if (obj == null) throw new TypeError("Cannot destructure " + obj); |
| 432 | } |
| 433 | `; |
| 434 | |
| 435 | helpers.objectWithoutPropertiesLoose = helper("7.0.0-beta.0")` |
| 436 | export default function _objectWithoutPropertiesLoose(source, excluded) { |
| 437 | if (source == null) return {}; |
| 438 | |
| 439 | var target = {}; |
| 440 | var sourceKeys = Object.keys(source); |
| 441 | var key, i; |
| 442 | |
| 443 | for (i = 0; i < sourceKeys.length; i++) { |
| 444 | key = sourceKeys[i]; |
| 445 | if (excluded.indexOf(key) >= 0) continue; |
| 446 | target[key] = source[key]; |
| 447 | } |
| 448 | |
| 449 | return target; |
| 450 | } |
| 451 | `; |
| 452 | |
| 453 | helpers.objectWithoutProperties = helper("7.0.0-beta.0")` |
| 454 | import objectWithoutPropertiesLoose from "objectWithoutPropertiesLoose"; |
| 455 | |
| 456 | export default function _objectWithoutProperties(source, excluded) { |
| 457 | if (source == null) return {}; |
| 458 | |
| 459 | var target = objectWithoutPropertiesLoose(source, excluded); |
| 460 | var key, i; |
| 461 | |
| 462 | if (Object.getOwnPropertySymbols) { |
| 463 | var sourceSymbolKeys = Object.getOwnPropertySymbols(source); |
| 464 | for (i = 0; i < sourceSymbolKeys.length; i++) { |
| 465 | key = sourceSymbolKeys[i]; |
| 466 | if (excluded.indexOf(key) >= 0) continue; |
| 467 | if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; |
| 468 | target[key] = source[key]; |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | return target; |
| 473 | } |
| 474 | `; |
| 475 | |
| 476 | helpers.assertThisInitialized = helper("7.0.0-beta.0")` |
| 477 | export default function _assertThisInitialized(self) { |
| 478 | if (self === void 0) { |
| 479 | throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); |
| 480 | } |
| 481 | return self; |
| 482 | } |
| 483 | `; |
| 484 | |
| 485 | helpers.possibleConstructorReturn = helper("7.0.0-beta.0")` |
| 486 | import assertThisInitialized from "assertThisInitialized"; |
| 487 | |
| 488 | export default function _possibleConstructorReturn(self, call) { |
| 489 | if (call && (typeof call === "object" || typeof call === "function")) { |
| 490 | return call; |
| 491 | } else if (call !== void 0) { |
| 492 | throw new TypeError("Derived constructors may only return object or undefined"); |
| 493 | } |
| 494 | |
| 495 | return assertThisInitialized(self); |
| 496 | } |
| 497 | `; |
| 498 | |
| 499 | // This is duplicated to packages/babel-plugin-transform-classes/src/inline-createSuper-helpers.js |
| 500 | helpers.createSuper = helper("7.9.0")` |
| 501 | import getPrototypeOf from "getPrototypeOf"; |
| 502 | import isNativeReflectConstruct from "isNativeReflectConstruct"; |
| 503 | import possibleConstructorReturn from "possibleConstructorReturn"; |
| 504 | |
| 505 | export default function _createSuper(Derived) { |
| 506 | var hasNativeReflectConstruct = isNativeReflectConstruct(); |
| 507 | |
| 508 | return function _createSuperInternal() { |
| 509 | var Super = getPrototypeOf(Derived), result; |
| 510 | if (hasNativeReflectConstruct) { |
| 511 | // NOTE: This doesn't work if this.__proto__.constructor has been modified. |
| 512 | var NewTarget = getPrototypeOf(this).constructor; |
| 513 | result = Reflect.construct(Super, arguments, NewTarget); |
| 514 | } else { |
| 515 | result = Super.apply(this, arguments); |
| 516 | } |
| 517 | return possibleConstructorReturn(this, result); |
| 518 | } |
| 519 | } |
| 520 | `; |
| 521 | |
| 522 | helpers.superPropBase = helper("7.0.0-beta.0")` |
| 523 | import getPrototypeOf from "getPrototypeOf"; |
| 524 | |
| 525 | export default function _superPropBase(object, property) { |
| 526 | // Yes, this throws if object is null to being with, that's on purpose. |
| 527 | while (!Object.prototype.hasOwnProperty.call(object, property)) { |
| 528 | object = getPrototypeOf(object); |
| 529 | if (object === null) break; |
| 530 | } |
| 531 | return object; |
| 532 | } |
| 533 | `; |
| 534 | |
| 535 | // need a bind because https://github.com/babel/babel/issues/14527 |
| 536 | // https://tc39.es/ecma262/multipage/reflection.html#sec-reflect.get |
| 537 | // |
| 538 | // 28.1.5 Reflect.get ( target, propertyKey [ , receiver ] ) |
| 539 | // |
| 540 | helpers.get = helper("7.0.0-beta.0")` |
| 541 | import superPropBase from "superPropBase"; |
| 542 | |
| 543 | export default function _get() { |
| 544 | if (typeof Reflect !== "undefined" && Reflect.get) { |
| 545 | _get = Reflect.get.bind(); |
| 546 | } else { |
| 547 | _get = function _get(target, property, receiver) { |
| 548 | var base = superPropBase(target, property); |
| 549 | |
| 550 | if (!base) return; |
| 551 | |
| 552 | var desc = Object.getOwnPropertyDescriptor(base, property); |
| 553 | if (desc.get) { |
| 554 | // STEP 3. If receiver is not present, then set receiver to target. |
| 555 | return desc.get.call(arguments.length < 3 ? target : receiver); |
| 556 | } |
| 557 | |
| 558 | return desc.value; |
| 559 | }; |
| 560 | } |
| 561 | return _get.apply(this, arguments); |
| 562 | } |
| 563 | `; |
| 564 | |
| 565 | helpers.set = helper("7.0.0-beta.0")` |
| 566 | import superPropBase from "superPropBase"; |
| 567 | import defineProperty from "defineProperty"; |
| 568 | |
| 569 | function set(target, property, value, receiver) { |
| 570 | if (typeof Reflect !== "undefined" && Reflect.set) { |
| 571 | set = Reflect.set; |
| 572 | } else { |
| 573 | set = function set(target, property, value, receiver) { |
| 574 | var base = superPropBase(target, property); |
| 575 | var desc; |
| 576 | |
| 577 | if (base) { |
| 578 | desc = Object.getOwnPropertyDescriptor(base, property); |
| 579 | if (desc.set) { |
| 580 | desc.set.call(receiver, value); |
| 581 | return true; |
| 582 | } else if (!desc.writable) { |
| 583 | // Both getter and non-writable fall into this. |
| 584 | return false; |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | // Without a super that defines the property, spec boils down to |
| 589 | // "define on receiver" for some reason. |
| 590 | desc = Object.getOwnPropertyDescriptor(receiver, property); |
| 591 | if (desc) { |
| 592 | if (!desc.writable) { |
| 593 | // Setter, getter, and non-writable fall into this. |
| 594 | return false; |
| 595 | } |
| 596 | |
| 597 | desc.value = value; |
| 598 | Object.defineProperty(receiver, property, desc); |
| 599 | } else { |
| 600 | // Avoid setters that may be defined on Sub's prototype, but not on |
| 601 | // the instance. |
| 602 | defineProperty(receiver, property, value); |
| 603 | } |
| 604 | |
| 605 | return true; |
| 606 | }; |
| 607 | } |
| 608 | |
| 609 | return set(target, property, value, receiver); |
| 610 | } |
| 611 | |
| 612 | export default function _set(target, property, value, receiver, isStrict) { |
| 613 | var s = set(target, property, value, receiver || target); |
| 614 | if (!s && isStrict) { |
| 615 | throw new TypeError('failed to set property'); |
| 616 | } |
| 617 | |
| 618 | return value; |
| 619 | } |
| 620 | `; |
| 621 | |
| 622 | helpers.taggedTemplateLiteral = helper("7.0.0-beta.0")` |
| 623 | export default function _taggedTemplateLiteral(strings, raw) { |
| 624 | if (!raw) { raw = strings.slice(0); } |
| 625 | return Object.freeze(Object.defineProperties(strings, { |
| 626 | raw: { value: Object.freeze(raw) } |
| 627 | })); |
| 628 | } |
| 629 | `; |
| 630 | |
| 631 | helpers.taggedTemplateLiteralLoose = helper("7.0.0-beta.0")` |
| 632 | export default function _taggedTemplateLiteralLoose(strings, raw) { |
| 633 | if (!raw) { raw = strings.slice(0); } |
| 634 | strings.raw = raw; |
| 635 | return strings; |
| 636 | } |
| 637 | `; |
| 638 | |
| 639 | helpers.readOnlyError = helper("7.0.0-beta.0")` |
| 640 | export default function _readOnlyError(name) { |
| 641 | throw new TypeError("\\"" + name + "\\" is read-only"); |
| 642 | } |
| 643 | `; |
| 644 | |
| 645 | helpers.writeOnlyError = helper("7.12.13")` |
| 646 | export default function _writeOnlyError(name) { |
| 647 | throw new TypeError("\\"" + name + "\\" is write-only"); |
| 648 | } |
| 649 | `; |
| 650 | |
| 651 | helpers.classNameTDZError = helper("7.0.0-beta.0")` |
| 652 | export default function _classNameTDZError(name) { |
| 653 | throw new ReferenceError("Class \\"" + name + "\\" cannot be referenced in computed property keys."); |
| 654 | } |
| 655 | `; |
| 656 | |
| 657 | helpers.temporalUndefined = helper("7.0.0-beta.0")` |
| 658 | // This function isn't mean to be called, but to be used as a reference. |
| 659 | // We can't use a normal object because it isn't hoisted. |
| 660 | export default function _temporalUndefined() {} |
| 661 | `; |
| 662 | |
| 663 | helpers.tdz = helper("7.5.5")` |
| 664 | export default function _tdzError(name) { |
| 665 | throw new ReferenceError(name + " is not defined - temporal dead zone"); |
| 666 | } |
| 667 | `; |
| 668 | |
| 669 | helpers.temporalRef = helper("7.0.0-beta.0")` |
| 670 | import undef from "temporalUndefined"; |
| 671 | import err from "tdz"; |
| 672 | |
| 673 | export default function _temporalRef(val, name) { |
| 674 | return val === undef ? err(name) : val; |
| 675 | } |
| 676 | `; |
| 677 | |
| 678 | helpers.slicedToArray = helper("7.0.0-beta.0")` |
| 679 | import arrayWithHoles from "arrayWithHoles"; |
| 680 | import iterableToArrayLimit from "iterableToArrayLimit"; |
| 681 | import unsupportedIterableToArray from "unsupportedIterableToArray"; |
| 682 | import nonIterableRest from "nonIterableRest"; |
| 683 | |
| 684 | export default function _slicedToArray(arr, i) { |
| 685 | return ( |
| 686 | arrayWithHoles(arr) || |
| 687 | iterableToArrayLimit(arr, i) || |
| 688 | unsupportedIterableToArray(arr, i) || |
| 689 | nonIterableRest() |
| 690 | ); |
| 691 | } |
| 692 | `; |
| 693 | |
| 694 | helpers.slicedToArrayLoose = helper("7.0.0-beta.0")` |
| 695 | import arrayWithHoles from "arrayWithHoles"; |
| 696 | import iterableToArrayLimitLoose from "iterableToArrayLimitLoose"; |
| 697 | import unsupportedIterableToArray from "unsupportedIterableToArray"; |
| 698 | import nonIterableRest from "nonIterableRest"; |
| 699 | |
| 700 | export default function _slicedToArrayLoose(arr, i) { |
| 701 | return ( |
| 702 | arrayWithHoles(arr) || |
| 703 | iterableToArrayLimitLoose(arr, i) || |
| 704 | unsupportedIterableToArray(arr, i) || |
| 705 | nonIterableRest() |
| 706 | ); |
| 707 | } |
| 708 | `; |
| 709 | |
| 710 | helpers.toArray = helper("7.0.0-beta.0")` |
| 711 | import arrayWithHoles from "arrayWithHoles"; |
| 712 | import iterableToArray from "iterableToArray"; |
| 713 | import unsupportedIterableToArray from "unsupportedIterableToArray"; |
| 714 | import nonIterableRest from "nonIterableRest"; |
| 715 | |
| 716 | export default function _toArray(arr) { |
| 717 | return ( |
| 718 | arrayWithHoles(arr) || |
| 719 | iterableToArray(arr) || |
| 720 | unsupportedIterableToArray(arr) || |
| 721 | nonIterableRest() |
| 722 | ); |
| 723 | } |
| 724 | `; |
| 725 | |
| 726 | helpers.toConsumableArray = helper("7.0.0-beta.0")` |
| 727 | import arrayWithoutHoles from "arrayWithoutHoles"; |
| 728 | import iterableToArray from "iterableToArray"; |
| 729 | import unsupportedIterableToArray from "unsupportedIterableToArray"; |
| 730 | import nonIterableSpread from "nonIterableSpread"; |
| 731 | |
| 732 | export default function _toConsumableArray(arr) { |
| 733 | return ( |
| 734 | arrayWithoutHoles(arr) || |
| 735 | iterableToArray(arr) || |
| 736 | unsupportedIterableToArray(arr) || |
| 737 | nonIterableSpread() |
| 738 | ); |
| 739 | } |
| 740 | `; |
| 741 | |
| 742 | helpers.arrayWithoutHoles = helper("7.0.0-beta.0")` |
| 743 | import arrayLikeToArray from "arrayLikeToArray"; |
| 744 | |
| 745 | export default function _arrayWithoutHoles(arr) { |
| 746 | if (Array.isArray(arr)) return arrayLikeToArray(arr); |
| 747 | } |
| 748 | `; |
| 749 | |
| 750 | helpers.arrayWithHoles = helper("7.0.0-beta.0")` |
| 751 | export default function _arrayWithHoles(arr) { |
| 752 | if (Array.isArray(arr)) return arr; |
| 753 | } |
| 754 | `; |
| 755 | |
| 756 | helpers.maybeArrayLike = helper("7.9.0")` |
| 757 | import arrayLikeToArray from "arrayLikeToArray"; |
| 758 | |
| 759 | export default function _maybeArrayLike(next, arr, i) { |
| 760 | if (arr && !Array.isArray(arr) && typeof arr.length === "number") { |
| 761 | var len = arr.length; |
| 762 | return arrayLikeToArray(arr, i !== void 0 && i < len ? i : len); |
| 763 | } |
| 764 | return next(arr, i); |
| 765 | } |
| 766 | `; |
| 767 | |
| 768 | helpers.iterableToArray = helper("7.0.0-beta.0")` |
| 769 | export default function _iterableToArray(iter) { |
| 770 | if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); |
| 771 | } |
| 772 | `; |
| 773 | |
| 774 | helpers.unsupportedIterableToArray = helper("7.9.0")` |
| 775 | import arrayLikeToArray from "arrayLikeToArray"; |
| 776 | |
| 777 | export default function _unsupportedIterableToArray(o, minLen) { |
| 778 | if (!o) return; |
| 779 | if (typeof o === "string") return arrayLikeToArray(o, minLen); |
| 780 | var n = Object.prototype.toString.call(o).slice(8, -1); |
| 781 | if (n === "Object" && o.constructor) n = o.constructor.name; |
| 782 | if (n === "Map" || n === "Set") return Array.from(o); |
| 783 | if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) |
| 784 | return arrayLikeToArray(o, minLen); |
| 785 | } |
| 786 | `; |
| 787 | |
| 788 | helpers.arrayLikeToArray = helper("7.9.0")` |
| 789 | export default function _arrayLikeToArray(arr, len) { |
| 790 | if (len == null || len > arr.length) len = arr.length; |
| 791 | for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; |
| 792 | return arr2; |
| 793 | } |
| 794 | `; |
| 795 | |
| 796 | helpers.nonIterableSpread = helper("7.0.0-beta.0")` |
| 797 | export default function _nonIterableSpread() { |
| 798 | throw new TypeError( |
| 799 | "Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method." |
| 800 | ); |
| 801 | } |
| 802 | `; |
| 803 | |
| 804 | helpers.nonIterableRest = helper("7.0.0-beta.0")` |
| 805 | export default function _nonIterableRest() { |
| 806 | throw new TypeError( |
| 807 | "Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method." |
| 808 | ); |
| 809 | } |
| 810 | `; |
| 811 | |
| 812 | helpers.createForOfIteratorHelper = helper("7.9.0")` |
| 813 | import unsupportedIterableToArray from "unsupportedIterableToArray"; |
| 814 | |
| 815 | // s: start (create the iterator) |
| 816 | // n: next |
| 817 | // e: error (called whenever something throws) |
| 818 | // f: finish (always called at the end) |
| 819 | |
| 820 | export default function _createForOfIteratorHelper(o, allowArrayLike) { |
| 821 | var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; |
| 822 | |
| 823 | if (!it) { |
| 824 | // Fallback for engines without symbol support |
| 825 | if ( |
| 826 | Array.isArray(o) || |
| 827 | (it = unsupportedIterableToArray(o)) || |
| 828 | (allowArrayLike && o && typeof o.length === "number") |
| 829 | ) { |
| 830 | if (it) o = it; |
| 831 | var i = 0; |
| 832 | var F = function(){}; |
| 833 | return { |
| 834 | s: F, |
| 835 | n: function() { |
| 836 | if (i >= o.length) return { done: true }; |
| 837 | return { done: false, value: o[i++] }; |
| 838 | }, |
| 839 | e: function(e) { throw e; }, |
| 840 | f: F, |
| 841 | }; |
| 842 | } |
| 843 | |
| 844 | throw new TypeError("Invalid attempt to iterate non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); |
| 845 | } |
| 846 | |
| 847 | var normalCompletion = true, didErr = false, err; |
| 848 | |
| 849 | return { |
| 850 | s: function() { |
| 851 | it = it.call(o); |
| 852 | }, |
| 853 | n: function() { |
| 854 | var step = it.next(); |
| 855 | normalCompletion = step.done; |
| 856 | return step; |
| 857 | }, |
| 858 | e: function(e) { |
| 859 | didErr = true; |
| 860 | err = e; |
| 861 | }, |
| 862 | f: function() { |
| 863 | try { |
| 864 | if (!normalCompletion && it.return != null) it.return(); |
| 865 | } finally { |
| 866 | if (didErr) throw err; |
| 867 | } |
| 868 | } |
| 869 | }; |
| 870 | } |
| 871 | `; |
| 872 | |
| 873 | helpers.createForOfIteratorHelperLoose = helper("7.9.0")` |
| 874 | import unsupportedIterableToArray from "unsupportedIterableToArray"; |
| 875 | |
| 876 | export default function _createForOfIteratorHelperLoose(o, allowArrayLike) { |
| 877 | var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; |
| 878 | |
| 879 | if (it) return (it = it.call(o)).next.bind(it); |
| 880 | |
| 881 | // Fallback for engines without symbol support |
| 882 | if ( |
| 883 | Array.isArray(o) || |
| 884 | (it = unsupportedIterableToArray(o)) || |
| 885 | (allowArrayLike && o && typeof o.length === "number") |
| 886 | ) { |
| 887 | if (it) o = it; |
| 888 | var i = 0; |
| 889 | return function() { |
| 890 | if (i >= o.length) return { done: true }; |
| 891 | return { done: false, value: o[i++] }; |
| 892 | } |
| 893 | } |
| 894 | |
| 895 | throw new TypeError("Invalid attempt to iterate non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); |
| 896 | } |
| 897 | `; |
| 898 | |
| 899 | helpers.skipFirstGeneratorNext = helper("7.0.0-beta.0")` |
| 900 | export default function _skipFirstGeneratorNext(fn) { |
| 901 | return function () { |
| 902 | var it = fn.apply(this, arguments); |
| 903 | it.next(); |
| 904 | return it; |
| 905 | } |
| 906 | } |
| 907 | `; |
| 908 | |
| 909 | helpers.toPrimitive = helper("7.1.5")` |
| 910 | export default function _toPrimitive( |
| 911 | input, |
| 912 | hint /*: "default" | "string" | "number" | void */ |
| 913 | ) { |
| 914 | if (typeof input !== "object" || input === null) return input; |
| 915 | var prim = input[Symbol.toPrimitive]; |
| 916 | if (prim !== undefined) { |
| 917 | var res = prim.call(input, hint || "default"); |
| 918 | if (typeof res !== "object") return res; |
| 919 | throw new TypeError("@@toPrimitive must return a primitive value."); |
| 920 | } |
| 921 | return (hint === "string" ? String : Number)(input); |
| 922 | } |
| 923 | `; |
| 924 | |
| 925 | helpers.toPropertyKey = helper("7.1.5")` |
| 926 | import toPrimitive from "toPrimitive"; |
| 927 | |
| 928 | export default function _toPropertyKey(arg) { |
| 929 | var key = toPrimitive(arg, "string"); |
| 930 | return typeof key === "symbol" ? key : String(key); |
| 931 | } |
| 932 | `; |
| 933 | |
| 934 | /** |
| 935 | * Add a helper that will throw a useful error if the transform fails to detect the class |
| 936 | * property assignment, so users know something failed. |
| 937 | */ |
| 938 | helpers.initializerWarningHelper = helper("7.0.0-beta.0")` |
| 939 | export default function _initializerWarningHelper(descriptor, context){ |
| 940 | throw new Error( |
| 941 | 'Decorating class property failed. Please ensure that ' + |
| 942 | 'proposal-class-properties is enabled and runs after the decorators transform.' |
| 943 | ); |
| 944 | } |
| 945 | `; |
| 946 | |
| 947 | /** |
| 948 | * Add a helper to call as a replacement for class property definition. |
| 949 | */ |
| 950 | helpers.initializerDefineProperty = helper("7.0.0-beta.0")` |
| 951 | export default function _initializerDefineProperty(target, property, descriptor, context){ |
| 952 | if (!descriptor) return; |
| 953 | |
| 954 | Object.defineProperty(target, property, { |
| 955 | enumerable: descriptor.enumerable, |
| 956 | configurable: descriptor.configurable, |
| 957 | writable: descriptor.writable, |
| 958 | value: descriptor.initializer ? descriptor.initializer.call(context) : void 0, |
| 959 | }); |
| 960 | } |
| 961 | `; |
| 962 | |
| 963 | /** |
| 964 | * Add a helper to take an initial descriptor, apply some decorators to it, and optionally |
| 965 | * define the property. |
| 966 | */ |
| 967 | helpers.applyDecoratedDescriptor = helper("7.0.0-beta.0")` |
| 968 | export default function _applyDecoratedDescriptor(target, property, decorators, descriptor, context){ |
| 969 | var desc = {}; |
| 970 | Object.keys(descriptor).forEach(function(key){ |
| 971 | desc[key] = descriptor[key]; |
| 972 | }); |
| 973 | desc.enumerable = !!desc.enumerable; |
| 974 | desc.configurable = !!desc.configurable; |
| 975 | if ('value' in desc || desc.initializer){ |
| 976 | desc.writable = true; |
| 977 | } |
| 978 | |
| 979 | desc = decorators.slice().reverse().reduce(function(desc, decorator){ |
| 980 | return decorator(target, property, desc) || desc; |
| 981 | }, desc); |
| 982 | |
| 983 | if (context && desc.initializer !== void 0){ |
| 984 | desc.value = desc.initializer ? desc.initializer.call(context) : void 0; |
| 985 | desc.initializer = undefined; |
| 986 | } |
| 987 | |
| 988 | if (desc.initializer === void 0){ |
| 989 | Object.defineProperty(target, property, desc); |
| 990 | desc = null; |
| 991 | } |
| 992 | |
| 993 | return desc; |
| 994 | } |
| 995 | `; |
| 996 | |
| 997 | helpers.classPrivateFieldLooseKey = helper("7.0.0-beta.0")` |
| 998 | var id = 0; |
| 999 | export default function _classPrivateFieldKey(name) { |
| 1000 | return "__private_" + (id++) + "_" + name; |
| 1001 | } |
| 1002 | `; |
| 1003 | |
| 1004 | helpers.classPrivateFieldLooseBase = helper("7.0.0-beta.0")` |
| 1005 | export default function _classPrivateFieldBase(receiver, privateKey) { |
| 1006 | if (!Object.prototype.hasOwnProperty.call(receiver, privateKey)) { |
| 1007 | throw new TypeError("attempted to use private field on non-instance"); |
| 1008 | } |
| 1009 | return receiver; |
| 1010 | } |
| 1011 | `; |
| 1012 | |
| 1013 | helpers.classPrivateFieldGet = helper("7.0.0-beta.0")` |
| 1014 | import classApplyDescriptorGet from "classApplyDescriptorGet"; |
| 1015 | import classExtractFieldDescriptor from "classExtractFieldDescriptor"; |
| 1016 | export default function _classPrivateFieldGet(receiver, privateMap) { |
| 1017 | var descriptor = classExtractFieldDescriptor(receiver, privateMap, "get"); |
| 1018 | return classApplyDescriptorGet(receiver, descriptor); |
| 1019 | } |
| 1020 | `; |
| 1021 | |
| 1022 | helpers.classPrivateFieldSet = helper("7.0.0-beta.0")` |
| 1023 | import classApplyDescriptorSet from "classApplyDescriptorSet"; |
| 1024 | import classExtractFieldDescriptor from "classExtractFieldDescriptor"; |
| 1025 | export default function _classPrivateFieldSet(receiver, privateMap, value) { |
| 1026 | var descriptor = classExtractFieldDescriptor(receiver, privateMap, "set"); |
| 1027 | classApplyDescriptorSet(receiver, descriptor, value); |
| 1028 | return value; |
| 1029 | } |
| 1030 | `; |
| 1031 | |
| 1032 | helpers.classPrivateFieldDestructureSet = helper("7.4.4")` |
| 1033 | import classApplyDescriptorDestructureSet from "classApplyDescriptorDestructureSet"; |
| 1034 | import classExtractFieldDescriptor from "classExtractFieldDescriptor"; |
| 1035 | export default function _classPrivateFieldDestructureSet(receiver, privateMap) { |
| 1036 | var descriptor = classExtractFieldDescriptor(receiver, privateMap, "set"); |
| 1037 | return classApplyDescriptorDestructureSet(receiver, descriptor); |
| 1038 | } |
| 1039 | `; |
| 1040 | |
| 1041 | helpers.classExtractFieldDescriptor = helper("7.13.10")` |
| 1042 | export default function _classExtractFieldDescriptor(receiver, privateMap, action) { |
| 1043 | if (!privateMap.has(receiver)) { |
| 1044 | throw new TypeError("attempted to " + action + " private field on non-instance"); |
| 1045 | } |
| 1046 | return privateMap.get(receiver); |
| 1047 | } |
| 1048 | `; |
| 1049 | |
| 1050 | helpers.classStaticPrivateFieldSpecGet = helper("7.0.2")` |
| 1051 | import classApplyDescriptorGet from "classApplyDescriptorGet"; |
| 1052 | import classCheckPrivateStaticAccess from "classCheckPrivateStaticAccess"; |
| 1053 | import classCheckPrivateStaticFieldDescriptor from "classCheckPrivateStaticFieldDescriptor"; |
| 1054 | export default function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { |
| 1055 | classCheckPrivateStaticAccess(receiver, classConstructor); |
| 1056 | classCheckPrivateStaticFieldDescriptor(descriptor, "get"); |
| 1057 | return classApplyDescriptorGet(receiver, descriptor); |
| 1058 | } |
| 1059 | `; |
| 1060 | |
| 1061 | helpers.classStaticPrivateFieldSpecSet = helper("7.0.2")` |
| 1062 | import classApplyDescriptorSet from "classApplyDescriptorSet"; |
| 1063 | import classCheckPrivateStaticAccess from "classCheckPrivateStaticAccess"; |
| 1064 | import classCheckPrivateStaticFieldDescriptor from "classCheckPrivateStaticFieldDescriptor"; |
| 1065 | export default function _classStaticPrivateFieldSpecSet(receiver, classConstructor, descriptor, value) { |
| 1066 | classCheckPrivateStaticAccess(receiver, classConstructor); |
| 1067 | classCheckPrivateStaticFieldDescriptor(descriptor, "set"); |
| 1068 | classApplyDescriptorSet(receiver, descriptor, value); |
| 1069 | return value; |
| 1070 | } |
| 1071 | `; |
| 1072 | |
| 1073 | helpers.classStaticPrivateMethodGet = helper("7.3.2")` |
| 1074 | import classCheckPrivateStaticAccess from "classCheckPrivateStaticAccess"; |
| 1075 | export default function _classStaticPrivateMethodGet(receiver, classConstructor, method) { |
| 1076 | classCheckPrivateStaticAccess(receiver, classConstructor); |
| 1077 | return method; |
| 1078 | } |
| 1079 | `; |
| 1080 | |
| 1081 | helpers.classStaticPrivateMethodSet = helper("7.3.2")` |
| 1082 | export default function _classStaticPrivateMethodSet() { |
| 1083 | throw new TypeError("attempted to set read only static private field"); |
| 1084 | } |
| 1085 | `; |
| 1086 | |
| 1087 | helpers.classApplyDescriptorGet = helper("7.13.10")` |
| 1088 | export default function _classApplyDescriptorGet(receiver, descriptor) { |
| 1089 | if (descriptor.get) { |
| 1090 | return descriptor.get.call(receiver); |
| 1091 | } |
| 1092 | return descriptor.value; |
| 1093 | } |
| 1094 | `; |
| 1095 | |
| 1096 | helpers.classApplyDescriptorSet = helper("7.13.10")` |
| 1097 | export default function _classApplyDescriptorSet(receiver, descriptor, value) { |
| 1098 | if (descriptor.set) { |
| 1099 | descriptor.set.call(receiver, value); |
| 1100 | } else { |
| 1101 | if (!descriptor.writable) { |
| 1102 | // This should only throw in strict mode, but class bodies are |
| 1103 | // always strict and private fields can only be used inside |
| 1104 | // class bodies. |
| 1105 | throw new TypeError("attempted to set read only private field"); |
| 1106 | } |
| 1107 | descriptor.value = value; |
| 1108 | } |
| 1109 | } |
| 1110 | `; |
| 1111 | |
| 1112 | helpers.classApplyDescriptorDestructureSet = helper("7.13.10")` |
| 1113 | export default function _classApplyDescriptorDestructureSet(receiver, descriptor) { |
| 1114 | if (descriptor.set) { |
| 1115 | if (!("__destrObj" in descriptor)) { |
| 1116 | descriptor.__destrObj = { |
| 1117 | set value(v) { |
| 1118 | descriptor.set.call(receiver, v) |
| 1119 | }, |
| 1120 | }; |
| 1121 | } |
| 1122 | return descriptor.__destrObj; |
| 1123 | } else { |
| 1124 | if (!descriptor.writable) { |
| 1125 | // This should only throw in strict mode, but class bodies are |
| 1126 | // always strict and private fields can only be used inside |
| 1127 | // class bodies. |
| 1128 | throw new TypeError("attempted to set read only private field"); |
| 1129 | } |
| 1130 | |
| 1131 | return descriptor; |
| 1132 | } |
| 1133 | } |
| 1134 | `; |
| 1135 | |
| 1136 | helpers.classStaticPrivateFieldDestructureSet = helper("7.13.10")` |
| 1137 | import classApplyDescriptorDestructureSet from "classApplyDescriptorDestructureSet"; |
| 1138 | import classCheckPrivateStaticAccess from "classCheckPrivateStaticAccess"; |
| 1139 | import classCheckPrivateStaticFieldDescriptor from "classCheckPrivateStaticFieldDescriptor"; |
| 1140 | export default function _classStaticPrivateFieldDestructureSet(receiver, classConstructor, descriptor) { |
| 1141 | classCheckPrivateStaticAccess(receiver, classConstructor); |
| 1142 | classCheckPrivateStaticFieldDescriptor(descriptor, "set"); |
| 1143 | return classApplyDescriptorDestructureSet(receiver, descriptor); |
| 1144 | } |
| 1145 | `; |
| 1146 | |
| 1147 | helpers.classCheckPrivateStaticAccess = helper("7.13.10")` |
| 1148 | export default function _classCheckPrivateStaticAccess(receiver, classConstructor) { |
| 1149 | if (receiver !== classConstructor) { |
| 1150 | throw new TypeError("Private static access of wrong provenance"); |
| 1151 | } |
| 1152 | } |
| 1153 | `; |
| 1154 | |
| 1155 | helpers.classCheckPrivateStaticFieldDescriptor = helper("7.13.10")` |
| 1156 | export default function _classCheckPrivateStaticFieldDescriptor(descriptor, action) { |
| 1157 | if (descriptor === undefined) { |
| 1158 | throw new TypeError("attempted to " + action + " private static field before its declaration"); |
| 1159 | } |
| 1160 | } |
| 1161 | `; |
| 1162 | |
| 1163 | helpers.decorate = helper("7.1.5")` |
| 1164 | import toArray from "toArray"; |
| 1165 | import toPropertyKey from "toPropertyKey"; |
| 1166 | |
| 1167 | // These comments are stripped by @babel/template |
| 1168 | /*:: |
| 1169 | type PropertyDescriptor = |
| 1170 | | { |
| 1171 | value: any, |
| 1172 | writable: boolean, |
| 1173 | configurable: boolean, |
| 1174 | enumerable: boolean, |
| 1175 | } |
| 1176 | | { |
| 1177 | get?: () => any, |
| 1178 | set?: (v: any) => void, |
| 1179 | configurable: boolean, |
| 1180 | enumerable: boolean, |
| 1181 | }; |
| 1182 | |
| 1183 | type FieldDescriptor ={ |
| 1184 | writable: boolean, |
| 1185 | configurable: boolean, |
| 1186 | enumerable: boolean, |
| 1187 | }; |
| 1188 | |
| 1189 | type Placement = "static" | "prototype" | "own"; |
| 1190 | type Key = string | symbol; // PrivateName is not supported yet. |
| 1191 | |
| 1192 | type ElementDescriptor = |
| 1193 | | { |
| 1194 | kind: "method", |
| 1195 | key: Key, |
| 1196 | placement: Placement, |
| 1197 | descriptor: PropertyDescriptor |
| 1198 | } |
| 1199 | | { |
| 1200 | kind: "field", |
| 1201 | key: Key, |
| 1202 | placement: Placement, |
| 1203 | descriptor: FieldDescriptor, |
| 1204 | initializer?: () => any, |
| 1205 | }; |
| 1206 | |
| 1207 | // This is exposed to the user code |
| 1208 | type ElementObjectInput = ElementDescriptor & { |
| 1209 | [@@toStringTag]?: "Descriptor" |
| 1210 | }; |
| 1211 | |
| 1212 | // This is exposed to the user code |
| 1213 | type ElementObjectOutput = ElementDescriptor & { |
| 1214 | [@@toStringTag]?: "Descriptor" |
| 1215 | extras?: ElementDescriptor[], |
| 1216 | finisher?: ClassFinisher, |
| 1217 | }; |
| 1218 | |
| 1219 | // This is exposed to the user code |
| 1220 | type ClassObject = { |
| 1221 | [@@toStringTag]?: "Descriptor", |
| 1222 | kind: "class", |
| 1223 | elements: ElementDescriptor[], |
| 1224 | }; |
| 1225 | |
| 1226 | type ElementDecorator = (descriptor: ElementObjectInput) => ?ElementObjectOutput; |
| 1227 | type ClassDecorator = (descriptor: ClassObject) => ?ClassObject; |
| 1228 | type ClassFinisher = <A, B>(cl: Class<A>) => Class<B>; |
| 1229 | |
| 1230 | // Only used by Babel in the transform output, not part of the spec. |
| 1231 | type ElementDefinition = |
| 1232 | | { |
| 1233 | kind: "method", |
| 1234 | value: any, |
| 1235 | key: Key, |
| 1236 | static?: boolean, |
| 1237 | decorators?: ElementDecorator[], |
| 1238 | } |
| 1239 | | { |
| 1240 | kind: "field", |
| 1241 | value: () => any, |
| 1242 | key: Key, |
| 1243 | static?: boolean, |
| 1244 | decorators?: ElementDecorator[], |
| 1245 | }; |
| 1246 | |
| 1247 | declare function ClassFactory<C>(initialize: (instance: C) => void): { |
| 1248 | F: Class<C>, |
| 1249 | d: ElementDefinition[] |
| 1250 | } |
| 1251 | |
| 1252 | */ |
| 1253 | |
| 1254 | /*:: |
| 1255 | // Various combinations with/without extras and with one or many finishers |
| 1256 | |
| 1257 | type ElementFinisherExtras = { |
| 1258 | element: ElementDescriptor, |
| 1259 | finisher?: ClassFinisher, |
| 1260 | extras?: ElementDescriptor[], |
| 1261 | }; |
| 1262 | |
| 1263 | type ElementFinishersExtras = { |
| 1264 | element: ElementDescriptor, |
| 1265 | finishers: ClassFinisher[], |
| 1266 | extras: ElementDescriptor[], |
| 1267 | }; |
| 1268 | |
| 1269 | type ElementsFinisher = { |
| 1270 | elements: ElementDescriptor[], |
| 1271 | finisher?: ClassFinisher, |
| 1272 | }; |
| 1273 | |
| 1274 | type ElementsFinishers = { |
| 1275 | elements: ElementDescriptor[], |
| 1276 | finishers: ClassFinisher[], |
| 1277 | }; |
| 1278 | |
| 1279 | */ |
| 1280 | |
| 1281 | /*:: |
| 1282 | |
| 1283 | type Placements = { |
| 1284 | static: Key[], |
| 1285 | prototype: Key[], |
| 1286 | own: Key[], |
| 1287 | }; |
| 1288 | |
| 1289 | */ |
| 1290 | |
| 1291 | // ClassDefinitionEvaluation (Steps 26-*) |
| 1292 | export default function _decorate( |
| 1293 | decorators /*: ClassDecorator[] */, |
| 1294 | factory /*: ClassFactory */, |
| 1295 | superClass /*: ?Class<*> */, |
| 1296 | mixins /*: ?Array<Function> */, |
| 1297 | ) /*: Class<*> */ { |
| 1298 | var api = _getDecoratorsApi(); |
| 1299 | if (mixins) { |
| 1300 | for (var i = 0; i < mixins.length; i++) { |
| 1301 | api = mixins[i](api); |
| 1302 | } |
| 1303 | } |
| 1304 | |
| 1305 | var r = factory(function initialize(O) { |
| 1306 | api.initializeInstanceElements(O, decorated.elements); |
| 1307 | }, superClass); |
| 1308 | var decorated = api.decorateClass( |
| 1309 | _coalesceClassElements(r.d.map(_createElementDescriptor)), |
| 1310 | decorators, |
| 1311 | ); |
| 1312 | |
| 1313 | api.initializeClassElements(r.F, decorated.elements); |
| 1314 | |
| 1315 | return api.runClassFinishers(r.F, decorated.finishers); |
| 1316 | } |
| 1317 | |
| 1318 | function _getDecoratorsApi() { |
| 1319 | _getDecoratorsApi = function() { |
| 1320 | return api; |
| 1321 | }; |
| 1322 | |
| 1323 | var api = { |
| 1324 | elementsDefinitionOrder: [["method"], ["field"]], |
| 1325 | |
| 1326 | // InitializeInstanceElements |
| 1327 | initializeInstanceElements: function( |
| 1328 | /*::<C>*/ O /*: C */, |
| 1329 | elements /*: ElementDescriptor[] */, |
| 1330 | ) { |
| 1331 | ["method", "field"].forEach(function(kind) { |
| 1332 | elements.forEach(function(element /*: ElementDescriptor */) { |
| 1333 | if (element.kind === kind && element.placement === "own") { |
| 1334 | this.defineClassElement(O, element); |
| 1335 | } |
| 1336 | }, this); |
| 1337 | }, this); |
| 1338 | }, |
| 1339 | |
| 1340 | // InitializeClassElements |
| 1341 | initializeClassElements: function( |
| 1342 | /*::<C>*/ F /*: Class<C> */, |
| 1343 | elements /*: ElementDescriptor[] */, |
| 1344 | ) { |
| 1345 | var proto = F.prototype; |
| 1346 | |
| 1347 | ["method", "field"].forEach(function(kind) { |
| 1348 | elements.forEach(function(element /*: ElementDescriptor */) { |
| 1349 | var placement = element.placement; |
| 1350 | if ( |
| 1351 | element.kind === kind && |
| 1352 | (placement === "static" || placement === "prototype") |
| 1353 | ) { |
| 1354 | var receiver = placement === "static" ? F : proto; |
| 1355 | this.defineClassElement(receiver, element); |
| 1356 | } |
| 1357 | }, this); |
| 1358 | }, this); |
| 1359 | }, |
| 1360 | |
| 1361 | // DefineClassElement |
| 1362 | defineClassElement: function( |
| 1363 | /*::<C>*/ receiver /*: C | Class<C> */, |
| 1364 | element /*: ElementDescriptor */, |
| 1365 | ) { |
| 1366 | var descriptor /*: PropertyDescriptor */ = element.descriptor; |
| 1367 | if (element.kind === "field") { |
| 1368 | var initializer = element.initializer; |
| 1369 | descriptor = { |
| 1370 | enumerable: descriptor.enumerable, |
| 1371 | writable: descriptor.writable, |
| 1372 | configurable: descriptor.configurable, |
| 1373 | value: initializer === void 0 ? void 0 : initializer.call(receiver), |
| 1374 | }; |
| 1375 | } |
| 1376 | Object.defineProperty(receiver, element.key, descriptor); |
| 1377 | }, |
| 1378 | |
| 1379 | // DecorateClass |
| 1380 | decorateClass: function( |
| 1381 | elements /*: ElementDescriptor[] */, |
| 1382 | decorators /*: ClassDecorator[] */, |
| 1383 | ) /*: ElementsFinishers */ { |
| 1384 | var newElements /*: ElementDescriptor[] */ = []; |
| 1385 | var finishers /*: ClassFinisher[] */ = []; |
| 1386 | var placements /*: Placements */ = { |
| 1387 | static: [], |
| 1388 | prototype: [], |
| 1389 | own: [], |
| 1390 | }; |
| 1391 | |
| 1392 | elements.forEach(function(element /*: ElementDescriptor */) { |
| 1393 | this.addElementPlacement(element, placements); |
| 1394 | }, this); |
| 1395 | |
| 1396 | elements.forEach(function(element /*: ElementDescriptor */) { |
| 1397 | if (!_hasDecorators(element)) return newElements.push(element); |
| 1398 | |
| 1399 | var elementFinishersExtras /*: ElementFinishersExtras */ = this.decorateElement( |
| 1400 | element, |
| 1401 | placements, |
| 1402 | ); |
| 1403 | newElements.push(elementFinishersExtras.element); |
| 1404 | newElements.push.apply(newElements, elementFinishersExtras.extras); |
| 1405 | finishers.push.apply(finishers, elementFinishersExtras.finishers); |
| 1406 | }, this); |
| 1407 | |
| 1408 | if (!decorators) { |
| 1409 | return { elements: newElements, finishers: finishers }; |
| 1410 | } |
| 1411 | |
| 1412 | var result /*: ElementsFinishers */ = this.decorateConstructor( |
| 1413 | newElements, |
| 1414 | decorators, |
| 1415 | ); |
| 1416 | finishers.push.apply(finishers, result.finishers); |
| 1417 | result.finishers = finishers; |
| 1418 | |
| 1419 | return result; |
| 1420 | }, |
| 1421 | |
| 1422 | // AddElementPlacement |
| 1423 | addElementPlacement: function( |
| 1424 | element /*: ElementDescriptor */, |
| 1425 | placements /*: Placements */, |
| 1426 | silent /*: boolean */, |
| 1427 | ) { |
| 1428 | var keys = placements[element.placement]; |
| 1429 | if (!silent && keys.indexOf(element.key) !== -1) { |
| 1430 | throw new TypeError("Duplicated element (" + element.key + ")"); |
| 1431 | } |
| 1432 | keys.push(element.key); |
| 1433 | }, |
| 1434 | |
| 1435 | // DecorateElement |
| 1436 | decorateElement: function( |
| 1437 | element /*: ElementDescriptor */, |
| 1438 | placements /*: Placements */, |
| 1439 | ) /*: ElementFinishersExtras */ { |
| 1440 | var extras /*: ElementDescriptor[] */ = []; |
| 1441 | var finishers /*: ClassFinisher[] */ = []; |
| 1442 | |
| 1443 | for ( |
| 1444 | var decorators = element.decorators, i = decorators.length - 1; |
| 1445 | i >= 0; |
| 1446 | i-- |
| 1447 | ) { |
| 1448 | // (inlined) RemoveElementPlacement |
| 1449 | var keys = placements[element.placement]; |
| 1450 | keys.splice(keys.indexOf(element.key), 1); |
| 1451 | |
| 1452 | var elementObject /*: ElementObjectInput */ = this.fromElementDescriptor( |
| 1453 | element, |
| 1454 | ); |
| 1455 | var elementFinisherExtras /*: ElementFinisherExtras */ = this.toElementFinisherExtras( |
| 1456 | (0, decorators[i])(elementObject) /*: ElementObjectOutput */ || |
| 1457 | elementObject, |
| 1458 | ); |
| 1459 | |
| 1460 | element = elementFinisherExtras.element; |
| 1461 | this.addElementPlacement(element, placements); |
| 1462 | |
| 1463 | if (elementFinisherExtras.finisher) { |
| 1464 | finishers.push(elementFinisherExtras.finisher); |
| 1465 | } |
| 1466 | |
| 1467 | var newExtras /*: ElementDescriptor[] | void */ = |
| 1468 | elementFinisherExtras.extras; |
| 1469 | if (newExtras) { |
| 1470 | for (var j = 0; j < newExtras.length; j++) { |
| 1471 | this.addElementPlacement(newExtras[j], placements); |
| 1472 | } |
| 1473 | extras.push.apply(extras, newExtras); |
| 1474 | } |
| 1475 | } |
| 1476 | |
| 1477 | return { element: element, finishers: finishers, extras: extras }; |
| 1478 | }, |
| 1479 | |
| 1480 | // DecorateConstructor |
| 1481 | decorateConstructor: function( |
| 1482 | elements /*: ElementDescriptor[] */, |
| 1483 | decorators /*: ClassDecorator[] */, |
| 1484 | ) /*: ElementsFinishers */ { |
| 1485 | var finishers /*: ClassFinisher[] */ = []; |
| 1486 | |
| 1487 | for (var i = decorators.length - 1; i >= 0; i--) { |
| 1488 | var obj /*: ClassObject */ = this.fromClassDescriptor(elements); |
| 1489 | var elementsAndFinisher /*: ElementsFinisher */ = this.toClassDescriptor( |
| 1490 | (0, decorators[i])(obj) /*: ClassObject */ || obj, |
| 1491 | ); |
| 1492 | |
| 1493 | if (elementsAndFinisher.finisher !== undefined) { |
| 1494 | finishers.push(elementsAndFinisher.finisher); |
| 1495 | } |
| 1496 | |
| 1497 | if (elementsAndFinisher.elements !== undefined) { |
| 1498 | elements = elementsAndFinisher.elements; |
| 1499 | |
| 1500 | for (var j = 0; j < elements.length - 1; j++) { |
| 1501 | for (var k = j + 1; k < elements.length; k++) { |
| 1502 | if ( |
| 1503 | elements[j].key === elements[k].key && |
| 1504 | elements[j].placement === elements[k].placement |
| 1505 | ) { |
| 1506 | throw new TypeError( |
| 1507 | "Duplicated element (" + elements[j].key + ")", |
| 1508 | ); |
| 1509 | } |
| 1510 | } |
| 1511 | } |
| 1512 | } |
| 1513 | } |
| 1514 | |
| 1515 | return { elements: elements, finishers: finishers }; |
| 1516 | }, |
| 1517 | |
| 1518 | // FromElementDescriptor |
| 1519 | fromElementDescriptor: function( |
| 1520 | element /*: ElementDescriptor */, |
| 1521 | ) /*: ElementObject */ { |
| 1522 | var obj /*: ElementObject */ = { |
| 1523 | kind: element.kind, |
| 1524 | key: element.key, |
| 1525 | placement: element.placement, |
| 1526 | descriptor: element.descriptor, |
| 1527 | }; |
| 1528 | |
| 1529 | var desc = { |
| 1530 | value: "Descriptor", |
| 1531 | configurable: true, |
| 1532 | }; |
| 1533 | Object.defineProperty(obj, Symbol.toStringTag, desc); |
| 1534 | |
| 1535 | if (element.kind === "field") obj.initializer = element.initializer; |
| 1536 | |
| 1537 | return obj; |
| 1538 | }, |
| 1539 | |
| 1540 | // ToElementDescriptors |
| 1541 | toElementDescriptors: function( |
| 1542 | elementObjects /*: ElementObject[] */, |
| 1543 | ) /*: ElementDescriptor[] */ { |
| 1544 | if (elementObjects === undefined) return; |
| 1545 | return toArray(elementObjects).map(function(elementObject) { |
| 1546 | var element = this.toElementDescriptor(elementObject); |
| 1547 | this.disallowProperty(elementObject, "finisher", "An element descriptor"); |
| 1548 | this.disallowProperty(elementObject, "extras", "An element descriptor"); |
| 1549 | return element; |
| 1550 | }, this); |
| 1551 | }, |
| 1552 | |
| 1553 | // ToElementDescriptor |
| 1554 | toElementDescriptor: function( |
| 1555 | elementObject /*: ElementObject */, |
| 1556 | ) /*: ElementDescriptor */ { |
| 1557 | var kind = String(elementObject.kind); |
| 1558 | if (kind !== "method" && kind !== "field") { |
| 1559 | throw new TypeError( |
| 1560 | 'An element descriptor\\'s .kind property must be either "method" or' + |
| 1561 | ' "field", but a decorator created an element descriptor with' + |
| 1562 | ' .kind "' + |
| 1563 | kind + |
| 1564 | '"', |
| 1565 | ); |
| 1566 | } |
| 1567 | |
| 1568 | var key = toPropertyKey(elementObject.key); |
| 1569 | |
| 1570 | var placement = String(elementObject.placement); |
| 1571 | if ( |
| 1572 | placement !== "static" && |
| 1573 | placement !== "prototype" && |
| 1574 | placement !== "own" |
| 1575 | ) { |
| 1576 | throw new TypeError( |
| 1577 | 'An element descriptor\\'s .placement property must be one of "static",' + |
| 1578 | ' "prototype" or "own", but a decorator created an element descriptor' + |
| 1579 | ' with .placement "' + |
| 1580 | placement + |
| 1581 | '"', |
| 1582 | ); |
| 1583 | } |
| 1584 | |
| 1585 | var descriptor /*: PropertyDescriptor */ = elementObject.descriptor; |
| 1586 | |
| 1587 | this.disallowProperty(elementObject, "elements", "An element descriptor"); |
| 1588 | |
| 1589 | var element /*: ElementDescriptor */ = { |
| 1590 | kind: kind, |
| 1591 | key: key, |
| 1592 | placement: placement, |
| 1593 | descriptor: Object.assign({}, descriptor), |
| 1594 | }; |
| 1595 | |
| 1596 | if (kind !== "field") { |
| 1597 | this.disallowProperty(elementObject, "initializer", "A method descriptor"); |
| 1598 | } else { |
| 1599 | this.disallowProperty( |
| 1600 | descriptor, |
| 1601 | "get", |
| 1602 | "The property descriptor of a field descriptor", |
| 1603 | ); |
| 1604 | this.disallowProperty( |
| 1605 | descriptor, |
| 1606 | "set", |
| 1607 | "The property descriptor of a field descriptor", |
| 1608 | ); |
| 1609 | this.disallowProperty( |
| 1610 | descriptor, |
| 1611 | "value", |
| 1612 | "The property descriptor of a field descriptor", |
| 1613 | ); |
| 1614 | |
| 1615 | element.initializer = elementObject.initializer; |
| 1616 | } |
| 1617 | |
| 1618 | return element; |
| 1619 | }, |
| 1620 | |
| 1621 | toElementFinisherExtras: function( |
| 1622 | elementObject /*: ElementObject */, |
| 1623 | ) /*: ElementFinisherExtras */ { |
| 1624 | var element /*: ElementDescriptor */ = this.toElementDescriptor( |
| 1625 | elementObject, |
| 1626 | ); |
| 1627 | var finisher /*: ClassFinisher */ = _optionalCallableProperty( |
| 1628 | elementObject, |
| 1629 | "finisher", |
| 1630 | ); |
| 1631 | var extras /*: ElementDescriptors[] */ = this.toElementDescriptors( |
| 1632 | elementObject.extras, |
| 1633 | ); |
| 1634 | |
| 1635 | return { element: element, finisher: finisher, extras: extras }; |
| 1636 | }, |
| 1637 | |
| 1638 | // FromClassDescriptor |
| 1639 | fromClassDescriptor: function( |
| 1640 | elements /*: ElementDescriptor[] */, |
| 1641 | ) /*: ClassObject */ { |
| 1642 | var obj = { |
| 1643 | kind: "class", |
| 1644 | elements: elements.map(this.fromElementDescriptor, this), |
| 1645 | }; |
| 1646 | |
| 1647 | var desc = { value: "Descriptor", configurable: true }; |
| 1648 | Object.defineProperty(obj, Symbol.toStringTag, desc); |
| 1649 | |
| 1650 | return obj; |
| 1651 | }, |
| 1652 | |
| 1653 | // ToClassDescriptor |
| 1654 | toClassDescriptor: function( |
| 1655 | obj /*: ClassObject */, |
| 1656 | ) /*: ElementsFinisher */ { |
| 1657 | var kind = String(obj.kind); |
| 1658 | if (kind !== "class") { |
| 1659 | throw new TypeError( |
| 1660 | 'A class descriptor\\'s .kind property must be "class", but a decorator' + |
| 1661 | ' created a class descriptor with .kind "' + |
| 1662 | kind + |
| 1663 | '"', |
| 1664 | ); |
| 1665 | } |
| 1666 | |
| 1667 | this.disallowProperty(obj, "key", "A class descriptor"); |
| 1668 | this.disallowProperty(obj, "placement", "A class descriptor"); |
| 1669 | this.disallowProperty(obj, "descriptor", "A class descriptor"); |
| 1670 | this.disallowProperty(obj, "initializer", "A class descriptor"); |
| 1671 | this.disallowProperty(obj, "extras", "A class descriptor"); |
| 1672 | |
| 1673 | var finisher = _optionalCallableProperty(obj, "finisher"); |
| 1674 | var elements = this.toElementDescriptors(obj.elements); |
| 1675 | |
| 1676 | return { elements: elements, finisher: finisher }; |
| 1677 | }, |
| 1678 | |
| 1679 | // RunClassFinishers |
| 1680 | runClassFinishers: function( |
| 1681 | constructor /*: Class<*> */, |
| 1682 | finishers /*: ClassFinisher[] */, |
| 1683 | ) /*: Class<*> */ { |
| 1684 | for (var i = 0; i < finishers.length; i++) { |
| 1685 | var newConstructor /*: ?Class<*> */ = (0, finishers[i])(constructor); |
| 1686 | if (newConstructor !== undefined) { |
| 1687 | // NOTE: This should check if IsConstructor(newConstructor) is false. |
| 1688 | if (typeof newConstructor !== "function") { |
| 1689 | throw new TypeError("Finishers must return a constructor."); |
| 1690 | } |
| 1691 | constructor = newConstructor; |
| 1692 | } |
| 1693 | } |
| 1694 | return constructor; |
| 1695 | }, |
| 1696 | |
| 1697 | disallowProperty: function(obj, name, objectType) { |
| 1698 | if (obj[name] !== undefined) { |
| 1699 | throw new TypeError(objectType + " can't have a ." + name + " property."); |
| 1700 | } |
| 1701 | } |
| 1702 | }; |
| 1703 | |
| 1704 | return api; |
| 1705 | } |
| 1706 | |
| 1707 | // ClassElementEvaluation |
| 1708 | function _createElementDescriptor( |
| 1709 | def /*: ElementDefinition */, |
| 1710 | ) /*: ElementDescriptor */ { |
| 1711 | var key = toPropertyKey(def.key); |
| 1712 | |
| 1713 | var descriptor /*: PropertyDescriptor */; |
| 1714 | if (def.kind === "method") { |
| 1715 | descriptor = { |
| 1716 | value: def.value, |
| 1717 | writable: true, |
| 1718 | configurable: true, |
| 1719 | enumerable: false, |
| 1720 | }; |
| 1721 | } else if (def.kind === "get") { |
| 1722 | descriptor = { get: def.value, configurable: true, enumerable: false }; |
| 1723 | } else if (def.kind === "set") { |
| 1724 | descriptor = { set: def.value, configurable: true, enumerable: false }; |
| 1725 | } else if (def.kind === "field") { |
| 1726 | descriptor = { configurable: true, writable: true, enumerable: true }; |
| 1727 | } |
| 1728 | |
| 1729 | var element /*: ElementDescriptor */ = { |
| 1730 | kind: def.kind === "field" ? "field" : "method", |
| 1731 | key: key, |
| 1732 | placement: def.static |
| 1733 | ? "static" |
| 1734 | : def.kind === "field" |
| 1735 | ? "own" |
| 1736 | : "prototype", |
| 1737 | descriptor: descriptor, |
| 1738 | }; |
| 1739 | if (def.decorators) element.decorators = def.decorators; |
| 1740 | if (def.kind === "field") element.initializer = def.value; |
| 1741 | |
| 1742 | return element; |
| 1743 | } |
| 1744 | |
| 1745 | // CoalesceGetterSetter |
| 1746 | function _coalesceGetterSetter( |
| 1747 | element /*: ElementDescriptor */, |
| 1748 | other /*: ElementDescriptor */, |
| 1749 | ) { |
| 1750 | if (element.descriptor.get !== undefined) { |
| 1751 | other.descriptor.get = element.descriptor.get; |
| 1752 | } else { |
| 1753 | other.descriptor.set = element.descriptor.set; |
| 1754 | } |
| 1755 | } |
| 1756 | |
| 1757 | // CoalesceClassElements |
| 1758 | function _coalesceClassElements( |
| 1759 | elements /*: ElementDescriptor[] */, |
| 1760 | ) /*: ElementDescriptor[] */ { |
| 1761 | var newElements /*: ElementDescriptor[] */ = []; |
| 1762 | |
| 1763 | var isSameElement = function( |
| 1764 | other /*: ElementDescriptor */, |
| 1765 | ) /*: boolean */ { |
| 1766 | return ( |
| 1767 | other.kind === "method" && |
| 1768 | other.key === element.key && |
| 1769 | other.placement === element.placement |
| 1770 | ); |
| 1771 | }; |
| 1772 | |
| 1773 | for (var i = 0; i < elements.length; i++) { |
| 1774 | var element /*: ElementDescriptor */ = elements[i]; |
| 1775 | var other /*: ElementDescriptor */; |
| 1776 | |
| 1777 | if ( |
| 1778 | element.kind === "method" && |
| 1779 | (other = newElements.find(isSameElement)) |
| 1780 | ) { |
| 1781 | if ( |
| 1782 | _isDataDescriptor(element.descriptor) || |
| 1783 | _isDataDescriptor(other.descriptor) |
| 1784 | ) { |
| 1785 | if (_hasDecorators(element) || _hasDecorators(other)) { |
| 1786 | throw new ReferenceError( |
| 1787 | "Duplicated methods (" + element.key + ") can't be decorated.", |
| 1788 | ); |
| 1789 | } |
| 1790 | other.descriptor = element.descriptor; |
| 1791 | } else { |
| 1792 | if (_hasDecorators(element)) { |
| 1793 | if (_hasDecorators(other)) { |
| 1794 | throw new ReferenceError( |
| 1795 | "Decorators can't be placed on different accessors with for " + |
| 1796 | "the same property (" + |
| 1797 | element.key + |
| 1798 | ").", |
| 1799 | ); |
| 1800 | } |
| 1801 | other.decorators = element.decorators; |
| 1802 | } |
| 1803 | _coalesceGetterSetter(element, other); |
| 1804 | } |
| 1805 | } else { |
| 1806 | newElements.push(element); |
| 1807 | } |
| 1808 | } |
| 1809 | |
| 1810 | return newElements; |
| 1811 | } |
| 1812 | |
| 1813 | function _hasDecorators(element /*: ElementDescriptor */) /*: boolean */ { |
| 1814 | return element.decorators && element.decorators.length; |
| 1815 | } |
| 1816 | |
| 1817 | function _isDataDescriptor(desc /*: PropertyDescriptor */) /*: boolean */ { |
| 1818 | return ( |
| 1819 | desc !== undefined && |
| 1820 | !(desc.value === undefined && desc.writable === undefined) |
| 1821 | ); |
| 1822 | } |
| 1823 | |
| 1824 | function _optionalCallableProperty /*::<T>*/( |
| 1825 | obj /*: T */, |
| 1826 | name /*: $Keys<T> */, |
| 1827 | ) /*: ?Function */ { |
| 1828 | var value = obj[name]; |
| 1829 | if (value !== undefined && typeof value !== "function") { |
| 1830 | throw new TypeError("Expected '" + name + "' to be a function"); |
| 1831 | } |
| 1832 | return value; |
| 1833 | } |
| 1834 | |
| 1835 | `; |
| 1836 | |
| 1837 | helpers.classPrivateMethodGet = helper("7.1.6")` |
| 1838 | export default function _classPrivateMethodGet(receiver, privateSet, fn) { |
| 1839 | if (!privateSet.has(receiver)) { |
| 1840 | throw new TypeError("attempted to get private field on non-instance"); |
| 1841 | } |
| 1842 | return fn; |
| 1843 | } |
| 1844 | `; |
| 1845 | |
| 1846 | helpers.checkPrivateRedeclaration = helper("7.14.1")` |
| 1847 | export default function _checkPrivateRedeclaration(obj, privateCollection) { |
| 1848 | if (privateCollection.has(obj)) { |
| 1849 | throw new TypeError("Cannot initialize the same private elements twice on an object"); |
| 1850 | } |
| 1851 | } |
| 1852 | `; |
| 1853 | |
| 1854 | helpers.classPrivateFieldInitSpec = helper("7.14.1")` |
| 1855 | import checkPrivateRedeclaration from "checkPrivateRedeclaration"; |
| 1856 | |
| 1857 | export default function _classPrivateFieldInitSpec(obj, privateMap, value) { |
| 1858 | checkPrivateRedeclaration(obj, privateMap); |
| 1859 | privateMap.set(obj, value); |
| 1860 | } |
| 1861 | `; |
| 1862 | |
| 1863 | helpers.classPrivateMethodInitSpec = helper("7.14.1")` |
| 1864 | import checkPrivateRedeclaration from "checkPrivateRedeclaration"; |
| 1865 | |
| 1866 | export default function _classPrivateMethodInitSpec(obj, privateSet) { |
| 1867 | checkPrivateRedeclaration(obj, privateSet); |
| 1868 | privateSet.add(obj); |
| 1869 | } |
| 1870 | `; |
| 1871 | |
| 1872 | if (!process.env.BABEL_8_BREAKING) { |
| 1873 | // Use readOnlyError instead |
| 1874 | helpers.classPrivateMethodSet = helper("7.1.6")` |
| 1875 | export default function _classPrivateMethodSet() { |
| 1876 | throw new TypeError("attempted to reassign private method"); |
| 1877 | } |
| 1878 | `; |
| 1879 | } |
| 1880 | |
| 1881 | helpers.identity = helper("7.17.0")` |
| 1882 | export default function _identity(x) { |
| 1883 | return x; |
| 1884 | } |
| 1885 | `; |