Skip to content

feat: improve reporting range for svelte/html-self-closing rule. #296

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 3, 2022
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
5 changes: 5 additions & 0 deletions .changeset/poor-books-raise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-svelte": patch
---

feat: improve reporting range for `svelte/html-self-closing` rule.
20 changes: 14 additions & 6 deletions src/rules/html-self-closing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ export default createRule("html-self-closing", {
},
],
},
create(ctx) {
create(context) {
let options = {
void: "always",
normal: "always",
component: "always",
svelte: "always",
}

const option = ctx.options?.[0]
const option = context.options?.[0]
switch (option) {
case "none":
options = {
Expand Down Expand Up @@ -120,17 +120,25 @@ export default createRule("html-self-closing", {
/**
* Report
*/
function report(node: AST.SvelteElement, close: boolean) {
function report(node: AST.SvelteElement, shouldBeClosed: boolean) {
const elementType = getElementType(node)

ctx.report({
context.report({
node,
messageId: close ? "requireClosing" : "disallowClosing",
loc: {
start: context
.getSourceCode()
.getLocFromIndex(
node.startTag.range[1] - (node.startTag.selfClosing ? 2 : 1),
),
end: node.loc.end,
},
messageId: shouldBeClosed ? "requireClosing" : "disallowClosing",
data: {
type: TYPE_MESSAGES[elementType],
},
*fix(fixer) {
if (close) {
if (shouldBeClosed) {
for (const child of node.children) {
yield fixer.removeRange(child.range)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
- message: Disallow self-closing on Svelte custom components.
line: 3
column: 3
column: 18
suggestions: null
- message: Disallow self-closing on Svelte custom components.
line: 4
column: 3
column: 15
suggestions: null
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
- message: Require self-closing on HTML void elements.
line: 5
column: 3
column: 7
suggestions: null
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
- message: Disallow self-closing on HTML elements.
line: 3
column: 3
column: 8
suggestions: null

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
- message: Disallow self-closing on HTML elements.
line: 3
column: 3
column: 8
suggestions: null
- message: Require self-closing on HTML void elements.
line: 4
column: 3
column: 7
suggestions: null
- message: Disallow self-closing on Svelte custom components.
line: 5
column: 3
column: 18
suggestions: null
- message: Require self-closing on Svelte special elements.
line: 8
column: 1
column: 13
suggestions: null

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
- message: Disallow self-closing on HTML elements.
line: 3
column: 3
column: 8
suggestions: null
- message: Disallow self-closing on Svelte custom components.
line: 4
column: 3
column: 18
suggestions: null
- message: Disallow self-closing on HTML void elements.
line: 5
column: 3
column: 8
suggestions: null
- message: Disallow self-closing on Svelte special elements.
line: 8
column: 1
column: 14
suggestions: null
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
- message: Disallow self-closing on Svelte special elements.
line: 2
column: 1
column: 14
suggestions: null
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
- message: Require self-closing on HTML elements.
line: 3
column: 3
column: 7
suggestions: null
- message: Require self-closing on Svelte custom components.
line: 4
column: 3
column: 17
suggestions: null
- message: Require self-closing on HTML void elements.
line: 5
column: 3
column: 7
suggestions: null
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
- message: Disallow self-closing on HTML void elements.
line: 3
column: 3
column: 8
suggestions: null