Skip to content

feat(html-self-closing): add configuration presets #216

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 29 commits into from
Aug 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
24e28b5
feat(html-self-closing)!: add configuration presets
marekvospel Aug 11, 2022
4dfbc56
fix(html-self-closing): type errors
marekvospel Aug 11, 2022
697db5b
fix(html-self-closing): typo
marekvospel Aug 11, 2022
946e406
fix(html-self-closing): lint error
marekvospel Aug 11, 2022
f4a83a5
test(html-self-closing): migrate test configs to new version
marekvospel Aug 11, 2022
18d2e98
test(html-self-closing): add preset tests
marekvospel Aug 11, 2022
627aef2
fix(html-self-closing): lint errors
marekvospel Aug 11, 2022
4a35c74
fix(html-self-closing): lint errors
marekvospel Aug 11, 2022
90e05ef
docs(html-self-closing): new confiuration
marekvospel Aug 11, 2022
89e4e15
feat(html-self-closing)!: make configuration [0] preset or override c…
marekvospel Aug 12, 2022
0eb6a5d
fix(html-self-closing): typo
marekvospel Aug 12, 2022
7e9ac9f
fix(html-self-closing): lint errors
marekvospel Aug 12, 2022
127144e
docs(html-self-closing): update configuration section
marekvospel Aug 12, 2022
ab0c9dd
chore(html-self-closing): reauested changes
marekvospel Aug 12, 2022
054baab
chore(html-self-closing): lint errors
marekvospel Aug 12, 2022
bad0e23
chore(html-self-closing): lint errors
marekvospel Aug 12, 2022
be083b4
fix(html-sef-closing): replace oneOf with AnyOf
marekvospel Aug 12, 2022
3cad987
test(html-sef-closing): generate test outputs for html preset test
marekvospel Aug 12, 2022
9ec3614
test(html-self-closing): test errors and outputs
marekvospel Aug 12, 2022
f94f01d
test(html-self-closing): fix preset test outputs
marekvospel Aug 12, 2022
f849461
test(html-self-closing): fix typo
marekvospel Aug 12, 2022
9b40fd0
test(html-self-closing): typo
marekvospel Aug 12, 2022
fa4350c
test(html-self-closing): typo
marekvospel Aug 12, 2022
562d44f
test(html-self-closing): typo
marekvospel Aug 12, 2022
f939691
test(html-self-closing) : seitched elements and componenta error loca…
marekvospel Aug 12, 2022
6fddc2e
test(html-self-closing): replace void components wth goid elements
marekvospel Aug 12, 2022
88cf5f6
tesr(html-self-closing): fix test outputs
marekvospel Aug 12, 2022
2d67419
chore: tiny changes
JounQin Aug 12, 2022
88ea618
Create swift-ads-type.md
ota-meshi Aug 12, 2022
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/swift-ads-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-svelte": minor
---

feat(html-self-closing): add configuration presets
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ jspm_packages/
# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache
# Optional eslint/stylelint cache, etc
.*cache

# Microbundle cache
.rpt2_cache/
Expand Down
31 changes: 24 additions & 7 deletions docs/rules/html-self-closing.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,26 +50,43 @@ You can choose either two styles for elements without content

## :wrench: Options

presets:
```jsonc
{
"svelte/html-self-closing": [
"error",
"all", // or "html" or "none"
]
}
```

config object:
```jsonc
{
"svelte/html-self-closing": [
"error",
{
"void": "always", // or "always" or "ignore"
"void": "always", // or "never" or "ignore"
"normal": "always", // or "never" or "ignore"
"component": "always", // or "never" or "ignore"
"svelte": "always" // or "never" or "ignore"
"svelte": "always" // or "never" or "igore"
}
]
}
```

- `void` (`"always"` by default)... Style of HTML void elements
- `component` (`"always"` by default)... Style of svelte components
- `svelte` (`"always"` by default)... Style of svelte special elements (`<svelte:head>`, `<svelte:self>`)
- `normal` (`"always"` by default)... Style of other elements
presets:
- `all` - all elements should be self closing (unless they have children)
- `html` - html-compliant - only void elements and svelte special elements should be self closing
- `none` - no elements should be self closing

config object:
- `void` (`"always"` in default preset)... Style of HTML void elements
- `component` (`"always"` in default preset)... Style of svelte components
- `svelte` (`"always"` in default preset)... Style of svelte special elements (`<svelte:head>`, `<svelte:self>`)
- `normal` (`"always"` in default preset)... Style of other elements

Every option can be set to
Every config oject option can be set to
- "always" (`<div />`)
- "never" (`<div></div>`)
- "ignore" (either `<div />` or `<div></div>`)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@
"access": "public"
},
"typeCoverage": {
"atLeast": 98.62,
"atLeast": 98.63,
"cache": true,
"detail": true,
"ignoreAsAssertion": true,
Expand Down
66 changes: 50 additions & 16 deletions src/rules/html-self-closing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,66 @@ export default createRule("html-self-closing", {
},
schema: [
{
type: "object",
properties: {
void: {
enum: ["never", "always", "ignore"],
anyOf: [
{
properties: {
void: {
enum: ["never", "always", "ignore"],
},
normal: {
enum: ["never", "always", "ignore"],
},
component: {
enum: ["never", "always", "ignore"],
},
svelte: {
enum: ["never", "always", "ignore"],
},
},
additionalProperties: false,
},
normal: {
enum: ["never", "always", "ignore"],
{
enum: ["all", "html", "none"],
},
component: {
enum: ["never", "always", "ignore"],
},
svelte: {
enum: ["never", "always", "ignore"],
},
},
additionalProperties: false,
],
},
],
},
create(ctx) {
const options = {
let options = {
void: "always",
normal: "always",
component: "always",
svelte: "always",
...ctx.options?.[0],
}

const option = ctx.options?.[0]
switch (option) {
case "none":
options = {
void: "never",
normal: "never",
component: "never",
svelte: "never",
}
break
case "html":
options = {
void: "always",
normal: "never",
component: "never",
svelte: "always",
}
break
default:
if (typeof option !== "object" || option === null) break

options = {
...options,
...option,
}

break
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"options": ["html"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[
{
"message": "Disallow self-closing on HTML elements.",
"line": 3,
"column": 3
},
{
"message": "Require self-closing on HTML void elements.",
"line": 4,
"column": 3
},
{
"message": "Disallow self-closing on Svelte custom components.",
"line": 5,
"column": 3
},
{
"message": "Require self-closing on Svelte special elements.",
"line": 8,
"column": 1
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!-- prettier-ignore -->
<div>
<div />
<img>
<TestComponent />
</div>
<!-- prettier-ignore -->
<svelte:head></svelte:head>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!-- prettier-ignore -->
<div>
<div ></div>
<img/>
<TestComponent ></TestComponent>
</div>
<!-- prettier-ignore -->
<svelte:head/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"options": ["none"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[
{
"message": "Disallow self-closing on HTML elements.",
"line": 3,
"column": 3
},
{
"message": "Disallow self-closing on Svelte custom components.",
"line": 4,
"column": 3
},
{
"message": "Disallow self-closing on HTML void elements.",
"line": 5,
"column": 3
},
{
"message": "Disallow self-closing on Svelte special elements.",
"line": 8,
"column": 1
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!-- prettier-ignore -->
<div>
<div />
<TestComponent />
<img />
</div>
<!-- prettier-ignore -->
<svelte:head />
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!-- prettier-ignore -->
<div>
<div ></div>
<TestComponent ></TestComponent>
<img >
</div>
<!-- prettier-ignore -->
<svelte:head ></svelte:head>