mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-15 12:51:30 -05:00
Improved error messages for property declarations
This commit is contained in:
@@ -15409,6 +15409,11 @@ namespace ts {
|
||||
let flags = 0;
|
||||
for (const modifier of node.modifiers) {
|
||||
switch (modifier.kind) {
|
||||
case SyntaxKind.ConstKeyword:
|
||||
if (node.kind !== SyntaxKind.EnumDeclaration && node.parent.kind === SyntaxKind.ClassDeclaration) {
|
||||
return grammarErrorOnNode(node, Diagnostics.A_class_member_cannot_have_the_0_keyword, tokenToString(SyntaxKind.ConstKeyword));
|
||||
}
|
||||
break;
|
||||
case SyntaxKind.PublicKeyword:
|
||||
case SyntaxKind.ProtectedKeyword:
|
||||
case SyntaxKind.PrivateKeyword:
|
||||
|
||||
@@ -791,7 +791,10 @@
|
||||
"category": "Error",
|
||||
"code": 1247
|
||||
},
|
||||
|
||||
"A class member cannot have the '{0}' keyword.": {
|
||||
"category": "Error",
|
||||
"code": 1248
|
||||
},
|
||||
"'with' statements are not allowed in an async function block.": {
|
||||
"category": "Error",
|
||||
"code": 1300
|
||||
|
||||
@@ -1134,6 +1134,14 @@ namespace ts {
|
||||
return token === t && tryParse(nextTokenCanFollowModifier);
|
||||
}
|
||||
|
||||
function nextTokenIsOnSameLineAndCanFollowModifier() {
|
||||
nextToken();
|
||||
if (scanner.hasPrecedingLineBreak()) {
|
||||
return false;
|
||||
}
|
||||
return canFollowModifier();
|
||||
}
|
||||
|
||||
function nextTokenCanFollowModifier() {
|
||||
if (token === SyntaxKind.ConstKeyword) {
|
||||
// 'const' is only a modifier if followed by 'enum'.
|
||||
@@ -1154,11 +1162,7 @@ namespace ts {
|
||||
return canFollowModifier();
|
||||
}
|
||||
|
||||
nextToken();
|
||||
if (scanner.hasPrecedingLineBreak()) {
|
||||
return false;
|
||||
}
|
||||
return canFollowModifier();
|
||||
return nextTokenIsOnSameLineAndCanFollowModifier();
|
||||
}
|
||||
|
||||
function parseAnyContextualModifier(): boolean {
|
||||
@@ -4923,15 +4927,31 @@ namespace ts {
|
||||
return decorators;
|
||||
}
|
||||
|
||||
function parseModifiers(): ModifiersArray {
|
||||
/*
|
||||
* There are situations in which a modifier like 'const' will appear unexpectedly, such as on a class member.
|
||||
* In those situations, if we are entirely sure that 'const' is not valid on its own (such as when ASI takes effect
|
||||
* and turns it into a standalone declaration), then it is better to parse it and report an error later.
|
||||
*
|
||||
* In such situations, 'permitInvalidConstAsModifier' should be set to true.
|
||||
*/
|
||||
function parseModifiers(permitInvalidConstAsModifier?: boolean): ModifiersArray {
|
||||
let flags = 0;
|
||||
let modifiers: ModifiersArray;
|
||||
while (true) {
|
||||
const modifierStart = scanner.getStartPos();
|
||||
const modifierKind = token;
|
||||
|
||||
if (!parseAnyContextualModifier()) {
|
||||
break;
|
||||
if (token === SyntaxKind.ConstKeyword && permitInvalidConstAsModifier) {
|
||||
// We need to ensure that any subsequent modifiers appear on the same line
|
||||
// so that when 'const' is a standalone declaration, we don't issue an error.
|
||||
if (!tryParse(nextTokenIsOnSameLineAndCanFollowModifier)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (!parseAnyContextualModifier()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!modifiers) {
|
||||
@@ -4976,7 +4996,7 @@ namespace ts {
|
||||
|
||||
const fullStart = getNodePos();
|
||||
const decorators = parseDecorators();
|
||||
const modifiers = parseModifiers();
|
||||
const modifiers = parseModifiers(/*permitInvalidConstAsModifier*/ true);
|
||||
|
||||
const accessor = tryParseAccessorDeclaration(fullStart, decorators, modifiers);
|
||||
if (accessor) {
|
||||
|
||||
Reference in New Issue
Block a user