Skip to content

Commit bf7d219

Browse files
authored
Changed the default order setting of vue/order-in-components rule. (#1181)
* Changed the default order setting of `vue/order-in-components` rule. - Add options for Vue.js 3.x - `emits` to after `props`. - `setup` to after `emits`. - `beforeUnmount` and `unmounted` to LIFECYCLE_HOOKS. - `renderTracked` and `renderTriggered` to LIFECYCLE_HOOKS. - Add options for Vue.js 2.x - `provide` and `inject` to after `mixins`. - `errorCaptured` to LIFECYCLE_HOOKS. * Update require-explicit-emits
1 parent 47960f6 commit bf7d219

File tree

4 files changed

+143
-9
lines changed

4 files changed

+143
-9
lines changed

lib/rules/order-in-components.js

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,54 @@ const utils = require('../utils')
88
const traverseNodes = require('vue-eslint-parser').AST.traverseNodes
99

1010
const defaultOrder = [
11+
// Side Effects (triggers effects outside the component)
1112
'el',
13+
14+
// Global Awareness (requires knowledge beyond the component)
1215
'name',
1316
'parent',
17+
18+
// Component Type (changes the type of the component)
1419
'functional',
20+
21+
// Template Modifiers (changes the way templates are compiled)
1522
['delimiters', 'comments'],
23+
24+
// Template Dependencies (assets used in the template)
1625
['components', 'directives', 'filters'],
26+
27+
// Composition (merges properties into the options)
1728
'extends',
1829
'mixins',
30+
['provide', 'inject'], // for Vue.js 2.2.0+
31+
32+
// Interface (the interface to the component)
1933
'inheritAttrs',
2034
'model',
2135
['props', 'propsData'],
22-
'fetch',
23-
'asyncData',
36+
'emits', // for Vue.js 3.x
37+
38+
// Note:
39+
// The `setup` option is included in the "Composition" category,
40+
// but the behavior of the `setup` option requires the definition of "Interface",
41+
// so we prefer to put the `setup` option after the "Interface".
42+
'setup', // for Vue 3.x
43+
44+
// Local State (local reactive properties)
45+
'fetch', // for Nuxt
46+
'asyncData', // for Nuxt
2447
'data',
2548
'computed',
49+
50+
// Events (callbacks triggered by reactive events)
2651
'watch',
2752
'LIFECYCLE_HOOKS',
53+
54+
// Non-Reactive Properties (instance properties independent of the reactivity system)
2855
'methods',
29-
'head',
56+
57+
// Rendering (the declarative description of the component output)
58+
'head', // for Nuxt
3059
['template', 'render'],
3160
'renderError'
3261
]
@@ -41,8 +70,13 @@ const groups = {
4170
'updated',
4271
'activated',
4372
'deactivated',
73+
'beforeUnmount', // for Vue.js 3.x
74+
'unmounted', // for Vue.js 3.x
4475
'beforeDestroy',
45-
'destroyed'
76+
'destroyed',
77+
'renderTracked', // for Vue.js 3.x
78+
'renderTriggered', // for Vue.js 3.x
79+
'errorCaptured' // for Vue.js 2.5.0+
4680
]
4781
}
4882

lib/rules/require-explicit-emits.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ const utils = require('../utils')
2424
// ------------------------------------------------------------------------------
2525

2626
const FIX_EMITS_AFTER_OPTIONS = [
27-
'props',
28-
'propsData',
2927
'setup',
3028
'data',
3129
'computed',
@@ -44,8 +42,13 @@ const FIX_EMITS_AFTER_OPTIONS = [
4442
'updated',
4543
'activated',
4644
'deactivated',
45+
'beforeUnmount',
46+
'unmounted',
4747
'beforeDestroy',
48-
'destroyed'
48+
'destroyed',
49+
'renderTracked',
50+
'renderTriggered',
51+
'errorCaptured'
4952
]
5053

5154
/**

tests/lib/rules/order-in-components.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,49 @@ ruleTester.run('order-in-components', rule, {
3535
`,
3636
parserOptions
3737
},
38+
{
39+
filename: 'example.vue',
40+
code: `
41+
export default {
42+
el,
43+
name,
44+
parent,
45+
functional,
46+
delimiters, comments,
47+
components, directives, filters,
48+
extends: MyComp,
49+
mixins,
50+
provide, inject,
51+
inheritAttrs,
52+
model,
53+
props, propsData,
54+
emits,
55+
setup,
56+
data,
57+
computed,
58+
watch,
59+
beforeCreate,
60+
created,
61+
beforeMount,
62+
mounted,
63+
beforeUpdate,
64+
updated,
65+
activated,
66+
deactivated,
67+
beforeUnmount,
68+
unmounted,
69+
beforeDestroy,
70+
destroyed,
71+
renderTracked,
72+
renderTriggered,
73+
errorCaptured,
74+
methods,
75+
template, render,
76+
renderError,
77+
};
78+
`,
79+
parserOptions
80+
},
3881
{
3982
filename: 'test.vue',
4083
code: `

tests/lib/rules/require-explicit-emits.js

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,8 +1282,62 @@ emits: {'foo': null}
12821282
<script>
12831283
export default {
12841284
name: '',
1285+
props: {},
1286+
emits: ['foo']
1287+
}
1288+
</script>
1289+
`
1290+
},
1291+
{
1292+
desc:
1293+
'Add the `emits` option with object syntax and define "foo" event.',
1294+
output: `
1295+
<template>
1296+
<div @click="$emit('foo')"/>
1297+
</template>
1298+
<script>
1299+
export default {
1300+
name: '',
1301+
props: {},
1302+
emits: {'foo': null}
1303+
}
1304+
</script>
1305+
`
1306+
}
1307+
]
1308+
}
1309+
]
1310+
},
1311+
{
1312+
filename: 'test.vue',
1313+
code: `
1314+
<template>
1315+
<div @click="$emit('foo')"/>
1316+
</template>
1317+
<script>
1318+
export default {
1319+
name: '',
1320+
watch: {}
1321+
}
1322+
</script>
1323+
`,
1324+
errors: [
1325+
{
1326+
message:
1327+
'The "foo" event has been triggered but not declared on `emits` option.',
1328+
suggestions: [
1329+
{
1330+
desc:
1331+
'Add the `emits` option with array syntax and define "foo" event.',
1332+
output: `
1333+
<template>
1334+
<div @click="$emit('foo')"/>
1335+
</template>
1336+
<script>
1337+
export default {
1338+
name: '',
12851339
emits: ['foo'],
1286-
props: {}
1340+
watch: {}
12871341
}
12881342
</script>
12891343
`
@@ -1299,7 +1353,7 @@ emits: ['foo'],
12991353
export default {
13001354
name: '',
13011355
emits: {'foo': null},
1302-
props: {}
1356+
watch: {}
13031357
}
13041358
</script>
13051359
`

0 commit comments

Comments
 (0)