Repository navigation
feat: add vue/block-attributes-order rule - #3287
Open
officialasishkumar wants to merge 2 commits into
Open
officialasishkumar wants to merge 2 commits into
officialasishkumar wants to merge 2 commits into
Conversation
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 detectedLatest commit: f4b9c41 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
Contributor
|
Hi @officialasishkumar, thanks for picking this up! Two small test suggestions:
|
…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`.
Contributor
Author
|
Added both as valid cases in f4b9c41: a |
This branch has not been deployed
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1970
Adds a new
vue/block-attributes-orderrule 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"] } ]] }elementis a CSS selector, as invue/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.orderitems are attribute name patterns, or arrays of patterns, that support regular expressions ("/^data-/u") and!negation (["/^foo/u", "!foo-bar"]), likesvelte/sort-attributes. An attribute uses the first item that matches it. Attributes that match no item must go after all others.langandsrclast on every block, including custom blocks. On<script>,genericgoes between them so that<script setup lang="ts" generic="T">from the Vue docs is accepted.vue/attributes-order.Includes tests, docs and a changeset. The rule isn't added to any preset config.