Skip to content

Commit

Permalink
Vuex and Pinia support for no-undef-properties (#2513)
Browse files Browse the repository at this point in the history
Co-authored-by: Flo Edelmann <[email protected]>
  • Loading branch information
marcjfj-vmlyr and FloEdelmann authored Sep 12, 2024
1 parent 3157fad commit 8b877f7
Show file tree
Hide file tree
Showing 2 changed files with 608 additions and 1 deletion.
53 changes: 52 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 {Set<string>}
*/
const propertiesDefinedByStoreHelpers = new Set()

/**
* @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)) ||
propertiesDefinedByStoreHelpers.has(name)
) {
return
}
Expand Down Expand Up @@ -331,6 +337,51 @@ 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) {
propertiesDefinedByStoreHelpers.add(name)
}
}
} else if (arg.type === 'ArrayExpression') {
// e.g. `mapMutations(['add'])`
for (const element of arg.elements) {
if (!element || !utils.isStringLiteral(element)) {
continue
}
const name = utils.getStringLiteralValue(element)
if (name) {
propertiesDefinedByStoreHelpers.add(name)
}
}
}
},
onVueObjectEnter(node) {
const ctx = getVueComponentContext(node)

Expand Down
Loading

0 comments on commit 8b877f7

Please sign in to comment.