Consistently use '...args' for diagnostic args (#53193)

This commit is contained in:
Jake Bailey
2023-03-20 12:53:09 -07:00
committed by GitHub
parent 001aa99734
commit 79a414bb5e
17 changed files with 249 additions and 218 deletions
+38 -35
View File
@@ -56,6 +56,7 @@ import {
Debug,
DeclarationWithTypeParameterChildren,
Diagnostic,
DiagnosticArguments,
DiagnosticCategory,
diagnosticCategoryName,
DiagnosticMessage,
@@ -3075,15 +3076,15 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
}
}
function createDiagnosticForNodeArray(nodes: NodeArray<Node>, message: DiagnosticMessage, arg0?: string | number, arg1?: string | number, arg2?: string | number): DiagnosticWithLocation {
function createDiagnosticForNodeArray(nodes: NodeArray<Node>, message: DiagnosticMessage, ...args: DiagnosticArguments): DiagnosticWithLocation {
const start = nodes.pos;
return createFileDiagnostic(sourceFile, start, nodes.end - start, message, arg0, arg1, arg2);
return createFileDiagnostic(sourceFile, start, nodes.end - start, message, ...args);
}
// Since these are syntactic diagnostics, parent might not have been set
// this means the sourceFile cannot be infered from the node
function createDiagnosticForNode(node: Node, message: DiagnosticMessage, arg0?: string | number, arg1?: string | number, arg2?: string | number): DiagnosticWithLocation {
return createDiagnosticForNodeInSourceFile(sourceFile, node, message, arg0, arg1, arg2);
function createDiagnosticForNode(node: Node, message: DiagnosticMessage, ...args: DiagnosticArguments): DiagnosticWithLocation {
return createDiagnosticForNodeInSourceFile(sourceFile, node, message, ...args);
}
});
}
@@ -3785,7 +3786,7 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
existingFile,
reason,
Diagnostics.Conflicting_definitions_for_0_found_at_1_and_2_Consider_installing_a_specific_version_of_this_library_to_resolve_the_conflict,
[typeReferenceDirective, resolvedTypeReferenceDirective.resolvedFileName, previousResolution.resolvedFileName]
[typeReferenceDirective, resolvedTypeReferenceDirective.resolvedFileName!, previousResolution.resolvedFileName!]
);
}
}
@@ -3840,11 +3841,12 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
const unqualifiedLibName = removeSuffix(removePrefix(libName, "lib."), ".d.ts");
const suggestion = getSpellingSuggestion(unqualifiedLibName, libs, identity);
const diagnostic = suggestion ? Diagnostics.Cannot_find_lib_definition_for_0_Did_you_mean_1 : Diagnostics.Cannot_find_lib_definition_for_0;
const args = suggestion ? [libName, suggestion] : [libName];
(fileProcessingDiagnostics ||= []).push({
kind: FilePreprocessingDiagnosticsKind.FilePreprocessingReferencedDiagnostic,
reason: { kind: FileIncludeKind.LibReferenceDirective, file: file.path, index, },
diagnostic,
args: [libName, suggestion]
args,
});
}
});
@@ -4343,7 +4345,7 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
function checkDeprecations(
deprecatedIn: string,
removedIn: string,
createDiagnostic: (name: string, value: string | undefined, useInstead: string | undefined, message: DiagnosticMessage, arg0?: string | number, arg1?: string | number, arg2?: string | number, arg3?: string | number) => void,
createDiagnostic: (name: string, value: string | undefined, useInstead: string | undefined, message: DiagnosticMessage, ...args: DiagnosticArguments) => void,
fn: (createDeprecatedDiagnostic: (name: string, value?: string, useInstead?: string) => void) => void,
) {
const deprecatedInVersion = new Version(deprecatedIn);
@@ -4377,14 +4379,14 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
}
function verifyDeprecatedCompilerOptions() {
function createDiagnostic(name: string, value: string | undefined, useInstead: string | undefined, message: DiagnosticMessage, arg0?: string | number, arg1?: string | number, arg2?: string | number, arg3?: string | number) {
function createDiagnostic(name: string, value: string | undefined, useInstead: string | undefined, message: DiagnosticMessage, ...args: DiagnosticArguments) {
if (useInstead) {
const details = chainDiagnosticMessages(/*details*/ undefined, Diagnostics.Use_0_instead, useInstead);
const chain = chainDiagnosticMessages(details, message, arg0, arg1, arg2, arg3);
const chain = chainDiagnosticMessages(details, message, ...args);
createDiagnosticForOption(/*onKey*/ !value, name, /*option2*/ undefined, chain);
}
else {
createDiagnosticForOption(/*onKey*/ !value, name, /*option2*/ undefined, message, arg0, arg1, arg2, arg3);
createDiagnosticForOption(/*onKey*/ !value, name, /*option2*/ undefined, message, ...args);
}
}
@@ -4423,8 +4425,8 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
}
function verifyDeprecatedProjectReference(ref: ProjectReference, parentFile: JsonSourceFile | undefined, index: number) {
function createDiagnostic(_name: string, _value: string | undefined, _useInstead: string | undefined, message: DiagnosticMessage, arg0?: string | number, arg1?: string | number, arg2?: string | number, arg3?: string | number) {
createDiagnosticForReference(parentFile, index, message, arg0, arg1, arg2, arg3);
function createDiagnostic(_name: string, _value: string | undefined, _useInstead: string | undefined, message: DiagnosticMessage, ...args: DiagnosticArguments) {
createDiagnosticForReference(parentFile, index, message, ...args);
}
checkDeprecations("5.0", "5.5", createDiagnostic, createDeprecatedDiagnostic => {
@@ -4434,7 +4436,7 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
});
}
function createDiagnosticExplainingFile(file: SourceFile | undefined, fileProcessingReason: FileIncludeReason | undefined, diagnostic: DiagnosticMessage, args: (string | number | undefined)[] | undefined): Diagnostic {
function createDiagnosticExplainingFile(file: SourceFile | undefined, fileProcessingReason: FileIncludeReason | undefined, diagnostic: DiagnosticMessage, args: DiagnosticArguments | undefined): Diagnostic {
let fileIncludeReasons: DiagnosticMessageChain[] | undefined;
let relatedInfo: Diagnostic[] | undefined;
let locationReason = isReferencedFile(fileProcessingReason) ? fileProcessingReason : undefined;
@@ -4464,7 +4466,7 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
}
}
function addFilePreprocessingFileExplainingDiagnostic(file: SourceFile | undefined, fileProcessingReason: FileIncludeReason, diagnostic: DiagnosticMessage, args?: (string | number | undefined)[]) {
function addFilePreprocessingFileExplainingDiagnostic(file: SourceFile | undefined, fileProcessingReason: FileIncludeReason, diagnostic: DiagnosticMessage, args?: DiagnosticArguments) {
(fileProcessingDiagnostics ||= []).push({
kind: FilePreprocessingDiagnosticsKind.FilePreprocessingFileExplainingDiagnostic,
file: file && file.path,
@@ -4474,7 +4476,7 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
});
}
function addProgramDiagnosticExplainingFile(file: SourceFile, diagnostic: DiagnosticMessage, args?: (string | number | undefined)[]) {
function addProgramDiagnosticExplainingFile(file: SourceFile, diagnostic: DiagnosticMessage, args?: DiagnosticArguments) {
programDiagnostics.add(createDiagnosticExplainingFile(file, /*fileProcessingReason*/ undefined, diagnostic, args));
}
@@ -4606,7 +4608,7 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
});
}
function createDiagnosticForOptionPathKeyValue(key: string, valueIndex: number, message: DiagnosticMessage, arg0?: string | number, arg1?: string | number, arg2?: string | number) {
function createDiagnosticForOptionPathKeyValue(key: string, valueIndex: number, message: DiagnosticMessage, ...args: DiagnosticArguments) {
let needCompilerDiagnostic = true;
const pathsSyntax = getOptionPathsSyntax();
for (const pathProp of pathsSyntax) {
@@ -4614,7 +4616,7 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
for (const keyProps of getPropertyAssignment(pathProp.initializer, key)) {
const initializer = keyProps.initializer;
if (isArrayLiteralExpression(initializer) && initializer.elements.length > valueIndex) {
programDiagnostics.add(createDiagnosticForNodeInSourceFile(options.configFile!, initializer.elements[valueIndex], message, arg0, arg1, arg2));
programDiagnostics.add(createDiagnosticForNodeInSourceFile(options.configFile!, initializer.elements[valueIndex], message, ...args));
needCompilerDiagnostic = false;
}
}
@@ -4622,23 +4624,23 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
}
if (needCompilerDiagnostic) {
programDiagnostics.add(createCompilerDiagnostic(message, arg0, arg1, arg2));
programDiagnostics.add(createCompilerDiagnostic(message, ...args));
}
}
function createDiagnosticForOptionPaths(onKey: boolean, key: string, message: DiagnosticMessage, arg0: string | number) {
function createDiagnosticForOptionPaths(onKey: boolean, key: string, message: DiagnosticMessage, ...args: DiagnosticArguments) {
let needCompilerDiagnostic = true;
const pathsSyntax = getOptionPathsSyntax();
for (const pathProp of pathsSyntax) {
if (isObjectLiteralExpression(pathProp.initializer) &&
createOptionDiagnosticInObjectLiteralSyntax(
pathProp.initializer, onKey, key, /*key2*/ undefined,
message, arg0)) {
message, ...args)) {
needCompilerDiagnostic = false;
}
}
if (needCompilerDiagnostic) {
programDiagnostics.add(createCompilerDiagnostic(message, arg0));
programDiagnostics.add(createCompilerDiagnostic(message, ...args));
}
}
@@ -4662,30 +4664,31 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
}
function createDiagnosticForOptionName(message: DiagnosticMessage, option1: string, option2?: string, option3?: string) {
createDiagnosticForOption(/*onKey*/ true, option1, option2, message, option1, option2, option3);
// TODO(jakebailey): this code makes assumptions about the format of the diagnostic messages.
createDiagnosticForOption(/*onKey*/ true, option1, option2, message, option1, option2!, option3!);
}
function createOptionValueDiagnostic(option1: string, message: DiagnosticMessage, arg0?: string, arg1?: string) {
createDiagnosticForOption(/*onKey*/ false, option1, /*option2*/ undefined, message, arg0, arg1);
function createOptionValueDiagnostic(option1: string, message: DiagnosticMessage, ...args: DiagnosticArguments) {
createDiagnosticForOption(/*onKey*/ false, option1, /*option2*/ undefined, message, ...args);
}
function createDiagnosticForReference(sourceFile: JsonSourceFile | undefined, index: number, message: DiagnosticMessage, arg0?: string | number, arg1?: string | number, arg2?: string | number, arg3?: string | number) {
function createDiagnosticForReference(sourceFile: JsonSourceFile | undefined, index: number, message: DiagnosticMessage, ...args: DiagnosticArguments) {
const referencesSyntax = firstDefined(getTsConfigPropArray(sourceFile || options.configFile, "references"),
property => isArrayLiteralExpression(property.initializer) ? property.initializer : undefined);
if (referencesSyntax && referencesSyntax.elements.length > index) {
programDiagnostics.add(createDiagnosticForNodeInSourceFile(sourceFile || options.configFile!, referencesSyntax.elements[index], message, arg0, arg1, arg2, arg3));
programDiagnostics.add(createDiagnosticForNodeInSourceFile(sourceFile || options.configFile!, referencesSyntax.elements[index], message, ...args));
}
else {
programDiagnostics.add(createCompilerDiagnostic(message, arg0, arg1, arg2, arg3));
programDiagnostics.add(createCompilerDiagnostic(message, ...args));
}
}
function createDiagnosticForOption(onKey: boolean, option1: string, option2: string | undefined, message: DiagnosticMessageChain): void;
function createDiagnosticForOption(onKey: boolean, option1: string, option2: string | undefined, message: DiagnosticMessage, arg0?: string | number, arg1?: string | number, arg2?: string | number, arg3?: string | number): void;
function createDiagnosticForOption(onKey: boolean, option1: string, option2: string | undefined, message: DiagnosticMessage | DiagnosticMessageChain, arg0?: string | number, arg1?: string | number, arg2?: string | number, arg3?: string | number): void {
function createDiagnosticForOption(onKey: boolean, option1: string, option2: string | undefined, message: DiagnosticMessage, ...args: DiagnosticArguments): void;
function createDiagnosticForOption(onKey: boolean, option1: string, option2: string | undefined, message: DiagnosticMessage | DiagnosticMessageChain, ...args: DiagnosticArguments): void {
const compilerOptionsObjectLiteralSyntax = getCompilerOptionsObjectLiteralSyntax();
const needCompilerDiagnostic = !compilerOptionsObjectLiteralSyntax ||
!createOptionDiagnosticInObjectLiteralSyntax(compilerOptionsObjectLiteralSyntax, onKey, option1, option2, message, arg0, arg1, arg2, arg3);
!createOptionDiagnosticInObjectLiteralSyntax(compilerOptionsObjectLiteralSyntax, onKey, option1, option2, message, ...args);
if (needCompilerDiagnostic) {
// eslint-disable-next-line local/no-in-operator
@@ -4693,7 +4696,7 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
programDiagnostics.add(createCompilerDiagnosticFromMessageChain(message));
}
else {
programDiagnostics.add(createCompilerDiagnostic(message, arg0, arg1, arg2, arg3));
programDiagnostics.add(createCompilerDiagnostic(message, ...args));
}
}
}
@@ -4715,9 +4718,9 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
}
function createOptionDiagnosticInObjectLiteralSyntax(objectLiteral: ObjectLiteralExpression, onKey: boolean, key1: string, key2: string | undefined, messageChain: DiagnosticMessageChain): boolean;
function createOptionDiagnosticInObjectLiteralSyntax(objectLiteral: ObjectLiteralExpression, onKey: boolean, key1: string, key2: string | undefined, message: DiagnosticMessage, arg0?: string | number, arg1?: string | number, arg2?: string | number, arg3?: string | number): boolean;
function createOptionDiagnosticInObjectLiteralSyntax(objectLiteral: ObjectLiteralExpression, onKey: boolean, key1: string, key2: string | undefined, message: DiagnosticMessage | DiagnosticMessageChain, arg0?: string | number, arg1?: string | number, arg2?: string | number, arg3?: string | number): boolean;
function createOptionDiagnosticInObjectLiteralSyntax(objectLiteral: ObjectLiteralExpression, onKey: boolean, key1: string, key2: string | undefined, message: DiagnosticMessage | DiagnosticMessageChain, arg0?: string | number, arg1?: string | number, arg2?: string | number, arg3?: string | number): boolean {
function createOptionDiagnosticInObjectLiteralSyntax(objectLiteral: ObjectLiteralExpression, onKey: boolean, key1: string, key2: string | undefined, message: DiagnosticMessage, ...args: DiagnosticArguments): boolean;
function createOptionDiagnosticInObjectLiteralSyntax(objectLiteral: ObjectLiteralExpression, onKey: boolean, key1: string, key2: string | undefined, message: DiagnosticMessage | DiagnosticMessageChain, ...args: DiagnosticArguments): boolean;
function createOptionDiagnosticInObjectLiteralSyntax(objectLiteral: ObjectLiteralExpression, onKey: boolean, key1: string, key2: string | undefined, message: DiagnosticMessage | DiagnosticMessageChain, ...args: DiagnosticArguments): boolean {
const props = getPropertyAssignment(objectLiteral, key1, key2);
for (const prop of props) {
// eslint-disable-next-line local/no-in-operator
@@ -4725,7 +4728,7 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
programDiagnostics.add(createDiagnosticForNodeFromMessageChain(options.configFile!, onKey ? prop.name : prop.initializer, message));
}
else {
programDiagnostics.add(createDiagnosticForNodeInSourceFile(options.configFile!, onKey ? prop.name : prop.initializer, message, arg0, arg1, arg2, arg3));
programDiagnostics.add(createDiagnosticForNodeInSourceFile(options.configFile!, onKey ? prop.name : prop.initializer, message, ...args));
}
}
return !!props.length;