-
-
Notifications
You must be signed in to change notification settings - Fork 38
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
feat: add svelte/component-tags-order
rule
#498
base: main
Are you sure you want to change the base?
Changes from all commits
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 `svelte/component-tags-order` rule |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
.type-coverage | ||
build | ||
/lib | ||
.npmrc | ||
.npmrc | ||
tests/fixtures/rules/component-tags-order | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
--- | ||
pageClass: "rule-details" | ||
sidebarDepth: 0 | ||
title: "svelte/component-tags-order" | ||
description: "Enforce order of component top-level elements" | ||
--- | ||
|
||
# svelte/component-tags-order | ||
|
||
> Enforce order of component top-level elements | ||
|
||
- :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 warns about the order of the top-level tags, such as `<script>`, template and `<style>`. | ||
|
||
<ESLintCodeBlock fix> | ||
|
||
<!--eslint-skip--> | ||
|
||
```svelte | ||
<svelte:options /> | ||
|
||
<!-- ✓ GOOD --> | ||
|
||
<script context="module"> | ||
/* eslint svelte/component-tags-order: "error" */ | ||
</script> | ||
|
||
<script> | ||
export let foo | ||
</script> | ||
|
||
<svelte:window /> | ||
<svelte:document /> | ||
<svelte:head /> | ||
<svelte:body /> | ||
<div /> | ||
|
||
<style> | ||
p { | ||
color: blue; | ||
} | ||
</style> | ||
``` | ||
|
||
</ESLintCodeBlock> | ||
|
||
--- | ||
|
||
<ESLintCodeBlock fix> | ||
|
||
<!-- prettier-ignore-start --> | ||
<!--eslint-skip--> | ||
|
||
```svelte | ||
<!-- ✗ BAD --> | ||
|
||
<script> | ||
/* eslint svelte/component-tags-order: "error" */ | ||
export let foo | ||
</script> | ||
|
||
<script context="module"> | ||
const bar = 42 | ||
</script> | ||
``` | ||
|
||
<!-- prettier-ignore-end --> | ||
|
||
</ESLintCodeBlock> | ||
|
||
--- | ||
|
||
<ESLintCodeBlock fix> | ||
|
||
<!-- prettier-ignore-start --> | ||
<!--eslint-skip--> | ||
|
||
```svelte | ||
<!-- ✗ BAD --> | ||
<div>{foo}</div> | ||
|
||
<style> | ||
div { | ||
color: blue; | ||
} | ||
</style> | ||
|
||
<script> | ||
/* eslint svelte/component-tags-order: "error" */ | ||
export let foo | ||
</script> | ||
|
||
``` | ||
|
||
<!-- prettier-ignore-end --> | ||
|
||
</ESLintCodeBlock> | ||
|
||
## :wrench: Options | ||
|
||
```json | ||
{ | ||
"svelte/component-tags-order": [ | ||
"error", | ||
{ | ||
"order": [ | ||
"SvelteScriptElement([context=module])", | ||
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 don't think it's a good idea to use a format specifically for this rule. 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 copied most parts from https://eslint.vuejs.org/rules/component-tags-order.html And I think that the ESLint selector is complicated for most users because they need to understand Svelte ESLint AST. That's why I go to use a special format. 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.
Are you saying we can't use I agree that esquery is difficult 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. By the way, eslint-plugin-vue implements a function that converts CSS selectors to matchers in order to use CSS selectors in defining |
||
"SvelteScriptElement", | ||
"SvelteElement([svelte:options])", | ||
"SvelteElement([svelte:window])", | ||
"SvelteElement([svelte:document])", | ||
"SvelteElement([svelte:head])", | ||
"SvelteElement([svelte:body])", | ||
"SvelteElement", | ||
"SvelteStyleElement" | ||
] | ||
} | ||
] | ||
} | ||
``` | ||
|
||
- Regarding the order notation | ||
|
||
There are some differences between `<script>`, `<style>` and elements. | ||
|
||
For `<script>`, write `SvelteScriptElement` or `SvelteScriptElement([attrKey=attrValue])` or `SvelteScriptElement([attrKey])`. (e.g. `SvelteScriptElement([context=module])`). If only attrKey is specified, matches if that attribute is present. (Matches regardless of the attribute value.)<br/> | ||
And multiple attributes can be specified. (e.g. `SvelteScriptElement([context=module, lang=ts])`) In this case, it will be evaluate as AND match.<br/> | ||
For `<style>`, almost same as `<script>` but write `SvelteStyleElement` instead of `SvelteScriptElement`.<br/> | ||
For elements, write `SvelteElement` or `SvelteElement([tagName])`.<br/> | ||
And multiple tag names can be specified. (e.g. `SvelteElement([div, span])`)<br> | ||
|
||
Also negation can be used for attribute values. (e.g. `SvelteElement:not([div, span])`) <br/> | ||
In this example, it matches template elements other than `<div>` and `<span>`. | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/sveltejs/eslint-plugin-svelte/blob/main/src/rules/component-tags-order.ts) | ||
- [Test source](https://github.com/sveltejs/eslint-plugin-svelte/blob/main/tests/src/rules/component-tags-order.ts) |
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.
How can I disable prettier for specific file?
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.
I often use
<!-- prettier-ignore -->
, but it is OK to use a.prettierignore
file.eslint-plugin-svelte/tests/fixtures/rules/indent/invalid/4-indent/test01-input.svelte
Line 1 in e386e34