Skip to content

Commit 1700708

Browse files
authored
Changed so that the names array can be specified in the one order option of the vue/component-tags-order rule, and the default setting has been changed. (#1189)
1 parent 7dee01d commit 1700708

File tree

4 files changed

+128
-25
lines changed

4 files changed

+128
-25
lines changed

docs/rules/comment-directive.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ The `eslint-disable`-like comments can be used in the `<template>` and in the bl
4949
</template>
5050
5151
<!-- eslint-disable-next-line vue/component-tags-order -->
52-
<script>
53-
</script>
52+
<style>
53+
</style>
5454
```
5555

5656
</eslint-code-block>
@@ -60,15 +60,16 @@ The `eslint-disable` comments has no effect after one block.
6060
<eslint-code-block :rules="{'vue/comment-directive': ['error'], 'vue/max-attributes-per-line': ['error'], 'vue/component-tags-order': ['error'] }">
6161

6262
```vue
63-
<template>
64-
</template>
65-
66-
<!-- eslint-disable vue/component-tags-order -->
67-
<style> /* <- Warning has been disabled. */
63+
<style>
6864
</style>
6965
70-
<script> /* <- Warning are not disabled. */
66+
<!-- eslint-disable -->
67+
<script> /* <- Warning has been disabled. */
7168
</script>
69+
70+
<template> <!-- <- Warning are not disabled. -->
71+
</template>
72+
7273
```
7374

7475
</eslint-code-block>

docs/rules/component-tags-order.md

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ This rule warns about the order of the `<script>`, `<template>` & `<style>` tags
1818
```json
1919
{
2020
"vue/component-tags-order": ["error", {
21-
"order": ["script", "template", "style"]
21+
"order": [ [ "script", "template" ], "style" ]
2222
}]
2323
}
2424
```
2525

26-
- `order` (`string[]`) ... The order of top-level element names. default `["script", "template", "style"]`.
26+
- `order` (`(string|string[])[]`) ... The order of top-level element names. default `[ [ "script", "template" ], "style" ]`.
2727

28-
### `{ "order": ["script", "template", "style"] }` (default)
28+
### `{ "order": [ [ "script", "template" ], "style" ] }` (default)
2929

3030
<eslint-code-block :rules="{'vue/component-tags-order': ['error']}">
3131

@@ -40,6 +40,17 @@ This rule warns about the order of the `<script>`, `<template>` & `<style>` tags
4040

4141
<eslint-code-block :rules="{'vue/component-tags-order': ['error']}">
4242

43+
```vue
44+
<!-- ✓ GOOD -->
45+
<template>...</template>
46+
<script>/* ... */</script>
47+
<style>/* ... */</style>
48+
```
49+
50+
</eslint-code-block>
51+
52+
<eslint-code-block :rules="{'vue/component-tags-order': ['error']}">
53+
4354
```vue
4455
<!-- ✗ BAD -->
4556
<style>/* ... */</style>
@@ -62,6 +73,17 @@ This rule warns about the order of the `<script>`, `<template>` & `<style>` tags
6273

6374
</eslint-code-block>
6475

76+
<eslint-code-block :rules="{'vue/component-tags-order': ['error', { 'order': ['template', 'script', 'style'] }]}">
77+
78+
```vue
79+
<!-- ✗ BAD -->
80+
<script>/* ... */</script>
81+
<template>...</template>
82+
<style>/* ... */</style>
83+
```
84+
85+
</eslint-code-block>
86+
6587
### `{ "order": ["docs", "template", "script", "style"] }`
6688

6789
<eslint-code-block :rules="{'vue/component-tags-order': ['error', { 'order': ['docs', 'template', 'script', 'style'] }]}">

lib/rules/component-tags-order.js

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
const utils = require('../utils')
1212

13-
const DEFAULT_ORDER = Object.freeze(['script', 'template', 'style'])
13+
const DEFAULT_ORDER = Object.freeze([['script', 'template'], 'style'])
1414

1515
// ------------------------------------------------------------------------------
1616
// Rule Definition
@@ -25,22 +25,44 @@ module.exports = {
2525
url: 'https://eslint.vuejs.org/rules/component-tags-order.html'
2626
},
2727
fixable: null,
28-
schema: {
29-
type: 'array',
30-
properties: {
31-
order: {
32-
type: 'array'
28+
schema: [
29+
{
30+
type: 'object',
31+
properties: {
32+
order: {
33+
type: 'array',
34+
items: {
35+
anyOf: [
36+
{ type: 'string' },
37+
{ type: 'array', items: { type: 'string' }, uniqueItems: true }
38+
]
39+
},
40+
uniqueItems: true,
41+
additionalItems: false
42+
}
3343
}
3444
}
35-
},
45+
],
3646
messages: {
3747
unexpected:
3848
'The <{{name}}> should be above the <{{firstUnorderedName}}> on line {{line}}.'
3949
}
4050
},
4151
create(context) {
42-
const order =
43-
(context.options[0] && context.options[0].order) || DEFAULT_ORDER
52+
/** @type {Map<string, number} */
53+
const orderMap = new Map()
54+
;(
55+
(context.options[0] && context.options[0].order) ||
56+
DEFAULT_ORDER
57+
).forEach((nameOrNames, index) => {
58+
if (Array.isArray(nameOrNames)) {
59+
for (const name of nameOrNames) {
60+
orderMap.set(name, index)
61+
}
62+
} else {
63+
orderMap.set(nameOrNames, index)
64+
}
65+
})
4466
const documentFragment =
4567
context.parserServices.getDocumentFragment &&
4668
context.parserServices.getDocumentFragment()
@@ -76,15 +98,15 @@ module.exports = {
7698
const elements = getTopLevelHTMLElements()
7799

78100
elements.forEach((element, index) => {
79-
const expectedIndex = order.indexOf(element.name)
101+
const expectedIndex = orderMap.get(element.name)
80102
if (expectedIndex < 0) {
81103
return
82104
}
83105
const firstUnordered = elements
84106
.slice(0, index)
85-
.filter((e) => expectedIndex < order.indexOf(e.name))
107+
.filter((e) => expectedIndex < orderMap.get(e.name))
86108
.sort(
87-
(e1, e2) => order.indexOf(e1.name) - order.indexOf(e2.name)
109+
(e1, e2) => orderMap.get(e1.name) - orderMap.get(e2.name)
88110
)[0]
89111
if (firstUnordered) {
90112
report(element, firstUnordered)

tests/lib/rules/component-tags-order.js

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,24 @@ tester.run('component-tags-order', rule, {
2222
valid: [
2323
// default
2424
'<script></script><template></template><style></style>',
25+
'<template></template><script></script><style></style>',
2526
'<script> /*script*/ </script><template><div id="id">text <!--comment--> </div><br></template><style>.button{ color: red; }</style>',
2627
'<docs></docs><script></script><template></template><style></style>',
2728
'<script></script><docs></docs><template></template><style></style>',
29+
'<docs></docs><template></template><script></script><style></style>',
30+
'<template></template><script></script><docs></docs><style></style>',
2831
'<script></script><template></template>',
32+
'<template></template><script></script>',
33+
`
34+
<template>
35+
</template>
36+
37+
<script>
38+
</script>
39+
40+
<style>
41+
</style>
42+
`,
2943
`
3044
<script>
3145
</script>
@@ -38,6 +52,11 @@ tester.run('component-tags-order', rule, {
3852
`,
3953

4054
// order
55+
{
56+
code: '<script></script><template></template><style></style>',
57+
output: null,
58+
options: [{ order: ['script', 'template', 'style'] }]
59+
},
4160
{
4261
code: '<template></template><script></script><style></style>',
4362
output: null,
@@ -65,6 +84,12 @@ tester.run('component-tags-order', rule, {
6584
output: null,
6685
options: [{ order: ['docs', 'script', 'template', 'style'] }]
6786
},
87+
{
88+
code:
89+
'<template></template><docs></docs><script></script><style></style>',
90+
output: null,
91+
options: [{ order: [['docs', 'script', 'template'], 'style'] }]
92+
},
6893

6994
`<script></script><style></style>`,
7095

@@ -73,8 +98,24 @@ tester.run('component-tags-order', rule, {
7398
'<template><div><!--test</div></template><style></style>'
7499
],
75100
invalid: [
101+
{
102+
code: '<style></style><template></template><script></script>',
103+
errors: [
104+
{
105+
message: 'The <template> should be above the <style> on line 1.',
106+
line: 1,
107+
column: 16
108+
},
109+
{
110+
message: 'The <script> should be above the <style> on line 1.',
111+
line: 1,
112+
column: 37
113+
}
114+
]
115+
},
76116
{
77117
code: '<template></template><script></script><style></style>',
118+
options: [{ order: ['script', 'template', 'style'] }],
78119
errors: [
79120
{
80121
message: 'The <script> should be above the <template> on line 1.',
@@ -83,12 +124,27 @@ tester.run('component-tags-order', rule, {
83124
}
84125
]
85126
},
127+
{
128+
code: `
129+
<template></template>
130+
131+
<style></style>
132+
133+
<script></script>`,
134+
errors: [
135+
{
136+
message: 'The <script> should be above the <style> on line 4.',
137+
line: 6
138+
}
139+
]
140+
},
86141
{
87142
code: `
88143
<template></template>
89144
<script></script>
90145
<style></style>
91146
`,
147+
options: [{ order: ['script', 'template', 'style'] }],
92148
errors: [
93149
{
94150
message: 'The <script> should be above the <template> on line 2.',
@@ -132,6 +188,7 @@ tester.run('component-tags-order', rule, {
132188
<script></script>
133189
<style></style>
134190
`,
191+
options: [{ order: ['script', 'template', 'style'] }],
135192
errors: [
136193
{
137194
message: 'The <script> should be above the <template> on line 2.',
@@ -147,6 +204,7 @@ tester.run('component-tags-order', rule, {
147204
<script></script>
148205
<style></style>
149206
`,
207+
options: [{ order: ['script', 'template', 'style'] }],
150208
errors: [
151209
{
152210
message: 'The <script> should be above the <template> on line 2.',
@@ -179,7 +237,7 @@ tester.run('component-tags-order', rule, {
179237
line: 3
180238
},
181239
{
182-
message: 'The <script> should be above the <template> on line 3.',
240+
message: 'The <script> should be above the <style> on line 2.',
183241
line: 4
184242
}
185243
]
@@ -197,7 +255,7 @@ tester.run('component-tags-order', rule, {
197255
line: 4
198256
},
199257
{
200-
message: 'The <script> should be above the <template> on line 4.',
258+
message: 'The <script> should be above the <style> on line 2.',
201259
line: 5
202260
}
203261
]

0 commit comments

Comments
 (0)