Skip to content

Enable eslint rules prefer-rest-params and prefer-spread #55181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,6 @@
],

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

// Pending https://github.com/typescript-eslint/typescript-eslint/issues/4820
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19844,7 +19844,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const childrenPropName = childPropName === undefined ? "children" : unescapeLeadingUnderscores(childPropName);
const childrenTargetType = getIndexedAccessType(target, getStringLiteralType(childrenPropName));
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;
invalidTextDiagnostic = { ...diagnostic, key: "!!ALREADY FORMATTED!!", message: formatMessage(/*dummy*/ undefined, diagnostic, tagNameText, childrenPropName, typeToString(childrenTargetType)) };
invalidTextDiagnostic = { ...diagnostic, key: "!!ALREADY FORMATTED!!", message: formatMessage(diagnostic, tagNameText, childrenPropName, typeToString(childrenTargetType)) };
}
return invalidTextDiagnostic;
}
Expand Down
6 changes: 3 additions & 3 deletions src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
arrayToMap,
assign,
BuildOptions,
cast,
changeExtension,
CharacterCodes,
combinePaths,
Expand Down Expand Up @@ -2018,9 +2019,8 @@ export function parseBuildCommand(args: readonly string[]): ParsedBuildCommand {
}

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

export type DiagnosticReporter = (diagnostic: Diagnostic) => void;
Expand Down
1 change: 1 addition & 0 deletions src/compiler/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2061,6 +2061,7 @@ export function compose<T>(a: (t: T) => T, b: (t: T) => T, c: (t: T) => T, d: (t
if (!!e) {
const args: ((t: T) => T)[] = [];
for (let i = 0; i < arguments.length; i++) {
// eslint-disable-next-line prefer-rest-params
args[i] = arguments[i];
}

Expand Down
5 changes: 2 additions & 3 deletions src/compiler/moduleNameResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,8 @@ import {
} from "./_namespaces/ts";

/** @internal */
export function trace(host: ModuleResolutionHost, message: DiagnosticMessage, ...args: any[]): void;
export function trace(host: ModuleResolutionHost): void {
host.trace!(formatMessage.apply(undefined, arguments));
export function trace(host: ModuleResolutionHost, message: DiagnosticMessage, ...args: any[]): void {
host.trace!(formatMessage(message, ...args));
}

/** @internal */
Expand Down
44 changes: 17 additions & 27 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8121,8 +8121,8 @@ export function setObjectAllocator(alloc: ObjectAllocator) {
}

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

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

/** @internal */
export function createDetachedDiagnostic(fileName: string, start: number, length: number, message: DiagnosticMessage, ...args: DiagnosticArguments): DiagnosticWithDetachedLocation;
/** @internal */
export function createDetachedDiagnostic(fileName: string, start: number, length: number, message: DiagnosticMessage): DiagnosticWithDetachedLocation {
export function createDetachedDiagnostic(fileName: string, start: number, length: number, message: DiagnosticMessage, ...args: DiagnosticArguments): DiagnosticWithDetachedLocation {
assertDiagnosticLocation(/*file*/ undefined, start, length);
let text = getLocaleSpecificMessage(message);

if (arguments.length > 4) {
text = formatStringFromArgs(text, arguments, 4);
if (some(args)) {
text = formatStringFromArgs(text, args);
}

return {
Expand Down Expand Up @@ -8218,15 +8216,13 @@ export function attachFileToDiagnostics(diagnostics: DiagnosticWithDetachedLocat
}

/** @internal */
export function createFileDiagnostic(file: SourceFile, start: number, length: number, message: DiagnosticMessage, ...args: DiagnosticArguments): DiagnosticWithLocation;
/** @internal */
export function createFileDiagnostic(file: SourceFile, start: number, length: number, message: DiagnosticMessage): DiagnosticWithLocation {
export function createFileDiagnostic(file: SourceFile, start: number, length: number, message: DiagnosticMessage, ...args: DiagnosticArguments): DiagnosticWithLocation {
assertDiagnosticLocation(file, start, length);

let text = getLocaleSpecificMessage(message);

if (arguments.length > 4) {
text = formatStringFromArgs(text, arguments, 4);
if (some(args)) {
text = formatStringFromArgs(text, args);
}

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

/** @internal */
export function formatMessage(_dummy: any, message: DiagnosticMessage, ...args: DiagnosticArguments): string;
/** @internal */
export function formatMessage(_dummy: any, message: DiagnosticMessage): string {
export function formatMessage(message: DiagnosticMessage, ...args: DiagnosticArguments): string {
let text = getLocaleSpecificMessage(message);

if (arguments.length > 2) {
text = formatStringFromArgs(text, arguments, 2);
if (some(args)) {
text = formatStringFromArgs(text, args);
}

return text;
}

/** @internal */
export function createCompilerDiagnostic(message: DiagnosticMessage, ...args: DiagnosticArguments): Diagnostic;
/** @internal */
export function createCompilerDiagnostic(message: DiagnosticMessage): Diagnostic {
export function createCompilerDiagnostic(message: DiagnosticMessage, ...args: DiagnosticArguments): Diagnostic {
let text = getLocaleSpecificMessage(message);

if (arguments.length > 1) {
text = formatStringFromArgs(text, arguments, 1);
if (some(args)) {
text = formatStringFromArgs(text, args);
}

return {
Expand Down Expand Up @@ -8293,13 +8285,11 @@ export function createCompilerDiagnosticFromMessageChain(chain: DiagnosticMessag
}

/** @internal */
export function chainDiagnosticMessages(details: DiagnosticMessageChain | DiagnosticMessageChain[] | undefined, message: DiagnosticMessage, ...args: DiagnosticArguments): DiagnosticMessageChain;
/** @internal */
export function chainDiagnosticMessages(details: DiagnosticMessageChain | DiagnosticMessageChain[] | undefined, message: DiagnosticMessage): DiagnosticMessageChain {
export function chainDiagnosticMessages(details: DiagnosticMessageChain | DiagnosticMessageChain[] | undefined, message: DiagnosticMessage, ...args: DiagnosticArguments): DiagnosticMessageChain {
let text = getLocaleSpecificMessage(message);

if (arguments.length > 2) {
text = formatStringFromArgs(text, arguments, 2);
if (some(args)) {
text = formatStringFromArgs(text, args);
}
return {
messageText: text,
Expand Down
3 changes: 2 additions & 1 deletion src/deprecatedCompat/deprecate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function formatDeprecationMessage(name: string, error: boolean | undefined, erro
deprecationMessage += `'${name}' `;
deprecationMessage += since ? `has been deprecated since v${since}` : "is deprecated";
deprecationMessage += error ? " and can no longer be used." : errorAfter ? ` and will no longer be usable after v${errorAfter}.` : ".";
deprecationMessage += message ? ` ${formatStringFromArgs(message, [name], 0)}` : "";
deprecationMessage += message ? ` ${formatStringFromArgs(message, [name])}` : "";
return deprecationMessage;
}

Expand Down Expand Up @@ -62,6 +62,7 @@ export function createDeprecation(name: string, options: DeprecationOptions = {}
function wrapFunction<F extends (...args: any[]) => any>(deprecation: () => void, func: F): F {
return function (this: unknown) {
deprecation();
// eslint-disable-next-line prefer-rest-params
return func.apply(this, arguments);
} as F;
}
Expand Down
8 changes: 4 additions & 4 deletions src/executeCommandLine/executeCommandLine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ function printEasyHelp(sys: System, simpleOptions: readonly CommandLineOption[])
output = [
...output,
...generateSectionOptionsOutput(sys, getDiagnosticText(Diagnostics.COMMAND_LINE_FLAGS), cliCommands, /*subCategory*/ false, /*beforeOptionsDescription*/ undefined, /*afterOptionsDescription*/ undefined),
...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"))
...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"))
];

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

function printAllHelp(sys: System, compilerOptions: readonly CommandLineOption[], buildOptions: readonly CommandLineOption[], watchOptions: readonly CommandLineOption[]) {
let output: string[] = [...getHeader(sys,`${getDiagnosticText(Diagnostics.tsc_Colon_The_TypeScript_Compiler)} - ${getDiagnosticText(Diagnostics.Version_0, version)}`)];
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"))];
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"))];
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))];
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"))];
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"))];
for (const line of output) {
sys.write(line);
}
}

function printBuildHelp(sys: System, buildOptions: readonly CommandLineOption[]) {
let output: string[] = [...getHeader(sys,`${getDiagnosticText(Diagnostics.tsc_Colon_The_TypeScript_Compiler)} - ${getDiagnosticText(Diagnostics.Version_0, version)}`)];
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"))];
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"))];
for (const line of output) {
sys.write(line);
}
Expand Down
10 changes: 5 additions & 5 deletions src/harness/fourslashImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3279,7 +3279,7 @@ export class TestState {
assert.equal(action.description, options.description);
}
else if (Array.isArray(options.description)) {
const description = ts.formatStringFromArgs(options.description[0], options.description, 1);
const description = ts.formatStringFromArgs(options.description[0], options.description.slice(1));
assert.equal(action.description, description);
}
else {
Expand Down Expand Up @@ -4333,7 +4333,7 @@ export class TestState {
public toggleLineComment(newFileContent: string): void {
const changes: ts.TextChange[] = [];
for (const range of this.getRanges()) {
changes.push.apply(changes, this.languageService.toggleLineComment(this.activeFile.fileName, range));
changes.push(...this.languageService.toggleLineComment(this.activeFile.fileName, range));
}

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

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

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

this.applyEdits(this.activeFile.fileName, changes);
Expand Down
2 changes: 2 additions & 0 deletions src/harness/harnessLanguageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export function makeDefaultProxy(info: ts.server.PluginCreateInfo): ts.LanguageS
for (const k of Object.keys(langSvc)) {
// eslint-disable-next-line local/only-arrow-functions
proxy[k] = function () {
// eslint-disable-next-line prefer-spread, prefer-rest-params
return langSvc[k].apply(langSvc, arguments);
};
}
Expand Down Expand Up @@ -901,6 +902,7 @@ class SessionServerHost implements ts.server.ServerHost, ts.server.Logger {
const langSvc: any = info.languageService;
// eslint-disable-next-line local/only-arrow-functions
proxy.getQuickInfoAtPosition = function () {
// eslint-disable-next-line prefer-spread, prefer-rest-params
const parts = langSvc.getQuickInfoAtPosition.apply(langSvc, arguments);
if (parts.displayParts.length > 0) {
parts.displayParts[0].text = "Proxied";
Expand Down
6 changes: 3 additions & 3 deletions src/services/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2614,7 +2614,7 @@ export function createLanguageService(
// If the line is not an empty line; otherwise no-op.
if (lineTextStart !== undefined) {
if (isJsx) {
textChanges.push.apply(textChanges, toggleMultilineComment(fileName, { pos: lineStarts[i] + leftMostPosition, end: sourceFile.getLineEndOfPosition(lineStarts[i]) }, isCommenting, isJsx));
textChanges.push(...toggleMultilineComment(fileName, { pos: lineStarts[i] + leftMostPosition, end: sourceFile.getLineEndOfPosition(lineStarts[i]) }, isCommenting, isJsx));
}
else if (isCommenting) {
textChanges.push({
Expand Down Expand Up @@ -2788,10 +2788,10 @@ export function createLanguageService(
if (commentRange) {
switch (commentRange.kind) {
case SyntaxKind.SingleLineCommentTrivia:
textChanges.push.apply(textChanges, toggleLineComment(fileName, { end: commentRange.end, pos: commentRange.pos + 1 }, /*insertComment*/ false));
textChanges.push(...toggleLineComment(fileName, { end: commentRange.end, pos: commentRange.pos + 1 }, /*insertComment*/ false));
break;
case SyntaxKind.MultiLineCommentTrivia:
textChanges.push.apply(textChanges, toggleMultilineComment(fileName, { end: commentRange.end, pos: commentRange.pos + 1 }, /*insertComment*/ false));
textChanges.push(...toggleMultilineComment(fileName, { end: commentRange.end, pos: commentRange.pos + 1 }, /*insertComment*/ false));
}

i = commentRange.end + 1;
Expand Down