Skip to content

Commit

Permalink
Fix vuejs#1819: Enforce order between script and script setup
Browse files Browse the repository at this point in the history
  • Loading branch information
doug-wade committed Mar 24, 2022
1 parent 8f09420 commit 5eafa3b
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 19 deletions.
66 changes: 49 additions & 17 deletions docs/rules/component-tags-order.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ title: vue/component-tags-order
description: enforce order of component top-level elements
since: v6.1.0
---

# vue/component-tags-order

> enforce order of component top-level elements
Expand All @@ -20,9 +21,12 @@ This rule warns about the order of the `<script>`, `<template>` & `<style>` tags

```json
{
"vue/component-tags-order": ["error", {
"order": [ [ "script", "template" ], "style" ]
}]
"vue/component-tags-order": [
"error",
{
"order": [["script", "template"], "style"]
}
]
}
```

Expand All @@ -34,9 +38,13 @@ This rule warns about the order of the `<script>`, `<template>` & `<style>` tags

```vue
<!-- ✓ GOOD -->
<script>/* ... */</script>
<script>
/* ... */
</script>
<template>...</template>
<style>/* ... */</style>
<style>
/* ... */
</style>
```

</eslint-code-block>
Expand All @@ -46,8 +54,12 @@ This rule warns about the order of the `<script>`, `<template>` & `<style>` tags
```vue
<!-- ✓ GOOD -->
<template>...</template>
<script>/* ... */</script>
<style>/* ... */</style>
<script>
/* ... */
</script>
<style>
/* ... */
</style>
```

</eslint-code-block>
Expand All @@ -56,8 +68,12 @@ This rule warns about the order of the `<script>`, `<template>` & `<style>` tags

```vue
<!-- ✗ BAD -->
<style>/* ... */</style>
<script>/* ... */</script>
<style>
/* ... */
</style>
<script>
/* ... */
</script>
<template>...</template>
```

Expand All @@ -70,8 +86,12 @@ This rule warns about the order of the `<script>`, `<template>` & `<style>` tags
```vue
<!-- ✓ GOOD -->
<template>...</template>
<script>/* ... */</script>
<style>/* ... */</style>
<script>
/* ... */
</script>
<style>
/* ... */
</style>
```

</eslint-code-block>
Expand All @@ -80,9 +100,13 @@ This rule warns about the order of the `<script>`, `<template>` & `<style>` tags

```vue
<!-- ✗ BAD -->
<script>/* ... */</script>
<script>
/* ... */
</script>
<template>...</template>
<style>/* ... */</style>
<style>
/* ... */
</style>
```

</eslint-code-block>
Expand All @@ -95,8 +119,12 @@ This rule warns about the order of the `<script>`, `<template>` & `<style>` tags
<!-- ✓ GOOD -->
<docs> documentation </docs>
<template>...</template>
<script>/* ... */</script>
<style>/* ... */</style>
<script>
/* ... */
</script>
<style>
/* ... */
</style>
```

</eslint-code-block>
Expand All @@ -106,9 +134,13 @@ This rule warns about the order of the `<script>`, `<template>` & `<style>` tags
```vue
<!-- ✗ BAD -->
<template>...</template>
<script>/* ... */</script>
<script>
/* ... */
</script>
<docs> documentation </docs>
<style>/* ... */</style>
<style>
/* ... */
</style>
```

</eslint-code-block>
Expand Down
16 changes: 14 additions & 2 deletions lib/rules/component-tags-order.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ module.exports = {
}
return []
}
/**
* @param {VElement} element
*/
function isSetupScript(element) {
return element.name === 'script' && utils.getAttribute(element, 'setup')
}

return {
Program(node) {
Expand All @@ -101,6 +107,12 @@ module.exports = {
}
const firstUnordered = elements
.slice(0, index)
.map((e) => {
return {
element: e,
name: isSetupScript(e) ? 'script/setup' : e.name
}
})
.filter((e) => expectedIndex < getOrderPosition(e.name))
.sort(
(e1, e2) => getOrderPosition(e1.name) - getOrderPosition(e2.name)
Expand All @@ -113,12 +125,12 @@ module.exports = {
data: {
name: element.name,
firstUnorderedName: firstUnordered.name,
line: firstUnordered.loc.start.line
line: firstUnordered.element.loc.start.line
},
*fix(fixer) {
// insert element before firstUnordered
const fixedElements = elements.flatMap((it) => {
if (it === firstUnordered) {
if (it === firstUnordered.element) {
return [element, it]
} else if (it === element) {
return []
Expand Down
12 changes: 12 additions & 0 deletions tests/lib/rules/component-tags-order.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ tester.run('component-tags-order', rule, {
'<template></template><script></script><docs></docs><style></style>',
'<script></script><template></template>',
'<template></template><script></script>',
'<script setup></script><script></script>',
'<docs></docs><template></template><script></script><script setup></script><style></style>',
`
<template>
</template>
Expand Down Expand Up @@ -102,6 +104,16 @@ tester.run('component-tags-order', rule, {
output: null,
options: [{ order: ['docs', 'script', 'template', 'style'] }]
},
{
code: '<script setup></script><script></script><template></template><style></style>',
output: null,
options: [{ order: ['script/setup', 'script', 'template', 'style'] }]
},
{
code: '<template></template><script setup></script><script></script><style></style>',
output: null,
options: [{ order: [['script/setup', 'script', 'template'], 'style'] }]
},
{
code: '<template></template><docs></docs><script></script><style></style>',
output: null,
Expand Down

0 comments on commit 5eafa3b

Please sign in to comment.