diff --git a/tsc/internal/transformers/estransforms/classfields.go b/tsc/internal/transformers/estransforms/classfields.go index 68dc3950413d9..94671bdd4238e 100644 --- a/tsc/internal/transformers/estransforms/classfields.go +++ b/tsc/internal/transformers/estransforms/classfields.go @@ -686,9 +686,9 @@ func (tx *classFieldsTransformer) visitMethodOrAccessorDeclaration(node *ast.Nod } // leave invalid code untransformed - info := tx.accessPrivateIdentifier(node.Name()) + info, _ := tx.getPrivateIdentifier(tx.getPrivateIdentifierEnvironment(), node.Name()) debug.Assert(info != nil, "Undeclared private name for property declaration.") - if !info.isValid { + if info.kind == printer.PrivateIdentifierKindUntransformed || !info.isValid { return node } @@ -895,11 +895,11 @@ func (tx *classFieldsTransformer) transformAutoAccessor(node *ast.PropertyDeclar func (tx *classFieldsTransformer) transformPrivateFieldInitializer(node *ast.PropertyDeclaration) *ast.Node { if tx.shouldTransformClassElementToWeakMap(node.AsNode()) { // If we are transforming private elements into WeakMap/WeakSet, we should elide the node. - info := tx.accessPrivateIdentifier(node.Name()) + info, _ := tx.getPrivateIdentifier(tx.getPrivateIdentifierEnvironment(), node.Name()) debug.Assert(info != nil, "Undeclared private name for property declaration.") // Leave invalid code untransformed - if !info.isValid { + if info.kind == printer.PrivateIdentifierKindUntransformed || !info.isValid { return node.AsNode() } diff --git a/tsc/testdata/baselines/reference/conformance/esDecorators-classDeclaration-methods-staticPrivate(target=es2015).errors.txt b/tsc/testdata/baselines/reference/conformance/esDecorators-classDeclaration-methods-staticPrivate(target=es2015).errors.txt new file mode 100644 index 0000000000000..25f63760f051a --- /dev/null +++ b/tsc/testdata/baselines/reference/conformance/esDecorators-classDeclaration-methods-staticPrivate(target=es2015).errors.txt @@ -0,0 +1,66 @@ +esDecorators-classDeclaration-methods-staticPrivate.ts(14,12): error TS2804: Duplicate identifier '#a'. Static and instance elements cannot share the same private name. +esDecorators-classDeclaration-methods-staticPrivate.ts(15,5): error TS2804: Duplicate identifier '#a'. Static and instance elements cannot share the same private name. +esDecorators-classDeclaration-methods-staticPrivate.ts(16,5): error TS2804: Duplicate identifier '#b'. Static and instance elements cannot share the same private name. +esDecorators-classDeclaration-methods-staticPrivate.ts(17,12): error TS2804: Duplicate identifier '#b'. Static and instance elements cannot share the same private name. +esDecorators-classDeclaration-methods-staticPrivate.ts(18,12): error TS2804: Duplicate identifier '#c'. Static and instance elements cannot share the same private name. +esDecorators-classDeclaration-methods-staticPrivate.ts(19,5): error TS2804: Duplicate identifier '#c'. Static and instance elements cannot share the same private name. +esDecorators-classDeclaration-methods-staticPrivate.ts(20,5): error TS2804: Duplicate identifier '#d'. Static and instance elements cannot share the same private name. +esDecorators-classDeclaration-methods-staticPrivate.ts(21,12): error TS2804: Duplicate identifier '#d'. Static and instance elements cannot share the same private name. +esDecorators-classDeclaration-methods-staticPrivate.ts(22,16): error TS2804: Duplicate identifier '#e'. Static and instance elements cannot share the same private name. +esDecorators-classDeclaration-methods-staticPrivate.ts(23,9): error TS2804: Duplicate identifier '#e'. Static and instance elements cannot share the same private name. +esDecorators-classDeclaration-methods-staticPrivate.ts(24,9): error TS2804: Duplicate identifier '#f'. Static and instance elements cannot share the same private name. +esDecorators-classDeclaration-methods-staticPrivate.ts(25,16): error TS2804: Duplicate identifier '#f'. Static and instance elements cannot share the same private name. + + +==== esDecorators-classDeclaration-methods-staticPrivate.ts (12 errors) ==== + declare let dec: any; + + class C { + @dec static #method1() {} + } + + @dec + class D { + static #method1() {} + } + + @dec + class E { + static #a() {} + ~~ +!!! error TS2804: Duplicate identifier '#a'. Static and instance elements cannot share the same private name. + #a() {} + ~~ +!!! error TS2804: Duplicate identifier '#a'. Static and instance elements cannot share the same private name. + #b() {} + ~~ +!!! error TS2804: Duplicate identifier '#b'. Static and instance elements cannot share the same private name. + static #b() {} + ~~ +!!! error TS2804: Duplicate identifier '#b'. Static and instance elements cannot share the same private name. + static #c = 0; + ~~ +!!! error TS2804: Duplicate identifier '#c'. Static and instance elements cannot share the same private name. + #c = 0; + ~~ +!!! error TS2804: Duplicate identifier '#c'. Static and instance elements cannot share the same private name. + #d = 0; + ~~ +!!! error TS2804: Duplicate identifier '#d'. Static and instance elements cannot share the same private name. + static #d = 0; + ~~ +!!! error TS2804: Duplicate identifier '#d'. Static and instance elements cannot share the same private name. + static get #e() { return 0; } + ~~ +!!! error TS2804: Duplicate identifier '#e'. Static and instance elements cannot share the same private name. + set #e(value: number) {} + ~~ +!!! error TS2804: Duplicate identifier '#e'. Static and instance elements cannot share the same private name. + get #f() { return 0; } + ~~ +!!! error TS2804: Duplicate identifier '#f'. Static and instance elements cannot share the same private name. + static set #f(value: number) {} + ~~ +!!! error TS2804: Duplicate identifier '#f'. Static and instance elements cannot share the same private name. + } + \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/conformance/esDecorators-classDeclaration-methods-staticPrivate(target=es2015).js b/tsc/testdata/baselines/reference/conformance/esDecorators-classDeclaration-methods-staticPrivate(target=es2015).js index 4db3b713a6ed3..33b81d3f1ca93 100644 --- a/tsc/testdata/baselines/reference/conformance/esDecorators-classDeclaration-methods-staticPrivate(target=es2015).js +++ b/tsc/testdata/baselines/reference/conformance/esDecorators-classDeclaration-methods-staticPrivate(target=es2015).js @@ -11,6 +11,22 @@ class C { class D { static #method1() {} } + +@dec +class E { + static #a() {} + #a() {} + #b() {} + static #b() {} + static #c = 0; + #c = 0; + #d = 0; + static #d = 0; + static get #e() { return 0; } + set #e(value: number) {} + get #f() { return 0; } + static set #f(value: number) {} +} //// [esDecorators-classDeclaration-methods-staticPrivate.js] @@ -51,3 +67,45 @@ let D = (() => { })(); return D = _classThis; })(); +let E = (() => { + var _E_instances, _E_a, _E_a_1, _E_b, _E_b_1, _E_c, _E_c_1, _E_d, _E_d_1, _E_e_get, _E_e_set, _E_f_get, _E_f_set; + let _classDecorators = [dec]; + let _classDescriptor; + let _classExtraInitializers = []; + let _classThis; + var E = _classThis = class { + constructor() { + _E_instances.add(this); + _E_c_1.set(this, 0); + _E_d_1 = { value: 0 }; + } + static #a() { } + #a() { } + #b() { } + static #b() { } + static #c = 0; + #c = 0; + #d = 0; + static #d = 0; + static get #e() { return 0; } + set #e(value) { } + get #f() { return 0; } + static set #f(value) { } + }; + _E_c_1 = new WeakMap(); + _E_d = new WeakMap(); + _E_instances = new WeakSet(); + __setFunctionName(_classThis, "E"); + (() => { + const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0; + __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers); + E = _classThis = _classDescriptor.value; + if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata }); + })(); + _E_c_1.set(_classThis, 0); + _E_d_1 = { value: 0 }; + (() => { + __runInitializers(_classThis, _classExtraInitializers); + })(); + return E = _classThis; +})(); diff --git a/tsc/testdata/baselines/reference/conformance/esDecorators-classDeclaration-methods-staticPrivate(target=es2022).errors.txt b/tsc/testdata/baselines/reference/conformance/esDecorators-classDeclaration-methods-staticPrivate(target=es2022).errors.txt new file mode 100644 index 0000000000000..25f63760f051a --- /dev/null +++ b/tsc/testdata/baselines/reference/conformance/esDecorators-classDeclaration-methods-staticPrivate(target=es2022).errors.txt @@ -0,0 +1,66 @@ +esDecorators-classDeclaration-methods-staticPrivate.ts(14,12): error TS2804: Duplicate identifier '#a'. Static and instance elements cannot share the same private name. +esDecorators-classDeclaration-methods-staticPrivate.ts(15,5): error TS2804: Duplicate identifier '#a'. Static and instance elements cannot share the same private name. +esDecorators-classDeclaration-methods-staticPrivate.ts(16,5): error TS2804: Duplicate identifier '#b'. Static and instance elements cannot share the same private name. +esDecorators-classDeclaration-methods-staticPrivate.ts(17,12): error TS2804: Duplicate identifier '#b'. Static and instance elements cannot share the same private name. +esDecorators-classDeclaration-methods-staticPrivate.ts(18,12): error TS2804: Duplicate identifier '#c'. Static and instance elements cannot share the same private name. +esDecorators-classDeclaration-methods-staticPrivate.ts(19,5): error TS2804: Duplicate identifier '#c'. Static and instance elements cannot share the same private name. +esDecorators-classDeclaration-methods-staticPrivate.ts(20,5): error TS2804: Duplicate identifier '#d'. Static and instance elements cannot share the same private name. +esDecorators-classDeclaration-methods-staticPrivate.ts(21,12): error TS2804: Duplicate identifier '#d'. Static and instance elements cannot share the same private name. +esDecorators-classDeclaration-methods-staticPrivate.ts(22,16): error TS2804: Duplicate identifier '#e'. Static and instance elements cannot share the same private name. +esDecorators-classDeclaration-methods-staticPrivate.ts(23,9): error TS2804: Duplicate identifier '#e'. Static and instance elements cannot share the same private name. +esDecorators-classDeclaration-methods-staticPrivate.ts(24,9): error TS2804: Duplicate identifier '#f'. Static and instance elements cannot share the same private name. +esDecorators-classDeclaration-methods-staticPrivate.ts(25,16): error TS2804: Duplicate identifier '#f'. Static and instance elements cannot share the same private name. + + +==== esDecorators-classDeclaration-methods-staticPrivate.ts (12 errors) ==== + declare let dec: any; + + class C { + @dec static #method1() {} + } + + @dec + class D { + static #method1() {} + } + + @dec + class E { + static #a() {} + ~~ +!!! error TS2804: Duplicate identifier '#a'. Static and instance elements cannot share the same private name. + #a() {} + ~~ +!!! error TS2804: Duplicate identifier '#a'. Static and instance elements cannot share the same private name. + #b() {} + ~~ +!!! error TS2804: Duplicate identifier '#b'. Static and instance elements cannot share the same private name. + static #b() {} + ~~ +!!! error TS2804: Duplicate identifier '#b'. Static and instance elements cannot share the same private name. + static #c = 0; + ~~ +!!! error TS2804: Duplicate identifier '#c'. Static and instance elements cannot share the same private name. + #c = 0; + ~~ +!!! error TS2804: Duplicate identifier '#c'. Static and instance elements cannot share the same private name. + #d = 0; + ~~ +!!! error TS2804: Duplicate identifier '#d'. Static and instance elements cannot share the same private name. + static #d = 0; + ~~ +!!! error TS2804: Duplicate identifier '#d'. Static and instance elements cannot share the same private name. + static get #e() { return 0; } + ~~ +!!! error TS2804: Duplicate identifier '#e'. Static and instance elements cannot share the same private name. + set #e(value: number) {} + ~~ +!!! error TS2804: Duplicate identifier '#e'. Static and instance elements cannot share the same private name. + get #f() { return 0; } + ~~ +!!! error TS2804: Duplicate identifier '#f'. Static and instance elements cannot share the same private name. + static set #f(value: number) {} + ~~ +!!! error TS2804: Duplicate identifier '#f'. Static and instance elements cannot share the same private name. + } + \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/conformance/esDecorators-classDeclaration-methods-staticPrivate(target=es2022).js b/tsc/testdata/baselines/reference/conformance/esDecorators-classDeclaration-methods-staticPrivate(target=es2022).js index 63b4ea82d5828..b141c3d07250a 100644 --- a/tsc/testdata/baselines/reference/conformance/esDecorators-classDeclaration-methods-staticPrivate(target=es2022).js +++ b/tsc/testdata/baselines/reference/conformance/esDecorators-classDeclaration-methods-staticPrivate(target=es2022).js @@ -11,6 +11,22 @@ class C { class D { static #method1() {} } + +@dec +class E { + static #a() {} + #a() {} + #b() {} + static #b() {} + static #c = 0; + #c = 0; + #d = 0; + static #d = 0; + static get #e() { return 0; } + set #e(value: number) {} + get #f() { return 0; } + static set #f(value: number) {} +} //// [esDecorators-classDeclaration-methods-staticPrivate.js] @@ -50,3 +66,36 @@ let D = (() => { }; return D = _classThis; })(); +let E = (() => { + var _E_a, _E_b, _E_c, _E_d, _E_e_get, _E_f_set; + let _classDecorators = [dec]; + let _classDescriptor; + let _classExtraInitializers = []; + let _classThis; + var E = class { + static { _classThis = this; } + static { __setFunctionName(this, "E"); } + static { + const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0; + __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers); + E = _classThis = _classDescriptor.value; + if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata }); + } + static #a() { } + #a() { } + #b() { } + static #b() { } + static #c = 0; + #c = 0; + #d = 0; + static #d = 0; + static get #e() { return 0; } + set #e(value) { } + get #f() { return 0; } + static set #f(value) { } + static { + __runInitializers(_classThis, _classExtraInitializers); + } + }; + return E = _classThis; +})(); diff --git a/tsc/testdata/baselines/reference/conformance/esDecorators-classDeclaration-methods-staticPrivate(target=esnext).errors.txt b/tsc/testdata/baselines/reference/conformance/esDecorators-classDeclaration-methods-staticPrivate(target=esnext).errors.txt new file mode 100644 index 0000000000000..25f63760f051a --- /dev/null +++ b/tsc/testdata/baselines/reference/conformance/esDecorators-classDeclaration-methods-staticPrivate(target=esnext).errors.txt @@ -0,0 +1,66 @@ +esDecorators-classDeclaration-methods-staticPrivate.ts(14,12): error TS2804: Duplicate identifier '#a'. Static and instance elements cannot share the same private name. +esDecorators-classDeclaration-methods-staticPrivate.ts(15,5): error TS2804: Duplicate identifier '#a'. Static and instance elements cannot share the same private name. +esDecorators-classDeclaration-methods-staticPrivate.ts(16,5): error TS2804: Duplicate identifier '#b'. Static and instance elements cannot share the same private name. +esDecorators-classDeclaration-methods-staticPrivate.ts(17,12): error TS2804: Duplicate identifier '#b'. Static and instance elements cannot share the same private name. +esDecorators-classDeclaration-methods-staticPrivate.ts(18,12): error TS2804: Duplicate identifier '#c'. Static and instance elements cannot share the same private name. +esDecorators-classDeclaration-methods-staticPrivate.ts(19,5): error TS2804: Duplicate identifier '#c'. Static and instance elements cannot share the same private name. +esDecorators-classDeclaration-methods-staticPrivate.ts(20,5): error TS2804: Duplicate identifier '#d'. Static and instance elements cannot share the same private name. +esDecorators-classDeclaration-methods-staticPrivate.ts(21,12): error TS2804: Duplicate identifier '#d'. Static and instance elements cannot share the same private name. +esDecorators-classDeclaration-methods-staticPrivate.ts(22,16): error TS2804: Duplicate identifier '#e'. Static and instance elements cannot share the same private name. +esDecorators-classDeclaration-methods-staticPrivate.ts(23,9): error TS2804: Duplicate identifier '#e'. Static and instance elements cannot share the same private name. +esDecorators-classDeclaration-methods-staticPrivate.ts(24,9): error TS2804: Duplicate identifier '#f'. Static and instance elements cannot share the same private name. +esDecorators-classDeclaration-methods-staticPrivate.ts(25,16): error TS2804: Duplicate identifier '#f'. Static and instance elements cannot share the same private name. + + +==== esDecorators-classDeclaration-methods-staticPrivate.ts (12 errors) ==== + declare let dec: any; + + class C { + @dec static #method1() {} + } + + @dec + class D { + static #method1() {} + } + + @dec + class E { + static #a() {} + ~~ +!!! error TS2804: Duplicate identifier '#a'. Static and instance elements cannot share the same private name. + #a() {} + ~~ +!!! error TS2804: Duplicate identifier '#a'. Static and instance elements cannot share the same private name. + #b() {} + ~~ +!!! error TS2804: Duplicate identifier '#b'. Static and instance elements cannot share the same private name. + static #b() {} + ~~ +!!! error TS2804: Duplicate identifier '#b'. Static and instance elements cannot share the same private name. + static #c = 0; + ~~ +!!! error TS2804: Duplicate identifier '#c'. Static and instance elements cannot share the same private name. + #c = 0; + ~~ +!!! error TS2804: Duplicate identifier '#c'. Static and instance elements cannot share the same private name. + #d = 0; + ~~ +!!! error TS2804: Duplicate identifier '#d'. Static and instance elements cannot share the same private name. + static #d = 0; + ~~ +!!! error TS2804: Duplicate identifier '#d'. Static and instance elements cannot share the same private name. + static get #e() { return 0; } + ~~ +!!! error TS2804: Duplicate identifier '#e'. Static and instance elements cannot share the same private name. + set #e(value: number) {} + ~~ +!!! error TS2804: Duplicate identifier '#e'. Static and instance elements cannot share the same private name. + get #f() { return 0; } + ~~ +!!! error TS2804: Duplicate identifier '#f'. Static and instance elements cannot share the same private name. + static set #f(value: number) {} + ~~ +!!! error TS2804: Duplicate identifier '#f'. Static and instance elements cannot share the same private name. + } + \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/conformance/esDecorators-classDeclaration-methods-staticPrivate(target=esnext).js b/tsc/testdata/baselines/reference/conformance/esDecorators-classDeclaration-methods-staticPrivate(target=esnext).js index 40084510a5fe9..c3bb3969ceabf 100644 --- a/tsc/testdata/baselines/reference/conformance/esDecorators-classDeclaration-methods-staticPrivate(target=esnext).js +++ b/tsc/testdata/baselines/reference/conformance/esDecorators-classDeclaration-methods-staticPrivate(target=esnext).js @@ -11,6 +11,22 @@ class C { class D { static #method1() {} } + +@dec +class E { + static #a() {} + #a() {} + #b() {} + static #b() {} + static #c = 0; + #c = 0; + #d = 0; + static #d = 0; + static get #e() { return 0; } + set #e(value: number) {} + get #f() { return 0; } + static set #f(value: number) {} +} //// [esDecorators-classDeclaration-methods-staticPrivate.js] @@ -23,3 +39,18 @@ class C { class D { static #method1() { } } +@dec +class E { + static #a() { } + #a() { } + #b() { } + static #b() { } + static #c = 0; + #c = 0; + #d = 0; + static #d = 0; + static get #e() { return 0; } + set #e(value) { } + get #f() { return 0; } + static set #f(value) { } +} diff --git a/tsc/testdata/tests/cases/conformance/esDecorators/classDeclaration/methods/esDecorators-classDeclaration-methods-staticPrivate.ts b/tsc/testdata/tests/cases/conformance/esDecorators/classDeclaration/methods/esDecorators-classDeclaration-methods-staticPrivate.ts index 89954a68bcc87..83b2759e7d1f9 100644 --- a/tsc/testdata/tests/cases/conformance/esDecorators/classDeclaration/methods/esDecorators-classDeclaration-methods-staticPrivate.ts +++ b/tsc/testdata/tests/cases/conformance/esDecorators/classDeclaration/methods/esDecorators-classDeclaration-methods-staticPrivate.ts @@ -12,3 +12,19 @@ class C { class D { static #method1() {} } + +@dec +class E { + static #a() {} + #a() {} + #b() {} + static #b() {} + static #c = 0; + #c = 0; + #d = 0; + static #d = 0; + static get #e() { return 0; } + set #e(value: number) {} + get #f() { return 0; } + static set #f(value: number) {} +}