-
-
Notifications
You must be signed in to change notification settings - Fork 680
⭐️New: Add vue/valid-v-bind-sync
rule
#647
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 6 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
e23f506
New: Add `vue/valid-v-bind-sync` rule
ota-meshi 142120f
update doc
ota-meshi 3820b96
update use messageId
ota-meshi 49b9684
Merge commit 'f98123cff43e833596c171f46577536cbbb32adc' into add-rule…
ota-meshi 5acb2e7
Change: null comparison to Boolean call.
ota-meshi 17b02f2
add testcase
ota-meshi e4222a2
Update: to use reference.variable
ota-meshi bce24e0
Merge branch 'master' into add-rule/valid-v-bind-sync
ota-meshi 2cbe6b0
fixed eslint error
ota-meshi 90a1304
Merge branch 'master' into add-rule/valid-v-bind-sync
ota-meshi e10b9a9
update document
ota-meshi 53ccd60
revert readme
ota-meshi e7d9145
fix eof
ota-meshi aa15ddb
fixed doc
ota-meshi b8c23e7
Merge branch 'master' into add-rule/valid-v-bind-sync
ota-meshi c115f54
update docs
ota-meshi 5082653
fixed lint errors
ota-meshi 543f0a7
Merge commit 'febc7271b9f253e0f7bad7b736b98cf2d156c12c' into add-rule…
ota-meshi 8a61053
fixed
ota-meshi b6df26b
Merge branch 'master' into add-rule/valid-v-bind-sync
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,50 @@ | ||
# enforce valid `.sync` modifier on `v-bind` directives (vue/valid-v-bind-sync) | ||
|
||
This rule checks whether every `.sync` modifier on `v-bind` directives is valid. | ||
|
||
## :book: Rule Details | ||
|
||
This rule reports `.sync` modifier on `v-bind` directives in the following cases: | ||
|
||
- The `.sync` modifier does not have the attribute value which is valid as LHS. E.g. `<MyComponent v-bind:aaa.sync="foo() + bar()" />` | ||
- The `.sync` modifier is on non Vue-components. E.g. `<input v-bind:aaa.sync="foo"></div>` | ||
- The `.sync` modifier's reference is iteration variables. E.g. `<div v-for="x in list"><MyComponent v-bind:aaa.sync="x" /></div>` | ||
|
||
This rule does not check syntax errors in directives because it's checked by [no-parsing-error] rule. | ||
|
||
:-1: Examples of **incorrect** code for this rule: | ||
|
||
```vue | ||
<MyComponent v-bind:aaa.sync="foo + bar" /> | ||
<MyComponent :aaa.sync="foo + bar" /> | ||
|
||
<input v-bind:aaa.sync="foo"> | ||
<input :aaa.sync="foo"> | ||
|
||
<div v-for="todo in todos"> | ||
<MyComponent v-bind:aaa.sync="todo" /> | ||
<MyComponent :aaa.sync="todo" /> | ||
</div> | ||
``` | ||
|
||
:+1: Examples of **correct** code for this rule: | ||
|
||
```vue | ||
<MyComponent v-bind:aaa.sync="foo"/> | ||
<MyComponent :aaa.sync="foo"/> | ||
|
||
<div v-for="todo in todos"> | ||
<MyComponent v-bind:aaa.sync="todo.name"/> | ||
<MyComponent :aaa.sync="todo.name"/> | ||
</div> | ||
``` | ||
|
||
## :wrench: Options | ||
|
||
Nothing. | ||
|
||
## :couple: Related rules | ||
|
||
- [no-parsing-error] | ||
|
||
[no-parsing-error]: no-parsing-error.md |
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,136 @@ | ||
/** | ||
* @fileoverview enforce valid `.sync` modifier on `v-bind` directives | ||
* @author Yosuke Ota | ||
*/ | ||
'use strict' | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
const utils = require('../utils') | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Helpers | ||
// ------------------------------------------------------------------------------ | ||
|
||
/** | ||
* Check whether the given node is valid or not. | ||
* @param {ASTNode} node The element node to check. | ||
* @returns {boolean} `true` if the node is valid. | ||
*/ | ||
function isValidElement (node) { | ||
if ( | ||
(!utils.isHtmlElementNode(node) && !utils.isSvgElementNode(node)) || | ||
utils.isHtmlWellKnownElementName(node.rawName) || | ||
utils.isSvgWellKnownElementName(node.rawName) | ||
) { | ||
// non Vue-component | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
/** | ||
* Check whether the given node can be LHS. | ||
* @param {ASTNode} node The node to check. | ||
* @returns {boolean} `true` if the node can be LHS. | ||
*/ | ||
function isLhs (node) { | ||
return Boolean(node) && ( | ||
node.type === 'Identifier' || | ||
node.type === 'MemberExpression' | ||
) | ||
} | ||
|
||
/** | ||
* Get the variable by names. | ||
* @param {string} name The variable name to find. | ||
* @param {ASTNode} leafNode The node to look up. | ||
* @returns {Variable|null} The found variable or null. | ||
*/ | ||
function getVariable (name, leafNode) { | ||
let node = leafNode | ||
|
||
while (node) { | ||
const variables = node.variables | ||
const variable = variables && variables.find(v => v.id.name === name) | ||
|
||
if (variable) { | ||
return variable | ||
} | ||
|
||
node = node.parent | ||
} | ||
|
||
return null | ||
} | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'enforce valid `.sync` modifier on `v-bind` directives', | ||
category: undefined, | ||
url: 'https://github.com/vuejs/eslint-plugin-vue/blob/v5.0.0-beta.4/docs/rules/valid-v-bind-sync.md' | ||
}, | ||
fixable: null, | ||
schema: [], | ||
messages: { | ||
unexpectedInvalidElement: "'.sync' modifiers aren't supported on <{{name}}> non Vue-components.", | ||
unexpectedNonLhsExpression: "'.sync' modifiers require the attribute value which is valid as LHS.", | ||
unexpectedUpdateIterationVariable: "'.sync' modifiers cannot update the iteration variable '{{varName}}' itself." | ||
} | ||
}, | ||
|
||
create (context) { | ||
return utils.defineTemplateBodyVisitor(context, { | ||
"VAttribute[directive=true][key.name='bind']" (node) { | ||
if (node.key.modifiers.indexOf('sync') < 0) { | ||
return | ||
} | ||
const element = node.parent.parent | ||
const name = element.name | ||
|
||
if (!isValidElement(element)) { | ||
context.report({ | ||
node, | ||
loc: node.loc, | ||
messageId: 'unexpectedInvalidElement', | ||
data: { name } | ||
}) | ||
} | ||
|
||
if (node.value) { | ||
if (!isLhs(node.value.expression)) { | ||
context.report({ | ||
node, | ||
loc: node.loc, | ||
messageId: 'unexpectedNonLhsExpression' | ||
}) | ||
} | ||
|
||
for (const reference of node.value.references) { | ||
const id = reference.id | ||
if (id.parent.type !== 'VExpressionContainer') { | ||
continue | ||
} | ||
|
||
const variable = getVariable(id.name, element) | ||
if (variable) { | ||
context.report({ | ||
node, | ||
loc: node.loc, | ||
messageId: 'unexpectedUpdateIterationVariable', | ||
data: { varName: id.name } | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
} |
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.