Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New: Add
vue/no-setup-props-destructure
rule (#1066)
* Add no-setup-props-destructure rule * update * Add testcases * update * update * update doc
- Loading branch information
Showing
6 changed files
with
572 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/no-setup-props-destructure | ||
description: disallow destructuring of `props` passed to `setup` | ||
--- | ||
# vue/no-setup-props-destructure | ||
> disallow destructuring of `props` passed to `setup` | ||
## :book: Rule Details | ||
|
||
This rule reports the destructuring of `props` passed to `setup` causing the value to lose reactivity. | ||
|
||
<eslint-code-block :rules="{'vue/no-setup-props-destructure': ['error']}"> | ||
|
||
```vue | ||
<script> | ||
export default { | ||
/* ✓ GOOD */ | ||
setup(props) { | ||
watch(() => { | ||
console.log(props.count) | ||
}) | ||
return () => { | ||
return h('div', props.count) | ||
} | ||
} | ||
} | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
Destructuring the `props` passed to `setup` will cause the value to lose reactivity. | ||
|
||
<eslint-code-block :rules="{'vue/no-setup-props-destructure': ['error']}"> | ||
|
||
```vue | ||
<script> | ||
export default { | ||
/* ✗ BAD */ | ||
setup({ count }) { | ||
watch(() => { | ||
console.log(count) // not going to detect changes | ||
}) | ||
return () => { | ||
return h('div', count) // not going to update | ||
} | ||
} | ||
} | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
Also, destructuring in root scope of `setup()` should error, but ok inside nested callbacks or returned render functions: | ||
|
||
<eslint-code-block :rules="{'vue/no-setup-props-destructure': ['error']}"> | ||
|
||
```vue | ||
<script> | ||
export default { | ||
setup(props) { | ||
/* ✗ BAD */ | ||
const { count } = props | ||
watch(() => { | ||
/* ✓ GOOD */ | ||
const { count } = props | ||
console.log(count) | ||
}) | ||
return () => { | ||
/* ✓ GOOD */ | ||
const { count } = props | ||
return h('div', count) | ||
} | ||
} | ||
} | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :wrench: Options | ||
|
||
Nothing. | ||
|
||
## :books: Further reading | ||
|
||
- [Vue RFCs - 0013-composition-api](https://github.com/vuejs/rfcs/blob/master/active-rfcs/0013-composition-api.md) | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-setup-props-destructure.js) | ||
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/no-setup-props-destructure.js) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
/** | ||
* @author Yosuke Ota | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
'use strict' | ||
const { findVariable } = require('eslint-utils') | ||
const utils = require('../utils') | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'disallow destructuring of `props` passed to `setup`', | ||
category: undefined, | ||
url: 'https://eslint.vuejs.org/rules/no-setup-props-destructure.html' | ||
}, | ||
fixable: null, | ||
schema: [], | ||
messages: { | ||
destructuring: 'Destructuring the `props` will cause the value to lose reactivity.', | ||
getProperty: 'Getting a value from the `props` in root scope of `setup()` will cause the value to lose reactivity.' | ||
} | ||
}, | ||
create (context) { | ||
const setupFunctions = new Map() | ||
const forbiddenNodes = new Map() | ||
|
||
function addForbiddenNode (property, node, messageId) { | ||
let list = forbiddenNodes.get(property) | ||
if (!list) { | ||
list = [] | ||
forbiddenNodes.set(property, list) | ||
} | ||
list.push({ | ||
node, | ||
messageId | ||
}) | ||
} | ||
|
||
function verify (left, right, { propsReferenceIds, setupProperty }) { | ||
if (!right) { | ||
return | ||
} | ||
|
||
if (left.type === 'ArrayPattern' || left.type === 'ObjectPattern') { | ||
if (propsReferenceIds.has(right)) { | ||
addForbiddenNode(setupProperty, left, 'getProperty') | ||
} | ||
} else if (left.type === 'Identifier' && right.type === 'MemberExpression') { | ||
if (propsReferenceIds.has(right.object)) { | ||
addForbiddenNode(setupProperty, right, 'getProperty') | ||
} | ||
} | ||
} | ||
|
||
let scopeStack = { upper: null, functionNode: null } | ||
|
||
return Object.assign( | ||
{ | ||
'Property[value.type=/^(Arrow)?FunctionExpression$/]' (node) { | ||
if (utils.getStaticPropertyName(node) !== 'setup') { | ||
return | ||
} | ||
const param = node.value.params[0] | ||
if (!param) { | ||
// no arguments | ||
return | ||
} | ||
if (param.type === 'RestElement') { | ||
// cannot check | ||
return | ||
} | ||
if (param.type === 'ArrayPattern' || param.type === 'ObjectPattern') { | ||
addForbiddenNode(node, param, 'destructuring') | ||
return | ||
} | ||
setupFunctions.set(node.value, { | ||
setupProperty: node, | ||
propsParam: param, | ||
propsReferenceIds: new Set() | ||
}) | ||
}, | ||
':function' (node) { | ||
scopeStack = { upper: scopeStack, functionNode: node } | ||
}, | ||
':function>*' (node) { | ||
const setupFunctionData = setupFunctions.get(node.parent) | ||
if (!setupFunctionData || setupFunctionData.propsParam !== node) { | ||
return | ||
} | ||
const variable = findVariable(context.getScope(), node) | ||
if (!variable) { | ||
return | ||
} | ||
const { propsReferenceIds } = setupFunctionData | ||
for (const reference of variable.references) { | ||
if (!reference.isRead()) { | ||
continue | ||
} | ||
|
||
propsReferenceIds.add(reference.identifier) | ||
} | ||
}, | ||
'VariableDeclarator' (node) { | ||
const setupFunctionData = setupFunctions.get(scopeStack.functionNode) | ||
if (!setupFunctionData) { | ||
return | ||
} | ||
verify(node.id, node.init, setupFunctionData) | ||
}, | ||
'AssignmentExpression' (node) { | ||
const setupFunctionData = setupFunctions.get(scopeStack.functionNode) | ||
if (!setupFunctionData) { | ||
return | ||
} | ||
verify(node.left, node.right, setupFunctionData) | ||
}, | ||
':function:exit' (node) { | ||
scopeStack = scopeStack.upper | ||
|
||
setupFunctions.delete(node) | ||
} | ||
}, | ||
utils.executeOnVue(context, obj => { | ||
const reportsList = obj.properties | ||
.map(item => forbiddenNodes.get(item)) | ||
.filter(reports => !!reports) | ||
for (const reports of reportsList) { | ||
for (const report of reports) { | ||
context.report(report) | ||
} | ||
} | ||
}) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.