Skip to content

Commit ac8d148

Browse files
committed
Add support for defineOptions and defineSlots to vue/no-unsupported-features rule
1 parent 1699cd2 commit ac8d148

File tree

6 files changed

+177
-2
lines changed

6 files changed

+177
-2
lines changed

Diff for: docs/rules/no-unsupported-features.md

+5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ This rule reports unsupported Vue.js syntax on the specified version.
2929
- `version` ... The `version` option accepts [the valid version range of `node-semver`](https://github.com/npm/node-semver#range-grammar). Set the version of Vue.js you are using. This option is required.
3030
- `ignores` ... You can use this `ignores` option to ignore the given features.
3131
The `"ignores"` option accepts an array of the following strings.
32+
- Vue.js 3.3.0+
33+
- `"define-slots"` ... `defineSlots()` macro.
34+
- `"define-options"` ... `defineOptions()` macro.
3235
- Vue.js 3.2.0+
3336
- `"v-memo"` ... [v-memo](https://vuejs.org/api/built-in-directives.html#v-memo) directive.
3437
- `"v-bind-prop-modifier-shorthand"` ... `v-bind` with `.prop` modifier shorthand.
@@ -100,6 +103,8 @@ The `"ignores"` option accepts an array of the following strings.
100103

101104
## :books: Further Reading
102105

106+
- [API - defineOptions()](https://vuejs.org/api/sfc-script-setup.html#defineoptions)
107+
- [API - defineSlots()](https://vuejs.org/api/sfc-script-setup.html#defineslots)
103108
- [API - v-memo](https://vuejs.org/api/built-in-directives.html#v-memo)
104109
- [API - v-is](https://v3.vuejs.org/api/directives.html#v-is)
105110
- [API - v-is (Old)](https://github.com/vuejs/docs-next/blob/008613756c3d781128d96b64a2d27f7598f8f548/src/api/directives.md#v-is)

Diff for: lib/rules/no-unsupported-features.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ const FEATURES = {
3232
// Vue.js 3.2.0+
3333
'v-memo': require('./syntaxes/v-memo'),
3434
'v-bind-prop-modifier-shorthand': require('./syntaxes/v-bind-prop-modifier-shorthand'),
35-
'v-bind-attr-modifier': require('./syntaxes/v-bind-attr-modifier')
35+
'v-bind-attr-modifier': require('./syntaxes/v-bind-attr-modifier'),
36+
// Vue.js 3.3.0+
37+
'define-options': require('./syntaxes/define-options'),
38+
'define-slots': require('./syntaxes/define-slots')
3639
}
3740

3841
const SYNTAX_NAMES = /** @type {(keyof FEATURES)[]} */ (Object.keys(FEATURES))
@@ -115,7 +118,12 @@ module.exports = {
115118
forbiddenVBindPropModifierShorthand:
116119
'`.prop` shorthand are not supported until Vue.js "3.2.0".',
117120
forbiddenVBindAttrModifier:
118-
'`.attr` modifiers on `v-bind` are not supported until Vue.js "3.2.0".'
121+
'`.attr` modifiers on `v-bind` are not supported until Vue.js "3.2.0".',
122+
// Vue.js 3.3.0+
123+
forbiddenDefineOptions:
124+
'`defineOptions()` macros are not supported until Vue.js "3.3.0".',
125+
forbiddenDefineSlots:
126+
'`defineSlots()` macros are not supported until Vue.js "3.3.0".'
119127
}
120128
},
121129
/** @param {RuleContext} context */

Diff for: lib/rules/syntaxes/define-options.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @author Yosuke Ota
3+
* See LICENSE file in root directory for full license.
4+
*/
5+
'use strict'
6+
7+
const utils = require('../../utils/index')
8+
9+
module.exports = {
10+
supported: '>=3.3.0',
11+
/** @param {RuleContext} context @returns {RuleListener} */
12+
createScriptVisitor(context) {
13+
return utils.defineScriptSetupVisitor(context, {
14+
onDefineOptionsEnter(node) {
15+
context.report({
16+
node,
17+
messageId: 'forbiddenDefineOptions'
18+
})
19+
}
20+
})
21+
}
22+
}

Diff for: lib/rules/syntaxes/define-slots.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @author Yosuke Ota
3+
* See LICENSE file in root directory for full license.
4+
*/
5+
'use strict'
6+
7+
const utils = require('../../utils/index')
8+
9+
module.exports = {
10+
supported: '>=3.3.0',
11+
/** @param {RuleContext} context @returns {RuleListener} */
12+
createScriptVisitor(context) {
13+
return utils.defineScriptSetupVisitor(context, {
14+
onDefineSlotsEnter(node) {
15+
context.report({
16+
node,
17+
messageId: 'forbiddenDefineSlots'
18+
})
19+
}
20+
})
21+
}
22+
}
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* @author Yosuke Ota
3+
* See LICENSE file in root directory for full license.
4+
*/
5+
'use strict'
6+
7+
const RuleTester = require('eslint').RuleTester
8+
const rule = require('../../../../lib/rules/no-unsupported-features')
9+
const utils = require('./utils')
10+
11+
const buildOptions = utils.optionsBuilder('define-options', '^3.2.0')
12+
const tester = new RuleTester({
13+
parser: require.resolve('vue-eslint-parser'),
14+
parserOptions: {
15+
ecmaVersion: 2019
16+
}
17+
})
18+
19+
tester.run('no-unsupported-features/define-options', rule, {
20+
valid: [
21+
{
22+
code: `
23+
<script setup>
24+
defineOptions({})
25+
</script>`,
26+
options: buildOptions({ version: '^3.3.0' })
27+
},
28+
{
29+
code: `
30+
<script setup>
31+
defineProps({})
32+
</script>`,
33+
options: buildOptions()
34+
},
35+
{
36+
code: `
37+
<script setup>
38+
defineOptions({})
39+
</script>`,
40+
options: buildOptions({ version: '^3.0.0', ignores: ['define-options'] })
41+
}
42+
],
43+
invalid: [
44+
{
45+
code: `
46+
<script setup>
47+
defineOptions({})
48+
</script>`,
49+
options: buildOptions(),
50+
errors: [
51+
{
52+
message:
53+
'`defineOptions()` macros are not supported until Vue.js "3.3.0".',
54+
line: 3
55+
}
56+
]
57+
}
58+
]
59+
})
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* @author Yosuke Ota
3+
* See LICENSE file in root directory for full license.
4+
*/
5+
'use strict'
6+
7+
const RuleTester = require('eslint').RuleTester
8+
const rule = require('../../../../lib/rules/no-unsupported-features')
9+
const utils = require('./utils')
10+
11+
const buildOptions = utils.optionsBuilder('define-slots', '^3.2.0')
12+
const tester = new RuleTester({
13+
parser: require.resolve('vue-eslint-parser'),
14+
parserOptions: {
15+
ecmaVersion: 2019
16+
}
17+
})
18+
19+
tester.run('no-unsupported-features/define-slots', rule, {
20+
valid: [
21+
{
22+
code: `
23+
<script setup>
24+
const slots = defineSlots()
25+
</script>`,
26+
options: buildOptions({ version: '^3.3.0' })
27+
},
28+
{
29+
code: `
30+
<script setup>
31+
defineProps({})
32+
</script>`,
33+
options: buildOptions()
34+
},
35+
{
36+
code: `
37+
<script setup>
38+
const slots = defineSlots()
39+
</script>`,
40+
options: buildOptions({ version: '^3.0.0', ignores: ['define-slots'] })
41+
}
42+
],
43+
invalid: [
44+
{
45+
code: `
46+
<script setup>
47+
const slots = defineSlots()
48+
</script>`,
49+
options: buildOptions(),
50+
errors: [
51+
{
52+
message:
53+
'`defineSlots()` macros are not supported until Vue.js "3.3.0".',
54+
line: 3
55+
}
56+
]
57+
}
58+
]
59+
})

0 commit comments

Comments
 (0)