Skip to content

Commit 9d122ea

Browse files
marekvospelJounQinota-meshi
authored
feat(html-self-closing): add configuration presets (#216)
* feat(html-self-closing)!: add configuration presets * fix(html-self-closing): type errors * fix(html-self-closing): typo * fix(html-self-closing): lint error * test(html-self-closing): migrate test configs to new version * test(html-self-closing): add preset tests * fix(html-self-closing): lint errors * fix(html-self-closing): lint errors * docs(html-self-closing): new confiuration * feat(html-self-closing)!: make configuration [0] preset or override config * fix(html-self-closing): typo * fix(html-self-closing): lint errors * docs(html-self-closing): update configuration section * chore(html-self-closing): reauested changes * chore(html-self-closing): lint errors * chore(html-self-closing): lint errors * fix(html-sef-closing): replace oneOf with AnyOf * test(html-sef-closing): generate test outputs for html preset test * test(html-self-closing): test errors and outputs * test(html-self-closing): fix preset test outputs * test(html-self-closing): fix typo * test(html-self-closing): typo * test(html-self-closing): typo * test(html-self-closing): typo * test(html-self-closing) : seitched elements and componenta error location * test(html-self-closing): replace void components wth goid elements * tesr(html-self-closing): fix test outputs * chore: tiny changes * Create swift-ads-type.md Co-authored-by: JounQin <[email protected]> Co-authored-by: Yosuke Ota <[email protected]>
1 parent 7c8d053 commit 9d122ea

13 files changed

+164
-26
lines changed

.changeset/swift-ads-type.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-plugin-svelte": minor
3+
---
4+
5+
feat(html-self-closing): add configuration presets

.gitignore

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ jspm_packages/
5050
# Optional npm cache directory
5151
.npm
5252

53-
# Optional eslint cache
54-
.eslintcache
53+
# Optional eslint/stylelint cache, etc
54+
.*cache
5555

5656
# Microbundle cache
5757
.rpt2_cache/

docs/rules/html-self-closing.md

+24-7
Original file line numberDiff line numberDiff line change
@@ -50,26 +50,43 @@ You can choose either two styles for elements without content
5050

5151
## :wrench: Options
5252

53+
presets:
54+
```jsonc
55+
{
56+
"svelte/html-self-closing": [
57+
"error",
58+
"all", // or "html" or "none"
59+
]
60+
}
61+
```
62+
63+
config object:
5364
```jsonc
5465
{
5566
"svelte/html-self-closing": [
5667
"error",
5768
{
58-
"void": "always", // or "always" or "ignore"
69+
"void": "always", // or "never" or "ignore"
5970
"normal": "always", // or "never" or "ignore"
6071
"component": "always", // or "never" or "ignore"
61-
"svelte": "always" // or "never" or "ignore"
72+
"svelte": "always" // or "never" or "igore"
6273
}
6374
]
6475
}
6576
```
6677

67-
- `void` (`"always"` by default)... Style of HTML void elements
68-
- `component` (`"always"` by default)... Style of svelte components
69-
- `svelte` (`"always"` by default)... Style of svelte special elements (`<svelte:head>`, `<svelte:self>`)
70-
- `normal` (`"always"` by default)... Style of other elements
78+
presets:
79+
- `all` - all elements should be self closing (unless they have children)
80+
- `html` - html-compliant - only void elements and svelte special elements should be self closing
81+
- `none` - no elements should be self closing
82+
83+
config object:
84+
- `void` (`"always"` in default preset)... Style of HTML void elements
85+
- `component` (`"always"` in default preset)... Style of svelte components
86+
- `svelte` (`"always"` in default preset)... Style of svelte special elements (`<svelte:head>`, `<svelte:self>`)
87+
- `normal` (`"always"` in default preset)... Style of other elements
7188

72-
Every option can be set to
89+
Every config oject option can be set to
7390
- "always" (`<div />`)
7491
- "never" (`<div></div>`)
7592
- "ignore" (either `<div />` or `<div></div>`)

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@
171171
"access": "public"
172172
},
173173
"typeCoverage": {
174-
"atLeast": 98.62,
174+
"atLeast": 98.63,
175175
"cache": true,
176176
"detail": true,
177177
"ignoreAsAssertion": true,

src/rules/html-self-closing.ts

+50-16
Original file line numberDiff line numberDiff line change
@@ -27,32 +27,66 @@ export default createRule("html-self-closing", {
2727
},
2828
schema: [
2929
{
30-
type: "object",
31-
properties: {
32-
void: {
33-
enum: ["never", "always", "ignore"],
30+
anyOf: [
31+
{
32+
properties: {
33+
void: {
34+
enum: ["never", "always", "ignore"],
35+
},
36+
normal: {
37+
enum: ["never", "always", "ignore"],
38+
},
39+
component: {
40+
enum: ["never", "always", "ignore"],
41+
},
42+
svelte: {
43+
enum: ["never", "always", "ignore"],
44+
},
45+
},
46+
additionalProperties: false,
3447
},
35-
normal: {
36-
enum: ["never", "always", "ignore"],
48+
{
49+
enum: ["all", "html", "none"],
3750
},
38-
component: {
39-
enum: ["never", "always", "ignore"],
40-
},
41-
svelte: {
42-
enum: ["never", "always", "ignore"],
43-
},
44-
},
45-
additionalProperties: false,
51+
],
4652
},
4753
],
4854
},
4955
create(ctx) {
50-
const options = {
56+
let options = {
5157
void: "always",
5258
normal: "always",
5359
component: "always",
5460
svelte: "always",
55-
...ctx.options?.[0],
61+
}
62+
63+
const option = ctx.options?.[0]
64+
switch (option) {
65+
case "none":
66+
options = {
67+
void: "never",
68+
normal: "never",
69+
component: "never",
70+
svelte: "never",
71+
}
72+
break
73+
case "html":
74+
options = {
75+
void: "always",
76+
normal: "never",
77+
component: "never",
78+
svelte: "always",
79+
}
80+
break
81+
default:
82+
if (typeof option !== "object" || option === null) break
83+
84+
options = {
85+
...options,
86+
...option,
87+
}
88+
89+
break
5690
}
5791

5892
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"options": ["html"]
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[
2+
{
3+
"message": "Disallow self-closing on HTML elements.",
4+
"line": 3,
5+
"column": 3
6+
},
7+
{
8+
"message": "Require self-closing on HTML void elements.",
9+
"line": 4,
10+
"column": 3
11+
},
12+
{
13+
"message": "Disallow self-closing on Svelte custom components.",
14+
"line": 5,
15+
"column": 3
16+
},
17+
{
18+
"message": "Require self-closing on Svelte special elements.",
19+
"line": 8,
20+
"column": 1
21+
}
22+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<!-- prettier-ignore -->
2+
<div>
3+
<div />
4+
<img>
5+
<TestComponent />
6+
</div>
7+
<!-- prettier-ignore -->
8+
<svelte:head></svelte:head>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<!-- prettier-ignore -->
2+
<div>
3+
<div ></div>
4+
<img/>
5+
<TestComponent ></TestComponent>
6+
</div>
7+
<!-- prettier-ignore -->
8+
<svelte:head/>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"options": ["none"]
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[
2+
{
3+
"message": "Disallow self-closing on HTML elements.",
4+
"line": 3,
5+
"column": 3
6+
},
7+
{
8+
"message": "Disallow self-closing on Svelte custom components.",
9+
"line": 4,
10+
"column": 3
11+
},
12+
{
13+
"message": "Disallow self-closing on HTML void elements.",
14+
"line": 5,
15+
"column": 3
16+
},
17+
{
18+
"message": "Disallow self-closing on Svelte special elements.",
19+
"line": 8,
20+
"column": 1
21+
}
22+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<!-- prettier-ignore -->
2+
<div>
3+
<div />
4+
<TestComponent />
5+
<img />
6+
</div>
7+
<!-- prettier-ignore -->
8+
<svelte:head />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<!-- prettier-ignore -->
2+
<div>
3+
<div ></div>
4+
<TestComponent ></TestComponent>
5+
<img >
6+
</div>
7+
<!-- prettier-ignore -->
8+
<svelte:head ></svelte:head>

0 commit comments

Comments
 (0)