From 68567359853efc456a22cc46eb1a7035be9ff15d Mon Sep 17 00:00:00 2001 From: Titian Cernicova-Dragomir Date: Fri, 24 May 2024 20:25:46 +0300 Subject: [PATCH] Remove members that are invalid computed properties. (#58646) --- src/compiler/transformers/declarations.ts | 31 +++++++++---------- ...clarationEmitAnyComputedPropertyInClass.js | 31 +++++++++++++++++++ ...tionEmitAnyComputedPropertyInClass.symbols | 18 +++++++++++ ...rationEmitAnyComputedPropertyInClass.types | 25 +++++++++++++++ ...solatedDeclarationErrorsClasses.errors.txt | 10 +----- .../transpile/declarationBasicSyntax.d.ts | 9 +----- .../declarationComputedPropertyNames.d.ts | 4 --- ...clarationEmitAnyComputedPropertyInClass.ts | 12 +++++++ 8 files changed, 102 insertions(+), 38 deletions(-) create mode 100644 tests/baselines/reference/declarationEmitAnyComputedPropertyInClass.js create mode 100644 tests/baselines/reference/declarationEmitAnyComputedPropertyInClass.symbols create mode 100644 tests/baselines/reference/declarationEmitAnyComputedPropertyInClass.types create mode 100644 tests/cases/compiler/declarationEmitAnyComputedPropertyInClass.ts diff --git a/src/compiler/transformers/declarations.ts b/src/compiler/transformers/declarations.ts index 547ec16cbc1..ab75abaad6e 100644 --- a/src/compiler/transformers/declarations.ts +++ b/src/compiler/transformers/declarations.ts @@ -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 (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 ( - 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 (!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). } } diff --git a/tests/baselines/reference/declarationEmitAnyComputedPropertyInClass.js b/tests/baselines/reference/declarationEmitAnyComputedPropertyInClass.js new file mode 100644 index 00000000000..c7d96ee96e0 --- /dev/null +++ b/tests/baselines/reference/declarationEmitAnyComputedPropertyInClass.js @@ -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 { +} diff --git a/tests/baselines/reference/declarationEmitAnyComputedPropertyInClass.symbols b/tests/baselines/reference/declarationEmitAnyComputedPropertyInClass.symbols new file mode 100644 index 00000000000..4442365ca00 --- /dev/null +++ b/tests/baselines/reference/declarationEmitAnyComputedPropertyInClass.symbols @@ -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)) +} + diff --git a/tests/baselines/reference/declarationEmitAnyComputedPropertyInClass.types b/tests/baselines/reference/declarationEmitAnyComputedPropertyInClass.types new file mode 100644 index 00000000000..c476c4f1910 --- /dev/null +++ b/tests/baselines/reference/declarationEmitAnyComputedPropertyInClass.types @@ -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 +> : ^^^ +} + diff --git a/tests/baselines/reference/isolatedDeclarationErrorsClasses.errors.txt b/tests/baselines/reference/isolatedDeclarationErrorsClasses.errors.txt index dbe84d2408b..90ceb463b9c 100644 --- a/tests/baselines/reference/isolatedDeclarationErrorsClasses.errors.txt +++ b/tests/baselines/reference/isolatedDeclarationErrorsClasses.errors.txt @@ -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. @@ -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; @@ -102,9 +100,6 @@ 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 { } @@ -112,9 +107,6 @@ isolatedDeclarationErrorsClasses.ts(56,5): error TS9013: Expression type can't b !!! 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;} ~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/tests/baselines/reference/transpile/declarationBasicSyntax.d.ts b/tests/baselines/reference/transpile/declarationBasicSyntax.d.ts index ac4f07f48e4..d79e24d1d43 100644 --- a/tests/baselines/reference/transpile/declarationBasicSyntax.d.ts +++ b/tests/baselines/reference/transpile/declarationBasicSyntax.d.ts @@ -56,7 +56,6 @@ export interface Foo { c?: string; } //// [class.d.ts] //// -declare const i: unique symbol; export declare class Bar { #private; a: string; @@ -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; diff --git a/tests/baselines/reference/transpile/declarationComputedPropertyNames.d.ts b/tests/baselines/reference/transpile/declarationComputedPropertyNames.d.ts index 24488ca4972..4ede31ab9e9 100644 --- a/tests/baselines/reference/transpile/declarationComputedPropertyNames.d.ts +++ b/tests/baselines/reference/transpile/declarationComputedPropertyNames.d.ts @@ -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; } diff --git a/tests/cases/compiler/declarationEmitAnyComputedPropertyInClass.ts b/tests/cases/compiler/declarationEmitAnyComputedPropertyInClass.ts new file mode 100644 index 00000000000..29874049583 --- /dev/null +++ b/tests/cases/compiler/declarationEmitAnyComputedPropertyInClass.ts @@ -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]() {}; +}