Skip to content

Commit 3b43d84

Browse files
authored
Enable eslint rules prefer-rest-params and prefer-spread (#55181)
1 parent b35fa04 commit 3b43d84

File tree

11 files changed

+40
-49
lines changed

11 files changed

+40
-49
lines changed

.eslintrc.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,6 @@
104104
],
105105

106106
// Todo: For each of these, investigate whether we want to enable them ✨
107-
"prefer-rest-params": "off",
108-
"prefer-spread": "off",
109107
"@typescript-eslint/no-unused-vars": "off",
110108

111109
// Pending https://github.com/typescript-eslint/typescript-eslint/issues/4820

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19844,7 +19844,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
1984419844
const childrenPropName = childPropName === undefined ? "children" : unescapeLeadingUnderscores(childPropName);
1984519845
const childrenTargetType = getIndexedAccessType(target, getStringLiteralType(childrenPropName));
1984619846
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;
19847-
invalidTextDiagnostic = { ...diagnostic, key: "!!ALREADY FORMATTED!!", message: formatMessage(/*dummy*/ undefined, diagnostic, tagNameText, childrenPropName, typeToString(childrenTargetType)) };
19847+
invalidTextDiagnostic = { ...diagnostic, key: "!!ALREADY FORMATTED!!", message: formatMessage(diagnostic, tagNameText, childrenPropName, typeToString(childrenTargetType)) };
1984819848
}
1984919849
return invalidTextDiagnostic;
1985019850
}

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/core.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2061,6 +2061,7 @@ export function compose<T>(a: (t: T) => T, b: (t: T) => T, c: (t: T) => T, d: (t
20612061
if (!!e) {
20622062
const args: ((t: T) => T)[] = [];
20632063
for (let i = 0; i < arguments.length; i++) {
2064+
// eslint-disable-next-line prefer-rest-params
20642065
args[i] = arguments[i];
20652066
}
20662067

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: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8121,8 +8121,8 @@ export function setObjectAllocator(alloc: ObjectAllocator) {
81218121
}
81228122

81238123
/** @internal */
8124-
export function formatStringFromArgs(text: string, args: ArrayLike<string | number>, baseIndex = 0): string {
8125-
return text.replace(/{(\d+)}/g, (_match, index: string) => "" + Debug.checkDefined(args[+index + baseIndex]));
8124+
export function formatStringFromArgs(text: string, args: DiagnosticArguments): string {
8125+
return text.replace(/{(\d+)}/g, (_match, index: string) => "" + Debug.checkDefined(args[+index]));
81268126
}
81278127

81288128
let localizedDiagnosticMessages: MapLike<string> | undefined;
@@ -8147,14 +8147,12 @@ export function getLocaleSpecificMessage(message: DiagnosticMessage) {
81478147
}
81488148

81498149
/** @internal */
8150-
export function createDetachedDiagnostic(fileName: string, start: number, length: number, message: DiagnosticMessage, ...args: DiagnosticArguments): DiagnosticWithDetachedLocation;
8151-
/** @internal */
8152-
export function createDetachedDiagnostic(fileName: string, start: number, length: number, message: DiagnosticMessage): DiagnosticWithDetachedLocation {
8150+
export function createDetachedDiagnostic(fileName: string, start: number, length: number, message: DiagnosticMessage, ...args: DiagnosticArguments): DiagnosticWithDetachedLocation {
81538151
assertDiagnosticLocation(/*file*/ undefined, start, length);
81548152
let text = getLocaleSpecificMessage(message);
81558153

8156-
if (arguments.length > 4) {
8157-
text = formatStringFromArgs(text, arguments, 4);
8154+
if (some(args)) {
8155+
text = formatStringFromArgs(text, args);
81588156
}
81598157

81608158
return {
@@ -8218,15 +8216,13 @@ export function attachFileToDiagnostics(diagnostics: DiagnosticWithDetachedLocat
82188216
}
82198217

82208218
/** @internal */
8221-
export function createFileDiagnostic(file: SourceFile, start: number, length: number, message: DiagnosticMessage, ...args: DiagnosticArguments): DiagnosticWithLocation;
8222-
/** @internal */
8223-
export function createFileDiagnostic(file: SourceFile, start: number, length: number, message: DiagnosticMessage): DiagnosticWithLocation {
8219+
export function createFileDiagnostic(file: SourceFile, start: number, length: number, message: DiagnosticMessage, ...args: DiagnosticArguments): DiagnosticWithLocation {
82248220
assertDiagnosticLocation(file, start, length);
82258221

82268222
let text = getLocaleSpecificMessage(message);
82278223

8228-
if (arguments.length > 4) {
8229-
text = formatStringFromArgs(text, arguments, 4);
8224+
if (some(args)) {
8225+
text = formatStringFromArgs(text, args);
82308226
}
82318227

82328228
return {
@@ -8243,26 +8239,22 @@ export function createFileDiagnostic(file: SourceFile, start: number, length: nu
82438239
}
82448240

82458241
/** @internal */
8246-
export function formatMessage(_dummy: any, message: DiagnosticMessage, ...args: DiagnosticArguments): string;
8247-
/** @internal */
8248-
export function formatMessage(_dummy: any, message: DiagnosticMessage): string {
8242+
export function formatMessage(message: DiagnosticMessage, ...args: DiagnosticArguments): string {
82498243
let text = getLocaleSpecificMessage(message);
82508244

8251-
if (arguments.length > 2) {
8252-
text = formatStringFromArgs(text, arguments, 2);
8245+
if (some(args)) {
8246+
text = formatStringFromArgs(text, args);
82538247
}
82548248

82558249
return text;
82568250
}
82578251

82588252
/** @internal */
8259-
export function createCompilerDiagnostic(message: DiagnosticMessage, ...args: DiagnosticArguments): Diagnostic;
8260-
/** @internal */
8261-
export function createCompilerDiagnostic(message: DiagnosticMessage): Diagnostic {
8253+
export function createCompilerDiagnostic(message: DiagnosticMessage, ...args: DiagnosticArguments): Diagnostic {
82628254
let text = getLocaleSpecificMessage(message);
82638255

8264-
if (arguments.length > 1) {
8265-
text = formatStringFromArgs(text, arguments, 1);
8256+
if (some(args)) {
8257+
text = formatStringFromArgs(text, args);
82668258
}
82678259

82688260
return {
@@ -8293,13 +8285,11 @@ export function createCompilerDiagnosticFromMessageChain(chain: DiagnosticMessag
82938285
}
82948286

82958287
/** @internal */
8296-
export function chainDiagnosticMessages(details: DiagnosticMessageChain | DiagnosticMessageChain[] | undefined, message: DiagnosticMessage, ...args: DiagnosticArguments): DiagnosticMessageChain;
8297-
/** @internal */
8298-
export function chainDiagnosticMessages(details: DiagnosticMessageChain | DiagnosticMessageChain[] | undefined, message: DiagnosticMessage): DiagnosticMessageChain {
8288+
export function chainDiagnosticMessages(details: DiagnosticMessageChain | DiagnosticMessageChain[] | undefined, message: DiagnosticMessage, ...args: DiagnosticArguments): DiagnosticMessageChain {
82998289
let text = getLocaleSpecificMessage(message);
83008290

8301-
if (arguments.length > 2) {
8302-
text = formatStringFromArgs(text, arguments, 2);
8291+
if (some(args)) {
8292+
text = formatStringFromArgs(text, args);
83038293
}
83048294
return {
83058295
messageText: text,

src/deprecatedCompat/deprecate.ts

Lines changed: 2 additions & 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

@@ -62,6 +62,7 @@ export function createDeprecation(name: string, options: DeprecationOptions = {}
6262
function wrapFunction<F extends (...args: any[]) => any>(deprecation: () => void, func: F): F {
6363
return function (this: unknown) {
6464
deprecation();
65+
// eslint-disable-next-line prefer-rest-params
6566
return func.apply(this, arguments);
6667
} as F;
6768
}

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: 5 additions & 5 deletions
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 {
@@ -4333,7 +4333,7 @@ export class TestState {
43334333
public toggleLineComment(newFileContent: string): void {
43344334
const changes: ts.TextChange[] = [];
43354335
for (const range of this.getRanges()) {
4336-
changes.push.apply(changes, this.languageService.toggleLineComment(this.activeFile.fileName, range));
4336+
changes.push(...this.languageService.toggleLineComment(this.activeFile.fileName, range));
43374337
}
43384338

43394339
this.applyEdits(this.activeFile.fileName, changes);
@@ -4344,7 +4344,7 @@ export class TestState {
43444344
public toggleMultilineComment(newFileContent: string): void {
43454345
const changes: ts.TextChange[] = [];
43464346
for (const range of this.getRanges()) {
4347-
changes.push.apply(changes, this.languageService.toggleMultilineComment(this.activeFile.fileName, range));
4347+
changes.push(...this.languageService.toggleMultilineComment(this.activeFile.fileName, range));
43484348
}
43494349

43504350
this.applyEdits(this.activeFile.fileName, changes);
@@ -4355,7 +4355,7 @@ export class TestState {
43554355
public commentSelection(newFileContent: string): void {
43564356
const changes: ts.TextChange[] = [];
43574357
for (const range of this.getRanges()) {
4358-
changes.push.apply(changes, this.languageService.commentSelection(this.activeFile.fileName, range));
4358+
changes.push(...this.languageService.commentSelection(this.activeFile.fileName, range));
43594359
}
43604360

43614361
this.applyEdits(this.activeFile.fileName, changes);
@@ -4366,7 +4366,7 @@ export class TestState {
43664366
public uncommentSelection(newFileContent: string): void {
43674367
const changes: ts.TextChange[] = [];
43684368
for (const range of this.getRanges()) {
4369-
changes.push.apply(changes, this.languageService.uncommentSelection(this.activeFile.fileName, range));
4369+
changes.push(...this.languageService.uncommentSelection(this.activeFile.fileName, range));
43704370
}
43714371

43724372
this.applyEdits(this.activeFile.fileName, changes);

src/harness/harnessLanguageService.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export function makeDefaultProxy(info: ts.server.PluginCreateInfo): ts.LanguageS
1717
for (const k of Object.keys(langSvc)) {
1818
// eslint-disable-next-line local/only-arrow-functions
1919
proxy[k] = function () {
20+
// eslint-disable-next-line prefer-spread, prefer-rest-params
2021
return langSvc[k].apply(langSvc, arguments);
2122
};
2223
}
@@ -901,6 +902,7 @@ class SessionServerHost implements ts.server.ServerHost, ts.server.Logger {
901902
const langSvc: any = info.languageService;
902903
// eslint-disable-next-line local/only-arrow-functions
903904
proxy.getQuickInfoAtPosition = function () {
905+
// eslint-disable-next-line prefer-spread, prefer-rest-params
904906
const parts = langSvc.getQuickInfoAtPosition.apply(langSvc, arguments);
905907
if (parts.displayParts.length > 0) {
906908
parts.displayParts[0].text = "Proxied";

src/services/services.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2614,7 +2614,7 @@ export function createLanguageService(
26142614
// If the line is not an empty line; otherwise no-op.
26152615
if (lineTextStart !== undefined) {
26162616
if (isJsx) {
2617-
textChanges.push.apply(textChanges, toggleMultilineComment(fileName, { pos: lineStarts[i] + leftMostPosition, end: sourceFile.getLineEndOfPosition(lineStarts[i]) }, isCommenting, isJsx));
2617+
textChanges.push(...toggleMultilineComment(fileName, { pos: lineStarts[i] + leftMostPosition, end: sourceFile.getLineEndOfPosition(lineStarts[i]) }, isCommenting, isJsx));
26182618
}
26192619
else if (isCommenting) {
26202620
textChanges.push({
@@ -2788,10 +2788,10 @@ export function createLanguageService(
27882788
if (commentRange) {
27892789
switch (commentRange.kind) {
27902790
case SyntaxKind.SingleLineCommentTrivia:
2791-
textChanges.push.apply(textChanges, toggleLineComment(fileName, { end: commentRange.end, pos: commentRange.pos + 1 }, /*insertComment*/ false));
2791+
textChanges.push(...toggleLineComment(fileName, { end: commentRange.end, pos: commentRange.pos + 1 }, /*insertComment*/ false));
27922792
break;
27932793
case SyntaxKind.MultiLineCommentTrivia:
2794-
textChanges.push.apply(textChanges, toggleMultilineComment(fileName, { end: commentRange.end, pos: commentRange.pos + 1 }, /*insertComment*/ false));
2794+
textChanges.push(...toggleMultilineComment(fileName, { end: commentRange.end, pos: commentRange.pos + 1 }, /*insertComment*/ false));
27952795
}
27962796

27972797
i = commentRange.end + 1;

0 commit comments

Comments
 (0)