Skip to content

Commit 79a414b

Browse files
authored
Consistently use '...args' for diagnostic args (#53193)
1 parent 001aa99 commit 79a414b

17 files changed

+249
-218
lines changed

src/compiler/binder.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import {
4444
declarationNameToString,
4545
DeleteExpression,
4646
DestructuringAssignment,
47+
DiagnosticArguments,
4748
DiagnosticCategory,
4849
DiagnosticMessage,
4950
DiagnosticRelatedInformation,
@@ -556,8 +557,8 @@ function createBinder(): (file: SourceFile, options: CompilerOptions) => void {
556557
* If so, the node _must_ be in the current file (as that's the only way anything could have traversed to it to yield it as the error node)
557558
* This version of `createDiagnosticForNode` uses the binder's context to account for this, and always yields correct diagnostics even in these situations.
558559
*/
559-
function createDiagnosticForNode(node: Node, message: DiagnosticMessage, arg0?: string | number, arg1?: string | number, arg2?: string | number): DiagnosticWithLocation {
560-
return createDiagnosticForNodeInSourceFile(getSourceFileOfNode(node) || file, node, message, arg0, arg1, arg2);
560+
function createDiagnosticForNode(node: Node, message: DiagnosticMessage, ...args: DiagnosticArguments): DiagnosticWithLocation {
561+
return createDiagnosticForNodeInSourceFile(getSourceFileOfNode(node) || file, node, message, ...args);
561562
}
562563

563564
function bindSourceFile(f: SourceFile, opts: CompilerOptions) {
@@ -840,7 +841,7 @@ function createBinder(): (file: SourceFile, options: CompilerOptions) => void {
840841
const declarationName = getNameOfDeclaration(node) || node;
841842
forEach(symbol.declarations, (declaration, index) => {
842843
const decl = getNameOfDeclaration(declaration) || declaration;
843-
const diag = createDiagnosticForNode(decl, message, messageNeedsName ? getDisplayName(declaration) : undefined);
844+
const diag = messageNeedsName ? createDiagnosticForNode(decl, message, getDisplayName(declaration)) : createDiagnosticForNode(decl, message);
844845
file.bindDiagnostics.push(
845846
multipleDefaultExports ? addRelatedInfo(diag, createDiagnosticForNode(declarationName, index === 0 ? Diagnostics.Another_export_default_is_here : Diagnostics.and_here)) : diag
846847
);
@@ -849,7 +850,7 @@ function createBinder(): (file: SourceFile, options: CompilerOptions) => void {
849850
}
850851
});
851852

852-
const diag = createDiagnosticForNode(declarationName, message, messageNeedsName ? getDisplayName(node) : undefined);
853+
const diag = messageNeedsName ? createDiagnosticForNode(declarationName, message, getDisplayName(node)) : createDiagnosticForNode(declarationName, message);
853854
file.bindDiagnostics.push(addRelatedInfo(diag, ...relatedInformation));
854855

855856
symbol = createSymbol(SymbolFlags.None, name);
@@ -2613,9 +2614,9 @@ function createBinder(): (file: SourceFile, options: CompilerOptions) => void {
26132614
}
26142615
}
26152616

2616-
function errorOnFirstToken(node: Node, message: DiagnosticMessage, arg0?: any, arg1?: any, arg2?: any) {
2617+
function errorOnFirstToken(node: Node, message: DiagnosticMessage, ...args: DiagnosticArguments) {
26172618
const span = getSpanOfTokenAtPosition(file, node.pos);
2618-
file.bindDiagnostics.push(createFileDiagnostic(file, span.start, span.length, message, arg0, arg1, arg2));
2619+
file.bindDiagnostics.push(createFileDiagnostic(file, span.start, span.length, message, ...args));
26192620
}
26202621

26212622
function errorOrSuggestionOnNode(isError: boolean, node: Node, message: DiagnosticMessage): void {

src/compiler/checker.ts

Lines changed: 68 additions & 67 deletions
Large diffs are not rendered by default.

src/compiler/commandLineParser.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
createGetCanonicalFileName,
2323
Debug,
2424
Diagnostic,
25+
DiagnosticArguments,
2526
DiagnosticMessage,
2627
Diagnostics,
2728
DidYouMeanOptionsDiagnostics,
@@ -1692,7 +1693,7 @@ export function createCompilerDiagnosticForInvalidCustomType(opt: CommandLineOpt
16921693
return createDiagnosticForInvalidCustomType(opt, createCompilerDiagnostic);
16931694
}
16941695

1695-
function createDiagnosticForInvalidCustomType(opt: CommandLineOptionOfCustomType, createDiagnostic: (message: DiagnosticMessage, arg0: string, arg1: string) => Diagnostic): Diagnostic {
1696+
function createDiagnosticForInvalidCustomType(opt: CommandLineOptionOfCustomType, createDiagnostic: (message: DiagnosticMessage, ...args: DiagnosticArguments) => Diagnostic): Diagnostic {
16961697
const namesOfType = arrayFrom(opt.type.keys());
16971698
const stringNames = (opt.deprecatedKeys ? namesOfType.filter(k => !opt.deprecatedKeys!.has(k)) : namesOfType).map(key => `'${key}'`).join(", ");
16981699
return createDiagnostic(Diagnostics.Argument_for_0_option_must_be_Colon_1, `--${opt.name}`, stringNames);
@@ -2994,9 +2995,9 @@ function parseJsonConfigFileContentWorker(
29942995
return "no-prop";
29952996
}
29962997

2997-
function createCompilerDiagnosticOnlyIfJson(message: DiagnosticMessage, arg0?: string, arg1?: string) {
2998+
function createCompilerDiagnosticOnlyIfJson(message: DiagnosticMessage, ...args: DiagnosticArguments) {
29982999
if (!sourceFile) {
2999-
errors.push(createCompilerDiagnostic(message, arg0, arg1));
3000+
errors.push(createCompilerDiagnostic(message, ...args));
30003001
}
30013002
}
30023003
}
@@ -3445,10 +3446,10 @@ function convertOptionsFromJson(optionsNameMap: Map<string, CommandLineOption>,
34453446
return defaultOptions;
34463447
}
34473448

3448-
function createDiagnosticForNodeInSourceFileOrCompilerDiagnostic(sourceFile: TsConfigSourceFile | undefined, node: Node | undefined, message: DiagnosticMessage, arg0?: string | number, arg1?: string | number, arg2?: string | number, arg3?: string | number) {
3449+
function createDiagnosticForNodeInSourceFileOrCompilerDiagnostic(sourceFile: TsConfigSourceFile | undefined, node: Node | undefined, message: DiagnosticMessage, ...args: DiagnosticArguments) {
34493450
return sourceFile && node ?
3450-
createDiagnosticForNodeInSourceFile(sourceFile, node, message, arg0, arg1, arg2, arg3) :
3451-
createCompilerDiagnostic(message, arg0, arg1, arg2, arg3);
3451+
createDiagnosticForNodeInSourceFile(sourceFile, node, message, ...args) :
3452+
createCompilerDiagnostic(message, ...args);
34523453
}
34533454

34543455
/** @internal */
@@ -3519,8 +3520,8 @@ function convertJsonOptionOfCustomType(
35193520
return validateJsonOptionValue(opt, val, errors, valueExpression, sourceFile);
35203521
}
35213522
else {
3522-
errors.push(createDiagnosticForInvalidCustomType(opt, (message, arg0, arg1) =>
3523-
createDiagnosticForNodeInSourceFileOrCompilerDiagnostic(sourceFile, valueExpression, message, arg0, arg1)));
3523+
errors.push(createDiagnosticForInvalidCustomType(opt, (message, ...args) =>
3524+
createDiagnosticForNodeInSourceFileOrCompilerDiagnostic(sourceFile, valueExpression, message, ...args)));
35243525
}
35253526
}
35263527

src/compiler/diagnosticMessages.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5242,7 +5242,7 @@
52425242
"category": "Error",
52435243
"code": 6258
52445244
},
5245-
"Found 1 error in {1}": {
5245+
"Found 1 error in {0}": {
52465246
"category": "Message",
52475247
"code": 6259
52485248
},

src/compiler/parser.ts

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ import {
6363
DefaultClause,
6464
DeleteExpression,
6565
Diagnostic,
66+
DiagnosticArguments,
6667
DiagnosticMessage,
6768
Diagnostics,
6869
DiagnosticWithDetachedLocation,
@@ -149,6 +150,7 @@ import {
149150
isJsxOpeningElement,
150151
isJsxOpeningFragment,
151152
isKeyword,
153+
isKeywordOrPunctuation,
152154
isLeftHandSideExpression,
153155
isLiteralKind,
154156
isMetaProperty,
@@ -306,6 +308,8 @@ import {
306308
PropertyDeclaration,
307309
PropertyName,
308310
PropertySignature,
311+
PunctuationOrKeywordSyntaxKind,
312+
PunctuationSyntaxKind,
309313
QualifiedName,
310314
QuestionDotToken,
311315
QuestionToken,
@@ -380,6 +384,7 @@ import {
380384
TypeQueryNode,
381385
TypeReferenceNode,
382386
UnaryExpression,
387+
unescapeLeadingUnderscores,
383388
UnionOrIntersectionTypeNode,
384389
UnionTypeNode,
385390
UpdateExpression,
@@ -2096,16 +2101,16 @@ namespace Parser {
20962101
return inContext(NodeFlags.AwaitContext);
20972102
}
20982103

2099-
function parseErrorAtCurrentToken(message: DiagnosticMessage, arg0?: any): DiagnosticWithDetachedLocation | undefined {
2100-
return parseErrorAt(scanner.getTokenStart(), scanner.getTokenEnd(), message, arg0);
2104+
function parseErrorAtCurrentToken(message: DiagnosticMessage, ...args: DiagnosticArguments): DiagnosticWithDetachedLocation | undefined {
2105+
return parseErrorAt(scanner.getTokenStart(), scanner.getTokenEnd(), message, ...args);
21012106
}
21022107

2103-
function parseErrorAtPosition(start: number, length: number, message: DiagnosticMessage, arg0?: any): DiagnosticWithDetachedLocation | undefined {
2108+
function parseErrorAtPosition(start: number, length: number, message: DiagnosticMessage, ...args: DiagnosticArguments): DiagnosticWithDetachedLocation | undefined {
21042109
// Don't report another error if it would just be at the same position as the last error.
21052110
const lastError = lastOrUndefined(parseDiagnostics);
21062111
let result: DiagnosticWithDetachedLocation | undefined;
21072112
if (!lastError || start !== lastError.start) {
2108-
result = createDetachedDiagnostic(fileName, start, length, message, arg0);
2113+
result = createDetachedDiagnostic(fileName, start, length, message, ...args);
21092114
parseDiagnostics.push(result);
21102115
}
21112116

@@ -2115,12 +2120,12 @@ namespace Parser {
21152120
return result;
21162121
}
21172122

2118-
function parseErrorAt(start: number, end: number, message: DiagnosticMessage, arg0?: any): DiagnosticWithDetachedLocation | undefined {
2119-
return parseErrorAtPosition(start, end - start, message, arg0);
2123+
function parseErrorAt(start: number, end: number, message: DiagnosticMessage, ...args: DiagnosticArguments): DiagnosticWithDetachedLocation | undefined {
2124+
return parseErrorAtPosition(start, end - start, message, ...args);
21202125
}
21212126

2122-
function parseErrorAtRange(range: TextRange, message: DiagnosticMessage, arg0?: any): void {
2123-
parseErrorAt(range.pos, range.end, message, arg0);
2127+
function parseErrorAtRange(range: TextRange, message: DiagnosticMessage, ...args: DiagnosticArguments): void {
2128+
parseErrorAt(range.pos, range.end, message, ...args);
21242129
}
21252130

21262131
function scanError(message: DiagnosticMessage, length: number): void {
@@ -2289,7 +2294,7 @@ namespace Parser {
22892294
return token() > SyntaxKind.LastReservedWord;
22902295
}
22912296

2292-
function parseExpected(kind: SyntaxKind, diagnosticMessage?: DiagnosticMessage, shouldAdvance = true): boolean {
2297+
function parseExpected(kind: PunctuationOrKeywordSyntaxKind, diagnosticMessage?: DiagnosticMessage, shouldAdvance = true): boolean {
22932298
if (token() === kind) {
22942299
if (shouldAdvance) {
22952300
nextToken();
@@ -2444,11 +2449,12 @@ namespace Parser {
24442449
nextTokenJSDoc();
24452450
return true;
24462451
}
2452+
Debug.assert(isKeywordOrPunctuation(kind));
24472453
parseErrorAtCurrentToken(Diagnostics._0_expected, tokenToString(kind));
24482454
return false;
24492455
}
24502456

2451-
function parseExpectedMatchingBrackets(openKind: SyntaxKind, closeKind: SyntaxKind, openParsed: boolean, openPosition: number) {
2457+
function parseExpectedMatchingBrackets(openKind: PunctuationSyntaxKind, closeKind: PunctuationSyntaxKind, openParsed: boolean, openPosition: number) {
24522458
if (token() === closeKind) {
24532459
nextToken();
24542460
return;
@@ -2489,16 +2495,18 @@ namespace Parser {
24892495
return undefined;
24902496
}
24912497

2492-
function parseExpectedToken<TKind extends SyntaxKind>(t: TKind, diagnosticMessage?: DiagnosticMessage, arg0?: any): Token<TKind>;
2493-
function parseExpectedToken(t: SyntaxKind, diagnosticMessage?: DiagnosticMessage, arg0?: any): Node {
2498+
function parseExpectedToken<TKind extends SyntaxKind>(t: TKind, diagnosticMessage?: DiagnosticMessage, arg0?: string): Token<TKind>;
2499+
function parseExpectedToken(t: SyntaxKind, diagnosticMessage?: DiagnosticMessage, arg0?: string): Node {
24942500
return parseOptionalToken(t) ||
2495-
createMissingNode(t, /*reportAtCurrentPosition*/ false, diagnosticMessage || Diagnostics._0_expected, arg0 || tokenToString(t));
2501+
createMissingNode(t, /*reportAtCurrentPosition*/ false, diagnosticMessage || Diagnostics._0_expected, arg0 || tokenToString(t)!);
24962502
}
24972503

24982504
function parseExpectedTokenJSDoc<TKind extends JSDocSyntaxKind>(t: TKind): Token<TKind>;
24992505
function parseExpectedTokenJSDoc(t: JSDocSyntaxKind): Node {
2500-
return parseOptionalTokenJSDoc(t) ||
2501-
createMissingNode(t, /*reportAtCurrentPosition*/ false, Diagnostics._0_expected, tokenToString(t));
2506+
const optional = parseOptionalTokenJSDoc(t);
2507+
if (optional) return optional;
2508+
Debug.assert(isKeywordOrPunctuation(t));
2509+
return createMissingNode(t, /*reportAtCurrentPosition*/ false, Diagnostics._0_expected, tokenToString(t));
25022510
}
25032511

25042512
function parseTokenNode<T extends Node>(): T {
@@ -2565,14 +2573,14 @@ namespace Parser {
25652573
return node;
25662574
}
25672575

2568-
function createMissingNode<T extends Node>(kind: T["kind"], reportAtCurrentPosition: false, diagnosticMessage?: DiagnosticMessage, arg0?: any): T;
2569-
function createMissingNode<T extends Node>(kind: T["kind"], reportAtCurrentPosition: boolean, diagnosticMessage: DiagnosticMessage, arg0?: any): T;
2570-
function createMissingNode<T extends Node>(kind: T["kind"], reportAtCurrentPosition: boolean, diagnosticMessage?: DiagnosticMessage, arg0?: any): T {
2576+
function createMissingNode<T extends Node>(kind: T["kind"], reportAtCurrentPosition: false, diagnosticMessage?: DiagnosticMessage, ...args: DiagnosticArguments): T;
2577+
function createMissingNode<T extends Node>(kind: T["kind"], reportAtCurrentPosition: boolean, diagnosticMessage: DiagnosticMessage, ...args: DiagnosticArguments): T;
2578+
function createMissingNode<T extends Node>(kind: T["kind"], reportAtCurrentPosition: boolean, diagnosticMessage?: DiagnosticMessage, ...args: DiagnosticArguments): T {
25712579
if (reportAtCurrentPosition) {
2572-
parseErrorAtPosition(scanner.getTokenFullStart(), 0, diagnosticMessage!, arg0);
2580+
parseErrorAtPosition(scanner.getTokenFullStart(), 0, diagnosticMessage!, ...args);
25732581
}
25742582
else if (diagnosticMessage) {
2575-
parseErrorAtCurrentToken(diagnosticMessage, arg0);
2583+
parseErrorAtCurrentToken(diagnosticMessage, ...args);
25762584
}
25772585

25782586
const pos = getNodePos();
@@ -3356,7 +3364,7 @@ namespace Parser {
33563364
case ParsingContext.HeritageClauseElement: return parseErrorAtCurrentToken(Diagnostics.Expression_expected);
33573365
case ParsingContext.VariableDeclarations:
33583366
return isKeyword(token())
3359-
? parseErrorAtCurrentToken(Diagnostics._0_is_not_allowed_as_a_variable_declaration_name, tokenToString(token()))
3367+
? parseErrorAtCurrentToken(Diagnostics._0_is_not_allowed_as_a_variable_declaration_name, tokenToString(token())!)
33603368
: parseErrorAtCurrentToken(Diagnostics.Variable_declaration_expected);
33613369
case ParsingContext.ObjectBindingElements: return parseErrorAtCurrentToken(Diagnostics.Property_destructuring_pattern_expected);
33623370
case ParsingContext.ArrayBindingElements: return parseErrorAtCurrentToken(Diagnostics.Array_element_destructuring_pattern_expected);
@@ -3366,7 +3374,7 @@ namespace Parser {
33663374
case ParsingContext.JSDocParameters: return parseErrorAtCurrentToken(Diagnostics.Parameter_declaration_expected);
33673375
case ParsingContext.Parameters:
33683376
return isKeyword(token())
3369-
? parseErrorAtCurrentToken(Diagnostics._0_is_not_allowed_as_a_parameter_name, tokenToString(token()))
3377+
? parseErrorAtCurrentToken(Diagnostics._0_is_not_allowed_as_a_parameter_name, tokenToString(token())!)
33703378
: parseErrorAtCurrentToken(Diagnostics.Parameter_declaration_expected);
33713379
case ParsingContext.TypeParameters: return parseErrorAtCurrentToken(Diagnostics.Type_parameter_declaration_expected);
33723380
case ParsingContext.TypeArguments: return parseErrorAtCurrentToken(Diagnostics.Type_argument_expected);
@@ -3471,7 +3479,7 @@ namespace Parser {
34713479
return !!(arr as MissingList<Node>).isMissingList;
34723480
}
34733481

3474-
function parseBracketedList<T extends Node>(kind: ParsingContext, parseElement: () => T, open: SyntaxKind, close: SyntaxKind): NodeArray<T> {
3482+
function parseBracketedList<T extends Node>(kind: ParsingContext, parseElement: () => T, open: PunctuationSyntaxKind, close: PunctuationSyntaxKind): NodeArray<T> {
34753483
if (parseExpected(open)) {
34763484
const result = parseDelimitedList(kind, parseElement);
34773485
parseExpected(close);
@@ -5653,6 +5661,7 @@ namespace Parser {
56535661
parseErrorAt(pos, end, Diagnostics.A_type_assertion_expression_is_not_allowed_in_the_left_hand_side_of_an_exponentiation_expression_Consider_enclosing_the_expression_in_parentheses);
56545662
}
56555663
else {
5664+
Debug.assert(isKeywordOrPunctuation(unaryOperator));
56565665
parseErrorAt(pos, end, Diagnostics.An_unary_expression_with_the_0_operator_is_not_allowed_in_the_left_hand_side_of_an_exponentiation_expression_Consider_enclosing_the_expression_in_parentheses, tokenToString(unaryOperator));
56575666
}
56585667
}
@@ -9119,7 +9128,7 @@ namespace Parser {
91199128

91209129
function parseReturnTag(start: number, tagName: Identifier, indent: number, indentText: string): JSDocReturnTag {
91219130
if (some(tags, isJSDocReturnTag)) {
9122-
parseErrorAt(tagName.pos, scanner.getTokenStart(), Diagnostics._0_tag_already_specified, tagName.escapedText);
9131+
parseErrorAt(tagName.pos, scanner.getTokenStart(), Diagnostics._0_tag_already_specified, unescapeLeadingUnderscores(tagName.escapedText));
91239132
}
91249133

91259134
const typeExpression = tryParseTypeExpression();
@@ -9128,7 +9137,7 @@ namespace Parser {
91289137

91299138
function parseTypeTag(start: number, tagName: Identifier, indent?: number, indentText?: string): JSDocTypeTag {
91309139
if (some(tags, isJSDocTypeTag)) {
9131-
parseErrorAt(tagName.pos, scanner.getTokenStart(), Diagnostics._0_tag_already_specified, tagName.escapedText);
9140+
parseErrorAt(tagName.pos, scanner.getTokenStart(), Diagnostics._0_tag_already_specified, unescapeLeadingUnderscores(tagName.escapedText));
91329141
}
91339142

91349143
const typeExpression = parseJSDocTypeExpression(/*mayOmitBraces*/ true);

0 commit comments

Comments
 (0)