Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/rules/indent.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,5 @@ This rule has an object option:
- `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).

- `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).

- `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.
85 changes: 49 additions & 36 deletions packages/eslint-plugin/lib/rules/indent/indent.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* @typedef {Object} Option2
* @property {number} [Option2.Attribute]
* @property {Record<string, number>} [Option2.tagChildrenIndent]
* @property {boolean} [Option2.ignoreComment]
*/

const { parseTemplateLiteral } = require("../utils/template-literal");
Expand Down Expand Up @@ -97,6 +98,10 @@ module.exports = {
},
additionalProperties: false,
},
ignoreComment: {
type: "boolean",
default: false,
},
},
additionalProperties: false,
},
Expand All @@ -110,6 +115,7 @@ module.exports = {
const sourceCode = getSourceCode(context);
const indentLevelOptions = (context.options && context.options[1]) || {};
const lines = sourceCode.getLines();
const ignoreComment = indentLevelOptions.ignoreComment === true;
const { indentType, indentSize, indentChar } = getIndentOptionInfo(context);

/**
Expand Down Expand Up @@ -265,6 +271,48 @@ module.exports = {
}
}

/**
* @type {RuleListener}
*/
const commentVisitor = {
Comment(node) {
indentLevel.indent(node);
},
CommentOpen: checkIndent,
CommentContent(node) {
indentLevel.indent(node);
if (hasTemplate(node)) {
node.parts.forEach((part) => {
if (part.type !== NODE_TYPES.Part) {
if (part.open) {
checkIndent(part.open);
}
if (part.close) {
checkIndent(part.close);
}
}
});
}

const lineNodes = splitToLineNodes(node);
lineNodes.forEach((lineNode) => {
if (lineNode.hasTemplate) {
return;
}
if (lineNode.value.trim().length) {
checkIndent(lineNode);
}
});
},
CommentClose: checkIndent,
"Comment:exit"(node) {
indentLevel.dedent(node);
},
"CommentContent:exit"(node) {
indentLevel.dedent(node);
},
};

/**
* @type {RuleListener}
*/
Expand Down Expand Up @@ -342,42 +390,7 @@ module.exports = {
"Text:exit"(node) {
indentLevel.dedent(node);
},
Comment(node) {
indentLevel.indent(node);
},
CommentOpen: checkIndent,
CommentContent(node) {
indentLevel.indent(node);
if (hasTemplate(node)) {
node.parts.forEach((part) => {
if (part.type !== NODE_TYPES.Part) {
if (part.open) {
checkIndent(part.open);
}
if (part.close) {
checkIndent(part.close);
}
}
});
}

const lineNodes = splitToLineNodes(node);
lineNodes.forEach((lineNode) => {
if (lineNode.hasTemplate) {
return;
}
if (lineNode.value.trim().length) {
checkIndent(lineNode);
}
});
},
CommentClose: checkIndent,
"Comment:exit"(node) {
indentLevel.dedent(node);
},
"CommentContent:exit"(node) {
indentLevel.dedent(node);
},
...(ignoreComment ? {} : commentVisitor),
};
return visitor;
}
Expand Down
61 changes: 61 additions & 0 deletions packages/eslint-plugin/tests/rules/indent.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,40 @@ text
},
],
},
{
code: `
<div>
text
<!--
comment
<div></div>
-->
</div>
`,
options: [
2,
{
ignoreComment: true,
},
],
},
{
code: `
<div>
text
<!--
comment
<div></div>
-->
</div>
`,
options: [
2,
{
ignoreComment: true,
},
],
},
],
invalid: [
{
Expand Down Expand Up @@ -1409,6 +1443,33 @@ text
</div>
`,
},
{
code: `
<div>
text
<!--
comment
<div></div>
-->
</div>
`,
output: `
<div>
text
<!--
comment
<div></div>
-->
</div>
`,
options: [
2,
{
ignoreComment: false,
},
],
errors: wrongIndentErrors(2),
},
],
};
}
Expand Down