Skip to content

Commit 3bb8c21

Browse files
committed
Add closing tag line validation matching ember-template-lint
1 parent 131cd12 commit 3bb8c21

2 files changed

Lines changed: 24 additions & 6 deletions

File tree

lib/rules/template-attribute-indentation.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -461,14 +461,20 @@ module.exports = {
461461
}
462462
}
463463

464-
function validateClosingTag(node) {
464+
function validateClosingTag(node, lastChildEndLine) {
465465
// `</tag>` is `2 + tag.length + 1` chars: `</` + tag + `>`
466466
const actualColumnStartLocation = 3 + node.tag.length;
467467
const actualColumn = node.loc.end.column - actualColumnStartLocation;
468468
const expectedColumn = node.loc.start.column;
469-
470-
if (actualColumn !== expectedColumn) {
471-
const actualLine = node.loc.end.line;
469+
const actualLine = node.loc.end.line;
470+
471+
// Closing tag must not appear before the last child ends (line check),
472+
// and must be column-aligned with the opening tag.
473+
// Note: Glimmer AST may not include trailing whitespace TextNodes, so the
474+
// closing tag can be on a LATER line than lastChildEndLine (whitespace gap).
475+
// The original ember-template-lint's strict line equality works because
476+
// Handlebars AST includes trailing TextNodes that span to the closing tag line.
477+
if (actualLine < lastChildEndLine || actualColumn !== expectedColumn) {
472478
context.report({
473479
node,
474480
messageId: 'incorrectClosingTag',
@@ -477,7 +483,7 @@ module.exports = {
477483
tagName: node.tag,
478484
actualLine,
479485
actualColumn,
480-
expectedLine: actualLine,
486+
expectedLine: lastChildEndLine,
481487
expectedColumn,
482488
},
483489
});
@@ -528,7 +534,12 @@ module.exports = {
528534
}
529535

530536
if (node.children.length > 0) {
531-
validateClosingTag(node);
537+
const lastChild = node.children.at(-1);
538+
const expectedStartLine =
539+
lastChild.type === 'GlimmerBlockStatement'
540+
? lastChild.loc.end.line + 1
541+
: lastChild.loc.end.line;
542+
validateClosingTag(node, expectedStartLine);
532543
}
533544
}
534545
}

tests/lib/rules/template-attribute-indentation.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,5 +1034,12 @@ hbsRuleTester.run('template-attribute-indentation (closing tag)', rule, {
10341034
options: [{ 'process-elements': true }],
10351035
errors: [{ messageId: 'incorrectClosingTag' }],
10361036
},
1037+
// Closing tag on same line as opening tag attributes (too early)
1038+
{
1039+
code: ['<div', ' class="foo"', '>content</div>'].join('\n'),
1040+
output: null,
1041+
options: [{ 'process-elements': true }],
1042+
errors: [{ messageId: 'incorrectClosingTag' }],
1043+
},
10371044
],
10381045
});

0 commit comments

Comments
 (0)