Skip to content

Commit 0bf977c

Browse files
committed
Address PR Feedback
1 parent cab476d commit 0bf977c

2 files changed

Lines changed: 75 additions & 0 deletions

File tree

lib/rules/template-attribute-indentation.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ module.exports = {
132132
"Incorrect indentation of close bracket '>' for the element '<{{tagName}}>' beginning at L{{actualLine}}:C{{actualColumn}}. Expected '<{{tagName}}>' to be at L{{expectedLine}}:C{{expectedColumn}}.",
133133
incorrectBlockParamIndentation:
134134
"Incorrect indentation of block params '{{blockParamStatement}}' beginning at L{{actualLine}}:C{{actualColumn}}. Expecting the block params to be at L{{expectedLine}}:C{{expectedColumn}}.",
135+
incorrectClosingTag:
136+
"Incorrect indentation of close tag '</{{tagName}}>' for element '<{{tagName}}>' beginning at L{{actualLine}}:C{{actualColumn}}. Expected '</{{tagName}}>' to be at L{{expectedLine}}:C{{expectedColumn}}.",
135137
},
136138
originallyFrom: {
137139
name: 'ember-template-lint',
@@ -459,6 +461,29 @@ module.exports = {
459461
}
460462
}
461463

464+
function validateClosingTag(node) {
465+
// `</tag>` is `2 + tag.length + 1` chars: `</` + tag + `>`
466+
const actualColumnStartLocation = 3 + node.tag.length;
467+
const actualColumn = node.loc.end.column - actualColumnStartLocation;
468+
const expectedColumn = node.loc.start.column;
469+
470+
if (actualColumn !== expectedColumn) {
471+
const actualLine = node.loc.end.line;
472+
context.report({
473+
node,
474+
messageId: 'incorrectClosingTag',
475+
loc: { line: actualLine, column: actualColumn },
476+
data: {
477+
tagName: node.tag,
478+
actualLine,
479+
actualColumn,
480+
expectedLine: actualLine,
481+
expectedColumn,
482+
},
483+
});
484+
}
485+
}
486+
462487
function validateNonBlockForm(node) {
463488
if (node.params.length > 0 || (node.hash && node.hash.pairs.length > 0)) {
464489
const nextLocation = validateParams(node);
@@ -501,6 +526,10 @@ module.exports = {
501526
const expectedCloseBraceLocation = validateParams(node);
502527
validateCloseBrace(node, expectedCloseBraceLocation);
503528
}
529+
530+
if (node.children.length > 0) {
531+
validateClosingTag(node);
532+
}
504533
}
505534
}
506535
},

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -990,3 +990,49 @@ gjsRuleTester.run('template-attribute-indentation', rule, {
990990
},
991991
],
992992
});
993+
994+
// ---- Closing tag tests ----
995+
996+
hbsRuleTester.run('template-attribute-indentation (closing tag)', rule, {
997+
valid: [
998+
// Closing tag correctly aligned with opening tag, after text content
999+
{
1000+
code: ['<div', ' class="foo"', '>', ' content', '</div>'].join('\n'),
1001+
options: [{ 'process-elements': true }],
1002+
},
1003+
// Closing tag correctly aligned, after element child
1004+
{
1005+
code: ['<div', ' class="foo"', '>', ' <span>text</span>', '</div>'].join('\n'),
1006+
options: [{ 'process-elements': true }],
1007+
},
1008+
// Short element — canApplyRule returns false (single-line, under maxLength)
1009+
{
1010+
code: '<div class="foo">content</div>',
1011+
options: [{ 'process-elements': true }],
1012+
},
1013+
],
1014+
1015+
invalid: [
1016+
// Closing tag indented too far (wrong column)
1017+
{
1018+
code: ['<div', ' class="foo"', '>', ' content', ' </div>'].join('\n'),
1019+
output: null,
1020+
options: [{ 'process-elements': true }],
1021+
errors: [{ messageId: 'incorrectClosingTag' }],
1022+
},
1023+
// Closing tag on same line as content (wrong column)
1024+
{
1025+
code: ['<MyComponent', ' @arg="val"', '>text</MyComponent>'].join('\n'),
1026+
output: null,
1027+
options: [{ 'process-elements': true }],
1028+
errors: [{ messageId: 'incorrectClosingTag' }],
1029+
},
1030+
// Closing tag indented when it should be at column 0
1031+
{
1032+
code: ['<MyComponent', ' @arg="val"', '>', ' text', ' </MyComponent>'].join('\n'),
1033+
output: null,
1034+
options: [{ 'process-elements': true }],
1035+
errors: [{ messageId: 'incorrectClosingTag' }],
1036+
},
1037+
],
1038+
});

0 commit comments

Comments
 (0)