Skip to content

Commit cac8167

Browse files
authored
fix(no-setup-props-reactivity-loss): report template literal (#2489)
1 parent 3ad09ef commit cac8167

File tree

2 files changed

+60
-3
lines changed

2 files changed

+60
-3
lines changed

lib/rules/no-setup-props-reactivity-loss.js

+22-3
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,35 @@ module.exports = {
7979
left.type !== 'ArrayPattern' &&
8080
left.type !== 'ObjectPattern' &&
8181
rightNode.type !== 'MemberExpression' &&
82-
rightNode.type !== 'ConditionalExpression'
82+
rightNode.type !== 'ConditionalExpression' &&
83+
rightNode.type !== 'TemplateLiteral'
8384
) {
8485
return
8586
}
8687

87-
/** @type {Expression | Super} */
88-
let rightId = rightNode
88+
if (rightNode.type === 'TemplateLiteral') {
89+
rightNode.expressions.some((expression) =>
90+
checkMemberAccess(expression, propsReferences, left, right)
91+
)
92+
} else {
93+
checkMemberAccess(rightNode, propsReferences, left, right)
94+
}
95+
}
96+
97+
/**
98+
* @param {Expression | Super} rightId
99+
* @param {ScopePropsReferences} propsReferences
100+
* @param {Pattern} left
101+
* @param {Expression} right
102+
* @return {boolean}
103+
*/
104+
function checkMemberAccess(rightId, propsReferences, left, right) {
89105
while (rightId.type === 'MemberExpression') {
90106
rightId = utils.skipChainExpression(rightId.object)
91107
}
92108
if (rightId.type === 'Identifier' && propsReferences.refs.has(rightId)) {
93109
report(left, 'getProperty', propsReferences.scopeName)
110+
return true
94111
}
95112
if (
96113
rightId.type === 'ConditionalExpression' &&
@@ -99,7 +116,9 @@ module.exports = {
99116
isPropsMemberAccessed(rightId.alternate, propsReferences))
100117
) {
101118
report(right, 'getProperty', propsReferences.scopeName)
119+
return true
102120
}
121+
return false
103122
}
104123

105124
/**

tests/lib/rules/no-setup-props-reactivity-loss.js

+38
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ tester.run('no-setup-props-reactivity-loss', rule, {
144144
const {x} = noProps
145145
({y} = noProps)
146146
const z = noProps.z
147+
const foo = \`\${noProp.foo}\`
147148
}
148149
}
149150
</script>
@@ -717,6 +718,43 @@ tester.run('no-setup-props-reactivity-loss', rule, {
717718
line: 4
718719
}
719720
]
721+
},
722+
{
723+
// https://github.com/vuejs/eslint-plugin-vue/issues/2470
724+
filename: 'test.vue',
725+
code: `
726+
<script>
727+
export default {
728+
setup(p) {
729+
const foo = \`\${p.x}\`
730+
}
731+
}
732+
</script>
733+
`,
734+
errors: [
735+
{
736+
messageId: 'getProperty',
737+
line: 5
738+
}
739+
]
740+
},
741+
{
742+
filename: 'test.vue',
743+
code: `
744+
<script>
745+
export default {
746+
setup(p) {
747+
const foo = \`bar\${p.x}bar\${p.y}\`
748+
}
749+
}
750+
</script>
751+
`,
752+
errors: [
753+
{
754+
messageId: 'getProperty',
755+
line: 5
756+
}
757+
]
720758
}
721759
]
722760
})

0 commit comments

Comments
 (0)