Skip to content

Commit

Permalink
Remove members that are invalid computed properties. (#58646)
Browse files Browse the repository at this point in the history
  • Loading branch information
dragomirtitian committed May 24, 2024
1 parent 87918f5 commit 6856735
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 38 deletions.
31 changes: 14 additions & 17 deletions src/compiler/transformers/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -997,28 +997,25 @@ export function transformDeclarations(context: TransformationContext) {
if (isDeclaration(input)) {
if (isDeclarationAndNotVisible(input)) return;
if (hasDynamicName(input)) {
if (
isolatedDeclarations
if (isolatedDeclarations) {
// Classes and object literals usually elide properties with computed names that are not of a literal type
// In isolated declarations TSC needs to error on these as we don't know the type in a DTE.
&& (isClassDeclaration(input.parent) || isObjectLiteralExpression(input.parent))
) {
context.addDiagnostic(createDiagnosticForNode(input, Diagnostics.Computed_property_names_on_class_or_object_literals_cannot_be_inferred_with_isolatedDeclarations));
}
if (
isolatedDeclarations
// Type declarations just need to double-check that the input computed name is an entity name expression
&& (isInterfaceDeclaration(input.parent) || isTypeLiteralNode(input.parent))
&& !isEntityNameExpression(input.name.expression)
) {
context.addDiagnostic(createDiagnosticForNode(input, Diagnostics.Computed_properties_must_be_number_or_string_literals_variables_or_dotted_expressions_with_isolatedDeclarations));
if (isClassDeclaration(input.parent) || isObjectLiteralExpression(input.parent)) {
context.addDiagnostic(createDiagnosticForNode(input, Diagnostics.Computed_property_names_on_class_or_object_literals_cannot_be_inferred_with_isolatedDeclarations));
return;
}
else if (
// Type declarations just need to double-check that the input computed name is an entity name expression
(isInterfaceDeclaration(input.parent) || isTypeLiteralNode(input.parent))
&& !isEntityNameExpression(input.name.expression)
) {
context.addDiagnostic(createDiagnosticForNode(input, Diagnostics.Computed_properties_must_be_number_or_string_literals_variables_or_dotted_expressions_with_isolatedDeclarations));
return;
}
}
if (!isEntityNameExpression(input.name.expression)) {
else if (!resolver.isLateBound(getParseTreeNode(input) as Declaration) || !isEntityNameExpression(input.name.expression)) {
return;
}
// A.B.C that is not late bound - usually this means the expression did not resolve.
// Check the entity name, and copy the declaration, rather than elide it (there's
// probably a checker error in the input, but this is most likely the desired output).
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//// [tests/cases/compiler/declarationEmitAnyComputedPropertyInClass.ts] ////

//// [ambient.d.ts]
declare module "abcdefgh";

//// [main.ts]
import Test from "abcdefgh";

export class C {
[Test.someKey]() {};
}


//// [main.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.C = void 0;
var abcdefgh_1 = require("abcdefgh");
var C = /** @class */ (function () {
function C() {
}
C.prototype[abcdefgh_1.default.someKey] = function () { };
;
return C;
}());
exports.C = C;


//// [main.d.ts]
export declare class C {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//// [tests/cases/compiler/declarationEmitAnyComputedPropertyInClass.ts] ////

=== ambient.d.ts ===
declare module "abcdefgh";
>"abcdefgh" : Symbol("abcdefgh", Decl(ambient.d.ts, 0, 0))

=== main.ts ===
import Test from "abcdefgh";
>Test : Symbol(Test, Decl(main.ts, 0, 6))

export class C {
>C : Symbol(C, Decl(main.ts, 0, 28))

[Test.someKey]() {};
>[Test.someKey] : Symbol(C[Test.someKey], Decl(main.ts, 2, 16))
>Test : Symbol(Test, Decl(main.ts, 0, 6))
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//// [tests/cases/compiler/declarationEmitAnyComputedPropertyInClass.ts] ////

=== ambient.d.ts ===
declare module "abcdefgh";
>"abcdefgh" : any

=== main.ts ===
import Test from "abcdefgh";
>Test : any
> : ^^^

export class C {
>C : C
> : ^

[Test.someKey]() {};
>[Test.someKey] : () => void
> : ^^^^^^^^^^
>Test.someKey : any
>Test : any
> : ^^^
>someKey : any
> : ^^^
}

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


==== isolatedDeclarationErrorsClasses.ts (27 errors) ====
==== isolatedDeclarationErrorsClasses.ts (25 errors) ====
export class Cls {

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

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

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

get [noAnnotationStringName]() { return 0;}
~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ export interface Foo {
c?: string;
}
//// [class.d.ts] ////
declare const i: unique symbol;
export declare class Bar {
#private;
a: string;
Expand All @@ -66,25 +65,19 @@ export declare class Bar {
protected f: string;
private g;
["h"]: string;
[i]: string;
}
export declare abstract class Baz {
abstract a: string;
abstract method(): void;
}
export {};


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


==== class.ts (2 errors) ====
==== class.ts (1 errors) ====
const i = Symbol();
~
!!! error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations.
!!! related TS9027 class.ts:1:7: Add a type annotation to the variable i.
export class Bar {
a: string;
b?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,6 @@ export interface B {
["2"]: number;
}
export declare class C {
[missing]: number;
[ns.missing]: number;
[presentNs.a]: number;
[Symbol.iterator]: number;
[1]: number;
["2"]: number;
}
Expand Down
12 changes: 12 additions & 0 deletions tests/cases/compiler/declarationEmitAnyComputedPropertyInClass.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// @declaration: true
// @declaration

// @filename: ambient.d.ts
declare module "abcdefgh";

// @filename: main.ts
import Test from "abcdefgh";

export class C {
[Test.someKey]() {};
}

0 comments on commit 6856735

Please sign in to comment.