Fixed bug that results in a false negative syntax error when a PEP-695 type alias uses a keyword as a name. This addresses #7933.

This commit is contained in:
Eric Traut 2024-05-15 19:56:08 -07:00
parent 7ccf956b0f
commit 022b30a1f8
2 changed files with 10 additions and 4 deletions

View File

@ -146,7 +146,6 @@ import {
StringTokenFlags,
Token,
TokenType,
softKeywords,
} from './tokenizerTypes';
interface ListResult<T> {
@ -2967,7 +2966,10 @@ export class Parser {
const peekToken2 = this._peekToken(2);
let isInvalidTypeToken = true;
if (peekToken1.type === TokenType.Identifier || peekToken1.type === TokenType.Keyword) {
if (
peekToken1.type === TokenType.Identifier ||
(peekToken1.type === TokenType.Keyword && KeywordToken.isSoftKeyword(peekToken1 as KeywordToken))
) {
if (peekToken2.type === TokenType.OpenBracket) {
isInvalidTypeToken = false;
} else if (
@ -5087,8 +5089,8 @@ export class Parser {
// If this is a "soft keyword", it can be converted into an identifier.
if (nextToken.type === TokenType.Keyword) {
const keywordType = this._peekKeywordType();
if (softKeywords.find((type) => type === keywordType)) {
const keywordToken = nextToken as KeywordToken;
if (KeywordToken.isSoftKeyword(keywordToken)) {
const keywordText = this._fileContents!.substr(nextToken.start, nextToken.length);
this._getNextToken();
return IdentifierToken.create(nextToken.start, nextToken.length, keywordText, nextToken.comments);

View File

@ -319,6 +319,10 @@ export namespace KeywordToken {
return token;
}
export function isSoftKeyword(token: KeywordToken) {
return softKeywords.some((t) => token.keywordType === t);
}
}
export interface StringToken extends Token {