Skip to content

Commit

Permalink
Update: add outerIIFEBody: "off" to indent rule (fixes #11377) (#12706)
Browse files Browse the repository at this point in the history
* Update: add outerIIFEBody: "off" to indent rule (fixes #11377)

* Fix typo
  • Loading branch information
kaicataldo committed Jan 6, 2020
1 parent b77b858 commit 313f70a
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 7 deletions.
34 changes: 30 additions & 4 deletions docs/rules/indent.md
Expand Up @@ -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.
Expand Down Expand Up @@ -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 }]*/
Expand All @@ -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:
Expand Down
13 changes: 10 additions & 3 deletions lib/rules/indent.js
Expand Up @@ -540,8 +540,15 @@ module.exports = {
]
},
outerIIFEBody: {
type: "integer",
minimum: 0
oneOf: [
{
type: "integer",
minimum: 0
},
{
enum: ["off"]
}
]
},
MemberExpression: {
oneOf: [
Expand Down Expand Up @@ -1092,7 +1099,6 @@ module.exports = {
},

"BlockStatement, ClassBody"(node) {

let blockIndentLevel;

if (node.parent && isOuterIIFE(node.parent)) {
Expand All @@ -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);
},

Expand Down
115 changes: 115 additions & 0 deletions tests/lib/rules/indent.js
Expand Up @@ -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 }]
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 313f70a

Please sign in to comment.