Skip to content

Commit 257c814

Browse files
committedJun 1, 2023
fix: [one-component-per-file] do not check createApp function written by the developer vuejs#2201
1 parent d815a48 commit 257c814

File tree

5 files changed

+50
-5
lines changed

5 files changed

+50
-5
lines changed
 

‎lib/rules/one-component-per-file.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ module.exports = {
2929
{},
3030
utils.executeOnVueComponent(context, (node, type) => {
3131
if (type === 'definition') {
32-
const defType = getVueComponentDefinitionType(node)
32+
const defType = getVueComponentDefinitionType(context, node)
3333
if (defType === 'mixin') {
3434
return
3535
}

‎lib/rules/require-expose.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ module.exports = {
272272
return
273273
}
274274
if (type === 'definition') {
275-
const defType = getVueComponentDefinitionType(component)
275+
const defType = getVueComponentDefinitionType(context, component)
276276
if (defType === 'mixin') {
277277
return
278278
}

‎lib/rules/require-name-property.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ module.exports = {
6060
}
6161
return utils.executeOnVue(context, (component, type) => {
6262
if (type === 'definition') {
63-
const defType = getVueComponentDefinitionType(component)
63+
const defType = getVueComponentDefinitionType(context, component)
6464
if (defType === 'mixin') {
6565
return
6666
}

‎lib/utils/index.js

+20-2
Original file line numberDiff line numberDiff line change
@@ -2479,10 +2479,11 @@ function isVueComponentFile(node, path) {
24792479
/**
24802480
* Get the Vue component definition type from given node
24812481
* Vue.component('xxx', {}) || component('xxx', {})
2482+
* @param {RuleContext} context The rule context to use parser services.
24822483
* @param {ObjectExpression} node Node to check
24832484
* @returns {'component' | 'mixin' | 'extend' | 'createApp' | 'defineComponent' | null}
24842485
*/
2485-
function getVueComponentDefinitionType(node) {
2486+
function getVueComponentDefinitionType(context, node) {
24862487
const parent = getParent(node)
24872488
if (parent.type === 'CallExpression') {
24882489
const callee = parent.callee
@@ -2527,6 +2528,23 @@ function getVueComponentDefinitionType(node) {
25272528
if (callee.name === 'createApp') {
25282529
// for Vue.js 3.x
25292530
// createApp({})
2531+
2532+
const variable = findVariable(context.getScope(), callee)
2533+
2534+
// only lint the createApp function that import from vue
2535+
if(variable !== null && variable.defs.length === 1) {
2536+
const def = variable.defs[0]
2537+
if (
2538+
def.type === 'ImportBinding' &&
2539+
def.node.type === 'ImportSpecifier' &&
2540+
def.node.imported.type === 'Identifier' &&
2541+
def.node.parent.type === 'ImportDeclaration' &&
2542+
def.node.parent.source.value !== 'vue'
2543+
) {
2544+
return null
2545+
}
2546+
}
2547+
25302548
const isAppVueComponent = isObjectArgument(parent)
25312549
return isAppVueComponent ? 'createApp' : null
25322550
}
@@ -2603,7 +2621,7 @@ function getVueObjectType(context, node) {
26032621
case 'CallExpression': {
26042622
// Vue.component('xxx', {}) || component('xxx', {})
26052623
if (
2606-
getVueComponentDefinitionType(node) != null &&
2624+
getVueComponentDefinitionType(context, node) != null &&
26072625
skipTSAsExpression(parent.arguments.slice(-1)[0]) === node
26082626
) {
26092627
return 'definition'

‎tests/lib/rules/one-component-per-file.js

+27
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,21 @@ ruleTester.run('one-component-per-file', rule, {
5454
Vue.mixin({})
5555
Vue.component('name', {})
5656
`
57+
},
58+
{
59+
filename: 'test.js',
60+
code: `
61+
import { createApp } from 'vue'
62+
createApp({})
63+
`
64+
},
65+
{
66+
filename: 'test.js',
67+
code: `
68+
import { createApp } from 'other.js'
69+
createApp({})
70+
createApp({})
71+
`
5772
}
5873
],
5974
invalid: [
@@ -95,6 +110,18 @@ ruleTester.run('one-component-per-file', rule, {
95110
'There is more than one component in this file.',
96111
'There is more than one component in this file.'
97112
]
113+
},
114+
{
115+
filename: 'test.vue',
116+
code: `
117+
import { createApp } from 'vue'
118+
createApp({})
119+
createApp({})
120+
`,
121+
errors: [
122+
'There is more than one component in this file.',
123+
'There is more than one component in this file.'
124+
]
98125
}
99126
]
100127
})

0 commit comments

Comments
 (0)