Skip to content

Commit

Permalink
Add Vuex support to vue/no-unused-properties (#2276)
Browse files Browse the repository at this point in the history
Co-authored-by: Flo Edelmann <[email protected]>
  • Loading branch information
fuale and FloEdelmann authored Dec 28, 2023
1 parent 63a17bd commit a89dd10
Show file tree
Hide file tree
Showing 2 changed files with 753 additions and 2 deletions.
84 changes: 82 additions & 2 deletions lib/rules/no-unused-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ module.exports = {
}
return container
}

/**
* @param {string[]} segments
* @param {Expression} propertyValue
Expand Down Expand Up @@ -350,6 +351,7 @@ module.exports = {
) {
continue
}

if (propertyReferences.hasProperty(property.name)) {
// used
if (
Expand Down Expand Up @@ -453,8 +455,84 @@ module.exports = {
}
}),
utils.defineVueVisitor(context, {
onVueObjectEnter(node) {
const container = getVueComponentPropertiesContainer(node)
/**
* e.g. `mapMutations({ add: 'increment' })`
* @param {Property} node
* @param {VueObjectData} vueData
*/
'CallExpression[callee.name=/^(mapMutations|mapActions)$/][arguments] ObjectExpression Property'(
node,
vueData
) {
const container = getVueComponentPropertiesContainer(vueData.node)

container.properties.push({
type: 'array',
name: utils.getStaticPropertyName(node),
groupName: 'methods',
node
})
},

/**
* e.g. `mapMutations(['add'])`
* @param {Literal} node
* @param {VueObjectData} vueData
*/
'CallExpression[callee.name=/^(mapMutations|mapActions)$/][arguments] ArrayExpression Literal'(
node,
vueData
) {
const container = getVueComponentPropertiesContainer(vueData.node)

container.properties.push({
type: 'array',
name: utils.getStringLiteralValue(node),
groupName: 'methods',
node
})
},

/**
* e.g. `mapState({ count: state => state.todosCount })`
* @param {Property} node
* @param {VueObjectData} vueData
*/
'CallExpression[callee.name=/^(mapState|mapGetters)$/][arguments] ObjectExpression Property'(
node,
vueData
) {
const container = getVueComponentPropertiesContainer(vueData.node)

container.properties.push({
type: 'array',
name: utils.getStaticPropertyName(node),
groupName: 'computed',
node
})
},

/**
* e.g. `mapState(['count'])`
* @param {Literal} node
* @param {VueObjectData} vueData
*/
'CallExpression[callee.name=/^(mapState|mapGetters)$/][arguments] ArrayExpression Literal'(
node,
vueData
) {
const container = getVueComponentPropertiesContainer(vueData.node)

container.properties.push({
type: 'array',
name: utils.getStringLiteralValue(node),
groupName: 'computed',
node
})
},

onVueObjectEnter(node, vueNode) {
const container = getVueComponentPropertiesContainer(vueNode.node)

for (const watcherOrExpose of utils.iterateProperties(
node,
Expand Down Expand Up @@ -495,8 +573,10 @@ module.exports = {
)
}
}

container.properties.push(...utils.iterateProperties(node, groups))
},

/** @param { (FunctionExpression | ArrowFunctionExpression) & { parent: Property }} node */
'ObjectExpression > Property > :function[params.length>0]'(
node,
Expand Down
Loading

0 comments on commit a89dd10

Please sign in to comment.