-
-
Notifications
You must be signed in to change notification settings - Fork 48
feat: add svelte/no-restricted-html-elements
rule
#499
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 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6f1cc06
implement rule
baseballyama 966237c
add test
baseballyama 65c1381
add changeset
baseballyama a8d4bbb
update config
baseballyama 56b8cc9
run update command
baseballyama cf80a29
flatten config
baseballyama 12f38f7
change category
baseballyama 721a532
run update cmd
baseballyama e4a872c
commit forgot file
baseballyama 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"eslint-plugin-svelte": minor | ||
--- | ||
|
||
feat: add `no-restricted-html-elements` rule |
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,107 @@ | ||
--- | ||
pageClass: "rule-details" | ||
sidebarDepth: 0 | ||
title: "svelte/no-restricted-html-elements" | ||
description: "disallow specific HTML elements" | ||
--- | ||
|
||
# svelte/no-restricted-html-elements | ||
|
||
> disallow specific HTML elements | ||
|
||
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> **_This rule has not been released yet._** </badge> | ||
|
||
## :book: Rule Details | ||
|
||
This rule reports to usage of resticted HTML elements. | ||
|
||
<ESLintCodeBlock> | ||
|
||
<!--eslint-skip--> | ||
|
||
```svelte | ||
<script> | ||
/* eslint svelte/no-restricted-html-elements: ["error", "h1", "h2", "h3", "h4", "h5", "h6"] */ | ||
</script> | ||
|
||
<!-- ✓ GOOD --> | ||
<div> | ||
<p>Hi!</p> | ||
</div> | ||
|
||
<!-- ✗ BAD --> | ||
<h1>foo</h1> | ||
|
||
<div> | ||
<h2>bar</h2> | ||
</div> | ||
``` | ||
|
||
</ESLintCodeBlock> | ||
|
||
--- | ||
|
||
<ESLintCodeBlock> | ||
|
||
<!--eslint-skip--> | ||
|
||
```svelte | ||
<script> | ||
/* eslint svelte/no-restricted-html-elements: ["error", { "elements": ["marquee"], "message": "Do not use deprecated HTML tags" }] */ | ||
</script> | ||
|
||
<!-- ✓ GOOD --> | ||
<div> | ||
<p>Hi!</p> | ||
</div> | ||
|
||
<!-- ✗ BAD --> | ||
<marquee>foo</marquee> | ||
|
||
<div> | ||
<marquee>bar</marquee> | ||
</div> | ||
``` | ||
|
||
</ESLintCodeBlock> | ||
|
||
## :wrench: Options | ||
|
||
This rule takes a list of strings, where each string is an HTML element name to be restricted: | ||
|
||
```json | ||
{ | ||
"svelte/no-restricted-html-elements": [ | ||
"error", | ||
"h1", | ||
"h2", | ||
"h3", | ||
"h4", | ||
"h5", | ||
"h6" | ||
] | ||
} | ||
``` | ||
|
||
Alternatively, the rule also accepts objects. | ||
|
||
```json | ||
{ | ||
"svelte/no-restricted-html-elements": [ | ||
"error", | ||
{ | ||
"elements": "h1", "h2", "h3", "h4", "h5", "h6", | ||
"message": "Prefer use of our custom <Heading /> component" | ||
}, | ||
{ | ||
"elements": ["marquee"], | ||
"message": "Do not use deprecated HTML tags" | ||
} | ||
] | ||
} | ||
``` | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/sveltejs/eslint-plugin-svelte/blob/main/src/rules/no-restricted-html-elements.ts) | ||
- [Test source](https://github.com/sveltejs/eslint-plugin-svelte/blob/main/tests/src/rules/no-restricted-html-elements.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { createRule } from "../utils" | ||
|
||
export default createRule("no-restricted-html-elements", { | ||
meta: { | ||
docs: { | ||
description: "disallow specific HTML elements", | ||
category: "Stylistic Issues", | ||
recommended: false, | ||
conflictWithPrettier: false, | ||
}, | ||
schema: { | ||
type: "array", | ||
items: { | ||
oneOf: [ | ||
{ type: "string" }, | ||
{ | ||
type: "object", | ||
properties: { | ||
elements: { | ||
type: "array", | ||
items: { | ||
type: ["string"], | ||
}, | ||
uniqueItems: true, | ||
minItems: 1, | ||
}, | ||
message: { type: "string", minLength: 1 }, | ||
}, | ||
additionalProperties: false, | ||
minItems: 1, | ||
}, | ||
], | ||
}, | ||
uniqueItems: true, | ||
minItems: 1, | ||
}, | ||
messages: {}, | ||
type: "suggestion", | ||
}, | ||
create(context) { | ||
return { | ||
SvelteElement(node) { | ||
if (node.kind !== "html") return | ||
const { name } = node | ||
if (name.type !== "SvelteName") return | ||
for (const option of context.options) { | ||
const message = | ||
option.message || | ||
`Unexpected use of forbidden HTML element ${name.name}.` | ||
const elements = option.elements || [option] | ||
for (const element of elements) { | ||
if (element === name.name) { | ||
context.report({ | ||
message, | ||
node: node.startTag, | ||
}) | ||
} | ||
} | ||
} | ||
}, | ||
} | ||
}, | ||
}) |
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
3 changes: 3 additions & 0 deletions
3
tests/fixtures/rules/no-restricted-html-elements/invalid/array-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,3 @@ | ||
{ | ||
"options": ["h1", "h2", "h3", "h4", "h5", "h6"] | ||
} |
24 changes: 24 additions & 0 deletions
24
tests/fixtures/rules/no-restricted-html-elements/invalid/array-errors.yaml
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 @@ | ||
- message: Unexpected use of forbidden HTML element h1. | ||
line: 1 | ||
column: 1 | ||
suggestions: null | ||
- message: Unexpected use of forbidden HTML element h2. | ||
line: 2 | ||
column: 1 | ||
suggestions: null | ||
- message: Unexpected use of forbidden HTML element h3. | ||
line: 3 | ||
column: 1 | ||
suggestions: null | ||
- message: Unexpected use of forbidden HTML element h4. | ||
line: 4 | ||
column: 1 | ||
suggestions: null | ||
- message: Unexpected use of forbidden HTML element h5. | ||
line: 5 | ||
column: 1 | ||
suggestions: null | ||
- message: Unexpected use of forbidden HTML element h6. | ||
line: 6 | ||
column: 1 | ||
suggestions: null |
6 changes: 6 additions & 0 deletions
6
tests/fixtures/rules/no-restricted-html-elements/invalid/array-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 @@ | ||
<h1>Main Title - H1</h1> | ||
<h2>Subtitle - H2</h2> | ||
<h3>Subsection Title - H3</h3> | ||
<h4>Sub-Subsection Title - H4</h4> | ||
<h5>Minor Title - H5</h5> | ||
<h6>Minor Subtitle - H6</h6> |
12 changes: 12 additions & 0 deletions
12
tests/fixtures/rules/no-restricted-html-elements/invalid/object-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,12 @@ | ||
{ | ||
"options": [ | ||
{ | ||
"elements": ["h1", "h2", "h3", "h4", "h5", "h6"], | ||
"message": "Prefer use of our custom <Heading /> component" | ||
}, | ||
{ | ||
"elements": ["marquee"], | ||
"message": "Do not use deprecated HTML tags" | ||
} | ||
] | ||
} |
48 changes: 48 additions & 0 deletions
48
tests/fixtures/rules/no-restricted-html-elements/invalid/object-errors.yaml
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,48 @@ | ||
- message: Prefer use of our custom <Heading /> component | ||
line: 1 | ||
column: 1 | ||
suggestions: null | ||
- message: Prefer use of our custom <Heading /> component | ||
line: 2 | ||
column: 1 | ||
suggestions: null | ||
- message: Prefer use of our custom <Heading /> component | ||
line: 3 | ||
column: 1 | ||
suggestions: null | ||
- message: Prefer use of our custom <Heading /> component | ||
line: 4 | ||
column: 1 | ||
suggestions: null | ||
- message: Prefer use of our custom <Heading /> component | ||
line: 5 | ||
column: 1 | ||
suggestions: null | ||
- message: Prefer use of our custom <Heading /> component | ||
line: 6 | ||
column: 1 | ||
suggestions: null | ||
- message: Do not use deprecated HTML tags | ||
line: 8 | ||
column: 1 | ||
suggestions: null | ||
- message: Do not use deprecated HTML tags | ||
line: 10 | ||
column: 1 | ||
suggestions: null | ||
- message: Do not use deprecated HTML tags | ||
line: 12 | ||
column: 1 | ||
suggestions: null | ||
- message: Do not use deprecated HTML tags | ||
line: 19 | ||
column: 3 | ||
suggestions: null | ||
- message: Do not use deprecated HTML tags | ||
line: 23 | ||
column: 3 | ||
suggestions: null | ||
- message: Prefer use of our custom <Heading /> component | ||
line: 27 | ||
column: 3 | ||
suggestions: null |
28 changes: 28 additions & 0 deletions
28
tests/fixtures/rules/no-restricted-html-elements/invalid/object-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,28 @@ | ||
<h1>Main Title - H1</h1> | ||
<h2>Subtitle - H2</h2> | ||
<h3>Subsection Title - H3</h3> | ||
<h4>Sub-Subsection Title - H4</h4> | ||
<h5>Minor Title - H5</h5> | ||
<h6>Minor Subtitle - H6</h6> | ||
|
||
<marquee>This text will scroll from right to left</marquee> | ||
|
||
<marquee direction="up">This text will scroll from bottom to top</marquee> | ||
|
||
<marquee | ||
direction="down" | ||
width="250" | ||
height="200" | ||
behavior="alternate" | ||
style="border:solid" | ||
> | ||
<marquee behavior="alternate"> This text will bounce </marquee> | ||
</marquee> | ||
|
||
<div> | ||
<marquee>This text will scroll from right to left</marquee> | ||
</div> | ||
|
||
<div> | ||
<h6>Minor Subtitle - H6</h6> | ||
</div> |
3 changes: 3 additions & 0 deletions
3
tests/fixtures/rules/no-restricted-html-elements/valid/array-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,3 @@ | ||
{ | ||
"options": ["h1", "h2", "h3", "h4", "h5", "h6"] | ||
} |
23 changes: 23 additions & 0 deletions
23
tests/fixtures/rules/no-restricted-html-elements/valid/array-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,23 @@ | ||
<div style="font-size: 2em; font-weight: bold;">This is a title</div> | ||
<div style="font-size: 1.5em; font-weight: bold;">This is a subtitle</div> | ||
<p>This is a paragraph. Some sample text goes here.</p> | ||
<ul> | ||
<li>This is</li> | ||
<li>a list item</li> | ||
</ul> | ||
<table> | ||
<tr> | ||
<td>Table Cell 1</td> | ||
<td>Table Cell 2</td> | ||
</tr> | ||
<tr> | ||
<td>Table Cell 3</td> | ||
<td>Table Cell 4</td> | ||
</tr> | ||
</table> | ||
<a href="https://example.com">This is a hyperlink to example.com</a> | ||
<form> | ||
<label for="fname">First name:</label><br /> | ||
<input type="text" id="fname" name="fname" /><br /> | ||
<input type="submit" value="Submit" /> | ||
</form> |
12 changes: 12 additions & 0 deletions
12
tests/fixtures/rules/no-restricted-html-elements/valid/object-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,12 @@ | ||
{ | ||
"options": [ | ||
{ | ||
"elements": ["h1", "h2", "h3", "h4", "h5", "h6"], | ||
"message": "Prefer use of our custom <Heading /> component" | ||
}, | ||
{ | ||
"elements": ["marquee"], | ||
"message": "Do not use deprecated HTML tags" | ||
} | ||
] | ||
} |
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.
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.
🙏