mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-28 08:57:35 -06:00
Error for abstract property with initialiser (#43615)
* Error for abstract property with initialiser * remove stray LF * update baselines
This commit is contained in:
parent
5ca7983ebe
commit
4a59b63f35
@ -33344,6 +33344,10 @@ namespace ts {
|
||||
if (isPrivateIdentifier(node.name) && hasStaticModifier(node) && node.initializer && languageVersion === ScriptTarget.ESNext && !compilerOptions.useDefineForClassFields) {
|
||||
error(node.initializer, Diagnostics.Static_fields_with_private_names_can_t_have_initializers_when_the_useDefineForClassFields_flag_is_not_specified_with_a_target_of_esnext_Consider_adding_the_useDefineForClassFields_flag);
|
||||
}
|
||||
// property signatures already report "initializer not allowed in ambient context" elsewhere
|
||||
if (hasSyntacticModifier(node, ModifierFlags.Abstract) && node.kind === SyntaxKind.PropertyDeclaration && node.initializer) {
|
||||
error(node, Diagnostics.Property_0_cannot_have_an_initializer_because_it_is_marked_abstract, declarationNameToString(node.name));
|
||||
}
|
||||
}
|
||||
|
||||
function checkPropertySignature(node: PropertySignature) {
|
||||
@ -33360,8 +33364,7 @@ namespace ts {
|
||||
// Grammar checking for modifiers is done inside the function checkGrammarFunctionLikeDeclaration
|
||||
checkFunctionOrMethodDeclaration(node);
|
||||
|
||||
// Abstract methods cannot have an implementation.
|
||||
// Extra checks are to avoid reporting multiple errors relating to the "abstractness" of the node.
|
||||
// method signatures already report "implementation not allowed in ambient context" elsewhere
|
||||
if (hasSyntacticModifier(node, ModifierFlags.Abstract) && node.kind === SyntaxKind.MethodDeclaration && node.body) {
|
||||
error(node, Diagnostics.Method_0_cannot_have_an_implementation_because_it_is_marked_abstract, declarationNameToString(node.name));
|
||||
}
|
||||
|
||||
@ -887,6 +887,10 @@
|
||||
"category": "Error",
|
||||
"code": 1266
|
||||
},
|
||||
"Property '{0}' cannot have an initializer because it is marked abstract.": {
|
||||
"category": "Error",
|
||||
"code": 1267
|
||||
},
|
||||
|
||||
"'with' statements are not allowed in an async function block.": {
|
||||
"category": "Error",
|
||||
|
||||
@ -0,0 +1,10 @@
|
||||
tests/cases/conformance/classes/propertyMemberDeclarations/abstractPropertyInitializer.ts(2,14): error TS1267: Property 'prop' cannot have an initializer because it is marked abstract.
|
||||
|
||||
|
||||
==== tests/cases/conformance/classes/propertyMemberDeclarations/abstractPropertyInitializer.ts (1 errors) ====
|
||||
abstract class C {
|
||||
abstract prop = 1
|
||||
~~~~
|
||||
!!! error TS1267: Property 'prop' cannot have an initializer because it is marked abstract.
|
||||
}
|
||||
|
||||
19
tests/baselines/reference/abstractPropertyInitializer.js
Normal file
19
tests/baselines/reference/abstractPropertyInitializer.js
Normal file
@ -0,0 +1,19 @@
|
||||
//// [abstractPropertyInitializer.ts]
|
||||
abstract class C {
|
||||
abstract prop = 1
|
||||
}
|
||||
|
||||
|
||||
//// [abstractPropertyInitializer.js]
|
||||
"use strict";
|
||||
var C = /** @class */ (function () {
|
||||
function C() {
|
||||
}
|
||||
return C;
|
||||
}());
|
||||
|
||||
|
||||
//// [abstractPropertyInitializer.d.ts]
|
||||
declare abstract class C {
|
||||
abstract prop: number;
|
||||
}
|
||||
@ -0,0 +1,8 @@
|
||||
=== tests/cases/conformance/classes/propertyMemberDeclarations/abstractPropertyInitializer.ts ===
|
||||
abstract class C {
|
||||
>C : Symbol(C, Decl(abstractPropertyInitializer.ts, 0, 0))
|
||||
|
||||
abstract prop = 1
|
||||
>prop : Symbol(C.prop, Decl(abstractPropertyInitializer.ts, 0, 18))
|
||||
}
|
||||
|
||||
@ -0,0 +1,9 @@
|
||||
=== tests/cases/conformance/classes/propertyMemberDeclarations/abstractPropertyInitializer.ts ===
|
||||
abstract class C {
|
||||
>C : C
|
||||
|
||||
abstract prop = 1
|
||||
>prop : number
|
||||
>1 : 1
|
||||
}
|
||||
|
||||
@ -1,9 +1,12 @@
|
||||
tests/cases/conformance/classes/propertyMemberDeclarations/accessorsOverrideProperty7.ts(2,14): error TS1267: Property 'p' cannot have an initializer because it is marked abstract.
|
||||
tests/cases/conformance/classes/propertyMemberDeclarations/accessorsOverrideProperty7.ts(5,9): error TS2611: 'p' is defined as a property in class 'A', but is overridden here in 'B' as an accessor.
|
||||
|
||||
|
||||
==== tests/cases/conformance/classes/propertyMemberDeclarations/accessorsOverrideProperty7.ts (1 errors) ====
|
||||
==== tests/cases/conformance/classes/propertyMemberDeclarations/accessorsOverrideProperty7.ts (2 errors) ====
|
||||
abstract class A {
|
||||
abstract p = 'yep'
|
||||
~
|
||||
!!! error TS1267: Property 'p' cannot have an initializer because it is marked abstract.
|
||||
}
|
||||
class B extends A {
|
||||
get p() { return 'oh no' } // error
|
||||
|
||||
@ -21,10 +21,11 @@ tests/cases/conformance/classes/members/privateNames/privateNamesIncompatibleMod
|
||||
tests/cases/conformance/classes/members/privateNames/privateNamesIncompatibleModifiers.ts(27,5): error TS1042: 'async' modifier cannot be used here.
|
||||
tests/cases/conformance/classes/members/privateNames/privateNamesIncompatibleModifiers.ts(28,5): error TS1042: 'async' modifier cannot be used here.
|
||||
tests/cases/conformance/classes/members/privateNames/privateNamesIncompatibleModifiers.ts(32,5): error TS18019: 'abstract' modifier cannot be used with a private identifier.
|
||||
tests/cases/conformance/classes/members/privateNames/privateNamesIncompatibleModifiers.ts(32,14): error TS1267: Property '#quux' cannot have an initializer because it is marked abstract.
|
||||
|
||||
|
||||
!!! error TS2318: Cannot find global type 'AsyncIterableIterator'.
|
||||
==== tests/cases/conformance/classes/members/privateNames/privateNamesIncompatibleModifiers.ts (22 errors) ====
|
||||
==== tests/cases/conformance/classes/members/privateNames/privateNamesIncompatibleModifiers.ts (23 errors) ====
|
||||
class A {
|
||||
public #foo = 3; // Error
|
||||
~~~~~~
|
||||
@ -101,5 +102,7 @@ tests/cases/conformance/classes/members/privateNames/privateNamesIncompatibleMod
|
||||
abstract #quux = 3; // Error
|
||||
~~~~~~~~
|
||||
!!! error TS18019: 'abstract' modifier cannot be used with a private identifier.
|
||||
~~~~~
|
||||
!!! error TS1267: Property '#quux' cannot have an initializer because it is marked abstract.
|
||||
}
|
||||
|
||||
@ -0,0 +1,5 @@
|
||||
// @strict: true
|
||||
// @declaration: true
|
||||
abstract class C {
|
||||
abstract prop = 1
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user