Skip to content

Commit

Permalink
Fix detect Nuxt3 defineNuxtComponent (#2311)
Browse files Browse the repository at this point in the history
  • Loading branch information
sugitata authored Nov 27, 2024
1 parent 618f49c commit d27c7e6
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 3 deletions.
14 changes: 11 additions & 3 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1581,7 +1581,7 @@ module.exports = {
* Get the Vue component definition type from given node
* Vue.component('xxx', {}) || component('xxx', {})
* @param {ObjectExpression} node Node to check
* @returns {'component' | 'mixin' | 'extend' | 'createApp' | 'defineComponent' | null}
* @returns {'component' | 'mixin' | 'extend' | 'createApp' | 'defineComponent' | 'defineNuxtComponent' | null}
*/
getVueComponentDefinitionType,
/**
Expand Down Expand Up @@ -2755,7 +2755,7 @@ function isVueComponentFile(node, path) {
* Get the Vue component definition type from given node
* Vue.component('xxx', {}) || component('xxx', {})
* @param {ObjectExpression} node Node to check
* @returns {'component' | 'mixin' | 'extend' | 'createApp' | 'defineComponent' | null}
* @returns {'component' | 'mixin' | 'extend' | 'createApp' | 'defineComponent' | 'defineNuxtComponent' | null}
*/
function getVueComponentDefinitionType(node) {
const parent = getParent(node)
Expand Down Expand Up @@ -2811,6 +2811,12 @@ function getVueComponentDefinitionType(node) {
const isDestructedVueComponent = isObjectArgument(parent)
return isDestructedVueComponent ? 'defineComponent' : null
}
if (callee.name === 'defineNuxtComponent') {
// for Nuxt 3.x
// defineNuxtComponent({})
const isDestructedVueComponent = isObjectArgument(parent)
return isDestructedVueComponent ? 'defineNuxtComponent' : null
}
}
}

Expand Down Expand Up @@ -2955,7 +2961,9 @@ function isSFCObject(context, node) {
}
const { callee } = parent
if (
(callee.type === 'Identifier' && callee.name === 'defineComponent') ||
(callee.type === 'Identifier' &&
(callee.name === 'defineComponent' ||
callee.name === 'defineNuxtComponent')) ||
(callee.type === 'MemberExpression' &&
callee.object.type === 'Identifier' &&
callee.object.name === 'Vue' &&
Expand Down
15 changes: 15 additions & 0 deletions tests/lib/rules/no-undef-components.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,21 @@ tester.run('no-undef-components', rule, {
}
]
},
{
filename: 'test.vue',
code: `
<template>
<CustomComponent />
</template>
<script>
export default defineNuxtComponent({
components: {
CustomComponent
}
})
</script>
`
},
{
filename: 'test.vue',
code: `
Expand Down
78 changes: 78 additions & 0 deletions tests/lib/rules/order-in-components.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,84 @@ ruleTester.run('order-in-components', rule, {
}
]
},
{
filename: 'test.vue',
code: `
import { defineComponent } from 'vue'
export default defineComponent({
name: 'app',
data () {
return {
msg: 'Welcome to Your Vue.js App'
}
},
props: {
propA: Number,
},
})
`,
output: `
import { defineComponent } from 'vue'
export default defineComponent({
name: 'app',
props: {
propA: Number,
},
data () {
return {
msg: 'Welcome to Your Vue.js App'
}
},
})
`,
languageOptions,
errors: [
{
message:
'The "props" property should be above the "data" property on line 5.',
line: 10
}
]
},
{
filename: 'test.vue',
code: `
import { defineNuxtComponent } from '#app'
export default defineNuxtComponent({
name: 'app',
data () {
return {
msg: 'Welcome to Your Vue.js App'
}
},
props: {
propA: Number,
},
})
`,
output: `
import { defineNuxtComponent } from '#app'
export default defineNuxtComponent({
name: 'app',
props: {
propA: Number,
},
data () {
return {
msg: 'Welcome to Your Vue.js App'
}
},
})
`,
languageOptions,
errors: [
{
message:
'The "props" property should be above the "data" property on line 5.',
line: 10
}
]
},
{
filename: 'test.jsx',
code: `
Expand Down
6 changes: 6 additions & 0 deletions tests/lib/utils/vue-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,12 @@ function invalidTests(ext) {
code: `export default defineComponent({})`,
languageOptions,
errors: [makeError(1)]
},
{
filename: `test.${ext}`,
code: `export default defineNuxtComponent({})`,
languageOptions,
errors: [makeError(1)]
}
]
}
Expand Down

0 comments on commit d27c7e6

Please sign in to comment.