inherit.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.createClass = exports.inherit = void 0;
  4. const extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array &&
  6. function (d, b) {
  7. d.__proto__ = b; // eslint-disable-line no-proto
  8. }) ||
  9. function (d, b) {
  10. // eslint-disable-next-line no-restricted-syntax
  11. for (const p in b) {
  12. if (Object.prototype.hasOwnProperty.call(b, p)) {
  13. d[p] = b[p];
  14. }
  15. }
  16. };
  17. /**
  18. * @see https://github.com/microsoft/TypeScript/blob/5c85febb0ce9d6088cbe9b09cb42f73f9ee8ea05/src/compiler/transformers/es2015.ts#L4309
  19. */
  20. // eslint-disable-next-line
  21. function inherit(cls, base) {
  22. extendStatics(cls, base);
  23. function tmp() {
  24. this.constructor = cls;
  25. }
  26. cls.prototype =
  27. base === null
  28. ? Object.create(base)
  29. : ((tmp.prototype = base.prototype), new tmp());
  30. }
  31. exports.inherit = inherit;
  32. class A {
  33. }
  34. const isNativeClass = /^\s*class\s+/.test(`${A}`) || /^\s*class\s*\{/.test(`${class {
  35. }}`);
  36. /**
  37. * Extends class with specified class name.
  38. */
  39. function createClass(className, base) {
  40. let cls;
  41. if (isNativeClass) {
  42. cls = class extends base {
  43. };
  44. }
  45. else {
  46. cls = function () {
  47. return base.apply(this, arguments); // eslint-disable-line
  48. };
  49. inherit(cls, base);
  50. }
  51. Object.defineProperty(cls, 'name', { value: className });
  52. return cls;
  53. }
  54. exports.createClass = createClass;
  55. //# sourceMappingURL=inherit.js.map