Skip to content

feat: add vue/block-attributes-order rule - #3287

Open
officialasishkumar wants to merge 2 commits into
vuejs:masterfrom
officialasishkumar:feat/block-attributes-order
Open

officialasishkumar wants to merge 2 commits into
vuejs:masterfrom
officialasishkumar:feat/block-attributes-order

Conversation

@officialasishkumar

Copy link
Copy Markdown
Contributor

Closes #1970

Adds a new vue/block-attributes-order rule that enforces a consistent order of the attributes on the top-level blocks of an SFC, so a codebase doesn't end up with a mix of <script setup lang="ts"> and <script lang="ts" setup>.

The option format follows the review feedback on the earlier attempt in #1973:

{
  "vue/block-attributes-order": ["error", [
    { "element": "script", "order": ["setup", ["!lang", "!generic", "!src"], "lang", "generic", "src"] },
    { "element": "template", "order": ["functional", ["!lang", "!src"], "lang", "src"] },
    { "element": "style", "order": ["scoped", "module", ["!lang", "!src"], "lang", "src"] },
    { "element": "*", "order": [["!lang", "!src"], "lang", "src"] }
  ]]
}
  • element is a CSS selector, as in vue/block-order (e.g. script[setup], i18n:not([locale=en]), *). Each block uses the first object that matches it. Blocks that match nothing are not checked.
  • order items are attribute name patterns, or arrays of patterns, that support regular expressions ("/^data-/u") and ! negation (["/^foo/u", "!foo-bar"]), like svelte/sort-attributes. An attribute uses the first item that matches it. Attributes that match no item must go after all others.
  • The default above (used when no option is given) puts lang and src last on every block, including custom blocks. On <script>, generic goes between them so that <script setup lang="ts" generic="T"> from the Vue docs is accepted.
  • The autofix moves an out-of-order attribute in front of the previous attribute and keeps the order of the attributes in between, like vue/attributes-order.

Includes tests, docs and a changeset. The rule isn't added to any preset config.

Add a new rule that enforces a consistent order of the attributes on the
top-level blocks of a single-file component, e.g. always
`<script setup lang="ts">` instead of a mix with `<script lang="ts" setup>`.

The option is an array of `{ element, order }` objects. `element` is a
CSS selector for the blocks (the first matching object is used), and
`order` lists attribute name patterns or arrays of patterns, which
support regular expressions and `!` negation. Attributes that match no
item must go after all others.

By default, `lang` and `src` go last on every block, with `generic`
placed between them on `<script>` as in the Vue documentation;
`setup`, `functional`, `scoped` and `module` go first on their blocks.

The autofix moves an out-of-order attribute before the previous
attribute while keeping the order of the attributes in between.

Closes vuejs#1970
@changeset-bot

changeset-bot Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: f4b9c41

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
eslint-plugin-vue Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@ValentinYoushkevich

Copy link
Copy Markdown
Contributor

Hi @officialasishkumar, thanks for picking this up! Two small test suggestions:

  1. A valid case for a file without an SFC, e.g. { filename: 'test.js', code: 'const a = 1' }, to pin that the rule doesn't crash there.
  2. A valid case with uppercase attribute names, e.g. <script LANG="ts" SETUP>. The rule skips it, which matches @vue/compiler-sfc (it doesn't treat SETUP/LANG as setup/lang), but a test would make that clearly intentional.

…butes

Add valid cases for a `.js` file, where there is no document fragment,
and for `<script LANG="ts" SETUP>`, whose attribute names are not
treated as `lang` and `setup`, matching `@vue/compiler-sfc`.
@officialasishkumar

Copy link
Copy Markdown
Contributor Author

Added both as valid cases in f4b9c41: a test.js file with const a = 1, and <script LANG="ts" SETUP> with a comment noting that attribute names are case-sensitive, as in @vue/compiler-sfc.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

tag-attr-order <script lang="ts" setup> vs <script setup lang="ts">

2 participants