Skip to content

Commit 9fed7d1

Browse files
committed
New error
1 parent 358ab6e commit 9fed7d1

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/compiler/parser.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2419,6 +2419,13 @@ namespace Parser {
24192419
}
24202420
}
24212421

2422+
function parseErrorForDeclarationNamedUndefined(name: Identifier) {
2423+
if (name.escapedText === "undefined") {
2424+
const pos = skipTrivia(sourceText, name.pos);
2425+
parseErrorAt(pos, name.end, Diagnostics.Declaration_name_conflicts_with_built_in_global_identifier_0, "undefined");
2426+
}
2427+
}
2428+
24222429
function getSpaceSuggestion(expressionText: string) {
24232430
for (const keyword of viableKeywordSuggestions) {
24242431
if (expressionText.length > keyword.length + 2 && startsWith(expressionText, keyword)) {
@@ -3923,6 +3930,9 @@ namespace Parser {
39233930
const pos = getNodePos();
39243931
const modifiers = parseModifiers(/*allowDecorators*/ false, /*permitConstAsModifier*/ true);
39253932
const name = parseIdentifier();
3933+
if (name.escapedText === "undefined") {
3934+
parseErrorForDeclarationNamedUndefined(name);
3935+
}
39263936
let constraint: TypeNode | undefined;
39273937
let expression: Expression | undefined;
39283938
if (parseOptional(SyntaxKind.ExtendsKeyword)) {
@@ -4369,6 +4379,9 @@ namespace Parser {
43694379
function parseMappedTypeParameter() {
43704380
const pos = getNodePos();
43714381
const name = parseIdentifierName();
4382+
if (name.escapedText === "undefined") {
4383+
parseErrorForDeclarationNamedUndefined(name);
4384+
}
43724385
parseExpected(SyntaxKind.InKeyword);
43734386
const type = parseType();
43744387
return finishNode(factory.createTypeParameterDeclaration(/*modifiers*/ undefined, name, type, /*defaultType*/ undefined), pos);
@@ -4733,6 +4746,9 @@ namespace Parser {
47334746
function parseTypeParameterOfInferType(): TypeParameterDeclaration {
47344747
const pos = getNodePos();
47354748
const name = parseIdentifier();
4749+
if (name.escapedText === "undefined") {
4750+
parseErrorForDeclarationNamedUndefined(name);
4751+
}
47364752
const constraint = tryParse(tryParseConstraintOfInferType);
47374753
const node = factory.createTypeParameterDeclaration(/*modifiers*/ undefined, name, constraint);
47384754
return finishNode(node, pos);
@@ -8097,6 +8113,9 @@ namespace Parser {
80978113

80988114
// We don't parse the name here in await context, instead we will report a grammar error in the checker.
80998115
const name = parseNameOfClassDeclarationOrExpression();
8116+
if (name?.escapedText === "undefined") {
8117+
parseErrorForDeclarationNamedUndefined(name);
8118+
}
81008119
const typeParameters = parseTypeParameters();
81018120
if (some(modifiers, isExportModifier)) setAwaitContext(/*value*/ true);
81028121
const heritageClauses = parseHeritageClauses();
@@ -8179,6 +8198,9 @@ namespace Parser {
81798198
function parseInterfaceDeclaration(pos: number, hasJSDoc: boolean, modifiers: NodeArray<ModifierLike> | undefined): InterfaceDeclaration {
81808199
parseExpected(SyntaxKind.InterfaceKeyword);
81818200
const name = parseIdentifier();
8201+
if (name.escapedText === "undefined") {
8202+
parseErrorForDeclarationNamedUndefined(name);
8203+
}
81828204
const typeParameters = parseTypeParameters();
81838205
const heritageClauses = parseHeritageClauses();
81848206
const members = parseObjectTypeMembers();
@@ -8192,6 +8214,9 @@ namespace Parser {
81928214
parseErrorAtCurrentToken(Diagnostics.Line_break_not_permitted_here);
81938215
}
81948216
const name = parseIdentifier();
8217+
if (name.escapedText === "undefined") {
8218+
parseErrorForDeclarationNamedUndefined(name);
8219+
}
81958220
const typeParameters = parseTypeParameters();
81968221
parseExpected(SyntaxKind.EqualsToken);
81978222
const type = token() === SyntaxKind.IntrinsicKeyword && tryParse(parseKeywordAndNoDot) || parseType();
@@ -8215,6 +8240,9 @@ namespace Parser {
82158240
function parseEnumDeclaration(pos: number, hasJSDoc: boolean, modifiers: NodeArray<ModifierLike> | undefined): EnumDeclaration {
82168241
parseExpected(SyntaxKind.EnumKeyword);
82178242
const name = parseIdentifier();
8243+
if (name.escapedText === "undefined") {
8244+
parseErrorForDeclarationNamedUndefined(name);
8245+
}
82188246
let members;
82198247
if (parseExpected(SyntaxKind.OpenBraceToken)) {
82208248
members = doOutsideOfYieldAndAwaitContext(() => parseDelimitedList(ParsingContext.EnumMembers, parseEnumMember));
@@ -8245,6 +8273,9 @@ namespace Parser {
82458273
// propagate the 'Namespace' flag across the names if set.
82468274
const namespaceFlag = flags & NodeFlags.Namespace;
82478275
const name = flags & NodeFlags.NestedNamespace ? parseIdentifierName() : parseIdentifier();
8276+
if (name.escapedText === "undefined") {
8277+
parseErrorForDeclarationNamedUndefined(name);
8278+
}
82488279
const body = parseOptional(SyntaxKind.DotToken)
82498280
? parseModuleOrNamespaceDeclaration(getNodePos(), /*hasJSDoc*/ false, /*modifiers*/ undefined, NodeFlags.NestedNamespace | namespaceFlag) as NamespaceDeclaration
82508281
: parseModuleBlock();
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
typeNamedUndefined.ts(3,17): error TS2397: Declaration name conflicts with built-in global identifier 'undefined'.
2+
typeNamedUndefined.ts(13,13): error TS2397: Declaration name conflicts with built-in global identifier 'undefined'.
3+
4+
5+
==== typeNamedUndefined.ts (2 errors) ====
6+
export namespace ns {
7+
const s = Symbol();
8+
export type undefined = typeof s;
9+
~~~~~~~~~
10+
!!! error TS2397: Declaration name conflicts with built-in global identifier 'undefined'.
11+
export function x(p: undefined): undefined { // global undefined
12+
return p;
13+
}
14+
}
15+
16+
export function x(p: ns.undefined) { // undefined from the namespace
17+
return p;
18+
}
19+
20+
export type undefined = "";
21+
~~~~~~~~~
22+
!!! error TS2397: Declaration name conflicts with built-in global identifier 'undefined'.
23+

0 commit comments

Comments
 (0)