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
Rule Enhancement Proposal: indent and multiline parameters. #6052
Comments
+1 |
I also think this should be an option in |
I also think it's important do implement this rule. Currently the sharable
The Google style also enforces double indentation on line-wrapped variable initializations and method chaining. |
@eslint/doctrine-team we need three 👍s and a champion right? I can champion this and giving my 👍. 2 more to go I guess? |
👍 from me. |
@BYK champion doesn't count as 👍, otherwise you'd just need two. :) I'll give my 👍 , So with @mysticatea and @ilyavolodin , that's three. |
Does this proposal include non-aligned multiline parameters? I have lots of code similar to this:
Currently eslint would expect the sample to have 4, 8 and 4 spaces for the lines A, B, C above, rather than 8, 12 and 8. |
function foo(a, b,
[c],
[
d,
e,
]
) {
//
} |
|
@robations Ah, ... Hmm, some similar codes have been posted to #4161, but #4161 seems about another problem. ..... I found #4696. |
Working on this. |
I wrote some tests, but haven't started the implementation yet. Could someone double-check these tests to make sure there aren't any glaring errors? (There are probably some typos that I'll find when I implement the feature, but I mainly want to verify that the tests are roughly correct, to ensure that I understand the desired behavior correctly.) // valid:
{
code:
"function foo(aaa,\n" +
" bbb, ccc, ddd) {\n" +
" bar();\n" +
"}",
options: [2, {FunctionDeclaration: {parameters: 1, body: 2}}]
},
{
code:
"function foo(aaa, bbb,\n" +
" ccc, ddd) {\n" +
" bar();\n" +
"}",
options: [2, {FunctionDeclaration: {parameters: 3, body: 1}}]
},
{
code:
"function foo(aaa,\n" +
" bbb,\n" +
" ccc) {\n" +
" bar();\n" +
"}",
options: [4, {FunctionDeclaration: {parameters: 1, body: 3}}]
},
{
code:
"function foo(aaa,\n" +
" bbb, ccc,\n" +
" ddd, eee, fff) {\n" +
" bar();\n" +
"}",
options: [2, {FunctionDeclaration: {parameters: "first", body: 1}}]
},
{
code:
"function foo(aaa, bbb)\n" +
"{\n" +
" bar();\n" +
"}",
options: [2, {FunctionDeclaration: {body: 3}}] // FIXME: what is the default for `parameters`?
},
{
code:
"function foo(\n" +
" aaa,\n" +
" bbb) {\n" +
" bar();\n" +
"}",
// edit: fixed body indentation for this case
options: [2, {FunctionDeclaration: {parameters: "first", body: 2}}] // FIXME: make sure this is correct
},
{
code:
"var foo = function(aaa,\n" +
" bbb,\n" +
" ccc,\n" +
" ddd) {\n" +
"bar();\n" +
"}",
options: [2, {FunctionExpression: {parameters: 2, body: 0}}]
},
{
code:
"var foo = function(aaa,\n" +
" bbb,\n" +
" ccc) {\n" +
" bar();\n" +
"}",
options: [2, {FunctionExpression: {parameters: 1, body: 10}}]
},
{
code:
"var foo = function(aaa,\n" +
" bbb, ccc, ddd,\n" +
" eee, fff) {\n" +
" bar();\n" +
"}",
options: [4, {FunctionExpression: {parameters: "first", body: 1}}]
},
{
code:
"var foo = function(\n" +
" aaa, bbb, ccc\n" +
" ddd, eee) {\n" +
" bar();\n" +
"}",
options: [2, {FunctionExpression: {parameters: "first", body: 3}}] // FIXME: make sure this is correct
}
// invalid:
{
code:
"function foo(aaa,\n" +
" bbb, ccc, ddd) {\n" +
" bar();\n" +
"}",
output:
"function foo(aaa,\n" +
" bbb, ccc, ddd) {\n" +
" bar();\n" +
"}",
options: [2, {FunctionDeclaration: {parameters: 1, body: 2}}],
errors: expectedErrors([[2, 2, 4, "Identifier"], [2, 4, 6, "ExpressionStatement"]])
},
{
code:
"function foo(aaa, bbb,\n" +
" ccc, ddd) {\n" +
"bar();\n" +
"}",
output:
"function foo(aaa, bbb,\n" +
" ccc, ddd) {\n" +
" bar();\n" +
"}",
options: [2, {FunctionDeclaration: {parameters: 3, body: 1}}],
errors: expectedErrors([[2, 6, 2, "Identifier"], [3, 0, 2, "ExpressionStatement"]])
},
{
code:
"function foo(aaa,\n" +
" bbb,\n" +
" ccc) {\n" +
" bar();\n" +
"}",
output:
"function foo(aaa,\n" +
" bbb,\n" +
" ccc) {\n" +
" bar();\n" +
"}",
options: [4, {FunctionDeclaration: {parameters: 1, body: 3}}],
errors: expectedErrors([[2, 4, 8, "Identifier"], [3, 2, 8, "Identifier"], [4, 12, 6, "ExpressionStatement"]])
},
{
code:
"function foo(aaa,\n" +
" bbb, ccc,\n" +
" ddd, eee, fff) {\n" +
" bar();\n" +
"}",
output:
"function foo(aaa,\n" +
" bbb, ccc,\n" +
" ddd, eee, fff) {\n" +
" bar();\n" +
"}",
options: [2, {FunctionDeclaration: {parameters: "first", body: 1}}],
errors: expectedErrors([[2, 13, 2, "Identifier"], [3, 13, 19, "Identifier"], [4, 2, 3, "ExpressionStatement"]])
},
{
code:
"function foo(aaa, bbb)\n" +
"{\n" +
"bar();\n" +
"}",
output:
"function foo(aaa, bbb)\n" +
"{\n" +
" bar();\n" +
"}",
options: [2, {FunctionDeclaration: {body: 3}}],
errors: expectedErrors([2, 6, 0, "ExpressionStatement"])
},
{
code:
"function foo(\n" +
"aaa,\n" +
" bbb) {\n" +
"bar();\n" +
"}",
output:
"function foo(\n" +
" aaa,\n" +
" bbb) {\n" +
" bar();\n" +
"}",
options: [2, {FunctionDeclaration: {parameters: "first", body: 4}}],
errors: expectedErrors([[2, 2, 0, "Identifier"], [3, 2, 4, "Identifier"], [4, 0, 4, "ExpressionStatement"]])
},
{
code:
"var foo = function(aaa\n" +
" bbb,\n" +
" ccc,\n" +
" ddd) {\n" +
" bar();\n" +
"}",
output:
"var foo = function(aaa\n" +
" bbb,\n" +
" ccc,\n" +
" ddd) {\n" +
"bar();\n" +
"}",
options: [2, {FunctionExpression: {parameters: 2, body: 0}}],
errors: expectedErrors([[2, 4, 2, "Identifier"], [4, 4, 6, "Identifier"], [5, 0, 2, "ExpressionStatement"]])
},
{
code:
"var foo = function(aaa,\n" +
" bbb,\n" +
" ccc) {\n" +
" bar();\n" +
"}",
output:
"var foo = function(aaa,\n" +
" bbb,\n" +
" ccc) {\n" +
" bar();\n" +
"}",
options: [2, {FunctionExpression: {parameters: 1, body: 10}}],
errors: expectedErrors([[2, 2, 3, "Identifier"], [3, 2, 1, "Identifier"], [4, 2, 10, "ExpressionStatement"]])
},
{
code:
"var foo = function(aaa,\n" +
" bbb, ccc, ddd\n" +
" eee, fff) {\n" +
" bar();\n" +
"}",
output:
"var foo = function(aaa,\n" +
" bbb, ccc, ddd,\n" +
" eee, fff) {\n" +
" bar();\n" +
"}",
options: [4, {FunctionExpression: {parameters: "first", body: 1}}],
errors: expectedErrors([[2, 19, 2, "Identifier"], [3, 19, 24, "Identifier"], [4, 4, 8, "ExpressionStatement"]])
},
{
code:
"var foo = function(\n" +
"aaa, bbb, ccc\n" +
" ddd, eee) {\n" +
" bar();\n" +
"}",
output:
"var foo = function(\n" +
" aaa, bbb, ccc\n" +
" ddd, eee) {\n" +
" bar();\n" +
"}",
options: [2, {FunctionExpression: {parameters: "first", body: 3}}],
errors: expectedErrors([[2, 2, 0, "Identifier"], [3, 2, 4, "Identifier"], [4, 6, 2, "ExpressionStatement"]])
} |
@not-an-aardvark Thank you for contributing!
|
It looks like with the current behavior, the indentation of function parameters isn't checked at all: // This is valid
/* eslint indent: [2, 2] */
function foo(
bar,
baz,
qux) {
qux();
} So we should only check parameter indentation if the user explicitly includes the |
From requireAlignedMultilineParams.
JSCS has the rule to align parameters vertically.
I think this is a responsibility of indent rule.
Those options are similar to existing
SwitchCase
andVariableDeclarator
options.But
"parameters": "first"
is special.If
"parameters": "first"
is specified, 2nd line of parameters and later will be aligned with the same column as the first parameter item.The text was updated successfully, but these errors were encountered: