Skip to content

Commit

Permalink
Update: add ignoreComments option to indent rule (fixes #9018) (#…
Browse files Browse the repository at this point in the history
…9752)

* Update: add `ignoreComments` option to `indent` rule (fixes #9018)

* Chore: Added basic tests for block comments
  • Loading branch information
platinumazure committed Dec 28, 2017
1 parent db431cb commit bbabf34
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 2 deletions.
19 changes: 18 additions & 1 deletion docs/rules/indent.md
Expand Up @@ -84,7 +84,8 @@ This rule has an object option:
* `"ObjectExpression"` (default: 1) enforces indentation level for properties in objects. It can be set to the string `"first"`, indicating that all properties in the object should be aligned with the first property. This can also be set to `"off"` to disable checking for object properties.
* `"ImportDeclaration"` (default: 1) enforces indentation level for import statements. It can be set to the string `"first"`, indicating that all imported members from a module should be aligned with the first member in the list. This can also be set to `"off"` to disable checking for imported module members.
* `"flatTernaryExpressions": true` (`false` by default) requires no indentation for ternary expressions which are nested in other ternary expressions.
* `ignoredNodes` accepts an array of [selectors](/docs/developer-guide/selectors.md). If an AST node is matched by any of the selectors, the indentation of tokens which are direct children of that node will be ignored. This can be used as an escape hatch to relax the rule if you disagree with the indentation that it enforces for a particular syntactic pattern.
* `"ignoredNodes"` accepts an array of [selectors](/docs/developer-guide/selectors.md). If an AST node is matched by any of the selectors, the indentation of tokens which are direct children of that node will be ignored. This can be used as an escape hatch to relax the rule if you disagree with the indentation that it enforces for a particular syntactic pattern.
* `"ignoreComments"` (default: false) can be used when comments do not need to be aligned with nodes on the previous or next line.

Level of indentation denotes the multiple of the indent specified. Example:

Expand Down Expand Up @@ -641,6 +642,22 @@ bar();
})
```

### ignoreComments

Examples of additional **correct** code for this rule with the `4, { "ignoreComments": true }` option:

```js
/*eslint indent: ["error", 4, { "ignoreComments": true }] */

if (foo) {
doSomething();

// comment intentionally de-indented
doSomethingElse();
}
```


## Compatibility

* **JSHint**: `indent`
Expand Down
12 changes: 11 additions & 1 deletion lib/rules/indent.js
Expand Up @@ -600,6 +600,9 @@ module.exports = {
pattern: ":exit$"
}
}
},
ignoreComments: {
type: "boolean"
}
},
additionalProperties: false
Expand Down Expand Up @@ -638,7 +641,8 @@ module.exports = {
ObjectExpression: 1,
ImportDeclaration: 1,
flatTernaryExpressions: false,
ignoredNodes: []
ignoredNodes: [],
ignoreComments: false
};

if (context.options.length) {
Expand Down Expand Up @@ -1457,6 +1461,12 @@ module.exports = {
},
"Program:exit"() {

// If ignoreComments option is enabled, ignore all comment tokens.
if (options.ignoreComments) {
sourceCode.getAllComments()
.forEach(comment => offsets.ignoreToken(comment));
}

// Invoke the queued offset listeners for the nodes that aren't ignored.
listenerCallQueue
.filter(nodeInfo => !ignoredNodes.has(nodeInfo.node))
Expand Down
62 changes: 62 additions & 0 deletions tests/lib/rules/indent.js
Expand Up @@ -4774,6 +4774,28 @@ ruleTester.run("indent", rule, {
]
`,
options: ["tab", { ArrayExpression: "first", ignoredNodes: ["CallExpression"] }]
},
{
code: unIndent`
if (foo) {
doSomething();
// Intentionally unindented comment
doSomethingElse();
}
`,
options: [4, { ignoreComments: true }]
},
{
code: unIndent`
if (foo) {
doSomething();
/* Intentionally unindented comment */
doSomethingElse();
}
`,
options: [4, { ignoreComments: true }]
}
],

Expand Down Expand Up @@ -9199,6 +9221,46 @@ ruleTester.run("indent", rule, {
ignoredNodes: ["ExpressionStatement > CallExpression > FunctionExpression.callee > BlockStatement"]
}],
errors: expectedErrors([3, 4, 0, "Identifier"])
},
{
code: unIndent`
if (foo) {
doSomething();
// Intentionally unindented comment
doSomethingElse();
}
`,
output: unIndent`
if (foo) {
doSomething();
// Intentionally unindented comment
doSomethingElse();
}
`,
options: [4, { ignoreComments: false }],
errors: expectedErrors([4, 4, 0, "Line"])
},
{
code: unIndent`
if (foo) {
doSomething();
/* Intentionally unindented comment */
doSomethingElse();
}
`,
output: unIndent`
if (foo) {
doSomething();
/* Intentionally unindented comment */
doSomethingElse();
}
`,
options: [4, { ignoreComments: false }],
errors: expectedErrors([4, 4, 0, "Block"])
}
]
});

1 comment on commit bbabf34

@j-f1
Copy link
Contributor

@j-f1 j-f1 commented on bbabf34 Dec 29, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This hasn’t been released yet. You’ll either have to wait for an official release (scheduled for January 5th) or install the unreleased version yourself to get this fix.

Please sign in to comment.