Skip to content

Commit d4fecd4

Browse files
author
Andy
authored
Have grammarErrorAtPos take the source file of its argument (#17834)
1 parent 9bcbc97 commit d4fecd4

File tree

1 file changed

+18
-22
lines changed

1 file changed

+18
-22
lines changed

src/compiler/checker.ts

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16363,7 +16363,7 @@ namespace ts {
1636316363
*/
1636416364
function checkCallExpression(node: CallExpression | NewExpression): Type {
1636516365
// Grammar checking; stop grammar-checking if checkGrammarTypeArguments return true
16366-
checkGrammarTypeArguments(node, node.typeArguments) || checkGrammarArguments(node, node.arguments);
16366+
checkGrammarTypeArguments(node, node.typeArguments) || checkGrammarArguments(node.arguments);
1636716367

1636816368
const signature = getResolvedSignature(node);
1636916369

@@ -16411,7 +16411,7 @@ namespace ts {
1641116411

1641216412
function checkImportCallExpression(node: ImportCall): Type {
1641316413
// Check grammar of dynamic import
16414-
checkGrammarArguments(node, node.arguments) || checkGrammarImportCallExpression(node);
16414+
checkGrammarArguments(node.arguments) || checkGrammarImportCallExpression(node);
1641516415

1641616416
if (node.arguments.length === 0) {
1641716417
return createPromiseReturnType(node, anyType);
@@ -18700,7 +18700,7 @@ namespace ts {
1870018700
function checkTypeReferenceNode(node: TypeReferenceNode | ExpressionWithTypeArguments) {
1870118701
checkGrammarTypeArguments(node, node.typeArguments);
1870218702
if (node.kind === SyntaxKind.TypeReference && node.typeName.jsdocDotPos !== undefined && !isInJavaScriptFile(node) && !isInJSDoc(node)) {
18703-
grammarErrorAtPos(getSourceFileOfNode(node), node.typeName.jsdocDotPos, 1, Diagnostics.JSDoc_types_can_only_be_used_inside_documentation_comments);
18703+
grammarErrorAtPos(node, node.typeName.jsdocDotPos, 1, Diagnostics.JSDoc_types_can_only_be_used_inside_documentation_comments);
1870418704

1870518705
}
1870618706
const type = getTypeFromTypeReference(node);
@@ -24136,8 +24136,7 @@ namespace ts {
2413624136
if (list && list.hasTrailingComma) {
2413724137
const start = list.end - ",".length;
2413824138
const end = list.end;
24139-
const sourceFile = getSourceFileOfNode(list[0]);
24140-
return grammarErrorAtPos(sourceFile, start, end - start, Diagnostics.Trailing_comma_not_allowed);
24139+
return grammarErrorAtPos(list[0], start, end - start, Diagnostics.Trailing_comma_not_allowed);
2414124140
}
2414224141
}
2414324142

@@ -24265,19 +24264,18 @@ namespace ts {
2426524264
checkGrammarForAtLeastOneTypeArgument(node, typeArguments);
2426624265
}
2426724266

24268-
function checkGrammarForOmittedArgument(node: CallExpression | NewExpression, args: NodeArray<Expression>): boolean {
24267+
function checkGrammarForOmittedArgument(args: NodeArray<Expression>): boolean {
2426924268
if (args) {
24270-
const sourceFile = getSourceFileOfNode(node);
2427124269
for (const arg of args) {
2427224270
if (arg.kind === SyntaxKind.OmittedExpression) {
24273-
return grammarErrorAtPos(sourceFile, arg.pos, 0, Diagnostics.Argument_expression_expected);
24271+
return grammarErrorAtPos(arg, arg.pos, 0, Diagnostics.Argument_expression_expected);
2427424272
}
2427524273
}
2427624274
}
2427724275
}
2427824276

24279-
function checkGrammarArguments(node: CallExpression | NewExpression, args: NodeArray<Expression>): boolean {
24280-
return checkGrammarForOmittedArgument(node, args);
24277+
function checkGrammarArguments(args: NodeArray<Expression>): boolean {
24278+
return checkGrammarForOmittedArgument(args);
2428124279
}
2428224280

2428324281
function checkGrammarHeritageClause(node: HeritageClause): boolean {
@@ -24287,8 +24285,7 @@ namespace ts {
2428724285
}
2428824286
if (types && types.length === 0) {
2428924287
const listType = tokenToString(node.token);
24290-
const sourceFile = getSourceFileOfNode(node);
24291-
return grammarErrorAtPos(sourceFile, types.pos, 0, Diagnostics._0_list_cannot_be_empty, listType);
24288+
return grammarErrorAtPos(node, types.pos, 0, Diagnostics._0_list_cannot_be_empty, listType);
2429224289
}
2429324290
return forEach(types, checkGrammarExpressionWithTypeArguments);
2429424291
}
@@ -24566,7 +24563,7 @@ namespace ts {
2456624563
return grammarErrorOnNode(accessor.name, Diagnostics.An_accessor_cannot_be_declared_in_an_ambient_context);
2456724564
}
2456824565
else if (accessor.body === undefined && !hasModifier(accessor, ModifierFlags.Abstract)) {
24569-
return grammarErrorAtPos(getSourceFileOfNode(accessor), accessor.end - 1, ";".length, Diagnostics._0_expected, "{");
24566+
return grammarErrorAtPos(accessor, accessor.end - 1, ";".length, Diagnostics._0_expected, "{");
2457024567
}
2457124568
else if (accessor.body && hasModifier(accessor, ModifierFlags.Abstract)) {
2457224569
return grammarErrorOnNode(accessor, Diagnostics.An_abstract_accessor_cannot_have_an_implementation);
@@ -24631,7 +24628,7 @@ namespace ts {
2463124628
return true;
2463224629
}
2463324630
else if (node.body === undefined) {
24634-
return grammarErrorAtPos(getSourceFileOfNode(node), node.end - 1, ";".length, Diagnostics._0_expected, "{");
24631+
return grammarErrorAtPos(node, node.end - 1, ";".length, Diagnostics._0_expected, "{");
2463524632
}
2463624633
}
2463724634

@@ -24723,7 +24720,7 @@ namespace ts {
2472324720

2472424721
if (node.initializer) {
2472524722
// Error on equals token which immediately precedes the initializer
24726-
return grammarErrorAtPos(getSourceFileOfNode(node), node.initializer.pos - 1, 1, Diagnostics.A_rest_element_cannot_have_an_initializer);
24723+
return grammarErrorAtPos(node, node.initializer.pos - 1, 1, Diagnostics.A_rest_element_cannot_have_an_initializer);
2472724724
}
2472824725
}
2472924726
}
@@ -24746,15 +24743,13 @@ namespace ts {
2474624743
else {
2474724744
// Error on equals token which immediate precedes the initializer
2474824745
const equalsTokenLength = "=".length;
24749-
return grammarErrorAtPos(getSourceFileOfNode(node), node.initializer.pos - equalsTokenLength,
24750-
equalsTokenLength, Diagnostics.Initializers_are_not_allowed_in_ambient_contexts);
24746+
return grammarErrorAtPos(node, node.initializer.pos - equalsTokenLength, equalsTokenLength, Diagnostics.Initializers_are_not_allowed_in_ambient_contexts);
2475124747
}
2475224748
}
2475324749
if (node.initializer && !(isConst(node) && isStringOrNumberLiteralExpression(node.initializer))) {
2475424750
// Error on equals token which immediate precedes the initializer
2475524751
const equalsTokenLength = "=".length;
24756-
return grammarErrorAtPos(getSourceFileOfNode(node), node.initializer.pos - equalsTokenLength,
24757-
equalsTokenLength, Diagnostics.Initializers_are_not_allowed_in_ambient_contexts);
24752+
return grammarErrorAtPos(node, node.initializer.pos - equalsTokenLength, equalsTokenLength, Diagnostics.Initializers_are_not_allowed_in_ambient_contexts);
2475824753
}
2475924754
}
2476024755
else if (!node.initializer) {
@@ -24823,7 +24818,7 @@ namespace ts {
2482324818
}
2482424819

2482524820
if (!declarationList.declarations.length) {
24826-
return grammarErrorAtPos(getSourceFileOfNode(declarationList), declarations.pos, declarations.end - declarations.pos, Diagnostics.Variable_declaration_list_cannot_be_empty);
24821+
return grammarErrorAtPos(declarationList, declarations.pos, declarations.end - declarations.pos, Diagnostics.Variable_declaration_list_cannot_be_empty);
2482724822
}
2482824823
}
2482924824

@@ -24876,7 +24871,8 @@ namespace ts {
2487624871
}
2487724872
}
2487824873

24879-
function grammarErrorAtPos(sourceFile: SourceFile, start: number, length: number, message: DiagnosticMessage, arg0?: any, arg1?: any, arg2?: any): boolean {
24874+
function grammarErrorAtPos(nodeForSourceFile: Node, start: number, length: number, message: DiagnosticMessage, arg0?: any, arg1?: any, arg2?: any): boolean {
24875+
const sourceFile = getSourceFileOfNode(nodeForSourceFile);
2488024876
if (!hasParseDiagnostics(sourceFile)) {
2488124877
diagnostics.add(createFileDiagnostic(sourceFile, start, length, message, arg0, arg1, arg2));
2488224878
return true;
@@ -24893,7 +24889,7 @@ namespace ts {
2489324889

2489424890
function checkGrammarConstructorTypeParameters(node: ConstructorDeclaration) {
2489524891
if (node.typeParameters) {
24896-
return grammarErrorAtPos(getSourceFileOfNode(node), node.typeParameters.pos, node.typeParameters.end - node.typeParameters.pos, Diagnostics.Type_parameters_cannot_appear_on_a_constructor_declaration);
24892+
return grammarErrorAtPos(node, node.typeParameters.pos, node.typeParameters.end - node.typeParameters.pos, Diagnostics.Type_parameters_cannot_appear_on_a_constructor_declaration);
2489724893
}
2489824894
}
2489924895

0 commit comments

Comments
 (0)