Skip to content

Commit eb572ad

Browse files
authored
feat: add ignoreComment options in indent (#445)
* feat: add ignoreCommentContent options in indent * add tests * add options doc * Update indent.js * update * Update indent.md
1 parent 9f998c4 commit eb572ad

File tree

3 files changed

+112
-36
lines changed

3 files changed

+112
-36
lines changed

docs/rules/indent.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,5 @@ This rule has an object option:
129129
- `Attribute` (default: 1): Specifies the attribute indentation level. e.g. indent of 2 spaces with `Attribute` set to `2` will indent the attributes with `4` spaces (2 x 2).
130130

131131
- `tagChildrenIndent` (default: `{}`): Specifies the indent increment of the child tags of the specified tag. e.g. For example, `"tagChildrenIndent": { "html": 0 }` will set the `<html/>` tag children to 0 indent (2 x 0).
132+
133+
- `ignoreComment` (default: `false`): When set to `true`, the indentation of HTML comments (including opening `<!--`, closing `-->`, and content) will not be checked. This is useful when you want to allow free-form indentation for comments.

packages/eslint-plugin/lib/rules/indent/indent.js

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* @typedef {Object} Option2
1919
* @property {number} [Option2.Attribute]
2020
* @property {Record<string, number>} [Option2.tagChildrenIndent]
21+
* @property {boolean} [Option2.ignoreComment]
2122
*/
2223

2324
const { parseTemplateLiteral } = require("../utils/template-literal");
@@ -97,6 +98,10 @@ module.exports = {
9798
},
9899
additionalProperties: false,
99100
},
101+
ignoreComment: {
102+
type: "boolean",
103+
default: false,
104+
},
100105
},
101106
additionalProperties: false,
102107
},
@@ -110,6 +115,7 @@ module.exports = {
110115
const sourceCode = getSourceCode(context);
111116
const indentLevelOptions = (context.options && context.options[1]) || {};
112117
const lines = sourceCode.getLines();
118+
const ignoreComment = indentLevelOptions.ignoreComment === true;
113119
const { indentType, indentSize, indentChar } = getIndentOptionInfo(context);
114120

115121
/**
@@ -265,6 +271,48 @@ module.exports = {
265271
}
266272
}
267273

274+
/**
275+
* @type {RuleListener}
276+
*/
277+
const commentVisitor = {
278+
Comment(node) {
279+
indentLevel.indent(node);
280+
},
281+
CommentOpen: checkIndent,
282+
CommentContent(node) {
283+
indentLevel.indent(node);
284+
if (hasTemplate(node)) {
285+
node.parts.forEach((part) => {
286+
if (part.type !== NODE_TYPES.Part) {
287+
if (part.open) {
288+
checkIndent(part.open);
289+
}
290+
if (part.close) {
291+
checkIndent(part.close);
292+
}
293+
}
294+
});
295+
}
296+
297+
const lineNodes = splitToLineNodes(node);
298+
lineNodes.forEach((lineNode) => {
299+
if (lineNode.hasTemplate) {
300+
return;
301+
}
302+
if (lineNode.value.trim().length) {
303+
checkIndent(lineNode);
304+
}
305+
});
306+
},
307+
CommentClose: checkIndent,
308+
"Comment:exit"(node) {
309+
indentLevel.dedent(node);
310+
},
311+
"CommentContent:exit"(node) {
312+
indentLevel.dedent(node);
313+
},
314+
};
315+
268316
/**
269317
* @type {RuleListener}
270318
*/
@@ -342,42 +390,7 @@ module.exports = {
342390
"Text:exit"(node) {
343391
indentLevel.dedent(node);
344392
},
345-
Comment(node) {
346-
indentLevel.indent(node);
347-
},
348-
CommentOpen: checkIndent,
349-
CommentContent(node) {
350-
indentLevel.indent(node);
351-
if (hasTemplate(node)) {
352-
node.parts.forEach((part) => {
353-
if (part.type !== NODE_TYPES.Part) {
354-
if (part.open) {
355-
checkIndent(part.open);
356-
}
357-
if (part.close) {
358-
checkIndent(part.close);
359-
}
360-
}
361-
});
362-
}
363-
364-
const lineNodes = splitToLineNodes(node);
365-
lineNodes.forEach((lineNode) => {
366-
if (lineNode.hasTemplate) {
367-
return;
368-
}
369-
if (lineNode.value.trim().length) {
370-
checkIndent(lineNode);
371-
}
372-
});
373-
},
374-
CommentClose: checkIndent,
375-
"Comment:exit"(node) {
376-
indentLevel.dedent(node);
377-
},
378-
"CommentContent:exit"(node) {
379-
indentLevel.dedent(node);
380-
},
393+
...(ignoreComment ? {} : commentVisitor),
381394
};
382395
return visitor;
383396
}

packages/eslint-plugin/tests/rules/indent.test.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,40 @@ text
519519
},
520520
],
521521
},
522+
{
523+
code: `
524+
<div>
525+
text
526+
<!--
527+
comment
528+
<div></div>
529+
-->
530+
</div>
531+
`,
532+
options: [
533+
2,
534+
{
535+
ignoreComment: true,
536+
},
537+
],
538+
},
539+
{
540+
code: `
541+
<div>
542+
text
543+
<!--
544+
comment
545+
<div></div>
546+
-->
547+
</div>
548+
`,
549+
options: [
550+
2,
551+
{
552+
ignoreComment: true,
553+
},
554+
],
555+
},
522556
],
523557
invalid: [
524558
{
@@ -1409,6 +1443,33 @@ text
14091443
</div>
14101444
`,
14111445
},
1446+
{
1447+
code: `
1448+
<div>
1449+
text
1450+
<!--
1451+
comment
1452+
<div></div>
1453+
-->
1454+
</div>
1455+
`,
1456+
output: `
1457+
<div>
1458+
text
1459+
<!--
1460+
comment
1461+
<div></div>
1462+
-->
1463+
</div>
1464+
`,
1465+
options: [
1466+
2,
1467+
{
1468+
ignoreComment: false,
1469+
},
1470+
],
1471+
errors: wrongIndentErrors(2),
1472+
},
14121473
],
14131474
};
14141475
}

0 commit comments

Comments
 (0)