Skip to content

Commit ed12bf8

Browse files
committed
Redo diagnostics to be spreads
1 parent b232ec4 commit ed12bf8

File tree

7 files changed

+32
-40
lines changed

7 files changed

+32
-40
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19821,7 +19821,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
1982119821
const childrenPropName = childPropName === undefined ? "children" : unescapeLeadingUnderscores(childPropName);
1982219822
const childrenTargetType = getIndexedAccessType(target, getStringLiteralType(childrenPropName));
1982319823
const diagnostic = Diagnostics._0_components_don_t_accept_text_as_child_elements_Text_in_JSX_has_the_type_string_but_the_expected_type_of_1_is_2;
19824-
invalidTextDiagnostic = { ...diagnostic, key: "!!ALREADY FORMATTED!!", message: formatMessage(/*dummy*/ undefined, diagnostic, tagNameText, childrenPropName, typeToString(childrenTargetType)) };
19824+
invalidTextDiagnostic = { ...diagnostic, key: "!!ALREADY FORMATTED!!", message: formatMessage(diagnostic, tagNameText, childrenPropName, typeToString(childrenTargetType)) };
1982519825
}
1982619826
return invalidTextDiagnostic;
1982719827
}

src/compiler/commandLineParser.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
arrayToMap,
77
assign,
88
BuildOptions,
9+
cast,
910
changeExtension,
1011
CharacterCodes,
1112
combinePaths,
@@ -2018,9 +2019,8 @@ export function parseBuildCommand(args: readonly string[]): ParsedBuildCommand {
20182019
}
20192020

