|
| 1 | +//// [tests/cases/compiler/accessorInAmbientContextES5.ts] //// |
| 2 | + |
| 3 | +//// [accessorInAmbientContextES5.ts] |
| 4 | +// Should allow accessor in ambient contexts even when targeting ES5 |
| 5 | + |
| 6 | +declare class AmbientClass { |
| 7 | + accessor prop1: string; |
| 8 | + static accessor prop2: number; |
| 9 | + private accessor prop3: boolean; |
| 10 | + private static accessor prop4: symbol; |
| 11 | +} |
| 12 | + |
| 13 | +declare namespace AmbientNamespace { |
| 14 | + class C { |
| 15 | + accessor prop: string; |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +// Should also work in .d.ts files (simulated with declare) |
| 20 | +declare module "some-module" { |
| 21 | + export class ExportedClass { |
| 22 | + accessor value: any; |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +// Regular class should still error when targeting ES5 |
| 27 | +class RegularClass { |
| 28 | + accessor shouldError: string; // Should still error |
| 29 | +} |
| 30 | + |
| 31 | +//// [accessorInAmbientContextES5.js] |
| 32 | +// Should allow accessor in ambient contexts even when targeting ES5 |
| 33 | +var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { |
| 34 | + if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); |
| 35 | + if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); |
| 36 | + return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); |
| 37 | +}; |
| 38 | +var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { |
| 39 | + if (kind === "m") throw new TypeError("Private method is not writable"); |
| 40 | + if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); |
| 41 | + if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); |
| 42 | + return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; |
| 43 | +}; |
| 44 | +var _RegularClass_shouldError_accessor_storage; |
| 45 | +// Regular class should still error when targeting ES5 |
| 46 | +var RegularClass = /** @class */ (function () { |
| 47 | + function RegularClass() { |
| 48 | + _RegularClass_shouldError_accessor_storage.set(this, void 0); |
| 49 | + } |
| 50 | + Object.defineProperty(RegularClass.prototype, "shouldError", { |
| 51 | + get: function () { return __classPrivateFieldGet(this, _RegularClass_shouldError_accessor_storage, "f"); } // Should still error |
| 52 | + , |
| 53 | + set: function (value) { __classPrivateFieldSet(this, _RegularClass_shouldError_accessor_storage, value, "f"); }, |
| 54 | + enumerable: false, |
| 55 | + configurable: true |
| 56 | + }); |
| 57 | + return RegularClass; |
| 58 | +}()); |
| 59 | +_RegularClass_shouldError_accessor_storage = new WeakMap(); |
| 60 | + |
| 61 | + |
| 62 | +//// [accessorInAmbientContextES5.d.ts] |
| 63 | +declare class AmbientClass { |
| 64 | + accessor prop1: string; |
| 65 | + static accessor prop2: number; |
| 66 | + private accessor prop3; |
| 67 | + private static accessor prop4; |
| 68 | +} |
| 69 | +declare namespace AmbientNamespace { |
| 70 | + class C { |
| 71 | + accessor prop: string; |
| 72 | + } |
| 73 | +} |
| 74 | +declare module "some-module" { |
| 75 | + class ExportedClass { |
| 76 | + accessor value: any; |
| 77 | + } |
| 78 | +} |
| 79 | +declare class RegularClass { |
| 80 | + accessor shouldError: string; |
| 81 | +} |
0 commit comments