-
-
Notifications
You must be signed in to change notification settings - Fork 48
feat: add svelte/html-self-closing
#190
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
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
39a1243
feat: add `svelte/html-self-closing`
marekvospel 60a23c9
chore: requested changes
marekvospel 4c61301
fix: test not passing, lint, edit rule docs
marekvospel fd98f59
Merge branch 'ota-meshi:main' into add-html-self-closing
marekvospel a07898b
chore(html-self-closing): move options from html object, move utils t…
marekvospel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
--- | ||
pageClass: "rule-details" | ||
sidebarDepth: 0 | ||
title: "svelte/html-self-closing" | ||
description: "enforce self-closing style" | ||
--- | ||
|
||
# svelte/html-self-closing | ||
|
||
> enforce self-closing style | ||
|
||
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> **_This rule has not been released yet._** </badge> | ||
- :wrench: The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can automatically fix some of the problems reported by this rule. | ||
|
||
## :book: Rule Details | ||
|
||
You can choose either two styles for elements without content | ||
|
||
- always: `<div />` | ||
- never: `<div></div>` | ||
|
||
<ESLintCodeBlock fix> | ||
|
||
<!-- prettier-ignore-start --> | ||
<!--eslint-skip--> | ||
|
||
```svelte | ||
<script> | ||
/* eslint svelte/html-self-closing: "error" */ | ||
</script> | ||
|
||
<!-- ✓ GOOD --> | ||
<div /> | ||
<p>Hello</p> | ||
<div><div /></div> | ||
<img> | ||
|
||
<!-- ✗ BAD --> | ||
<div></div> | ||
<p> </p> | ||
<div><div></div></div> | ||
<img /> | ||
``` | ||
|
||
<!-- prettier-ignore-end --> | ||
|
||
</ESLintCodeBlock> | ||
|
||
## :wrench: Options | ||
|
||
```json | ||
{ | ||
"svelte/html-self-closing": [ | ||
"error", | ||
{ | ||
"html": { | ||
"void": "always", // or "always" or "ignore" | ||
"normal": "always", // or "never" or "ignore" | ||
"component": "always" // or "never" or "ignore" | ||
} | ||
} | ||
] | ||
} | ||
``` | ||
|
||
- `html.void` (`"always"` by default)... Style of HTML void elements | ||
- `html.component` (`"always"` by default)... Style of svelte components | ||
- `html.normal` (`"always"` by default)... Style of other elements | ||
marekvospel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Every option can be set to | ||
- "always" (`<div />`) | ||
- "never" (`<div></div>`) | ||
- "ignore" (either `<div />` or `<div></div>`) | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/ota-meshi/eslint-plugin-svelte/blob/main/src/rules/html-self-closing.ts) | ||
- [Test source](https://github.com/ota-meshi/eslint-plugin-svelte/blob/main/tests/src/rules/html-self-closing.ts) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
import type { AST } from "svelte-eslint-parser" | ||
import { createRule } from "../utils" | ||
import { | ||
getNodeName, | ||
isCustomComponent, | ||
isVoidHtmlElement, | ||
} from "../utils/template-utils" | ||
|
||
const TYPE_MESSAGES = { | ||
normal: "HTML elements", | ||
void: "HTML void elements", | ||
component: "Svelte custom components", | ||
} | ||
|
||
type ElementTypes = "normal" | "void" | "component" | ||
|
||
export default createRule("html-self-closing", { | ||
meta: { | ||
docs: { | ||
description: "enforce self-closing style", | ||
category: "Stylistic Issues", | ||
recommended: false, | ||
conflictWithPrettier: true, | ||
}, | ||
type: "layout", | ||
fixable: "code", | ||
messages: { | ||
requireClosing: "Require self-closing on {{type}}.", | ||
disallowClosing: "Disallow self-closing on {{type}}.", | ||
}, | ||
schema: [ | ||
{ | ||
type: "object", | ||
properties: { | ||
html: { | ||
type: "object", | ||
properties: { | ||
void: { | ||
enum: ["never", "always", "ignore"], | ||
}, | ||
normal: { | ||
enum: ["never", "always", "ignore"], | ||
}, | ||
component: { | ||
enum: ["never", "always", "ignore"], | ||
}, | ||
}, | ||
additionalProperties: false, | ||
}, | ||
}, | ||
additionalProperties: false, | ||
}, | ||
], | ||
}, | ||
create(ctx) { | ||
const options = { | ||
html: { | ||
void: "always", | ||
normal: "always", | ||
component: "always", | ||
}, | ||
...ctx.options?.[0], | ||
} | ||
marekvospel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* Get SvelteElement type. | ||
* If element is custom component "component" is returned | ||
* If element is void element "void" is returned | ||
* otherwise "normal" is returned | ||
*/ | ||
function getElementType(node: AST.SvelteElement): ElementTypes { | ||
if (isCustomComponent(node)) return "component" | ||
if (isVoidHtmlElement(node)) return "void" | ||
return "normal" | ||
} | ||
|
||
/** | ||
* Returns true if element has no children, or has only whitespace text | ||
*/ | ||
function isElementEmpty(node: AST.SvelteElement): boolean { | ||
if (node.children.length <= 0) return true | ||
|
||
for (const child of node.children) { | ||
if (child.type !== "SvelteText") return false | ||
if (!/^\s*$/.test(child.value)) return false | ||
} | ||
|
||
return true | ||
} | ||
|
||
/** | ||
* Report | ||
*/ | ||
function report(node: AST.SvelteElement, close: boolean) { | ||
const elementType = getElementType(node) | ||
|
||
ctx.report({ | ||
node, | ||
messageId: close ? "requireClosing" : "disallowClosing", | ||
data: { | ||
type: TYPE_MESSAGES[elementType], | ||
}, | ||
*fix(fixer) { | ||
if (close) { | ||
for (const child of node.children) { | ||
yield fixer.removeRange(child.range) | ||
} | ||
|
||
yield fixer.insertTextBeforeRange( | ||
[node.startTag.range[1] - 1, node.startTag.range[1]], | ||
"/", | ||
) | ||
|
||
if (node.endTag) yield fixer.removeRange(node.endTag.range) | ||
} else { | ||
yield fixer.removeRange([ | ||
node.startTag.range[1] - 2, | ||
node.startTag.range[1] - 1, | ||
]) | ||
|
||
if (!isVoidHtmlElement(node)) | ||
yield fixer.insertTextAfter(node, `</${getNodeName(node)}>`) | ||
} | ||
}, | ||
}) | ||
} | ||
|
||
return { | ||
SvelteElement(node: AST.SvelteElement) { | ||
if (!isElementEmpty(node)) return | ||
|
||
const elementType = getElementType(node) | ||
|
||
const elementTypeOptions = options.html[elementType] | ||
if (elementTypeOptions === "ignore") return | ||
const shouldBeClosed = elementTypeOptions === "always" | ||
|
||
if (shouldBeClosed && !node.startTag.selfClosing) { | ||
report(node, true) | ||
} else if (!shouldBeClosed && node.startTag.selfClosing) { | ||
report(node, false) | ||
} | ||
}, | ||
} | ||
}, | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import type { AST } from "svelte-eslint-parser" | ||
marekvospel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import voidElements from "./void-elements" | ||
|
||
/** | ||
* Returns name of SvelteElement | ||
*/ | ||
export function getNodeName(node: AST.SvelteElement): string { | ||
return "name" in node.name ? node.name.name : node.name.property.name | ||
marekvospel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/** | ||
* Returns true if Element is custom component | ||
*/ | ||
export function isCustomComponent(node: AST.SvelteElement): boolean { | ||
return node.kind === "component" | ||
} | ||
marekvospel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* Returns true if element is known void element | ||
* {@link https://developer.mozilla.org/en-US/docs/Glossary/Empty_element} | ||
*/ | ||
export function isVoidHtmlElement(node: AST.SvelteElement): boolean { | ||
return voidElements.includes(getNodeName(node)) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const voidElements = [ | ||
"area", | ||
"base", | ||
"br", | ||
"col", | ||
"embed", | ||
"hr", | ||
"img", | ||
"input", | ||
"keygen", | ||
"link", | ||
"menuitem", | ||
"meta", | ||
"param", | ||
"source", | ||
"track", | ||
"wbr", | ||
] | ||
|
||
export default voidElements |
9 changes: 9 additions & 0 deletions
9
tests/fixtures/rules/html-self-closing/invalid/component-never/_config.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"options": [ | ||
{ | ||
"html": { | ||
"component": "never" | ||
} | ||
} | ||
] | ||
} |
7 changes: 7 additions & 0 deletions
7
tests/fixtures/rules/html-self-closing/invalid/component-never/component-never-errors.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[ | ||
{ | ||
"message": "Disallow self-closing on Svelte custom components.", | ||
"line": 3, | ||
"column": 3 | ||
} | ||
] |
4 changes: 4 additions & 0 deletions
4
tests/fixtures/rules/html-self-closing/invalid/component-never/component-never-input.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<!-- prettier-ignore --> | ||
<div> | ||
<CustomElement /> | ||
</div> |
4 changes: 4 additions & 0 deletions
4
tests/fixtures/rules/html-self-closing/invalid/component-never/component-never-output.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<!-- prettier-ignore --> | ||
<div> | ||
<CustomElement ></CustomElement> | ||
</div> |
9 changes: 9 additions & 0 deletions
9
tests/fixtures/rules/html-self-closing/invalid/normal-ignore/_config.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"options": [ | ||
{ | ||
"html": { | ||
"normal": "ignore" | ||
} | ||
} | ||
] | ||
} |
7 changes: 7 additions & 0 deletions
7
tests/fixtures/rules/html-self-closing/invalid/normal-ignore/normal-any-errors.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[ | ||
{ | ||
"message": "Disallow self-closing on HTML void elements.", | ||
"line": 5, | ||
"column": 3 | ||
} | ||
] |
6 changes: 6 additions & 0 deletions
6
tests/fixtures/rules/html-self-closing/invalid/normal-ignore/normal-any-input.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<!-- prettier-ignore --> | ||
<div> | ||
<div /> | ||
<div></div> | ||
<img /> | ||
</div> |
6 changes: 6 additions & 0 deletions
6
tests/fixtures/rules/html-self-closing/invalid/normal-ignore/normal-any-output.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<!-- prettier-ignore --> | ||
<div> | ||
<div /> | ||
<div></div> | ||
<img > | ||
</div> |
9 changes: 9 additions & 0 deletions
9
tests/fixtures/rules/html-self-closing/invalid/normal-never/_config.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"options": [ | ||
{ | ||
"html": { | ||
"normal": "never" | ||
} | ||
} | ||
] | ||
} |
7 changes: 7 additions & 0 deletions
7
tests/fixtures/rules/html-self-closing/invalid/normal-never/component-never-errors.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[ | ||
{ | ||
"message": "Disallow self-closing on HTML elements.", | ||
"line": 3, | ||
"column": 3 | ||
} | ||
] |
4 changes: 4 additions & 0 deletions
4
tests/fixtures/rules/html-self-closing/invalid/normal-never/component-never-input.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<!-- prettier-ignore --> | ||
<div> | ||
<div /> | ||
</div> |
4 changes: 4 additions & 0 deletions
4
tests/fixtures/rules/html-self-closing/invalid/normal-never/component-never-output.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<!-- prettier-ignore --> | ||
<div> | ||
<div ></div> | ||
</div> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes the template less html compliant,
div
is not self-closing in HTML, we should have three options:'all' | 'html-compliant' | 'none'
, and'html-compliant'
should be default IMO.Of course the options naming can be better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should make the default of
prettier-plugin-svelte
the default behavior rather than the HTML standard. So I thinkall
should be the default.prettier-plugin-svelte
has long been the default style for the community.Turn off these rules when users use plugins with
prettier-plugin-svelte
, but I think it makes sense to adopt the same style asprettier-plugin-svelte
by default.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the current options can already do the same. Why do you think we need three options?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ota-meshi See also sveltejs/prettier-plugin-svelte#284
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh. I didn't know that.
Hmm. The maintainers don't seem to be involved in the discussion, so I'm still not sure if they're thinking of doing so by default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know, I'm just saying we can have such
single
option, maybe.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good idea, but I'd also keep the current configuration in overrides section, because there could be someone who only wants self closing on tags in specific category, and it would be waste to just throw away the option to set that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@marekvospel Do you currently have time to work on this PR?
I think the new single option is that we can add features later, so if you don't have time now, after merging this PR, we can work on a new PR when you/we have time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ota-meshi no, I'll have time in 2 weeks or later. I'll create a new PR when I have time
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay! I will merge this PR. Thank you for the great job!