Skip to content

Commit 60a8ccc

Browse files
committed
catch store.watch with string argument
1 parent a89259d commit 60a8ccc

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

rules/vuex/store-watch-with-string.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict'
2+
3+
module.exports = {
4+
pattern: /\bstore\s*.\s*watch\s*\(\s*['"`]/,
5+
warning: function (match) {
6+
return {
7+
reason: 'The new store.watch takes a function as the first argument, allowing you to watch not only state, but also custom getters',
8+
fix: (
9+
'Update store.watch to use a function rather than a string path for the first argument (see link below for an example)'
10+
),
11+
docsHash: 'store-watch-with-String-Property-Path-deprecated'
12+
}
13+
}
14+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
'use strict'
2+
3+
const check = createRuleChecker('vuex/store-watch-with-string')
4+
5+
describe('Rule: store-watch-with-string', () => {
6+
it('does not match an empty line', () => {
7+
const warning = check('')
8+
expect(warning).toBe(null)
9+
})
10+
11+
it('does not match store.watch with no arguments', () => {
12+
const warning = check(`
13+
store.watch(
14+
`)
15+
expect(warning).toBe(null)
16+
})
17+
18+
it('does not match store.watch with arrow function arg', () => {
19+
const warning = check(`
20+
store.watch(state => {
21+
`)
22+
expect(warning).toBe(null)
23+
})
24+
25+
it('does not match store.watch with regular function arg', () => {
26+
const warning = check(`
27+
store.watch(function (state) {
28+
`)
29+
expect(warning).toBe(null)
30+
})
31+
32+
it('matches store.watch with a string argument', () => {
33+
const warning = check(`
34+
store.watch('user.notifications', callback)
35+
`)
36+
expect(warning).toBeTruthy()
37+
expect(warning.fix).toBe('Update store.watch to use a function rather than a string path for the first argument (see link below for an example)')
38+
})
39+
40+
it('matches store.watch with a string argument and a bunch of spaces', () => {
41+
const warning = check(`
42+
store . watch ( 'user.notifications' , callback)
43+
`)
44+
expect(warning).toBeTruthy()
45+
expect(warning.fix).toBe('Update store.watch to use a function rather than a string path for the first argument (see link below for an example)')
46+
})
47+
48+
it('matches store.watch with a string argument using double quotes', () => {
49+
const warning = check(`
50+
store.watch('user.notifications', callback)
51+
`)
52+
expect(warning).toBeTruthy()
53+
expect(warning.fix).toBe('Update store.watch to use a function rather than a string path for the first argument (see link below for an example)')
54+
})
55+
56+
it('matches store.watch with a string argument using backticks', () => {
57+
const warning = check(`
58+
store.watch(\`user.notifications\`, callback)
59+
`)
60+
expect(warning).toBeTruthy()
61+
expect(warning.fix).toBe('Update store.watch to use a function rather than a string path for the first argument (see link below for an example)')
62+
})
63+
})

0 commit comments

Comments
 (0)