eslint-stylistic / eslint-stylistic

Monorepo for ESLint Stylistic plugins and configs
https://eslint.style
MIT License
1.47k stars 102 forks source link

`max-len`: add an option to ignore lines containing ONLY a string or template literal #608

Open CrookedGrandma opened 1 week ago

CrookedGrandma commented 1 week ago

Clear and concise description of the problem

I want an option to ignore the maximum line length set by the max-len rule for lines that contain only one string or one template literal, so that lines containing multiple strings or more than just a string can be forced to divide into multiple lines, while lines containing just one long string are allowed.

Suggested solution

In max-len, there already are checks in place that can ignore the maximum length of a line if the line contains a string: https://github.com/eslint-stylistic/eslint-stylistic/blob/20158934a4e8822f1e6b92c1266ab60534273d50/packages/eslint-plugin/rules/max-len/max-len._js_.ts#L354 or a template literal: https://github.com/eslint-stylistic/eslint-stylistic/blob/20158934a4e8822f1e6b92c1266ab60534273d50/packages/eslint-plugin/rules/max-len/max-len._js_.ts#L355

I'd like to expand the option set to add the options ignoreOnlyStrings and ignoreOnlyTemplateLiterals (or with better names, if you can think of some), which do the same thing as the existing options, but only if the line in question contains only one string or template literal, respectively.

Alternative

No response

Additional context

The following example shows the intended working of the new option:

/* eslint @stylistic/max-len: ["error", { code: 80, ignoreOnlyStrings: true }] */
function testFunction(arg1: string, arg2: string, arg3: string, arg4: string, arg5: string = "empty") { } // This should fail
const a = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q"]; // This should fail
const x = [
    "short line 1"
    "very very very very very very very very very very very very very very long line", // This should be fine
    "short line 2",
];

Validations

Contributes

CrookedGrandma commented 1 week ago

This option would also solve #515