Skip to content

Commit 52e3825

Browse files
authored
feat: check template indent (#283)
* feat: check template indent * update es-html-parser * fix * Update indent.test.js * fix types * update * t * t * Update integration.test.js * Update indent.test.js * Update indent.test.js * update versions
1 parent 4ee5ef3 commit 52e3825

File tree

23 files changed

+321
-64
lines changed

23 files changed

+321
-64
lines changed

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

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* @typedef { import("../../types").Tag } Tag
66
* @typedef { import("../../types").RuleListener } RuleListener
77
* @typedef { import("../../types").Context } Context
8+
* @typedef { import("../../types").TemplateText } TemplateText
89
* @typedef { import("eslint").AST.Token } Token
910
* @typedef { import("eslint").SourceCode } SourceCode
1011
* @typedef { import("eslint").AST.Range } Range
@@ -24,8 +25,14 @@
2425
*/
2526

2627
const { parse } = require("@html-eslint/template-parser");
28+
const { NodeTypes } = require("es-html-parser");
2729
const { RULE_CATEGORY } = require("../../constants");
28-
const { splitToLineNodes, isLine, isTag } = require("../utils/node");
30+
const {
31+
splitToLineNodes,
32+
isLine,
33+
isTag,
34+
hasTemplate,
35+
} = require("../utils/node");
2936
const {
3037
shouldCheckTaggedTemplateExpression,
3138
shouldCheckTemplateLiteral,
@@ -173,7 +180,7 @@ module.exports = {
173180
let parentIgnoringChildCount = 0;
174181

175182
/**
176-
* @param {AnyNode | Line} node
183+
* @param {AnyNode | Line | TemplateText} node
177184
* @returns {string}
178185
*/
179186
function getActualIndent(node) {
@@ -228,7 +235,7 @@ module.exports = {
228235
}
229236

230237
/**
231-
* @param {AnyNode | Line} node
238+
* @param {AnyNode | Line | TemplateText} node
232239
*/
233240
function checkIndent(node) {
234241
if (parentIgnoringChildCount > 0) {
@@ -303,10 +310,23 @@ module.exports = {
303310
},
304311
Text(node) {
305312
indentLevel.indent(node);
313+
if (hasTemplate(node)) {
314+
node.parts.forEach((part) => {
315+
if (part.type !== NodeTypes.Part) {
316+
if (part.open) {
317+
checkIndent(part.open);
318+
}
319+
if (part.close) {
320+
checkIndent(part.close);
321+
}
322+
}
323+
});
324+
}
325+
306326
const lineNodes = splitToLineNodes(node);
307327

308328
lineNodes.forEach((lineNode) => {
309-
if (lineNode.skipIndentCheck) {
329+
if (lineNode.hasTemplate) {
310330
return;
311331
}
312332
if (lineNode.value.trim().length) {
@@ -323,9 +343,22 @@ module.exports = {
323343
CommentOpen: checkIndent,
324344
CommentContent(node) {
325345
indentLevel.indent(node);
346+
if (hasTemplate(node)) {
347+
node.parts.forEach((part) => {
348+
if (part.type !== NodeTypes.Part) {
349+
if (part.open) {
350+
checkIndent(part.open);
351+
}
352+
if (part.close) {
353+
checkIndent(part.close);
354+
}
355+
}
356+
});
357+
}
358+
326359
const lineNodes = splitToLineNodes(node);
327360
lineNodes.forEach((lineNode) => {
328-
if (lineNode.skipIndentCheck) {
361+
if (lineNode.hasTemplate) {
329362
return;
330363
}
331364
if (lineNode.value.trim().length) {
@@ -363,7 +396,7 @@ module.exports = {
363396
};
364397

365398
/**
366-
* @param {AnyNode | Line} node
399+
* @param {AnyNode | Line | TemplateText} node
367400
* @param {string} actualIndent
368401
* @return {{range: Range; loc: SourceLocation}}
369402
*/

packages/eslint-plugin/lib/rules/no-aria-hidden-body.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ module.exports = {
4141
if (!ariaHiddenAttr) {
4242
return;
4343
}
44-
if (ariaHiddenAttr.value && ariaHiddenAttr.value.templates.length) {
44+
if (ariaHiddenAttr.value && ariaHiddenAttr.value.parts.length) {
4545
return;
4646
}
4747

packages/eslint-plugin/lib/rules/no-extra-spacing-text.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ module.exports = {
9898
const indexStart = node.range[0] + matcher.lastIndex - space.length;
9999
const indexEnd = indexStart + space.length;
100100

101-
const hasOverlap = isOverlapWithTemplates(node.templates, [
101+
const hasOverlap = isOverlapWithTemplates(node.parts, [
102102
indexStart,
103103
indexEnd,
104104
]);

packages/eslint-plugin/lib/rules/no-multiple-empty-lines.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ module.exports = {
6262
/**
6363
* @param {string[]} lines
6464
* @param {number} lineOffset
65-
* @param {((CommentContent | Text)['templates'][number])[]} tokens
65+
* @param {((CommentContent | Text)['parts'][number])[]} tokens
6666
*/
6767
function check(lines, lineOffset, tokens) {
6868
/** @type {number[]} */

packages/eslint-plugin/lib/rules/no-positive-tabindex.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ module.exports = {
4242
if (
4343
tabIndexAttr &&
4444
tabIndexAttr.value &&
45-
!tabIndexAttr.value.templates.length &&
45+
!tabIndexAttr.value.parts.length &&
4646
parseInt(tabIndexAttr.value.value, 10) > 0
4747
) {
4848
context.report({

packages/eslint-plugin/lib/rules/no-target-blank.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ module.exports = {
5050
const href = findAttr(node, "href");
5151
if (href && href.value && isExternalLink(href.value.value)) {
5252
const rel = findAttr(node, "rel");
53-
if (rel && rel.value && rel.value.templates.length) {
53+
if (rel && rel.value && rel.value.parts.length) {
5454
return;
5555
}
5656

packages/eslint-plugin/lib/rules/no-trailing-spaces.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ module.exports = {
4545
* @param {string} source
4646
* @param {string[]} lines
4747
* @param {number} rangeOffset
48-
* @param {((CommentContent | Text)['templates'][number])[]} tokens
48+
* @param {((CommentContent | Text)['parts'][number])[]} tokens
4949
*/
5050
function check(source, lines, rangeOffset, tokens) {
5151
let rangeIndex = rangeOffset;

packages/eslint-plugin/lib/rules/prefer-https.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ module.exports = {
8888
*/
8989
function check(node) {
9090
const attributeValue = getResourceAttributeValue(node);
91-
if (attributeValue && !attributeValue.templates.length) {
91+
if (attributeValue && !attributeValue.parts.length) {
9292
const protocol = getProtocol(attributeValue.value);
9393
if (protocol === "http:") {
9494
context.report({

packages/eslint-plugin/lib/rules/require-button-type.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ module.exports = {
4949
});
5050
} else if (
5151
!VALID_BUTTON_TYPES_SET.has(typeAttr.value.value) &&
52-
!typeAttr.value.templates.length
52+
!typeAttr.value.parts.length
5353
) {
5454
context.report({
5555
node: typeAttr,

packages/eslint-plugin/lib/rules/require-form-method.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* @typedef { import("../types").RuleModule } RuleModule
33
*/
44

5+
const { NodeTypes } = require("es-html-parser");
56
const { RULE_CATEGORY } = require("../constants");
67
const { findAttr } = require("./utils/node");
78
const { createVisitors } = require("./utils/visitors");
@@ -63,8 +64,8 @@ module.exports = {
6364
}
6465

6566
if (
66-
method.value.templates &&
67-
method.value.templates.some((template) => template.isTemplate)
67+
method.value.parts &&
68+
method.value.parts.some((part) => part.type !== NodeTypes.Part)
6869
) {
6970
return;
7071
}

0 commit comments

Comments
 (0)