From 313f70ac9a3cf5d1558d2427b00dd75666e18cf4 Mon Sep 17 00:00:00 2001 From: Kai Cataldo Date: Mon, 6 Jan 2020 12:31:59 -0500 Subject: [PATCH] Update: add outerIIFEBody: "off" to indent rule (fixes #11377) (#12706) * Update: add outerIIFEBody: "off" to indent rule (fixes #11377) * Fix typo --- docs/rules/indent.md | 34 +++++++++-- lib/rules/indent.js | 13 ++++- tests/lib/rules/indent.js | 115 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 155 insertions(+), 7 deletions(-) diff --git a/docs/rules/indent.md b/docs/rules/indent.md index 8d7f9d05946..7c2e6b7a623 100644 --- a/docs/rules/indent.md +++ b/docs/rules/indent.md @@ -70,7 +70,7 @@ This rule has an object option: * `"SwitchCase"` (default: 0) enforces indentation level for `case` clauses in `switch` statements * `"VariableDeclarator"` (default: 1) enforces indentation level for `var` declarators; can also take an object to define separate rules for `var`, `let` and `const` declarations. It can also be `"first"`, indicating all the declarators should be aligned with the first declarator. -* `"outerIIFEBody"` (default: 1) enforces indentation level for file-level IIFEs. +* `"outerIIFEBody"` (default: 1) enforces indentation level for file-level IIFEs. This can also be set to `"off"` to disable checking for file-level IIFEs. * `"MemberExpression"` (default: 1) enforces indentation level for multi-line property chains. This can also be set to `"off"` to disable checking for MemberExpression indentation. * `"FunctionDeclaration"` takes an object to define rules for function declarations. * `parameters` (default: 1) enforces indentation level for parameters in a function declaration. This can either be a number indicating indentation level, or the string `"first"` indicating that all parameters of the declaration must be aligned with the first parameter. This can also be set to `"off"` to disable checking for FunctionDeclaration parameters. @@ -281,12 +281,12 @@ Examples of **incorrect** code for this rule with the options `2, { "outerIIFEBo })(); -if(y) { +if (y) { console.log('foo'); } ``` -Examples of **correct** code for this rule with the options `2, {"outerIIFEBody": 0}`: +Examples of **correct** code for this rule with the options `2, { "outerIIFEBody": 0 }`: ```js /*eslint indent: ["error", 2, { "outerIIFEBody": 0 }]*/ @@ -300,11 +300,37 @@ function foo(x) { })(); -if(y) { +if (y) { console.log('foo'); } ``` +Examples of **correct** code for this rule with the options `2, { "outerIIFEBody": "off" }`: + +```js +/*eslint indent: ["error", 2, { "outerIIFEBody": "off" }]*/ + +(function() { + +function foo(x) { + return x + 1; +} + +})(); + +(function() { + + function foo(x) { + return x + 1; + } + +})(); + +if (y) { + console.log('foo'); +} +``` + ### MemberExpression Examples of **incorrect** code for this rule with the `2, { "MemberExpression": 1 }` options: diff --git a/lib/rules/indent.js b/lib/rules/indent.js index 9fa62c84a67..59d50e2cc81 100644 --- a/lib/rules/indent.js +++ b/lib/rules/indent.js @@ -540,8 +540,15 @@ module.exports = { ] }, outerIIFEBody: { - type: "integer", - minimum: 0 + oneOf: [ + { + type: "integer", + minimum: 0 + }, + { + enum: ["off"] + } + ] }, MemberExpression: { oneOf: [ @@ -1092,7 +1099,6 @@ module.exports = { }, "BlockStatement, ClassBody"(node) { - let blockIndentLevel; if (node.parent && isOuterIIFE(node.parent)) { @@ -1112,6 +1118,7 @@ module.exports = { if (!astUtils.STATEMENT_LIST_PARENTS.has(node.parent.type)) { offsets.setDesiredOffset(sourceCode.getFirstToken(node), sourceCode.getFirstToken(node.parent), 0); } + addElementListIndent(node.body, sourceCode.getFirstToken(node), sourceCode.getLastToken(node), blockIndentLevel); }, diff --git a/tests/lib/rules/indent.js b/tests/lib/rules/indent.js index 93ebb2824a0..99e7dfa0276 100644 --- a/tests/lib/rules/indent.js +++ b/tests/lib/rules/indent.js @@ -1787,6 +1787,49 @@ ruleTester.run("indent", rule, { `, options: [2, { outerIIFEBody: 0 }] }, + { + code: unIndent` + (function(x) { + return x + 1; + })(); + `, + options: [4, { outerIIFEBody: "off" }] + }, + { + code: unIndent` + (function(x) { + return x + 1; + })(); + `, + options: [4, { outerIIFEBody: "off" }] + }, + { + code: unIndent` + ;(() => { + function x(y) { + return y + 1; + } + })(); + `, + options: [4, { outerIIFEBody: "off" }] + }, + { + code: unIndent` + ;(() => { + function x(y) { + return y + 1; + } + })(); + `, + options: [4, { outerIIFEBody: "off" }] + }, + { + code: unIndent` + function foo() { + } + `, + options: [4, { outerIIFEBody: "off" }] + }, { code: "Buffer.length", options: [4, { MemberExpression: 1 }] @@ -6986,6 +7029,78 @@ ruleTester.run("indent", rule, { options: ["tab", { outerIIFEBody: 3 }], errors: expectedErrors("tab", [[3, 2, 4, "Keyword"]]) }, + { + code: unIndent` + (function(){ + function foo(x) { + return x + 1; + } + })(); + `, + output: unIndent` + (function(){ + function foo(x) { + return x + 1; + } + })(); + `, + options: [4, { outerIIFEBody: "off" }], + errors: expectedErrors([[3, 8, 4, "Keyword"]]) + }, + { + code: unIndent` + (function(){ + function foo(x) { + return x + 1; + } + })(); + `, + output: unIndent` + (function(){ + function foo(x) { + return x + 1; + } + })(); + `, + options: [4, { outerIIFEBody: "off" }], + errors: expectedErrors([[3, 4, 0, "Keyword"]]) + }, + { + code: unIndent` + (() => { + function foo(x) { + return x + 1; + } + })(); + `, + output: unIndent` + (() => { + function foo(x) { + return x + 1; + } + })(); + `, + options: [4, { outerIIFEBody: "off" }], + errors: expectedErrors([[3, 8, 4, "Keyword"]]) + }, + { + code: unIndent` + (() => { + function foo(x) { + return x + 1; + } + })(); + `, + output: unIndent` + (() => { + function foo(x) { + return x + 1; + } + })(); + `, + options: [4, { outerIIFEBody: "off" }], + errors: expectedErrors([[3, 4, 0, "Keyword"]]) + }, { code: unIndent` Buffer