|
| 1 | +/** |
| 2 | + * @author Sosuke Suzuki |
| 3 | + */ |
| 4 | + |
| 5 | +// ------------------------------------------------------------------------------ |
| 6 | +// Requirements |
| 7 | +// ------------------------------------------------------------------------------ |
| 8 | + |
| 9 | +const rule = require('../../../lib/rules/no-arrow-functions-in-watch') |
| 10 | +const RuleTester = require('eslint').RuleTester |
| 11 | + |
| 12 | +// ------------------------------------------------------------------------------ |
| 13 | +// Tests |
| 14 | +// ------------------------------------------------------------------------------ |
| 15 | + |
| 16 | +const ruleTester = new RuleTester({ |
| 17 | + parserOptions: { |
| 18 | + ecmaVersion: 2018, |
| 19 | + sourceType: 'module' |
| 20 | + } |
| 21 | +}) |
| 22 | +ruleTester.run('no-arrow-functions-in-watch', rule, { |
| 23 | + valid: [ |
| 24 | + { |
| 25 | + filename: 'test.vue', |
| 26 | + code: ` |
| 27 | + export default {} |
| 28 | + ` |
| 29 | + }, |
| 30 | + { |
| 31 | + filename: 'test.vue', |
| 32 | + code: ` |
| 33 | + export default { |
| 34 | + watch: {} |
| 35 | + } |
| 36 | + ` |
| 37 | + }, |
| 38 | + { |
| 39 | + filename: 'test.vue', |
| 40 | + code: ` |
| 41 | + export default { |
| 42 | + watch: { |
| 43 | + foo() {} |
| 44 | + }, |
| 45 | + } |
| 46 | + ` |
| 47 | + }, |
| 48 | + { |
| 49 | + filename: 'test.vue', |
| 50 | + code: ` |
| 51 | + export default { |
| 52 | + watch: { |
| 53 | + foo: function() {} |
| 54 | + }, |
| 55 | + } |
| 56 | + ` |
| 57 | + }, |
| 58 | + { |
| 59 | + filename: 'test.vue', |
| 60 | + code: ` |
| 61 | + export default { |
| 62 | + watch: { |
| 63 | + foo() {}, |
| 64 | + bar() {} |
| 65 | + }, |
| 66 | + } |
| 67 | + ` |
| 68 | + }, |
| 69 | + { |
| 70 | + filename: 'test.vue', |
| 71 | + code: ` |
| 72 | + export default { |
| 73 | + watch: { |
| 74 | + foo: function() {}, |
| 75 | + bar: function() {} |
| 76 | + }, |
| 77 | + } |
| 78 | + ` |
| 79 | + }, |
| 80 | + { |
| 81 | + filename: 'test.vue', |
| 82 | + code: ` |
| 83 | + export default { |
| 84 | + watch: { |
| 85 | + ...obj, |
| 86 | + foo: function() {}, |
| 87 | + bar: function() {} |
| 88 | + }, |
| 89 | + } |
| 90 | + ` |
| 91 | + }, |
| 92 | + { |
| 93 | + filename: 'test.vue', |
| 94 | + code: ` |
| 95 | + export default { |
| 96 | + data: { |
| 97 | + a: 1, |
| 98 | + b: 2, |
| 99 | + c: 3, |
| 100 | + d: 4, |
| 101 | + e: { |
| 102 | + f: { |
| 103 | + g: 5 |
| 104 | + } |
| 105 | + } |
| 106 | + }, |
| 107 | + watch: { |
| 108 | + a: function (val, oldVal) { |
| 109 | + console.log('new: %s, old: %s', val, oldVal) |
| 110 | + }, |
| 111 | + b: 'someMethod', |
| 112 | + c: { |
| 113 | + handler: function (val, oldVal) {}, |
| 114 | + deep: true |
| 115 | + }, |
| 116 | + d: { |
| 117 | + handler: 'someMethod', |
| 118 | + immediate: true |
| 119 | + }, |
| 120 | + e: [ |
| 121 | + 'handle1', |
| 122 | + function handle2 (val, oldVal) {}, |
| 123 | + { |
| 124 | + handler: function handle3 (val, oldVal) {}, |
| 125 | + /* ... */ |
| 126 | + } |
| 127 | + ], |
| 128 | + 'e.f': function (val, oldVal) { /* ... */ } |
| 129 | + } |
| 130 | + }` |
| 131 | + } |
| 132 | + ], |
| 133 | + invalid: [ |
| 134 | + { |
| 135 | + filename: 'test.vue', |
| 136 | + code: ` |
| 137 | + export default { |
| 138 | + watch: { |
| 139 | + foo: () => {} |
| 140 | + }, |
| 141 | + }`, |
| 142 | + errors: ['You should not use an arrow function to define a watcher.'] |
| 143 | + }, |
| 144 | + { |
| 145 | + filename: 'test.vue', |
| 146 | + code: ` |
| 147 | + export default { |
| 148 | + watch: { |
| 149 | + foo() {}, |
| 150 | + bar: () => {} |
| 151 | + } |
| 152 | + }`, |
| 153 | + errors: [{ |
| 154 | + message: 'You should not use an arrow function to define a watcher.', |
| 155 | + line: 5 |
| 156 | + }] |
| 157 | + }, |
| 158 | + { |
| 159 | + filename: 'test.vue', |
| 160 | + code: ` |
| 161 | + export default { |
| 162 | + watch: { |
| 163 | + foo: function() {}, |
| 164 | + bar: () => {} |
| 165 | + } |
| 166 | + }`, |
| 167 | + errors: [{ |
| 168 | + message: 'You should not use an arrow function to define a watcher.', |
| 169 | + line: 5 |
| 170 | + }] |
| 171 | + }, |
| 172 | + { |
| 173 | + filename: 'test.vue', |
| 174 | + code: ` |
| 175 | + export default { |
| 176 | + data: { |
| 177 | + a: 1, |
| 178 | + b: 2, |
| 179 | + c: 3, |
| 180 | + d: 4, |
| 181 | + e: { |
| 182 | + f: { |
| 183 | + g: 5 |
| 184 | + } |
| 185 | + } |
| 186 | + }, |
| 187 | + watch: { |
| 188 | + a: (val, oldVal) => { |
| 189 | + console.log('new: %s, old: %s', val, oldVal) |
| 190 | + }, |
| 191 | + b: 'someMethod', |
| 192 | + c: { |
| 193 | + handler: function (val, oldVal) {}, |
| 194 | + deep: true |
| 195 | + }, |
| 196 | + d: { |
| 197 | + handler: 'someMethod', |
| 198 | + immediate: true |
| 199 | + }, |
| 200 | + e: [ |
| 201 | + 'handle1', |
| 202 | + function handle2 (val, oldVal) {}, |
| 203 | + { |
| 204 | + handler: function handle3 (val, oldVal) {}, |
| 205 | + /* ... */ |
| 206 | + } |
| 207 | + ], |
| 208 | + 'e.f': function (val, oldVal) { /* ... */ } |
| 209 | + } |
| 210 | + }`, |
| 211 | + errors: [{ |
| 212 | + message: 'You should not use an arrow function to define a watcher.', |
| 213 | + line: 15 |
| 214 | + }] |
| 215 | + } |
| 216 | + ] |
| 217 | +}) |
0 commit comments