Skip to content

Add block-attributes-order rule #1973

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

Closed
wants to merge 10 commits into from
Closed
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ yarn.lock
yarn-error.log
docs/.vuepress/dist
typings/eslint/lib/rules
.DS_Store
3 changes: 2 additions & 1 deletion docs/rules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ For example:
```json
{
"rules": {
"vue/block-lang": "error"
"vue/block-attributes-order": "error"
}
}
```
Expand All @@ -206,6 +206,7 @@ For example:

| Rule ID | Description | | |
|:--------|:------------|:--:|:--:|
| [vue/block-attributes-order](./block-attributes-order.md) | enforce order of block attributes | :wrench: | :hammer: |
| [vue/block-lang](./block-lang.md) | disallow use other than available `lang` | | :hammer: |
| [vue/block-tag-newline](./block-tag-newline.md) | enforce line breaks after opening and before closing block-level tags | :wrench: | :lipstick: |
| [vue/component-api-style](./component-api-style.md) | enforce component API style | | :hammer: |
Expand Down
129 changes: 129 additions & 0 deletions docs/rules/block-attributes-order.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
---
pageClass: rule-details
sidebarDepth: 0
title: vue/block-attributes-order
description: enforce order of block attributes
since: v4.3.0
Copy link
Member

Choose a reason for hiding this comment

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

This rule is not yet released. Please remove this line and re-run npm run update.

---
# vue/block-attributes-order

> enforce order of block attributes

- :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 aims to enforce ordering of block attributes.

### The default order
Copy link
Member

Choose a reason for hiding this comment

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

I think we need to discuss option formats.

If we want to be more granular and configurable by the user, I suggest using an array of objects with elements and ordering, like this:

{'vue/block-attributes-order': ['error',
  [
    { element: 'script', order: ['setup, 'lang', 'src'] },
    { element: 'template', order: ['functional', 'lang', 'src'] },
    { element: 'style', order: ['scoped', 'module', 'lang', 'src'] },
    { element: 'i18n', order: ['global', 'locale', 'lang', 'src'] },
  ]
]}

You can use CSS selectors for the element property. This is similar to the vue/component-tags-order option.
The order option specifies the order of the attributes. It can use attribute names, regular expressions and their arrays, and negation. This is similar to the svelte/sort-attributes option.

If you want lang and src to always be at the back:

{'vue/block-attributes-order': ['error',
  [
    { element: '*', order: [['/.*/', '!/^(?:src|lang)$/'], 'lang', 'src'] }
  ]
]}

Patterns can be used to correct some order even for unknown blocks and unknown attributes.

What do you think?

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated as comments.


```json
[
{
"element": "style",
"order": [
"module",
"scoped",
"lang",
"src"
]
},
{
"element": "script",
"order": [
"setup",
"lang",
"src"
]
},
{
"element": "template",
"order": [
"functional",
"lang",
"src"
]
}
]
```

<eslint-code-block fix :rules="{'vue/block-attributes-order': ['error']}">

```vue
<template functional lang="pug" src="./template.pug"></template>
<script setup lang="ts" src="./script.ts"></script>
<style module scoped lang="css" src="./style.css"></style>
```

</eslint-code-block>

<eslint-code-block fix :rules="{'vue/block-attributes-order': ['error']}">

```vue
<template src="./template.pug" functional lang="pug"></template>
<script src="./script.ts" lang="ts" setup></script>
<style src="./style.css" lang="css" scoped module></style>
```

</eslint-code-block>

### Custom orders

<eslint-code-block fix :rules="{'vue/block-attributes-order': [
'error',
[
{
element: 'template',
order: ['src', 'lang', 'functional']
},
{
element: 'script[setup]',
order: ['setup', 'src|foo', 'lang']
},
{
element: 'script',
order: ['src', 'lang', 'setup']
},
{
element: 'style',
order: [['src', 'scoped'], 'module', 'lang']
}
]
]}">

```vue
<template functional lang="pug" src="./template.pug"></template>
<script src="./script.ts" lang="ts"></script>
<script src="./script.ts" lang="ts" setup foo></script>
<style src="./style.css" scoped module lang="css"></style>
```

</eslint-code-block>

### Custom blocks

<eslint-code-block fix :rules="{'vue/block-attributes-order': [
'error',
[
{
element: 'foobarbaz',
order: ['foo', 'bar', 'baz']
}
]
]}">

```vue
<foobarbaz foo bar baz></foobarbaz>
<foobarbaz baz bar foo></foobarbaz>
```

</eslint-code-block>

## :rocket: Version

This rule was introduced in eslint-plugin-vue v4.3.0

## :mag: Implementation

- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/block-attributes-order.js)
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/block-attributes-order.js)
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module.exports = {
'arrow-spacing': require('./rules/arrow-spacing'),
'attribute-hyphenation': require('./rules/attribute-hyphenation'),
'attributes-order': require('./rules/attributes-order'),
'block-attributes-order': require('./rules/block-attributes-order'),
'block-lang': require('./rules/block-lang'),
'block-spacing': require('./rules/block-spacing'),
'block-tag-newline': require('./rules/block-tag-newline'),
Expand Down
8 changes: 4 additions & 4 deletions lib/rules/attributes-order.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,10 +378,10 @@ module.exports = {
{
type: 'array',
items: {
enum: Object.values(ATTRS),
uniqueItems: true,
additionalItems: false
}
enum: Object.values(ATTRS)
},
uniqueItems: true,
additionalItems: false
}
]
},
Expand Down
Loading