-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add rule
no-deprecated-raw-special-elements
(#918)
~Adds a new Svelte 5 specific rule that ensure special elements are used with `svelte:` prefix. This rule will help on migrating from Svelte 4, as such elements were supported without the prefix in Svelte 4.~ Adds a new rule that recommends not using raw special elements and fixes to using the `svelte:` prefix. Raw special elements are deprecated from v5 on. Closes #913 --------- Co-authored-by: Yosuke Ota <[email protected]>
- Loading branch information
Showing
12 changed files
with
169 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 | ||
--- | ||
|
||
Added new `no-deprecated-raw-special-elements` rule |
This file contains 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 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 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,52 @@ | ||
--- | ||
pageClass: 'rule-details' | ||
sidebarDepth: 0 | ||
title: 'svelte/no-deprecated-raw-special-elements' | ||
description: 'Recommends not using raw special elements in Svelte versions previous to 5.' | ||
--- | ||
|
||
# svelte/no-deprecated-raw-special-elements | ||
|
||
> Recommends not using raw special elements in Svelte versions previous to 5. | ||
- :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 | ||
|
||
This rule reports the usage of `head`, `body`, `window`, `document`, `element` and `options` HTML elements. These elements were valid in in versions proior to 5, but since Svelte 5 they must be used with `svelte:`. | ||
|
||
<ESLintCodeBlock fix> | ||
|
||
<!--eslint-skip--> | ||
|
||
```svelte | ||
<script> | ||
/* eslint svelte/no-deprecated-raw-special-elements: "error" */ | ||
</script> | ||
<!-- ✓ GOOD --> | ||
<svelte:head> | ||
<title>Valid</title> | ||
</svelte:head> | ||
<!-- ✗ BAD --> | ||
<head> | ||
<title>Invalid</title> | ||
</head> | ||
``` | ||
|
||
</ESLintCodeBlock> | ||
|
||
## :wrench: Options | ||
|
||
Nothing. | ||
|
||
## :books: Further Reading | ||
|
||
- See special elements section in [Svelte docs](https://svelte.dev/docs/svelte/svelte-window) | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/sveltejs/eslint-plugin-svelte/blob/main/packages/eslint-plugin-svelte/src/rules/no-deprecated-raw-special-elements.ts) | ||
- [Test source](https://github.com/sveltejs/eslint-plugin-svelte/blob/main/packages/eslint-plugin-svelte/tests/src/rules/no-deprecated-raw-special-elements.ts) |
This file contains 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
47 changes: 47 additions & 0 deletions
47
packages/eslint-plugin-svelte/src/rules/no-deprecated-raw-special-elements.ts
This file contains 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,47 @@ | ||
import type { AST } from 'svelte-eslint-parser'; | ||
import { createRule } from '../utils'; | ||
|
||
const INVALID_HTML_ELEMENTS = ['head', 'body', 'window', 'document', 'element', 'options']; | ||
const VALID_PREFIX = 'svelte:'; | ||
|
||
export default createRule('no-deprecated-raw-special-elements', { | ||
meta: { | ||
docs: { | ||
description: 'Recommends not using raw special elements in Svelte versions previous to 5.', | ||
category: 'Possible Errors', | ||
// TODO: Switch to recommended in the major version | ||
recommended: false | ||
}, | ||
schema: [], | ||
messages: { | ||
deprecatedElement: | ||
'Special {{name}} element is deprecated in v5, use svelte:{{name}} instead.' | ||
}, | ||
type: 'problem', // 'problem', or 'layout', | ||
fixable: 'code' | ||
}, | ||
create(context) { | ||
return { | ||
'SvelteElement[kind="html"]'(node: AST.SvelteHTMLElement) { | ||
const { name } = node.name; | ||
if (INVALID_HTML_ELEMENTS.includes(name)) { | ||
context.report({ | ||
node, | ||
messageId: 'deprecatedElement', | ||
data: { name }, | ||
*fix(fixer) { | ||
const { endTag } = node; | ||
yield fixer.insertTextBeforeRange([node.range[0] + 1, node.range[1]], VALID_PREFIX); | ||
if (endTag) { | ||
yield fixer.insertTextBeforeRange( | ||
[endTag.range[0] + 2, endTag.range[1]], | ||
VALID_PREFIX | ||
); | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
}; | ||
} | ||
}); |
This file contains 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
24 changes: 24 additions & 0 deletions
24
...svelte/tests/fixtures/rules/no-deprecated-raw-special-elements/invalid/test01-errors.yaml
This file contains 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: Special head element is deprecated in v5, use svelte:head instead. | ||
line: 1 | ||
column: 1 | ||
suggestions: null | ||
- message: Special body element is deprecated in v5, use svelte:body instead. | ||
line: 2 | ||
column: 1 | ||
suggestions: null | ||
- message: Special window element is deprecated in v5, use svelte:window instead. | ||
line: 3 | ||
column: 1 | ||
suggestions: null | ||
- message: Special document element is deprecated in v5, use svelte:document instead. | ||
line: 4 | ||
column: 1 | ||
suggestions: null | ||
- message: Special element element is deprecated in v5, use svelte:element instead. | ||
line: 5 | ||
column: 1 | ||
suggestions: null | ||
- message: Special options element is deprecated in v5, use svelte:options instead. | ||
line: 6 | ||
column: 1 | ||
suggestions: null |
6 changes: 6 additions & 0 deletions
6
...velte/tests/fixtures/rules/no-deprecated-raw-special-elements/invalid/test01-input.svelte
This file contains 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 @@ | ||
<head></head> | ||
<body></body> | ||
<window></window> | ||
<document></document> | ||
<element this={{}}></element> | ||
<options></options> |
6 changes: 6 additions & 0 deletions
6
...elte/tests/fixtures/rules/no-deprecated-raw-special-elements/invalid/test01-output.svelte
This file contains 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 @@ | ||
<svelte:head></svelte:head> | ||
<svelte:body></svelte:body> | ||
<svelte:window></svelte:window> | ||
<svelte:document></svelte:document> | ||
<svelte:element this={{}}></svelte:element> | ||
<svelte:options></svelte:options> |
8 changes: 8 additions & 0 deletions
8
...-svelte/tests/fixtures/rules/no-deprecated-raw-special-elements/valid/test01-input.svelte
This file contains 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,8 @@ | ||
<svelte:options /> | ||
|
||
<svelte:body /> | ||
<svelte:document /> | ||
<svelte:element this={{}}></svelte:element> | ||
<svelte:head></svelte:head> | ||
|
||
<svelte:window /> |
12 changes: 12 additions & 0 deletions
12
packages/eslint-plugin-svelte/tests/src/rules/no-deprecated-raw-special-elements.ts
This file contains 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 @@ | ||
import { RuleTester } from '../../utils/eslint-compat'; | ||
import rule from '../../../src/rules/no-deprecated-raw-special-elements'; | ||
import { loadTestCases } from '../../utils/utils'; | ||
|
||
const tester = new RuleTester({ | ||
languageOptions: { | ||
ecmaVersion: 2020, | ||
sourceType: 'module' | ||
} | ||
}); | ||
|
||
tester.run('no-deprecated-raw-special-elements', rule as any, loadTestCases('no-deprecated-raw-special-elements')); |