mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Remove members that are invalid computed properties. (#58646)
This commit is contained in:
@@ -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).
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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 {
|
||||
}
|
||||
@@ -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))
|
||||
}
|
||||
|
||||
@@ -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
|
||||
> : ^^^
|
||||
}
|
||||
|
||||
@@ -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;}
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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]() {};
|
||||
}
|
||||
Reference in New Issue
Block a user