Skip to content

feat(no-ref-as-operand): use ref.value to replace ref when emit #2680

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

Merged
merged 8 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from 6 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
4 changes: 3 additions & 1 deletion docs/rules/no-ref-as-operand.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ You must use `.value` to access the `Ref` value.
import { ref } from 'vue'

export default {
setup() {
setup(_props, { emit }) {
const count = ref(0)
const ok = ref(true)

Expand All @@ -34,12 +34,14 @@ export default {
count.value + 1
1 + count.value
var msg = ok.value ? 'yes' : 'no'
emit('increment', count.value)

/* ✗ BAD */
count++
count + 1
1 + count
var msg = ok ? 'yes' : 'no'
emit('increment', count)

return {
count
Expand Down
342 changes: 262 additions & 80 deletions lib/rules/no-ref-as-operand.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
'use strict'

const { findVariable } = require('@eslint-community/eslint-utils')
const { extractRefObjectReferences } = require('../utils/ref-object-references')
const utils = require('../utils')

Expand All @@ -24,6 +25,40 @@ function isRefInit(data) {
}
return data.defineChain.includes(/** @type {any} */ (init))
}

/**
* Get the callee member node from the given CallExpression
* @param {CallExpression} node CallExpression
*/
function getNameParamNode(node) {
const nameLiteralNode = node.arguments[0]
if (nameLiteralNode && utils.isStringLiteral(nameLiteralNode)) {
const name = utils.getStringLiteralValue(nameLiteralNode)
if (name != null) {
return { name, loc: nameLiteralNode.loc }
}
}

// cannot check
return null
}

/**
* Get the callee member node from the given CallExpression
* @param {CallExpression} node CallExpression
*/
function getCalleeMemberNode(node) {
const callee = utils.skipChainExpression(node.callee)

if (callee.type === 'MemberExpression') {
const name = utils.getStaticPropertyName(callee)
if (name) {
return { name, member: callee }
}
}
return null
}

module.exports = {
meta: {
type: 'suggestion',
Expand All @@ -44,6 +79,7 @@ module.exports = {
create(context) {
/** @type {RefObjectReferences} */
let refReferences
const setupContexts = new Map()

/**
* @param {Identifier} node
Expand All @@ -64,90 +100,236 @@ module.exports = {
}
})
}
return {
Program() {
refReferences = extractRefObjectReferences(context)
},
// if (refValue)
/** @param {Identifier} node */
'IfStatement>Identifier'(node) {
reportIfRefWrapped(node)
},
// switch (refValue)
/** @param {Identifier} node */
'SwitchStatement>Identifier'(node) {
reportIfRefWrapped(node)
},
// -refValue, +refValue, !refValue, ~refValue, typeof refValue
/** @param {Identifier} node */
'UnaryExpression>Identifier'(node) {
reportIfRefWrapped(node)
},
// refValue++, refValue--
/** @param {Identifier} node */
'UpdateExpression>Identifier'(node) {
reportIfRefWrapped(node)
},
// refValue+1, refValue-1
/** @param {Identifier} node */
'BinaryExpression>Identifier'(node) {
reportIfRefWrapped(node)
},
// refValue+=1, refValue-=1, foo+=refValue, foo-=refValue
/** @param {Identifier & {parent: AssignmentExpression}} node */
'AssignmentExpression>Identifier'(node) {
if (node.parent.operator === '=' && node.parent.left !== node) {
return
}
reportIfRefWrapped(node)
},
// refValue || other, refValue && other. ignore: other || refValue
/** @param {Identifier & {parent: LogicalExpression}} node */
'LogicalExpression>Identifier'(node) {
if (node.parent.left !== node) {
return
}
// Report only constants.
const data = refReferences.get(node)
if (
!data ||
!data.variableDeclaration ||
data.variableDeclaration.kind !== 'const'
) {
return
}
reportIfRefWrapped(node)
},
// refValue ? x : y
/** @param {Identifier & {parent: ConditionalExpression}} node */
'ConditionalExpression>Identifier'(node) {
if (node.parent.test !== node) {
return
}
reportIfRefWrapped(node)
},
// `${refValue}`
/** @param {Identifier} node */
'TemplateLiteral>Identifier'(node) {
reportIfRefWrapped(node)
},
// refValue.x
/** @param {Identifier & {parent: MemberExpression}} node */
'MemberExpression>Identifier'(node) {
if (node.parent.object !== node) {

const programNode = context.getSourceCode().ast

const callVisitor = {
/**
* @param {CallExpression} node
* @param {import('../utils').VueObjectData} [info]
*/
CallExpression(node, info) {
const nameWithLoc = getNameParamNode(node)
if (!nameWithLoc) {
// cannot check
return
}
const name = utils.getStaticPropertyName(node.parent)
if (
name === 'value' ||
name == null ||
// WritableComputedRef
name === 'effect'
) {
return

// verify setup context
const setupContext = setupContexts.get(info ? info.node : programNode)
if (setupContext) {
const { contextReferenceIds, emitReferenceIds } = setupContext
if (
node.callee.type === 'Identifier' &&
emitReferenceIds.has(node.callee)
) {
// verify setup(props,{emit}) {emit()}
node.arguments
.filter((node) => node.type === 'Identifier')
.forEach((node) => {
reportIfRefWrapped(node)
})
} else {
const emit = getCalleeMemberNode(node)
if (
emit &&
emit.name === 'emit' &&
emit.member.object.type === 'Identifier' &&
contextReferenceIds.has(emit.member.object)
) {
// verify setup(props,context) {context.emit()}
node.arguments
.filter((node) => node.type === 'Identifier')
.forEach((node) => {
reportIfRefWrapped(node)
})
}
}
}
reportIfRefWrapped(node)
}
}

return utils.compositingVisitors(
{
Program() {
refReferences = extractRefObjectReferences(context)
},
// if (refValue)
/** @param {Identifier} node */
'IfStatement>Identifier'(node) {
reportIfRefWrapped(node)
},
// switch (refValue)
/** @param {Identifier} node */
'SwitchStatement>Identifier'(node) {
reportIfRefWrapped(node)
},
// -refValue, +refValue, !refValue, ~refValue, typeof refValue
/** @param {Identifier} node */
'UnaryExpression>Identifier'(node) {
reportIfRefWrapped(node)
},
// refValue++, refValue--
/** @param {Identifier} node */
'UpdateExpression>Identifier'(node) {
reportIfRefWrapped(node)
},
// refValue+1, refValue-1
/** @param {Identifier} node */
'BinaryExpression>Identifier'(node) {
reportIfRefWrapped(node)
},
// refValue+=1, refValue-=1, foo+=refValue, foo-=refValue
/** @param {Identifier & {parent: AssignmentExpression}} node */
'AssignmentExpression>Identifier'(node) {
if (node.parent.operator === '=' && node.parent.left !== node) {
return
}
reportIfRefWrapped(node)
},
// refValue || other, refValue && other. ignore: other || refValue
/** @param {Identifier & {parent: LogicalExpression}} node */
'LogicalExpression>Identifier'(node) {
if (node.parent.left !== node) {
return
}
// Report only constants.
const data = refReferences.get(node)
if (
!data ||
!data.variableDeclaration ||
data.variableDeclaration.kind !== 'const'
) {
return
}
reportIfRefWrapped(node)
},
// refValue ? x : y
/** @param {Identifier & {parent: ConditionalExpression}} node */
'ConditionalExpression>Identifier'(node) {
if (node.parent.test !== node) {
return
}
reportIfRefWrapped(node)
},
// `${refValue}`
/** @param {Identifier} node */
'TemplateLiteral>Identifier'(node) {
reportIfRefWrapped(node)
},
// refValue.x
/** @param {Identifier & {parent: MemberExpression}} node */
'MemberExpression>Identifier'(node) {
if (node.parent.object !== node) {
return
}
const name = utils.getStaticPropertyName(node.parent)
if (
name === 'value' ||
name == null ||
// WritableComputedRef
name === 'effect'
) {
return
}
reportIfRefWrapped(node)
}
},
utils.compositingVisitors(
utils.defineScriptSetupVisitor(context, {
onDefineEmitsEnter(node) {
if (
!node.parent ||
node.parent.type !== 'VariableDeclarator' ||
node.parent.init !== node
) {
return
}

const emitParam = node.parent.id
if (emitParam.type !== 'Identifier') {
return
}

// const emit = defineEmits()
const variable = findVariable(
utils.getScope(context, emitParam),
emitParam
)
if (!variable) {
return
}
const emitReferenceIds = new Set()
for (const reference of variable.references) {
emitReferenceIds.add(reference.identifier)
}
setupContexts.set(programNode, {
contextReferenceIds: new Set(),
emitReferenceIds
})
},
...callVisitor
}),
utils.defineVueVisitor(context, {
onSetupFunctionEnter(node, { node: vueNode }) {
const contextParam = utils.skipDefaultParamValue(node.params[1])
if (!contextParam) {
// no arguments
return
}
if (
contextParam.type === 'RestElement' ||
contextParam.type === 'ArrayPattern'
) {
// cannot check
return
}
const contextReferenceIds = new Set()
const emitReferenceIds = new Set()
if (contextParam.type === 'ObjectPattern') {
const emitProperty = utils.findAssignmentProperty(
contextParam,
'emit'
)
if (!emitProperty || emitProperty.value.type !== 'Identifier') {
return
}
const emitParam = emitProperty.value
// `setup(props, {emit})`
const variable = findVariable(
utils.getScope(context, emitParam),
emitParam
)
if (!variable) {
return
}
for (const reference of variable.references) {
emitReferenceIds.add(reference.identifier)
}
} else {
// `setup(props, context)`
const variable = findVariable(
utils.getScope(context, contextParam),
contextParam
)
if (!variable) {
return
}
for (const reference of variable.references) {
contextReferenceIds.add(reference.identifier)
}
}
setupContexts.set(vueNode, {
contextReferenceIds,
emitReferenceIds
})
},
...callVisitor,
onVueObjectExit(node) {
setupContexts.delete(node)
}
})
)
)
}
}
Loading