Skip to content

Commit 8efec75

Browse files
authored
Add vue/component-api-style rule (#1626)
* Add `vue/component-api-style` rule * Update package.json
1 parent eaf6584 commit 8efec75

File tree

6 files changed

+1047
-0
lines changed

6 files changed

+1047
-0
lines changed

docs/rules/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ For example:
288288
|:--------|:------------|:---|
289289
| [vue/block-lang](./block-lang.md) | disallow use other than available `lang` | |
290290
| [vue/block-tag-newline](./block-tag-newline.md) | enforce line breaks after opening and before closing block-level tags | :wrench: |
291+
| [vue/component-api-style](./component-api-style.md) | enforce component API style | |
291292
| [vue/component-name-in-template-casing](./component-name-in-template-casing.md) | enforce specific casing for the component naming style in template | :wrench: |
292293
| [vue/custom-event-name-casing](./custom-event-name-casing.md) | enforce specific casing for custom event name | |
293294
| [vue/html-button-has-type](./html-button-has-type.md) | disallow usage of button without an explicit type attribute | |

docs/rules/component-api-style.md

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
---
2+
pageClass: rule-details
3+
sidebarDepth: 0
4+
title: vue/component-api-style
5+
description: enforce component API style
6+
---
7+
# vue/component-api-style
8+
9+
> enforce component API style
10+
11+
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge>
12+
13+
## :book: Rule Details
14+
15+
This rule aims to make the API style you use to define Vue components consistent in your project.
16+
17+
For example, if you want to allow only `<script setup>` and Composition API.
18+
(This is the default for this rule.)
19+
20+
<eslint-code-block :rules="{'vue/component-api-style': ['error']}">
21+
22+
```vue
23+
<!-- ✓ GOOD -->
24+
<script setup>
25+
import { ref } from 'vue'
26+
const msg = ref('Hello World!')
27+
</script>
28+
```
29+
30+
</eslint-code-block>
31+
32+
<eslint-code-block :rules="{'vue/component-api-style': ['error']}">
33+
34+
```vue
35+
<script>
36+
import { ref } from 'vue'
37+
export default {
38+
/* ✓ GOOD */
39+
setup() {
40+
const msg = ref('Hello World!')
41+
// ...
42+
return {
43+
msg,
44+
// ...
45+
}
46+
}
47+
}
48+
</script>
49+
```
50+
51+
</eslint-code-block>
52+
53+
<eslint-code-block :rules="{'vue/component-api-style': ['error']}">
54+
55+
```vue
56+
<script>
57+
export default {
58+
/* ✗ BAD */
59+
data () {
60+
return {
61+
msg: 'Hello World!',
62+
// ...
63+
}
64+
},
65+
// ...
66+
}
67+
</script>
68+
```
69+
70+
</eslint-code-block>
71+
72+
## :wrench: Options
73+
74+
```json
75+
{
76+
"vue/component-api-style": ["error",
77+
["script-setup", "composition"] // "script-setup", "composition", or "options"
78+
]
79+
}
80+
```
81+
82+
- Array options ... Defines the API styles you want to allow. Default is `["script-setup", "composition"]`. You can use the following values.
83+
- `"script-setup"` ... If set, allows `<script setup>`.
84+
- `"composition"` ... If set, allows Composition API (not `<script setup>`).
85+
- `"options"` ... If set, allows Options API.
86+
87+
### `["options"]`
88+
89+
<eslint-code-block :rules="{'vue/component-api-style': ['error', ['options']]}">
90+
91+
```vue
92+
<script>
93+
export default {
94+
/* ✓ GOOD */
95+
data () {
96+
return {
97+
msg: 'Hello World!',
98+
// ...
99+
}
100+
},
101+
// ...
102+
}
103+
</script>
104+
```
105+
106+
</eslint-code-block>
107+
108+
<eslint-code-block :rules="{'vue/component-api-style': ['error', ['options']]}">
109+
110+
```vue
111+
<!-- ✗ BAD -->
112+
<script setup>
113+
import { ref } from 'vue'
114+
const msg = ref('Hello World!')
115+
</script>
116+
```
117+
118+
</eslint-code-block>
119+
120+
<eslint-code-block :rules="{'vue/component-api-style': ['error', ['options']]}">
121+
122+
```vue
123+
<script>
124+
import { ref } from 'vue'
125+
export default {
126+
/* ✗ BAD */
127+
setup() {
128+
const msg = ref('Hello World!')
129+
// ...
130+
return {
131+
msg,
132+
// ...
133+
}
134+
}
135+
}
136+
</script>
137+
```
138+
139+
</eslint-code-block>
140+
141+
## :mag: Implementation
142+
143+
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/component-api-style.js)
144+
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/component-api-style.js)

lib/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ module.exports = {
2121
'comma-spacing': require('./rules/comma-spacing'),
2222
'comma-style': require('./rules/comma-style'),
2323
'comment-directive': require('./rules/comment-directive'),
24+
'component-api-style': require('./rules/component-api-style'),
2425
'component-definition-name-casing': require('./rules/component-definition-name-casing'),
2526
'component-name-in-template-casing': require('./rules/component-name-in-template-casing'),
2627
'component-tags-order': require('./rules/component-tags-order'),

0 commit comments

Comments
 (0)