Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new
vue/no-arrow-functions-in-watch
rule (#1155)
* Add 'vue/no-arrow-functions-in-watch' rule * Add property type check in 'vue/no-arrow-functions-in-watch' * Modify to check error line info in 'vue/no-arrow-functions-in-watch' tests
- Loading branch information
1 parent
f03b2d8
commit f8b3014
Showing
5 changed files
with
319 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,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 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,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.type === 'Property' && property.value.type === 'ArrowFunctionExpression') { | ||
context.report({ | ||
node: property, | ||
message: 'You should not use an arrow function to define a watcher.' | ||
}) | ||
} | ||
} | ||
}) | ||
} | ||
} |
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,217 @@ | ||
/** | ||
* @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 { | ||
watch: { | ||
...obj, | ||
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: [{ | ||
message: 'You should not use an arrow function to define a watcher.', | ||
line: 5 | ||
}] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
export default { | ||
watch: { | ||
foo: function() {}, | ||
bar: () => {} | ||
} | ||
}`, | ||
errors: [{ | ||
message: 'You should not use an arrow function to define a watcher.', | ||
line: 5 | ||
}] | ||
}, | ||
{ | ||
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: [{ | ||
message: 'You should not use an arrow function to define a watcher.', | ||
line: 15 | ||
}] | ||
} | ||
] | ||
}) |