Skip to content

Commit

Permalink
check composable in member expression
Browse files Browse the repository at this point in the history
  • Loading branch information
ktsn committed Jun 13, 2023
1 parent 3194be0 commit 1a62c8d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/rules/composable-placement.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Rule } from 'eslint'
import * as ESTree from 'estree'
import { AST } from 'vue-eslint-parser'

const composableNameRE = /^use[A-Z0-9]/
Expand All @@ -24,10 +25,23 @@ function getScriptSetupElement(context: Rule.RuleContext): AST.VElement | null {
return scripts.find((e) => hasAttribute(e, 'setup')) ?? null
}

function getCalleeName(node: ESTree.Node): string | null {
if (node.type === 'Identifier') {
return node.name
}

if (node.type === 'MemberExpression') {
return getCalleeName(node.property)
}

return null
}

export default {
meta: {
type: 'problem',
},

create(context) {
const scriptSetup = getScriptSetupElement(context)

Expand All @@ -44,10 +58,7 @@ export default {

return {
CallExpression(node) {
if (
node.callee.type === 'Identifier' &&
node.callee.name.match(composableNameRE)
) {
if (getCalleeName(node.callee)?.match(composableNameRE)) {
const scope = context.sourceCode.getScope(node)
const block = scope.block as Rule.Node

Expand Down
19 changes: 19 additions & 0 deletions test/composable-placement.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ describe('vue-composable/composable-placement', () => {
}
`,
},
{
code: `
import * as foo from './foo'
export function useBar() {
foo.useFoo()
}
`,
},
{
code: `
import { defineComponent } from 'vue'
Expand Down Expand Up @@ -76,6 +85,16 @@ describe('vue-composable/composable-placement', () => {
`,
errors: [{}],
},
{
code: `
import * as foo from './foo'
export function bar() {
foo.useFoo()
}
`,
errors: [{}],
},
{
code: `
import { useFoo } from './foo'
Expand Down

0 comments on commit 1a62c8d

Please sign in to comment.