-
-
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
Changes from 6 commits
6f1cc06
966237c
65c1381
a8d4bbb
56b8cc9
cf80a29
12f38f7
721a532
e4a872c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
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) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { createRule } from "../utils" | ||
|
||
export default createRule("no-restricted-html-elements", { | ||
meta: { | ||
docs: { | ||
description: "disallow specific HTML elements", | ||
category: "Extension Rules", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm. I think the better category for this rule is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I misunderstood the meaning of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like we need to run There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
We can always make changes if we have ideas for categorization that make it easier for users to understand 😉 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought about it a bit, but I think |
||
recommended: 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, | ||
}) | ||
} | ||
} | ||
} | ||
}, | ||
} | ||
}, | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"options": ["h1", "h2", "h3", "h4", "h5", "h6"] | ||
} |
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 |
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> |
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" | ||
} | ||
] | ||
} |
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 |
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> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"options": ["h1", "h2", "h3", "h4", "h5", "h6"] | ||
} |
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> |
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" | ||
} | ||
] | ||
} |
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.
🙏