Skip to content

Commit 332d213

Browse files
not-an-aardvarknzakas
authored andcommitted
Update: Ensure indent handles nested functions correctly (fixes #7249) (#7265)
1 parent c36d842 commit 332d213

2 files changed

Lines changed: 95 additions & 2 deletions

File tree

lib/rules/indent.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,7 @@ module.exports = {
939939
if (options.FunctionDeclaration.parameters === "first" && node.params.length) {
940940
checkNodesIndent(node.params.slice(1), node.params[0].loc.start.column);
941941
} else if (options.FunctionDeclaration.parameters !== null) {
942-
checkNodesIndent(node.params, indentSize * options.FunctionDeclaration.parameters);
942+
checkNodesIndent(node.params, getNodeIndent(node).goodChar + indentSize * options.FunctionDeclaration.parameters);
943943
}
944944
},
945945

@@ -950,7 +950,7 @@ module.exports = {
950950
if (options.FunctionExpression.parameters === "first" && node.params.length) {
951951
checkNodesIndent(node.params.slice(1), node.params[0].loc.start.column);
952952
} else if (options.FunctionExpression.parameters !== null) {
953-
checkNodesIndent(node.params, indentSize * options.FunctionExpression.parameters);
953+
checkNodesIndent(node.params, getNodeIndent(node).goodChar + indentSize * options.FunctionExpression.parameters);
954954
}
955955
}
956956
};

tests/lib/rules/indent.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1561,13 +1561,42 @@ ruleTester.run("indent", rule, {
15611561
"}",
15621562
options: [2]
15631563
},
1564+
{
1565+
code:
1566+
"function foo() {\n" +
1567+
" function bar() {\n" +
1568+
" baz();\n" +
1569+
" }\n" +
1570+
"}",
1571+
options: [2, {FunctionDeclaration: {body: 1}}]
1572+
},
15641573
{
15651574
code:
15661575
"function foo() {\n" +
15671576
" bar();\n" +
15681577
" \t\t}",
15691578
options: [2]
15701579
},
1580+
{
1581+
code:
1582+
"function foo() {\n" +
1583+
" function bar(baz,\n" +
1584+
" qux) {\n" +
1585+
" foobar();\n" +
1586+
" }\n" +
1587+
"}",
1588+
options: [2, {FunctionDeclaration: {body: 1, parameters: 2}}]
1589+
},
1590+
{
1591+
code:
1592+
"function foo() {\n" +
1593+
" var bar = function(baz,\n" +
1594+
" qux) {\n" +
1595+
" foobar();\n" +
1596+
" };\n" +
1597+
"}",
1598+
options: [2, {FunctionExpression: {parameters: 3}}]
1599+
}
15711600
],
15721601
invalid: [
15731602
{
@@ -2878,6 +2907,70 @@ ruleTester.run("indent", rule, {
28782907
"}",
28792908
options: ["tab"],
28802909
errors: expectedErrors("tab", [[3, "1 tab", "2 spaces", "ExpressionStatement"], [4, "1 tab", "14 spaces", "ExpressionStatement"]])
2910+
},
2911+
{
2912+
code:
2913+
"function foo() {\n" +
2914+
" bar();\n" +
2915+
"\t\t}",
2916+
output:
2917+
"function foo() {\n" +
2918+
" bar();\n" +
2919+
"}",
2920+
options: [2],
2921+
errors: expectedErrors([[3, "0 spaces", "2 tabs", "BlockStatement"]])
2922+
},
2923+
{
2924+
code:
2925+
"function foo() {\n" +
2926+
" function bar() {\n" +
2927+
" baz();\n" +
2928+
" }\n" +
2929+
"}",
2930+
output:
2931+
"function foo() {\n" +
2932+
" function bar() {\n" +
2933+
" baz();\n" +
2934+
" }\n" +
2935+
"}",
2936+
options: [2, {FunctionDeclaration: {body: 1}}],
2937+
errors: expectedErrors([3, 4, 8, "ExpressionStatement"])
2938+
},
2939+
{
2940+
code:
2941+
"function foo() {\n" +
2942+
" function bar(baz,\n" +
2943+
" qux) {\n" +
2944+
" foobar();\n" +
2945+
" }\n" +
2946+
"}",
2947+
output:
2948+
"function foo() {\n" +
2949+
" function bar(baz,\n" +
2950+
" qux) {\n" +
2951+
" foobar();\n" +
2952+
" }\n" +
2953+
"}",
2954+
options: [2, {FunctionDeclaration: {body: 1, parameters: 2}}],
2955+
errors: expectedErrors([3, 6, 4, "Identifier"])
2956+
},
2957+
{
2958+
code:
2959+
"function foo() {\n" +
2960+
" var bar = function(baz,\n" +
2961+
" qux) {\n" +
2962+
" foobar();\n" +
2963+
" };\n" +
2964+
"}",
2965+
output:
2966+
"function foo() {\n" +
2967+
" var bar = function(baz,\n" +
2968+
" qux) {\n" +
2969+
" foobar();\n" +
2970+
" };\n" +
2971+
"}",
2972+
options: [2, {FunctionExpression: {parameters: 3}}],
2973+
errors: expectedErrors([3, 8, 10, "Identifier"])
28812974
}
28822975
]
28832976
});

0 commit comments

Comments
 (0)