Skip to content
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

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/odd-cows-notice.md
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
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
.type-coverage
build
/lib
.npmrc
.npmrc
tests/fixtures/rules/component-tags-order
Copy link
Member Author

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?

Copy link
Member

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.

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ These rules relate to style guidelines, and are therefore quite subjective:

| Rule ID | Description | |
|:--------|:------------|:---|
| [svelte/component-tags-order](https://sveltejs.github.io/eslint-plugin-svelte/rules/component-tags-order/) | Enforce order of component top-level elements | :wrench: |
| [svelte/derived-has-same-inputs-outputs](https://sveltejs.github.io/eslint-plugin-svelte/rules/derived-has-same-inputs-outputs/) | derived store should use same variable names between values and callback | |
| [svelte/first-attribute-linebreak](https://sveltejs.github.io/eslint-plugin-svelte/rules/first-attribute-linebreak/) | enforce the location of first attribute | :wrench: |
| [svelte/html-closing-bracket-spacing](https://sveltejs.github.io/eslint-plugin-svelte/rules/html-closing-bracket-spacing/) | require or disallow a space before tag's closing brackets | :wrench: |
Expand Down
1 change: 1 addition & 0 deletions docs/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ These rules relate to style guidelines, and are therefore quite subjective:

| Rule ID | Description | |
| :------------------------------------------------------------------------------------------------------- | :--------------------------------------------------------------------------------- | :------- |
| [svelte/component-tags-order](./rules/component-tags-order.md) | Enforce order of component top-level elements | :wrench: |
| [svelte/derived-has-same-inputs-outputs](./rules/derived-has-same-inputs-outputs.md) | derived store should use same variable names between values and callback | |
| [svelte/first-attribute-linebreak](./rules/first-attribute-linebreak.md) | enforce the location of first attribute | :wrench: |
| [svelte/html-closing-bracket-spacing](./rules/html-closing-bracket-spacing.md) | require or disallow a space before tag's closing brackets | :wrench: |
Expand Down
142 changes: 142 additions & 0 deletions docs/rules/component-tags-order.md
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])",
Copy link
Member

Choose a reason for hiding this comment

The 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.
For example, using the CSS selector format most users already know the format. (However, users may be a little surprised because the colon must be escaped. e.g. svelte\:options)
Is the format currently used by the rule inspired by something?

Copy link
Member Author

@baseballyama baseballyama Jun 10, 2023

Choose a reason for hiding this comment

The 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 while implementing the rule, I felt that we need to support and condition, but I couldn't find the way to use and condition in the ESLint selector.
https://eslint.org/docs/latest/extend/selectors

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.
(But yeah, I didn't support escaping things...)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And while implementing the rule, I felt that we need to support and condition, but I couldn't find the way to use and condition in the ESLint selector.

Are you saying we can't use and in CSS selectors?
I think that the and condition in the CSS selector just connects without using spaces. e.g. script[context=module][lang=ts]
And in modern CSS selectors the or condition uses :is(). e.g. :is(script, style)

https://eslint.org/docs/latest/extend/selectors

I agree that esquery is difficult for users to understand.

Copy link
Member

Choose a reason for hiding this comment

The 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 vue/component-tags-order rule option.

https://github.com/vuejs/eslint-plugin-vue/blob/81ce0cecb87eae60830b9b973790d4f2386062c1/lib/utils/selector.js#L22

"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)
Loading