20202021
/** @internal */
2021-
export function getDiagnosticText(_message: DiagnosticMessage, ..._args: any[]): string {
2022-
const diagnostic = createCompilerDiagnostic.apply(undefined, arguments);
2023-
return diagnostic.messageText as string;
2022+
export function getDiagnosticText(message: DiagnosticMessage, ...args: any[]): string {
2023+
return cast(createCompilerDiagnostic(message, ...args).messageText, isString);
20242024
}
20252025

20262026
export type DiagnosticReporter = (diagnostic: Diagnostic) => void;

src/compiler/moduleNameResolver.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,8 @@ import {
115115
} from "./_namespaces/ts";
116116

117117
/** @internal */
118-
export function trace(host: ModuleResolutionHost, message: DiagnosticMessage, ...args: any[]): void;
119-
export function trace(host: ModuleResolutionHost): void {
120-
host.trace!(formatMessage.apply(undefined, arguments));
118+
export function trace(host: ModuleResolutionHost, message: DiagnosticMessage, ...args: any[]): void {
119+
host.trace!(formatMessage(message, ...args));
121120
}
122121

123122
/** @internal */

src/compiler/utilities.ts

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8110,9 +8110,10 @@ export function setObjectAllocator(alloc: ObjectAllocator) {
81108110
forEach(objectAllocatorPatchers, fn => fn(objectAllocator));
81118111
}
81128112

8113+
// TODO(jakebailey): replace args type with DiagnosticArguments
81138114
/** @internal */
8114-
export function formatStringFromArgs(text: string, args: ArrayLike<string | number>, baseIndex = 0): string {
8115-
return text.replace(/{(\d+)}/g, (_match, index: string) => "" + Debug.checkDefined(args[+index + baseIndex]));
8115+
export function formatStringFromArgs(text: string, args: ArrayLike<string | number>): string {
8116+
return text.replace(/{(\d+)}/g, (_match, index: string) => "" + Debug.checkDefined(args[+index]));
81168117
}
81178118

81188119
let localizedDiagnosticMessages: MapLike<string> | undefined;
@@ -8137,14 +8138,13 @@ export function getLocaleSpecificMessage(message: DiagnosticMessage) {
81378138
}
81388139

81398140
/** @internal */
8140-
export function createDetachedDiagnostic(fileName: string, start: number, length: number, message: DiagnosticMessage, ...args: DiagnosticArguments): DiagnosticWithDetachedLocation;
8141-
/** @internal */
8142-
export function createDetachedDiagnostic(fileName: string, start: number, length: number, message: DiagnosticMessage): DiagnosticWithDetachedLocation {
8141+
export function createDetachedDiagnostic(fileName: string, start: number, length: number, message: DiagnosticMessage, ...args: DiagnosticArguments): DiagnosticWithDetachedLocation {
81438142
assertDiagnosticLocation(/*file*/ undefined, start, length);
81448143
let text = getLocaleSpecificMessage(message);
81458144

8146-
if (arguments.length > 4) {
8147-
text = formatStringFromArgs(text, arguments, 4);
8145+
// TODO(jakebailey): would love to use length here; maybe use some instead?
8146+
if (args.length > 0) {
8147+
text = formatStringFromArgs(text, args);
81488148
}
81498149

81508150
return {
@@ -8208,15 +8208,14 @@ export function attachFileToDiagnostics(diagnostics: DiagnosticWithDetachedLocat
82088208
}
82098209

82108210
/** @internal */
8211-
export function createFileDiagnostic(file: SourceFile, start: number, length: number, message: DiagnosticMessage, ...args: DiagnosticArguments): DiagnosticWithLocation;
8212-
/** @internal */
8213-
export function createFileDiagnostic(file: SourceFile, start: number, length: number, message: DiagnosticMessage): DiagnosticWithLocation {
8211+
export function createFileDiagnostic(file: SourceFile, start: number, length: number, message: DiagnosticMessage, ...args: DiagnosticArguments): DiagnosticWithLocation {
82148212
assertDiagnosticLocation(file, start, length);
82158213

82168214
let text = getLocaleSpecificMessage(message);
82178215

8218-
if (arguments.length > 4) {
8219-
text = formatStringFromArgs(text, arguments, 4);
8216+
// TODO(jakebailey): would love to use length here; maybe use some instead?
8217+
if (args.length > 0) {
8218+
text = formatStringFromArgs(text, args);
82208219
}
82218220

82228221
return {
@@ -8233,26 +8232,22 @@ export function createFileDiagnostic(file: SourceFile, start: number, length: nu
82338232
}
82348233

82358234
/** @internal */
8236-
export function formatMessage(_dummy: any, message: DiagnosticMessage, ...args: DiagnosticArguments): string;
8237-
/** @internal */
8238-
export function formatMessage(_dummy: any, message: DiagnosticMessage): string {
8235+
export function formatMessage(message: DiagnosticMessage, ...args: DiagnosticArguments): string {
82398236
let text = getLocaleSpecificMessage(message);
82408237

8241-
if (arguments.length > 2) {
8242-
text = formatStringFromArgs(text, arguments, 2);
8238+
if (length(args)) {
8239+
text = formatStringFromArgs(text, args);
82438240
}
82448241

82458242
return text;
82468243
}
82478244

82488245
/** @internal */
8249-
export function createCompilerDiagnostic(message: DiagnosticMessage, ...args: DiagnosticArguments): Diagnostic;
8250-
/** @internal */
8251-
export function createCompilerDiagnostic(message: DiagnosticMessage): Diagnostic {
8246+
export function createCompilerDiagnostic(message: DiagnosticMessage, ...args: DiagnosticArguments): Diagnostic {
82528247
let text = getLocaleSpecificMessage(message);
82538248

8254-
if (arguments.length > 1) {
8255-
text = formatStringFromArgs(text, arguments, 1);
8249+
if (length(args)) {
8250+
text = formatStringFromArgs(text, args);
82568251
}
82578252

82588253
return {
@@ -8283,13 +8278,11 @@ export function createCompilerDiagnosticFromMessageChain(chain: DiagnosticMessag
82838278
}
82848279

82858280
/** @internal */
8286-
export function chainDiagnosticMessages(details: DiagnosticMessageChain | DiagnosticMessageChain[] | undefined, message: DiagnosticMessage, ...args: DiagnosticArguments): DiagnosticMessageChain;
8287-
/** @internal */
8288-
export function chainDiagnosticMessages(details: DiagnosticMessageChain | DiagnosticMessageChain[] | undefined, message: DiagnosticMessage): DiagnosticMessageChain {
8281+
export function chainDiagnosticMessages(details: DiagnosticMessageChain | DiagnosticMessageChain[] | undefined, message: DiagnosticMessage, ...args: DiagnosticArguments): DiagnosticMessageChain {
82898282
let text = getLocaleSpecificMessage(message);
82908283

8291-
if (arguments.length > 2) {
8292-
text = formatStringFromArgs(text, arguments, 2);
8284+
if (length(args)) {
8285+
text = formatStringFromArgs(text, args);
82938286
}
82948287
return {
82958288
messageText: text,

src/deprecatedCompat/deprecate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function formatDeprecationMessage(name: string, error: boolean | undefined, erro
2424
deprecationMessage += `'${name}' `;
2525
deprecationMessage += since ? `has been deprecated since v${since}` : "is deprecated";
2626
deprecationMessage += error ? " and can no longer be used." : errorAfter ? ` and will no longer be usable after v${errorAfter}.` : ".";
27-
deprecationMessage += message ? ` ${formatStringFromArgs(message, [name], 0)}` : "";
27+
deprecationMessage += message ? ` ${formatStringFromArgs(message, [name])}` : "";
2828
return deprecationMessage;
2929
}
3030

src/executeCommandLine/executeCommandLine.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ function printEasyHelp(sys: System, simpleOptions: readonly CommandLineOption[])
486486
output = [
487487
...output,
488488
...generateSectionOptionsOutput(sys, getDiagnosticText(Diagnostics.COMMAND_LINE_FLAGS), cliCommands, /*subCategory*/ false, /*beforeOptionsDescription*/ undefined, /*afterOptionsDescription*/ undefined),
489-
...generateSectionOptionsOutput(sys, getDiagnosticText(Diagnostics.COMMON_COMPILER_OPTIONS), configOpts, /*subCategory*/ false, /*beforeOptionsDescription*/ undefined, formatMessage(/*dummy*/ undefined, Diagnostics.You_can_learn_about_all_of_the_compiler_options_at_0, "https://aka.ms/tsc"))
489+
...generateSectionOptionsOutput(sys, getDiagnosticText(Diagnostics.COMMON_COMPILER_OPTIONS), configOpts, /*subCategory*/ false, /*beforeOptionsDescription*/ undefined, formatMessage(Diagnostics.You_can_learn_about_all_of_the_compiler_options_at_0, "https://aka.ms/tsc"))
490490
];
491491

492492
for (const line of output) {
@@ -504,17 +504,17 @@ function printEasyHelp(sys: System, simpleOptions: readonly CommandLineOption[])
504504

505505
function printAllHelp(sys: System, compilerOptions: readonly CommandLineOption[], buildOptions: readonly CommandLineOption[], watchOptions: readonly CommandLineOption[]) {
506506
let output: string[] = [...getHeader(sys,`${getDiagnosticText(Diagnostics.tsc_Colon_The_TypeScript_Compiler)} - ${getDiagnosticText(Diagnostics.Version_0, version)}`)];
507-
output = [...output, ...generateSectionOptionsOutput(sys, getDiagnosticText(Diagnostics.ALL_COMPILER_OPTIONS), compilerOptions, /*subCategory*/ true, /*beforeOptionsDescription*/ undefined, formatMessage(/*dummy*/ undefined, Diagnostics.You_can_learn_about_all_of_the_compiler_options_at_0, "https://aka.ms/tsc"))];
507+
output = [...output, ...generateSectionOptionsOutput(sys, getDiagnosticText(Diagnostics.ALL_COMPILER_OPTIONS), compilerOptions, /*subCategory*/ true, /*beforeOptionsDescription*/ undefined, formatMessage(Diagnostics.You_can_learn_about_all_of_the_compiler_options_at_0, "https://aka.ms/tsc"))];
508508
output = [...output, ...generateSectionOptionsOutput(sys, getDiagnosticText(Diagnostics.WATCH_OPTIONS), watchOptions, /*subCategory*/ false, getDiagnosticText(Diagnostics.Including_watch_w_will_start_watching_the_current_project_for_the_file_changes_Once_set_you_can_config_watch_mode_with_Colon))];
509-
output = [...output, ...generateSectionOptionsOutput(sys, getDiagnosticText(Diagnostics.BUILD_OPTIONS), buildOptions, /*subCategory*/ false, formatMessage(/*dummy*/ undefined, Diagnostics.Using_build_b_will_make_tsc_behave_more_like_a_build_orchestrator_than_a_compiler_This_is_used_to_trigger_building_composite_projects_which_you_can_learn_more_about_at_0, "https://aka.ms/tsc-composite-builds"))];
509+
output = [...output, ...generateSectionOptionsOutput(sys, getDiagnosticText(Diagnostics.BUILD_OPTIONS), buildOptions, /*subCategory*/ false, formatMessage(Diagnostics.Using_build_b_will_make_tsc_behave_more_like_a_build_orchestrator_than_a_compiler_This_is_used_to_trigger_building_composite_projects_which_you_can_learn_more_about_at_0, "https://aka.ms/tsc-composite-builds"))];
510510
for (const line of output) {
511511
sys.write(line);
512512
}
513513
}
514514

515515
function printBuildHelp(sys: System, buildOptions: readonly CommandLineOption[]) {
516516
let output: string[] = [...getHeader(sys,`${getDiagnosticText(Diagnostics.tsc_Colon_The_TypeScript_Compiler)} - ${getDiagnosticText(Diagnostics.Version_0, version)}`)];
517-
output = [...output, ...generateSectionOptionsOutput(sys, getDiagnosticText(Diagnostics.BUILD_OPTIONS), buildOptions, /*subCategory*/ false, formatMessage(/*dummy*/ undefined, Diagnostics.Using_build_b_will_make_tsc_behave_more_like_a_build_orchestrator_than_a_compiler_This_is_used_to_trigger_building_composite_projects_which_you_can_learn_more_about_at_0, "https://aka.ms/tsc-composite-builds"))];
517+
output = [...output, ...generateSectionOptionsOutput(sys, getDiagnosticText(Diagnostics.BUILD_OPTIONS), buildOptions, /*subCategory*/ false, formatMessage(Diagnostics.Using_build_b_will_make_tsc_behave_more_like_a_build_orchestrator_than_a_compiler_This_is_used_to_trigger_building_composite_projects_which_you_can_learn_more_about_at_0, "https://aka.ms/tsc-composite-builds"))];
518518
for (const line of output) {
519519
sys.write(line);
520520
}

src/harness/fourslashImpl.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3279,7 +3279,7 @@ export class TestState {
32793279
assert.equal(action.description, options.description);
32803280
}
32813281
else if (Array.isArray(options.description)) {
3282-
const description = ts.formatStringFromArgs(options.description[0], options.description, 1);
3282+
const description = ts.formatStringFromArgs(options.description[0], options.description.slice(1));
32833283
assert.equal(action.description, description);
32843284
}
32853285
else {

0 commit comments

Comments
 (0)