-
-
Notifications
You must be signed in to change notification settings - Fork 681
Add new vue/no-arrow-functions-in-watch
rule
#1155
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
ota-meshi
merged 3 commits into
vuejs:master
from
sosukesuzuki:feature/no-arrow-functions-in-watch
May 23, 2020
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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,61 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/no-arrow-functions-in-watch | ||
description: disallow arrow functions to define watcher | ||
--- | ||
# vue/no-arrow-functions-in-watch | ||
> disallow using arrow functions to define watcher | ||
|
||
## :book: Rule Details | ||
|
||
This rules disallows using arrow functions to defined watcher.The reason is arrow functions bind the parent context, so `this` will not be the Vue instance as you expect.([see here for more details](https://vuejs.org/v2/api/#watch)) | ||
|
||
<eslint-code-block :rules="{'vue/no-arrow-functions-in-watch': ['error']}"> | ||
|
||
```vue | ||
<script> | ||
export default { | ||
watch: { | ||
/* ✓ GOOD */ | ||
a: function (val, oldVal) { | ||
console.log('new: %s, old: %s', val, oldVal) | ||
}, | ||
b: 'someMethod', | ||
c: { | ||
handler: function (val, oldVal) { /* ... */ }, | ||
deep: true | ||
}, | ||
d: { | ||
handler: 'someMethod', | ||
immediate: true | ||
}, | ||
e: [ | ||
'handle1', | ||
function handle2 (val, oldVal) { /* ... */ }, | ||
{ | ||
handler: function handle3 (val, oldVal) { /* ... */ }, | ||
/* ... */ | ||
} | ||
], | ||
'e.f': function (val, oldVal) { /* ... */ } | ||
|
||
/* ✗ BAD */ | ||
foo: (val, oldVal) => { | ||
console.log('new: %s, old: %s', val, oldVal) | ||
} | ||
} | ||
} | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :wrench: Options | ||
|
||
Nothing. | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-arrow-functions-in-watch.js) | ||
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/no-arrow-functions-in-watch.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* @author Sosuke Suzuki | ||
*/ | ||
'use strict' | ||
|
||
const utils = require('../utils') | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'disallow using arrow functions to define watcher', | ||
categories: undefined, | ||
url: 'https://eslint.vuejs.org/rules/no-arrow-functions-in-watch.html' | ||
}, | ||
fixable: null, | ||
schema: [] | ||
}, | ||
create (context) { | ||
return utils.executeOnVue(context, (obj) => { | ||
const watchNode = obj.properties.find((property) => utils.getStaticPropertyName(property) === 'watch') | ||
if (watchNode == null) { | ||
return | ||
} | ||
const watchValue = watchNode.value | ||
if (watchValue.type !== 'ObjectExpression') { | ||
return | ||
} | ||
for (const property of watchValue.properties) { | ||
if (property.value.type === 'ArrowFunctionExpression') { | ||
context.report({ | ||
node: property, | ||
message: 'You should not use an arrow function to define a watcher.' | ||
}) | ||
} | ||
} | ||
}) | ||
} | ||
} |
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,196 @@ | ||||||||||||||||
/** | ||||||||||||||||
* @author Sosuke Suzuki | ||||||||||||||||
*/ | ||||||||||||||||
|
||||||||||||||||
// ------------------------------------------------------------------------------ | ||||||||||||||||
// Requirements | ||||||||||||||||
// ------------------------------------------------------------------------------ | ||||||||||||||||
|
||||||||||||||||
const rule = require('../../../lib/rules/no-arrow-functions-in-watch') | ||||||||||||||||
const RuleTester = require('eslint').RuleTester | ||||||||||||||||
|
||||||||||||||||
// ------------------------------------------------------------------------------ | ||||||||||||||||
// Tests | ||||||||||||||||
// ------------------------------------------------------------------------------ | ||||||||||||||||
|
||||||||||||||||
const ruleTester = new RuleTester({ | ||||||||||||||||
parserOptions: { | ||||||||||||||||
ecmaVersion: 2018, | ||||||||||||||||
sourceType: 'module' | ||||||||||||||||
} | ||||||||||||||||
}) | ||||||||||||||||
ruleTester.run('no-arrow-functions-in-watch', rule, { | ||||||||||||||||
valid: [ | ||||||||||||||||
{ | ||||||||||||||||
filename: 'test.vue', | ||||||||||||||||
code: ` | ||||||||||||||||
export default {} | ||||||||||||||||
` | ||||||||||||||||
}, | ||||||||||||||||
{ | ||||||||||||||||
filename: 'test.vue', | ||||||||||||||||
code: ` | ||||||||||||||||
export default { | ||||||||||||||||
watch: {} | ||||||||||||||||
} | ||||||||||||||||
` | ||||||||||||||||
}, | ||||||||||||||||
{ | ||||||||||||||||
filename: 'test.vue', | ||||||||||||||||
code: ` | ||||||||||||||||
export default { | ||||||||||||||||
watch: { | ||||||||||||||||
foo() {} | ||||||||||||||||
}, | ||||||||||||||||
} | ||||||||||||||||
` | ||||||||||||||||
}, | ||||||||||||||||
{ | ||||||||||||||||
filename: 'test.vue', | ||||||||||||||||
code: ` | ||||||||||||||||
export default { | ||||||||||||||||
watch: { | ||||||||||||||||
foo: function() {} | ||||||||||||||||
}, | ||||||||||||||||
} | ||||||||||||||||
` | ||||||||||||||||
}, | ||||||||||||||||
{ | ||||||||||||||||
filename: 'test.vue', | ||||||||||||||||
code: ` | ||||||||||||||||
export default { | ||||||||||||||||
watch: { | ||||||||||||||||
foo() {}, | ||||||||||||||||
bar() {} | ||||||||||||||||
}, | ||||||||||||||||
} | ||||||||||||||||
` | ||||||||||||||||
}, | ||||||||||||||||
{ | ||||||||||||||||
filename: 'test.vue', | ||||||||||||||||
code: ` | ||||||||||||||||
export default { | ||||||||||||||||
watch: { | ||||||||||||||||
foo: function() {}, | ||||||||||||||||
bar: function() {} | ||||||||||||||||
}, | ||||||||||||||||
} | ||||||||||||||||
` | ||||||||||||||||
}, | ||||||||||||||||
{ | ||||||||||||||||
filename: 'test.vue', | ||||||||||||||||
code: ` | ||||||||||||||||
export default { | ||||||||||||||||
data: { | ||||||||||||||||
a: 1, | ||||||||||||||||
b: 2, | ||||||||||||||||
c: 3, | ||||||||||||||||
d: 4, | ||||||||||||||||
e: { | ||||||||||||||||
f: { | ||||||||||||||||
g: 5 | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
}, | ||||||||||||||||
watch: { | ||||||||||||||||
a: function (val, oldVal) { | ||||||||||||||||
console.log('new: %s, old: %s', val, oldVal) | ||||||||||||||||
}, | ||||||||||||||||
b: 'someMethod', | ||||||||||||||||
c: { | ||||||||||||||||
handler: function (val, oldVal) {}, | ||||||||||||||||
deep: true | ||||||||||||||||
}, | ||||||||||||||||
d: { | ||||||||||||||||
handler: 'someMethod', | ||||||||||||||||
immediate: true | ||||||||||||||||
}, | ||||||||||||||||
e: [ | ||||||||||||||||
'handle1', | ||||||||||||||||
function handle2 (val, oldVal) {}, | ||||||||||||||||
{ | ||||||||||||||||
handler: function handle3 (val, oldVal) {}, | ||||||||||||||||
/* ... */ | ||||||||||||||||
} | ||||||||||||||||
], | ||||||||||||||||
'e.f': function (val, oldVal) { /* ... */ } | ||||||||||||||||
} | ||||||||||||||||
}` | ||||||||||||||||
} | ||||||||||||||||
], | ||||||||||||||||
invalid: [ | ||||||||||||||||
{ | ||||||||||||||||
filename: 'test.vue', | ||||||||||||||||
code: ` | ||||||||||||||||
export default { | ||||||||||||||||
watch: { | ||||||||||||||||
foo: () => {} | ||||||||||||||||
}, | ||||||||||||||||
}`, | ||||||||||||||||
errors: ['You should not use an arrow function to define a watcher.'] | ||||||||||||||||
}, | ||||||||||||||||
{ | ||||||||||||||||
filename: 'test.vue', | ||||||||||||||||
code: ` | ||||||||||||||||
export default { | ||||||||||||||||
watch: { | ||||||||||||||||
foo() {}, | ||||||||||||||||
bar: () => {} | ||||||||||||||||
} | ||||||||||||||||
}`, | ||||||||||||||||
errors: ['You should not use an arrow function to define a watcher.'] | ||||||||||||||||
}, | ||||||||||||||||
{ | ||||||||||||||||
filename: 'test.vue', | ||||||||||||||||
code: ` | ||||||||||||||||
export default { | ||||||||||||||||
watch: { | ||||||||||||||||
foo: function() {}, | ||||||||||||||||
bar: () => {} | ||||||||||||||||
} | ||||||||||||||||
}`, | ||||||||||||||||
errors: ['You should not use an arrow function to define a watcher.'] | ||||||||||||||||
}, | ||||||||||||||||
{ | ||||||||||||||||
filename: 'test.vue', | ||||||||||||||||
code: ` | ||||||||||||||||
export default { | ||||||||||||||||
data: { | ||||||||||||||||
a: 1, | ||||||||||||||||
b: 2, | ||||||||||||||||
c: 3, | ||||||||||||||||
d: 4, | ||||||||||||||||
e: { | ||||||||||||||||
f: { | ||||||||||||||||
g: 5 | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
}, | ||||||||||||||||
watch: { | ||||||||||||||||
a: (val, oldVal) => { | ||||||||||||||||
console.log('new: %s, old: %s', val, oldVal) | ||||||||||||||||
}, | ||||||||||||||||
b: 'someMethod', | ||||||||||||||||
c: { | ||||||||||||||||
handler: function (val, oldVal) {}, | ||||||||||||||||
deep: true | ||||||||||||||||
}, | ||||||||||||||||
d: { | ||||||||||||||||
handler: 'someMethod', | ||||||||||||||||
immediate: true | ||||||||||||||||
}, | ||||||||||||||||
e: [ | ||||||||||||||||
'handle1', | ||||||||||||||||
function handle2 (val, oldVal) {}, | ||||||||||||||||
{ | ||||||||||||||||
handler: function handle3 (val, oldVal) {}, | ||||||||||||||||
/* ... */ | ||||||||||||||||
} | ||||||||||||||||
], | ||||||||||||||||
'e.f': function (val, oldVal) { /* ... */ } | ||||||||||||||||
} | ||||||||||||||||
}`, | ||||||||||||||||
errors: ['You should not use an arrow function to define a watcher.'] | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you check the location with some test cases?
Suggested change
|
||||||||||||||||
} | ||||||||||||||||
] | ||||||||||||||||
}) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you check
type
is a"Property"
?Probably get an error with "SpreadElement".
https://github.com/estree/estree/blob/master/es2015.md#expressions
https://github.com/estree/estree/blob/master/es2018.md#expressions