Skip to content

Commit 9a49b93

Browse files
committed
feat: improve html-self-closing to report range
1 parent bfd79f8 commit 9a49b93

File tree

11 files changed

+31
-67
lines changed

11 files changed

+31
-67
lines changed

src/rules/html-self-closing.ts

+14-6
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,15 @@ export default createRule("html-self-closing", {
5252
},
5353
],
5454
},
55-
create(ctx) {
55+
create(context) {
5656
let options = {
5757
void: "always",
5858
normal: "always",
5959
component: "always",
6060
svelte: "always",
6161
}
6262

63-
const option = ctx.options?.[0]
63+
const option = context.options?.[0]
6464
switch (option) {
6565
case "none":
6666
options = {
@@ -120,17 +120,25 @@ export default createRule("html-self-closing", {
120120
/**
121121
* Report
122122
*/
123-
function report(node: AST.SvelteElement, close: boolean) {
123+
function report(node: AST.SvelteElement, shouldBeClosed: boolean) {
124124
const elementType = getElementType(node)
125125

126-
ctx.report({
126+
context.report({
127127
node,
128-
messageId: close ? "requireClosing" : "disallowClosing",
128+
loc: {
129+
start: context
130+
.getSourceCode()
131+
.getLocFromIndex(
132+
node.startTag.range[1] - (node.startTag.selfClosing ? 2 : 1),
133+
),
134+
end: node.loc.end,
135+
},
136+
messageId: shouldBeClosed ? "requireClosing" : "disallowClosing",
129137
data: {
130138
type: TYPE_MESSAGES[elementType],
131139
},
132140
*fix(fixer) {
133-
if (close) {
141+
if (shouldBeClosed) {
134142
for (const child of node.children) {
135143
yield fixer.removeRange(child.range)
136144
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
- message: Disallow self-closing on Svelte custom components.
22
line: 3
3-
column: 3
3+
column: 18
44
suggestions: null
55
- message: Disallow self-closing on Svelte custom components.
66
line: 4
7-
column: 3
7+
column: 15
88
suggestions: null
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
- message: Require self-closing on HTML void elements.
22
line: 5
3-
column: 3
3+
column: 7
44
suggestions: null
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
- message: Disallow self-closing on HTML elements.
22
line: 3
3-
column: 3
3+
column: 8
44
suggestions: null

tests/fixtures/rules/html-self-closing/invalid/presets/html/preset-html-errors.json

-22
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
- message: Disallow self-closing on HTML elements.
22
line: 3
3-
column: 3
3+
column: 8
44
suggestions: null
55
- message: Require self-closing on HTML void elements.
66
line: 4
7-
column: 3
7+
column: 7
88
suggestions: null
99
- message: Disallow self-closing on Svelte custom components.
1010
line: 5
11-
column: 3
11+
column: 18
1212
suggestions: null
1313
- message: Require self-closing on Svelte special elements.
1414
line: 8
15-
column: 1
15+
column: 13
1616
suggestions: null

tests/fixtures/rules/html-self-closing/invalid/presets/none/preset-none-errors.json

-22
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
- message: Disallow self-closing on HTML elements.
22
line: 3
3-
column: 3
3+
column: 8
44
suggestions: null
55
- message: Disallow self-closing on Svelte custom components.
66
line: 4
7-
column: 3
7+
column: 18
88
suggestions: null
99
- message: Disallow self-closing on HTML void elements.
1010
line: 5
11-
column: 3
11+
column: 8
1212
suggestions: null
1313
- message: Disallow self-closing on Svelte special elements.
1414
line: 8
15-
column: 1
15+
column: 14
1616
suggestions: null
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
- message: Disallow self-closing on Svelte special elements.
22
line: 2
3-
column: 1
3+
column: 14
44
suggestions: null
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
- message: Require self-closing on HTML elements.
22
line: 3
3-
column: 3
3+
column: 7
44
suggestions: null
55
- message: Require self-closing on Svelte custom components.
66
line: 4
7-
column: 3
7+
column: 17
88
suggestions: null
99
- message: Require self-closing on HTML void elements.
1010
line: 5
11-
column: 3
11+
column: 7
1212
suggestions: null
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
- message: Disallow self-closing on HTML void elements.
22
line: 3
3-
column: 3
3+
column: 8
44
suggestions: null

0 commit comments

Comments
 (0)