diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 1ecf30690a0bc..94bee2069583b 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -1175,6 +1175,10 @@ namespace ts { return true; } + if (isComputedPropertyName(current) && isPropertySignature(current.parent) && current.parent.name === current) { + return true; + } + const initializerOfProperty = current.parent && current.parent.kind === SyntaxKind.PropertyDeclaration && (current.parent).initializer === current; diff --git a/tests/baselines/reference/unnecessaryReferenceChecksWithPropertySignature.errors.txt b/tests/baselines/reference/unnecessaryReferenceChecksWithPropertySignature.errors.txt new file mode 100644 index 0000000000000..5cd390c1f97fb --- /dev/null +++ b/tests/baselines/reference/unnecessaryReferenceChecksWithPropertySignature.errors.txt @@ -0,0 +1,27 @@ +tests/cases/compiler/unnecessaryReferenceChecksWithPropertySignature.ts(6,4): error TS2449: Class 'C' used before its declaration. +tests/cases/compiler/unnecessaryReferenceChecksWithPropertySignature.ts(13,3): error TS1166: A computed property name in a class property declaration must refer to an expression whose type is a literal type or a 'unique symbol' type. +tests/cases/compiler/unnecessaryReferenceChecksWithPropertySignature.ts(13,4): error TS2449: Class 'D' used before its declaration. + + +==== tests/cases/compiler/unnecessaryReferenceChecksWithPropertySignature.ts (3 errors) ==== + interface C { + [C.a](): void; + [C.b]: void; + } + class C { + [C.c] = 1; + ~ +!!! error TS2449: Class 'C' used before its declaration. + static readonly a = Symbol(); + static readonly b = Symbol(); + static readonly c = Symbol(); + } + + class D { + [D.a] = 0; + ~~~~~ +!!! error TS1166: A computed property name in a class property declaration must refer to an expression whose type is a literal type or a 'unique symbol' type. + ~ +!!! error TS2449: Class 'D' used before its declaration. + static a = "a"; + } \ No newline at end of file diff --git a/tests/baselines/reference/unnecessaryReferenceChecksWithPropertySignature.js b/tests/baselines/reference/unnecessaryReferenceChecksWithPropertySignature.js new file mode 100644 index 0000000000000..66b51ad1802bf --- /dev/null +++ b/tests/baselines/reference/unnecessaryReferenceChecksWithPropertySignature.js @@ -0,0 +1,35 @@ +//// [unnecessaryReferenceChecksWithPropertySignature.ts] +interface C { + [C.a](): void; + [C.b]: void; +} +class C { + [C.c] = 1; + static readonly a = Symbol(); + static readonly b = Symbol(); + static readonly c = Symbol(); +} + +class D { + [D.a] = 0; + static a = "a"; +} + +//// [unnecessaryReferenceChecksWithPropertySignature.js] +var _a, _b; +class C { + constructor() { + this[_a] = 1; + } +} +_a = C.c; +C.a = Symbol(); +C.b = Symbol(); +C.c = Symbol(); +class D { + constructor() { + this[_b] = 0; + } +} +_b = D.a; +D.a = "a"; diff --git a/tests/baselines/reference/unnecessaryReferenceChecksWithPropertySignature.symbols b/tests/baselines/reference/unnecessaryReferenceChecksWithPropertySignature.symbols new file mode 100644 index 0000000000000..67a662c02528c --- /dev/null +++ b/tests/baselines/reference/unnecessaryReferenceChecksWithPropertySignature.symbols @@ -0,0 +1,50 @@ +=== tests/cases/compiler/unnecessaryReferenceChecksWithPropertySignature.ts === +interface C { +>C : Symbol(C, Decl(unnecessaryReferenceChecksWithPropertySignature.ts, 0, 0), Decl(unnecessaryReferenceChecksWithPropertySignature.ts, 3, 1)) + + [C.a](): void; +>[C.a] : Symbol(C[C.a], Decl(unnecessaryReferenceChecksWithPropertySignature.ts, 0, 13)) +>C.a : Symbol(C.a, Decl(unnecessaryReferenceChecksWithPropertySignature.ts, 5, 12)) +>C : Symbol(C, Decl(unnecessaryReferenceChecksWithPropertySignature.ts, 0, 0), Decl(unnecessaryReferenceChecksWithPropertySignature.ts, 3, 1)) +>a : Symbol(C.a, Decl(unnecessaryReferenceChecksWithPropertySignature.ts, 5, 12)) + + [C.b]: void; +>[C.b] : Symbol(C[C.b], Decl(unnecessaryReferenceChecksWithPropertySignature.ts, 1, 16)) +>C.b : Symbol(C.b, Decl(unnecessaryReferenceChecksWithPropertySignature.ts, 6, 31)) +>C : Symbol(C, Decl(unnecessaryReferenceChecksWithPropertySignature.ts, 0, 0), Decl(unnecessaryReferenceChecksWithPropertySignature.ts, 3, 1)) +>b : Symbol(C.b, Decl(unnecessaryReferenceChecksWithPropertySignature.ts, 6, 31)) +} +class C { +>C : Symbol(C, Decl(unnecessaryReferenceChecksWithPropertySignature.ts, 0, 0), Decl(unnecessaryReferenceChecksWithPropertySignature.ts, 3, 1)) + + [C.c] = 1; +>[C.c] : Symbol(C[C.c], Decl(unnecessaryReferenceChecksWithPropertySignature.ts, 4, 9)) +>C.c : Symbol(C.c, Decl(unnecessaryReferenceChecksWithPropertySignature.ts, 7, 31)) +>C : Symbol(C, Decl(unnecessaryReferenceChecksWithPropertySignature.ts, 0, 0), Decl(unnecessaryReferenceChecksWithPropertySignature.ts, 3, 1)) +>c : Symbol(C.c, Decl(unnecessaryReferenceChecksWithPropertySignature.ts, 7, 31)) + + static readonly a = Symbol(); +>a : Symbol(C.a, Decl(unnecessaryReferenceChecksWithPropertySignature.ts, 5, 12)) +>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --)) + + static readonly b = Symbol(); +>b : Symbol(C.b, Decl(unnecessaryReferenceChecksWithPropertySignature.ts, 6, 31)) +>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --)) + + static readonly c = Symbol(); +>c : Symbol(C.c, Decl(unnecessaryReferenceChecksWithPropertySignature.ts, 7, 31)) +>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --)) +} + +class D { +>D : Symbol(D, Decl(unnecessaryReferenceChecksWithPropertySignature.ts, 9, 1)) + + [D.a] = 0; +>[D.a] : Symbol(D[D.a], Decl(unnecessaryReferenceChecksWithPropertySignature.ts, 11, 9)) +>D.a : Symbol(D.a, Decl(unnecessaryReferenceChecksWithPropertySignature.ts, 12, 12)) +>D : Symbol(D, Decl(unnecessaryReferenceChecksWithPropertySignature.ts, 9, 1)) +>a : Symbol(D.a, Decl(unnecessaryReferenceChecksWithPropertySignature.ts, 12, 12)) + + static a = "a"; +>a : Symbol(D.a, Decl(unnecessaryReferenceChecksWithPropertySignature.ts, 12, 12)) +} diff --git a/tests/baselines/reference/unnecessaryReferenceChecksWithPropertySignature.types b/tests/baselines/reference/unnecessaryReferenceChecksWithPropertySignature.types new file mode 100644 index 0000000000000..ee0fc7def5444 --- /dev/null +++ b/tests/baselines/reference/unnecessaryReferenceChecksWithPropertySignature.types @@ -0,0 +1,56 @@ +=== tests/cases/compiler/unnecessaryReferenceChecksWithPropertySignature.ts === +interface C { +>C : C + + [C.a](): void; +>[C.a] : () => void +>C.a : unique symbol +>C : typeof C +>a : unique symbol + + [C.b]: void; +>[C.b] : void +>C.b : unique symbol +>C : typeof C +>b : unique symbol +} +class C { +>C : C + + [C.c] = 1; +>[C.c] : number +>C.c : unique symbol +>C : typeof C +>c : unique symbol +>1 : 1 + + static readonly a = Symbol(); +>a : unique symbol +>Symbol() : unique symbol +>Symbol : SymbolConstructor + + static readonly b = Symbol(); +>b : unique symbol +>Symbol() : unique symbol +>Symbol : SymbolConstructor + + static readonly c = Symbol(); +>c : unique symbol +>Symbol() : unique symbol +>Symbol : SymbolConstructor +} + +class D { +>D : D + + [D.a] = 0; +>[D.a] : number +>D.a : string +>D : typeof D +>a : string +>0 : 0 + + static a = "a"; +>a : string +>"a" : "a" +} diff --git a/tests/cases/compiler/unnecessaryReferenceChecksWithPropertySignature.ts b/tests/cases/compiler/unnecessaryReferenceChecksWithPropertySignature.ts new file mode 100644 index 0000000000000..aa86e144680f2 --- /dev/null +++ b/tests/cases/compiler/unnecessaryReferenceChecksWithPropertySignature.ts @@ -0,0 +1,17 @@ +// @target: es6 + +interface C { + [C.a](): void; + [C.b]: void; +} +class C { + [C.c] = 1; + static readonly a = Symbol(); + static readonly b = Symbol(); + static readonly c = Symbol(); +} + +class D { + [D.a] = 0; + static a = "a"; +} \ No newline at end of file