Skip to content

Commit 6856735

Browse files
Remove members that are invalid computed properties. (microsoft#58646)
1 parent 87918f5 commit 6856735

8 files changed

+102
-38
lines changed

src/compiler/transformers/declarations.ts

+14-17
Original file line numberDiff line numberDiff line change
@@ -997,28 +997,25 @@ export function transformDeclarations(context: TransformationContext) {
997997
if (isDeclaration(input)) {
998998
if (isDeclarationAndNotVisible(input)) return;
999999
if (hasDynamicName(input)) {
1000-
if (
1001-
isolatedDeclarations
1000+
if (isolatedDeclarations) {
10021001
// Classes and object literals usually elide properties with computed names that are not of a literal type
10031002
// In isolated declarations TSC needs to error on these as we don't know the type in a DTE.
1004-
&& (isClassDeclaration(input.parent) || isObjectLiteralExpression(input.parent))
1005-
) {
1006-
context.addDiagnostic(createDiagnosticForNode(input, Diagnostics.Computed_property_names_on_class_or_object_literals_cannot_be_inferred_with_isolatedDeclarations));
1007-
}
1008-
if (
1009-
isolatedDeclarations
1010-
// Type declarations just need to double-check that the input computed name is an entity name expression
1011-
&& (isInterfaceDeclaration(input.parent) || isTypeLiteralNode(input.parent))
1012-
&& !isEntityNameExpression(input.name.expression)
1013-
) {
1014-
context.addDiagnostic(createDiagnosticForNode(input, Diagnostics.Computed_properties_must_be_number_or_string_literals_variables_or_dotted_expressions_with_isolatedDeclarations));
1003+
if (isClassDeclaration(input.parent) || isObjectLiteralExpression(input.parent)) {
1004+
context.addDiagnostic(createDiagnosticForNode(input, Diagnostics.Computed_property_names_on_class_or_object_literals_cannot_be_inferred_with_isolatedDeclarations));
1005+
return;
1006+
}
1007+
else if (
1008+
// Type declarations just need to double-check that the input computed name is an entity name expression
1009+
(isInterfaceDeclaration(input.parent) || isTypeLiteralNode(input.parent))
1010+
&& !isEntityNameExpression(input.name.expression)
1011+
) {
1012+
context.addDiagnostic(createDiagnosticForNode(input, Diagnostics.Computed_properties_must_be_number_or_string_literals_variables_or_dotted_expressions_with_isolatedDeclarations));
1013+
return;
1014+
}
10151015
}
1016-
if (!isEntityNameExpression(input.name.expression)) {
1016+
else if (!resolver.isLateBound(getParseTreeNode(input) as Declaration) || !isEntityNameExpression(input.name.expression)) {
10171017
return;
10181018
}
1019-
// A.B.C that is not late bound - usually this means the expression did not resolve.
1020-
// Check the entity name, and copy the declaration, rather than elide it (there's
1021-
// probably a checker error in the input, but this is most likely the desired output).
10221019
}
10231020
}
10241021

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//// [tests/cases/compiler/declarationEmitAnyComputedPropertyInClass.ts] ////
2+
3+
//// [ambient.d.ts]
4+
declare module "abcdefgh";
5+
6+
//// [main.ts]
7+
import Test from "abcdefgh";
8+
9+
export class C {
10+
[Test.someKey]() {};
11+
}
12+
13+
14+
//// [main.js]
15+
"use strict";
16+
Object.defineProperty(exports, "__esModule", { value: true });
17+
exports.C = void 0;
18+
var abcdefgh_1 = require("abcdefgh");
19+
var C = /** @class */ (function () {
20+
function C() {
21+
}
22+
C.prototype[abcdefgh_1.default.someKey] = function () { };
23+
;
24+
return C;
25+
}());
26+
exports.C = C;
27+
28+
29+
//// [main.d.ts]
30+
export declare class C {
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//// [tests/cases/compiler/declarationEmitAnyComputedPropertyInClass.ts] ////
2+
3+
=== ambient.d.ts ===
4+
declare module "abcdefgh";
5+
>"abcdefgh" : Symbol("abcdefgh", Decl(ambient.d.ts, 0, 0))
6+
7+
=== main.ts ===
8+
import Test from "abcdefgh";
9+
>Test : Symbol(Test, Decl(main.ts, 0, 6))
10+
11+
export class C {
12+
>C : Symbol(C, Decl(main.ts, 0, 28))
13+
14+
[Test.someKey]() {};
15+
>[Test.someKey] : Symbol(C[Test.someKey], Decl(main.ts, 2, 16))
16+
>Test : Symbol(Test, Decl(main.ts, 0, 6))
17+
}
18+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//// [tests/cases/compiler/declarationEmitAnyComputedPropertyInClass.ts] ////
2+
3+
=== ambient.d.ts ===
4+
declare module "abcdefgh";
5+
>"abcdefgh" : any
6+
7+
=== main.ts ===
8+
import Test from "abcdefgh";
9+
>Test : any
10+
> : ^^^
11+
12+
export class C {
13+
>C : C
14+
> : ^
15+
16+
[Test.someKey]() {};
17+
>[Test.someKey] : () => void
18+
> : ^^^^^^^^^^
19+
>Test.someKey : any
20+
>Test : any
21+
> : ^^^
22+
>someKey : any
23+
> : ^^^
24+
}
25+

tests/baselines/reference/isolatedDeclarationErrorsClasses.errors.txt

+1-9
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@ isolatedDeclarationErrorsClasses.ts(36,5): error TS9038: Computed property names
1111
isolatedDeclarationErrorsClasses.ts(36,6): error TS2304: Cannot find name 'missing'.
1212
isolatedDeclarationErrorsClasses.ts(38,5): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.
1313
isolatedDeclarationErrorsClasses.ts(40,5): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.
14-
isolatedDeclarationErrorsClasses.ts(42,5): error TS9008: Method must have an explicit return type annotation with --isolatedDeclarations.
1514
isolatedDeclarationErrorsClasses.ts(42,5): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.
1615
isolatedDeclarationErrorsClasses.ts(44,5): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.
1716
isolatedDeclarationErrorsClasses.ts(44,35): error TS7006: Parameter 'v' implicitly has an 'any' type.
18-
isolatedDeclarationErrorsClasses.ts(44,35): error TS9011: Parameter must have an explicit type annotation with --isolatedDeclarations.
1917
isolatedDeclarationErrorsClasses.ts(46,9): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.
2018
isolatedDeclarationErrorsClasses.ts(48,9): error TS7032: Property '[noParamAnnotationStringName]' implicitly has type 'any', because its set accessor lacks a parameter type annotation.
2119
isolatedDeclarationErrorsClasses.ts(48,9): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.
@@ -27,7 +25,7 @@ isolatedDeclarationErrorsClasses.ts(56,5): error TS7010: '[noAnnotationLiteralNa
2725
isolatedDeclarationErrorsClasses.ts(56,5): error TS9013: Expression type can't be inferred with --isolatedDeclarations.
2826

2927

30-
==== isolatedDeclarationErrorsClasses.ts (27 errors) ====
28+
==== isolatedDeclarationErrorsClasses.ts (25 errors) ====
3129
export class Cls {
3230

3331
field = 1 + 1;
@@ -102,19 +100,13 @@ isolatedDeclarationErrorsClasses.ts(56,5): error TS9013: Expression type can't b
102100

103101
[noAnnotationStringName]() { }
104102
~~~~~~~~~~~~~~~~~~~~~~~~
105-
!!! error TS9008: Method must have an explicit return type annotation with --isolatedDeclarations.
106-
!!! related TS9034 isolatedDeclarationErrorsClasses.ts:42:5: Add a return type to the method
107-
~~~~~~~~~~~~~~~~~~~~~~~~
108103
!!! error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.
109104

110105
[noParamAnnotationStringName](v): void { }
111106
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
112107
!!! error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.
113108
~
114109
!!! error TS7006: Parameter 'v' implicitly has an 'any' type.
115-
~
116-
!!! error TS9011: Parameter must have an explicit type annotation with --isolatedDeclarations.
117-
!!! related TS9028 isolatedDeclarationErrorsClasses.ts:44:35: Add a type annotation to the parameter v.
118110

119111
get [noAnnotationStringName]() { return 0;}
120112
~~~~~~~~~~~~~~~~~~~~~~~~

tests/baselines/reference/transpile/declarationBasicSyntax.d.ts

+1-8
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ export interface Foo {
5656
c?: string;
5757
}
5858
//// [class.d.ts] ////
59-
declare const i: unique symbol;
6059
export declare class Bar {
6160
#private;
6261
a: string;
@@ -66,25 +65,19 @@ export declare class Bar {
6665
protected f: string;
6766
private g;
6867
["h"]: string;
69-
[i]: string;
7068
}
7169
export declare abstract class Baz {
7270
abstract a: string;
7371
abstract method(): void;
7472
}
75-
export {};
7673

7774

7875
//// [Diagnostics reported]
79-
class.ts(1,7): error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations.
8076
class.ts(11,5): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.
8177

8278

83-
==== class.ts (2 errors) ====
79+
==== class.ts (1 errors) ====
8480
const i = Symbol();
85-
~
86-
!!! error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations.
87-
!!! related TS9027 class.ts:1:7: Add a type annotation to the variable i.
8881
export class Bar {
8982
a: string;
9083
b?: string;

tests/baselines/reference/transpile/declarationComputedPropertyNames.d.ts

-4
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,6 @@ export interface B {
6767
["2"]: number;
6868
}
6969
export declare class C {
70-
[missing]: number;
71-
[ns.missing]: number;
72-
[presentNs.a]: number;
73-
[Symbol.iterator]: number;
7470
[1]: number;
7571
["2"]: number;
7672
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// @declaration: true
2+
// @declaration
3+
4+
// @filename: ambient.d.ts
5+
declare module "abcdefgh";
6+
7+
// @filename: main.ts
8+
import Test from "abcdefgh";
9+
10+
export class C {
11+
[Test.someKey]() {};
12+
}

0 commit comments

Comments
 (0)