Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vuex and Pinia support for no-undef-properties #2513

Merged
merged 7 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 56 additions & 1 deletion lib/rules/no-undef-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ module.exports = {
).map(toRegExp)
const propertyReferenceExtractor = definePropertyReferenceExtractor(context)
const programNode = context.getSourceCode().ast
/**
* Property names identified as defined via a Vuex or Pinia helpers
* @type {string[]}
*/
const propertiesDefinedByVuexOrPiniaHelpers = []
marcjfj-vmlyr marked this conversation as resolved.
Show resolved Hide resolved

/**
* @param {ASTNode} node
Expand Down Expand Up @@ -185,7 +190,8 @@ module.exports = {
report(node, name, messageId = 'undef') {
if (
reserved.includes(name) ||
ignores.some((ignore) => ignore.test(name))
ignores.some((ignore) => ignore.test(name)) ||
propertiesDefinedByVuexOrPiniaHelpers.includes(name)
) {
return
}
Expand Down Expand Up @@ -331,6 +337,55 @@ module.exports = {
}
}),
utils.defineVueVisitor(context, {
/**
* @param {CallExpression} node
*/
CallExpression(node) {
if (node.callee.type !== 'Identifier') return
/** @type {'methods'|'computed'|null} */
let groupName = null
if (/^mapMutations|mapActions$/u.test(node.callee.name)) {
groupName = GROUP_METHODS
} else if (
/^mapState|mapGetters|mapWritableState$/u.test(node.callee.name)
) {
groupName = GROUP_COMPUTED_PROPERTY
}

if (!groupName || node.arguments.length === 0) return
// On Pinia the store is always the first argument
const arg =
node.arguments.length === 2 ? node.arguments[1] : node.arguments[0]
if (arg.type === 'ObjectExpression') {
// e.g.
// `mapMutations({ add: 'increment' })`
// `mapState({ count: state => state.todosCount })`
for (const prop of arg.properties) {
const name =
prop.type === 'SpreadElement'
? null
: utils.getStaticPropertyName(prop)
if (name) {
propertiesDefinedByVuexOrPiniaHelpers.push(name)
}
}
} else if (arg.type === 'ArrayExpression') {
// e.g. `mapMutations(['add'])`
for (const element of arg.elements) {
if (
!element ||
(element.type !== 'Literal' &&
element.type !== 'TemplateLiteral')
) {
marcjfj-vmlyr marked this conversation as resolved.
Show resolved Hide resolved
continue
}
const name = utils.getStringLiteralValue(element)
if (name) {
propertiesDefinedByVuexOrPiniaHelpers.push(name)
}
}
}
},
onVueObjectEnter(node) {
const ctx = getVueComponentContext(node)

Expand Down
241 changes: 241 additions & 0 deletions tests/lib/rules/no-undef-properties.js
marcjfj-vmlyr marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,248 @@ tester.run('no-undef-properties', rule, {
}
}
},
{
// Vuex
filename: 'test.vue',
code: `
<script>
import { mapState } from 'Vuex';
marcjfj-vmlyr marked this conversation as resolved.
Show resolved Hide resolved

export default {
computed: {
...mapState({
a: (vm) => vm.a,
b: (vm) => vm.b,
})
},
methods: {
c() {return this.a * this.b}
}
}
</script>
<template>
{{ a }} {{ b }}
</template>
`
},
{
filename: 'test.vue',
code: `
<script>
import { mapActions } from 'Vuex';

export default {
methods: {
...mapActions({
a: 'a',
b: 'b',
}),
c() {return this.a()},
d() {return this.b()},
}
}
</script>
<template>
{{ a }} {{ b }}
</template>
`
},
{
filename: 'test.vue',
code: `
<script>
import { mapMutations } from 'Vuex';

export default {
methods: {
...mapMutations({
a: 'a',
b: 'b',
}),
c() {return this.a()},
d() {return this.b()},
}
}
</script>
<template>
{{ a }} {{ b }}
</template>
`
},
{
filename: 'test.vue',
code: `
<script>
import { mapActions } from 'Vuex';

export default {
methods: {
...mapActions(['a', 'b']),
c() {return this.a()},
d() {return this.b()},
}
}
</script>
<template>
{{ a }} {{ b }}
</template>
`
},
{
filename: 'test.vue',
code: `
<script>
import { mapMutations } from 'Vuex';

export default {
methods: {
...mapMutations(['a', 'b']),
c() {return this.a()},
d() {return this.b()},
}
}
</script>
<template>
{{ a }} {{ b }}
</template>
`
},
{
filename: 'test.vue',
code: `
<script>
import { mapGetters } from 'Vuex';

export default {
computed: {
...mapGetters(['a', 'b'])
},
methods: {
c() {return this.a},
d() {return this.b},
}
}
</script>
<template>
{{ a }} {{ b }}
</template>
`
},
{
// Pinia
filename: 'test.vue',
code: `
<script>
import { mapGetters } from 'pinia'
import { useStore } from '../store'

export default {
computed: {
...mapGetters(useStore, ['a', 'b'])
},
methods: {
c() {return this.a},
d() {return this.b},
}
}
</script>
<template>
{{ a }} {{ b }}
</template>
`
},
{
filename: 'test.vue',
code: `
<script>
import { mapState } from 'pinia'
import { useStore } from '../store'

export default {
computed: {
...mapState(useStore, {
a: 'a',
b: store => store.b,
})
},
methods: {
c() {return this.a},
d() {return this.b},
}
}
</script>
<template>
{{ a }} {{ b }}
</template>
`
},
{
filename: 'test.vue',
code: `
<script>
import { mapWritableState } from 'pinia'
import { useStore } from '../store'

export default {
computed: {
...mapWritableState(useStore, {
a: 'a',
b: 'b',
})
},
methods: {
c() {return this.a},
d() {return this.b},
}
}
</script>
<template>
{{ a }} {{ b }}
</template>
`
},
{
filename: 'test.vue',
code: `
<script>
import { mapWritableState } from 'pinia'
import { useStore } from '../store'

export default {
computed: {
...mapWritableState(useStore, ['a', 'b'])
},
methods: {
c() {return this.a},
d() {return this.b},
}
}
</script>
<template>
{{ a }} {{ b }}
</template>
`
},
{
filename: 'test.vue',
code: `
<script>
import { mapActions } from 'pinia'
import { useStore } from '../store'

export default {
methods: {
...mapActions(useStore, ['a', 'b']),
c() {return this.a()},
d() {return this.b()},
}
}
</script>
<template>
{{ a() }} {{ b() }}
</template>
`
},
`
<script setup>
const model = defineModel();
Expand Down