-
-
Notifications
You must be signed in to change notification settings - Fork 680
Add vue/no-ref-object-destructure
rule
#1965
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
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d67615b
Add `vue/no-ref-object-destructure` rule
ota-meshi ac5135a
add test cases
ota-meshi 31adf4c
fix test case
ota-meshi 5ae5a0c
Apply suggestions from code review
ota-meshi 297a003
remove comment in testcases
ota-meshi 7256205
add rfc link to doc
ota-meshi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,54 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/no-ref-object-destructure | ||
description: disallow destructuring of ref objects that can lead to loss of reactivity | ||
--- | ||
# vue/no-ref-object-destructure | ||
|
||
> disallow destructuring of ref objects that can lead to loss of reactivity | ||
|
||
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge> | ||
|
||
## :book: Rule Details | ||
|
||
This rule reports the destructuring of ref objects causing the value to lose reactivity. | ||
|
||
<eslint-code-block :rules="{'vue/no-ref-object-destructure': ['error']}" language="javascript" filename="example.js" > | ||
|
||
```js | ||
import { ref } from 'vue' | ||
const count = ref(0) | ||
const v1 = count.value /* ✗ BAD */ | ||
const { value: v2 } = count /* ✗ BAD */ | ||
const v3 = computed(() => count.value /* ✓ GOOD */) | ||
const v4 = fn(count.value) /* ✗ BAD */ | ||
const v5 = fn(count) /* ✓ GOOD */ | ||
const v6 = computed(() => fn(count.value) /* ✓ GOOD */) | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
This rule also supports Reactivity Transform, but Reactivity Transform is an experimental feature and may have false positives due to future Vue changes. | ||
|
||
<eslint-code-block :rules="{'vue/no-ref-object-destructure': ['error']}" language="javascript" filename="example.js" > | ||
|
||
```js | ||
const count = $ref(0) | ||
const v1 = count /* ✗ BAD */ | ||
const v2 = $computed(() => count /* ✓ GOOD */) | ||
const v3 = fn(count) /* ✗ BAD */ | ||
const v4 = fn($$(count)) /* ✓ GOOD */ | ||
const v5 = $computed(() => fn(count) /* ✓ GOOD */) | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :wrench: Options | ||
|
||
Nothing. | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-ref-object-destructure.js) | ||
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/no-ref-object-destructure.js) |
This file contains hidden or 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 hidden or 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 hidden or 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,183 @@ | ||
/** | ||
* @author Yosuke Ota <https://github.com/ota-meshi> | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
'use strict' | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
const utils = require('../utils') | ||
const { | ||
extractRefObjectReferences, | ||
extractReactiveVariableReferences | ||
} = require('../utils/ref-object-references') | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Helpers | ||
// ------------------------------------------------------------------------------ | ||
|
||
/** | ||
* @typedef {import('../utils/ref-object-references').RefObjectReferences} RefObjectReferences | ||
* @typedef {import('../utils/ref-object-references').RefObjectReference} RefObjectReference | ||
*/ | ||
|
||
/** | ||
* Checks whether writing assigns a value to the given pattern. | ||
* @param {Pattern | AssignmentProperty | Property} node | ||
* @returns {boolean} | ||
*/ | ||
function isUpdate(node) { | ||
const parent = node.parent | ||
if (parent.type === 'UpdateExpression' && parent.argument === node) { | ||
// e.g. `pattern++` | ||
return true | ||
} | ||
if (parent.type === 'AssignmentExpression' && parent.left === node) { | ||
// e.g. `pattern = 42` | ||
return true | ||
} | ||
if ( | ||
(parent.type === 'Property' && parent.value === node) || | ||
parent.type === 'ArrayPattern' || | ||
(parent.type === 'ObjectPattern' && | ||
parent.properties.includes(/** @type {any} */ (node))) || | ||
(parent.type === 'AssignmentPattern' && parent.left === node) || | ||
parent.type === 'RestElement' || | ||
(parent.type === 'MemberExpression' && parent.object === node) | ||
) { | ||
return isUpdate(parent) | ||
} | ||
return false | ||
} | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: | ||
'disallow destructuring of ref objects that can lead to loss of reactivity', | ||
categories: undefined, | ||
url: 'https://eslint.vuejs.org/rules/no-ref-object-destructure.html' | ||
}, | ||
fixable: null, | ||
schema: [], | ||
messages: { | ||
getValueInSameScope: | ||
'Getting a value from the ref object in the same scope will cause the value to lose reactivity.', | ||
getReactiveVariableInSameScope: | ||
'Getting a reactive variable in the same scope will cause the value to lose reactivity.' | ||
} | ||
}, | ||
/** | ||
* @param {RuleContext} context | ||
* @returns {RuleListener} | ||
*/ | ||
create(context) { | ||
/** | ||
* @typedef {object} ScopeStack | ||
* @property {ScopeStack | null} upper | ||
* @property {Program | FunctionExpression | FunctionDeclaration | ArrowFunctionExpression} node | ||
*/ | ||
/** @type {ScopeStack} */ | ||
let scopeStack = { upper: null, node: context.getSourceCode().ast } | ||
/** @type {Map<CallExpression, ScopeStack>} */ | ||
const scopes = new Map() | ||
|
||
const refObjectReferences = extractRefObjectReferences(context) | ||
const reactiveVariableReferences = | ||
extractReactiveVariableReferences(context) | ||
|
||
/** | ||
* Verify the given ref object value. `refObj = ref(); refObj.value;` | ||
* @param {Expression | Super | ObjectPattern} node | ||
*/ | ||
function verifyRefObjectValue(node) { | ||
const ref = refObjectReferences.get(node) | ||
if (!ref) { | ||
return | ||
} | ||
if (scopes.get(ref.define) !== scopeStack) { | ||
// Not in the same scope | ||
return | ||
} | ||
|
||
context.report({ | ||
node, | ||
messageId: 'getValueInSameScope' | ||
}) | ||
} | ||
|
||
/** | ||
* Verify the given reactive variable. `refVal = $ref(); refVal;` | ||
* @param {Identifier} node | ||
*/ | ||
function verifyReactiveVariable(node) { | ||
const ref = reactiveVariableReferences.get(node) | ||
if (!ref || ref.escape) { | ||
return | ||
} | ||
if (scopes.get(ref.define) !== scopeStack) { | ||
// Not in the same scope | ||
return | ||
} | ||
|
||
context.report({ | ||
node, | ||
messageId: 'getReactiveVariableInSameScope' | ||
}) | ||
} | ||
|
||
return { | ||
':function'(node) { | ||
scopeStack = { upper: scopeStack, node } | ||
}, | ||
':function:exit'() { | ||
scopeStack = scopeStack.upper || scopeStack | ||
}, | ||
CallExpression(node) { | ||
scopes.set(node, scopeStack) | ||
}, | ||
/** | ||
* Check for `refObj.value`. | ||
*/ | ||
'MemberExpression:exit'(node) { | ||
if (isUpdate(node)) { | ||
// e.g. `refObj.value = 42`, `refObj.value++` | ||
return | ||
} | ||
const name = utils.getStaticPropertyName(node) | ||
if (name !== 'value') { | ||
return | ||
} | ||
verifyRefObjectValue(node.object) | ||
}, | ||
/** | ||
* Check for `{value} = refObj`. | ||
*/ | ||
'ObjectPattern:exit'(node) { | ||
const prop = utils.findAssignmentProperty(node, 'value') | ||
if (!prop) { | ||
return | ||
} | ||
verifyRefObjectValue(node) | ||
}, | ||
/** | ||
* Check for reactive variable`. | ||
* @param {Identifier} node | ||
*/ | ||
'Identifier:exit'(node) { | ||
if (isUpdate(node)) { | ||
// e.g. `reactiveVariable = 42`, `reactiveVariable++` | ||
return | ||
} | ||
verifyReactiveVariable(node) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.