From c98436138df3fec0bfdf56bc017c1c30b2dc14e1 Mon Sep 17 00:00:00 2001 From: kingwl Date: Tue, 8 May 2018 23:57:51 +0800 Subject: [PATCH 1/2] fix unnecessary Reference Check with property signature --- src/compiler/checker.ts | 4 +++ ...aryReferenceChecksWithPropertySignature.js | 16 ++++++++++ ...ferenceChecksWithPropertySignature.symbols | 28 +++++++++++++++++ ...ReferenceChecksWithPropertySignature.types | 30 +++++++++++++++++++ ...aryReferenceChecksWithPropertySignature.ts | 10 +++++++ 5 files changed, 88 insertions(+) create mode 100644 tests/baselines/reference/unnecessaryReferenceChecksWithPropertySignature.js create mode 100644 tests/baselines/reference/unnecessaryReferenceChecksWithPropertySignature.symbols create mode 100644 tests/baselines/reference/unnecessaryReferenceChecksWithPropertySignature.types create mode 100644 tests/cases/compiler/unnecessaryReferenceChecksWithPropertySignature.ts 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.js b/tests/baselines/reference/unnecessaryReferenceChecksWithPropertySignature.js new file mode 100644 index 0000000000000..b2ba0cea535bd --- /dev/null +++ b/tests/baselines/reference/unnecessaryReferenceChecksWithPropertySignature.js @@ -0,0 +1,16 @@ +//// [unnecessaryReferenceChecksWithPropertySignature.ts] +interface C { + [C.a](): void; + [C.b]: void; +} +class C { + static readonly a = Symbol(); + static readonly b = Symbol(); +} + + +//// [unnecessaryReferenceChecksWithPropertySignature.js] +class C { +} +C.a = Symbol(); +C.b = Symbol(); diff --git a/tests/baselines/reference/unnecessaryReferenceChecksWithPropertySignature.symbols b/tests/baselines/reference/unnecessaryReferenceChecksWithPropertySignature.symbols new file mode 100644 index 0000000000000..9c2aae6d34852 --- /dev/null +++ b/tests/baselines/reference/unnecessaryReferenceChecksWithPropertySignature.symbols @@ -0,0 +1,28 @@ +=== 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, 4, 9)) +>C : Symbol(C, Decl(unnecessaryReferenceChecksWithPropertySignature.ts, 0, 0), Decl(unnecessaryReferenceChecksWithPropertySignature.ts, 3, 1)) +>a : Symbol(C.a, Decl(unnecessaryReferenceChecksWithPropertySignature.ts, 4, 9)) + + [C.b]: void; +>[C.b] : Symbol(C[C.b], Decl(unnecessaryReferenceChecksWithPropertySignature.ts, 1, 16)) +>C.b : Symbol(C.b, Decl(unnecessaryReferenceChecksWithPropertySignature.ts, 5, 31)) +>C : Symbol(C, Decl(unnecessaryReferenceChecksWithPropertySignature.ts, 0, 0), Decl(unnecessaryReferenceChecksWithPropertySignature.ts, 3, 1)) +>b : Symbol(C.b, Decl(unnecessaryReferenceChecksWithPropertySignature.ts, 5, 31)) +} +class C { +>C : Symbol(C, Decl(unnecessaryReferenceChecksWithPropertySignature.ts, 0, 0), Decl(unnecessaryReferenceChecksWithPropertySignature.ts, 3, 1)) + + static readonly a = Symbol(); +>a : Symbol(C.a, Decl(unnecessaryReferenceChecksWithPropertySignature.ts, 4, 9)) +>Symbol : Symbol(Symbol, Decl(lib.es6.d.ts, --, --), Decl(lib.es6.d.ts, --, --), Decl(lib.es6.d.ts, --, --)) + + static readonly b = Symbol(); +>b : Symbol(C.b, Decl(unnecessaryReferenceChecksWithPropertySignature.ts, 5, 31)) +>Symbol : Symbol(Symbol, Decl(lib.es6.d.ts, --, --), Decl(lib.es6.d.ts, --, --), Decl(lib.es6.d.ts, --, --)) +} + diff --git a/tests/baselines/reference/unnecessaryReferenceChecksWithPropertySignature.types b/tests/baselines/reference/unnecessaryReferenceChecksWithPropertySignature.types new file mode 100644 index 0000000000000..b3582acdbc1b6 --- /dev/null +++ b/tests/baselines/reference/unnecessaryReferenceChecksWithPropertySignature.types @@ -0,0 +1,30 @@ +=== 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 + + static readonly a = Symbol(); +>a : unique symbol +>Symbol() : unique symbol +>Symbol : SymbolConstructor + + static readonly b = Symbol(); +>b : unique symbol +>Symbol() : unique symbol +>Symbol : SymbolConstructor +} + diff --git a/tests/cases/compiler/unnecessaryReferenceChecksWithPropertySignature.ts b/tests/cases/compiler/unnecessaryReferenceChecksWithPropertySignature.ts new file mode 100644 index 0000000000000..83befc8be61b6 --- /dev/null +++ b/tests/cases/compiler/unnecessaryReferenceChecksWithPropertySignature.ts @@ -0,0 +1,10 @@ +// @target: es6 + +interface C { + [C.a](): void; + [C.b]: void; +} +class C { + static readonly a = Symbol(); + static readonly b = Symbol(); +} From 57fd9ebe97b3f80121042ffb92eaa873e09e6d67 Mon Sep 17 00:00:00 2001 From: kingwl Date: Mon, 18 Jun 2018 20:50:26 +0800 Subject: [PATCH 2/2] add more testcase --- ...enceChecksWithPropertySignature.errors.txt | 27 +++++++++++++ ...aryReferenceChecksWithPropertySignature.js | 21 +++++++++- ...ferenceChecksWithPropertySignature.symbols | 38 +++++++++++++++---- ...ReferenceChecksWithPropertySignature.types | 26 +++++++++++++ ...aryReferenceChecksWithPropertySignature.ts | 7 ++++ 5 files changed, 110 insertions(+), 9 deletions(-) create mode 100644 tests/baselines/reference/unnecessaryReferenceChecksWithPropertySignature.errors.txt 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 index b2ba0cea535bd..66b51ad1802bf 100644 --- a/tests/baselines/reference/unnecessaryReferenceChecksWithPropertySignature.js +++ b/tests/baselines/reference/unnecessaryReferenceChecksWithPropertySignature.js @@ -4,13 +4,32 @@ interface C { [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 index 9c2aae6d34852..67a662c02528c 100644 --- a/tests/baselines/reference/unnecessaryReferenceChecksWithPropertySignature.symbols +++ b/tests/baselines/reference/unnecessaryReferenceChecksWithPropertySignature.symbols @@ -4,25 +4,47 @@ interface C { [C.a](): void; >[C.a] : Symbol(C[C.a], Decl(unnecessaryReferenceChecksWithPropertySignature.ts, 0, 13)) ->C.a : Symbol(C.a, Decl(unnecessaryReferenceChecksWithPropertySignature.ts, 4, 9)) +>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, 4, 9)) +>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, 5, 31)) +>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, 5, 31)) +>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, 4, 9)) ->Symbol : Symbol(Symbol, Decl(lib.es6.d.ts, --, --), Decl(lib.es6.d.ts, --, --), Decl(lib.es6.d.ts, --, --)) +>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, 5, 31)) ->Symbol : Symbol(Symbol, Decl(lib.es6.d.ts, --, --), Decl(lib.es6.d.ts, --, --), Decl(lib.es6.d.ts, --, --)) +>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 index b3582acdbc1b6..ee0fc7def5444 100644 --- a/tests/baselines/reference/unnecessaryReferenceChecksWithPropertySignature.types +++ b/tests/baselines/reference/unnecessaryReferenceChecksWithPropertySignature.types @@ -17,6 +17,13 @@ interface C { 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 @@ -25,6 +32,25 @@ class C { 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 index 83befc8be61b6..aa86e144680f2 100644 --- a/tests/cases/compiler/unnecessaryReferenceChecksWithPropertySignature.ts +++ b/tests/cases/compiler/unnecessaryReferenceChecksWithPropertySignature.ts @@ -5,6 +5,13 @@ interface C { [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