Skip to content

Commit

Permalink
fix: useState hook x-model 不支持绑定到state的属性上
Browse files Browse the repository at this point in the history
  • Loading branch information
peakchen90 committed Oct 7, 2019
1 parent 615e29e commit 4ae1dab
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
22 changes: 16 additions & 6 deletions lib/rules/no-unused-vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,22 @@ module.exports = {

function checkUseData(stateName, setStateName) {
const references = context.getScope().references;
const valid = references.find(ref => (
ref.identifier.name === stateName
&& ref.identifier.parent.type === 'JSXExpressionContainer'
&& ref.identifier.parent.parent.type === 'JSXAttribute'
&& ref.identifier.parent.parent.name.name === `${prefix}-model`
));
const valid = references.find(({ identifier }) => {
if (identifier.name !== stateName) {
return false;
}

let parent = identifier.parent;
while (parent && parent.type === 'MemberExpression') {
parent = parent.parent;
}

return parent
&& parent.type === 'JSXExpressionContainer'
&& parent.parent.type === 'JSXAttribute'
&& parent.parent.name.name === `${prefix}-model`;
});

if (valid) {
context.markVariableAsUsed(setStateName);
}
Expand Down
8 changes: 8 additions & 0 deletions tests/lib/rules/no-unused-vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ new RuleTester({
}
}).run('no-unused-vars', rule, {
valid: [
{
code: `
export default () => {
const [data, setData] = useState(0);
return <input x-model={data.foo}/>
}
`
},
{
code: `
export default () => {
Expand Down

0 comments on commit 4ae1dab

Please sign in to comment.