Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
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