Skip to content

Commit

Permalink
feat: improve reporting range for svelte/html-self-closing rule. (#296
Browse files Browse the repository at this point in the history
)

* feat: improve html-self-closing to report range

* Create poor-books-raise.md
  • Loading branch information
ota-meshi committed Nov 3, 2022
1 parent bfd79f8 commit 695e2e5
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 67 deletions.
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

0 comments on commit 695e2e5

Please sign in to comment.