|
| 1 | +/* eslint-disable eslint-plugin/consistent-output */ |
| 2 | +/** |
| 3 | + * @fileoverview disallow using deprecated events api |
| 4 | + * @author yoyo930021 |
| 5 | + */ |
| 6 | +'use strict' |
| 7 | + |
| 8 | +// ------------------------------------------------------------------------------ |
| 9 | +// Requirements |
| 10 | +// ------------------------------------------------------------------------------ |
| 11 | + |
| 12 | +const rule = require('../../../lib/rules/no-deprecated-events-api') |
| 13 | + |
| 14 | +const RuleTester = require('eslint').RuleTester |
| 15 | + |
| 16 | +const parserOptions = { |
| 17 | + ecmaVersion: 2018, |
| 18 | + sourceType: 'module' |
| 19 | +} |
| 20 | + |
| 21 | +// ------------------------------------------------------------------------------ |
| 22 | +// Tests |
| 23 | +// ------------------------------------------------------------------------------ |
| 24 | + |
| 25 | +const ruleTester = new RuleTester() |
| 26 | +ruleTester.run('no-deprecated-events-api', rule, { |
| 27 | + |
| 28 | + valid: [ |
| 29 | + { |
| 30 | + filename: 'test.js', |
| 31 | + code: ` |
| 32 | + createApp({ |
| 33 | + mounted () { |
| 34 | + this.$emit('start') |
| 35 | + } |
| 36 | + }) |
| 37 | + `, |
| 38 | + parserOptions |
| 39 | + }, |
| 40 | + { |
| 41 | + filename: 'test.js', |
| 42 | + code: ` |
| 43 | + createApp({ |
| 44 | + methods: { |
| 45 | + click () { |
| 46 | + this.$emit('click') |
| 47 | + } |
| 48 | + } |
| 49 | + }) |
| 50 | + `, |
| 51 | + parserOptions |
| 52 | + }, |
| 53 | + { |
| 54 | + filename: 'test.js', |
| 55 | + code: ` |
| 56 | + const another = function () { |
| 57 | + this.$on('start', args => { |
| 58 | + console.log('start') |
| 59 | + }) |
| 60 | + } |
| 61 | +
|
| 62 | + createApp({ |
| 63 | + mounted () { |
| 64 | + this.$emit('start') |
| 65 | + } |
| 66 | + }) |
| 67 | + `, |
| 68 | + parserOptions |
| 69 | + }, |
| 70 | + { |
| 71 | + filename: 'test.js', |
| 72 | + code: ` |
| 73 | + app.component('some-comp', { |
| 74 | + mounted () { |
| 75 | + this.$emit('start') |
| 76 | + } |
| 77 | + }) |
| 78 | + `, |
| 79 | + parserOptions |
| 80 | + }, |
| 81 | + { |
| 82 | + filename: 'test.vue', |
| 83 | + code: ` |
| 84 | + export default { |
| 85 | + mounted () { |
| 86 | + this.$emit('start') |
| 87 | + } |
| 88 | + } |
| 89 | + `, |
| 90 | + parserOptions |
| 91 | + }, |
| 92 | + { |
| 93 | + filename: 'test.vue', |
| 94 | + code: ` |
| 95 | + import mitt from 'mitt' |
| 96 | + const emitter = mitt() |
| 97 | +
|
| 98 | + export default { |
| 99 | + setup () { |
| 100 | + emitter.on('foo', e => console.log('foo', e)) |
| 101 | + emitter.emit('foo', { a: 'b' }) |
| 102 | + emitter.off('foo', onFoo) |
| 103 | + } |
| 104 | + } |
| 105 | + `, |
| 106 | + parserOptions |
| 107 | + } |
| 108 | + ], |
| 109 | + |
| 110 | + invalid: [ |
| 111 | + { |
| 112 | + filename: 'test.js', |
| 113 | + code: ` |
| 114 | + app.component('some-comp', { |
| 115 | + mounted () { |
| 116 | + this.$on('start', function (args) { |
| 117 | + console.log('start', args) |
| 118 | + }) |
| 119 | + } |
| 120 | + }) |
| 121 | + `, |
| 122 | + parserOptions, |
| 123 | + errors: [{ |
| 124 | + message: 'The Events api `$on`, `$off` `$once` is deprecated. Using external library instead, for example mitt.', |
| 125 | + line: 4 |
| 126 | + }] |
| 127 | + }, |
| 128 | + { |
| 129 | + filename: 'test.js', |
| 130 | + code: ` |
| 131 | + app.component('some-comp', { |
| 132 | + mounted () { |
| 133 | + this.$off('start') |
| 134 | + } |
| 135 | + }) |
| 136 | + `, |
| 137 | + parserOptions, |
| 138 | + errors: [{ |
| 139 | + message: 'The Events api `$on`, `$off` `$once` is deprecated. Using external library instead, for example mitt.', |
| 140 | + line: 4 |
| 141 | + }] |
| 142 | + }, |
| 143 | + { |
| 144 | + filename: 'test.vue', |
| 145 | + code: ` |
| 146 | + export default { |
| 147 | + mounted () { |
| 148 | + this.$once('start', function () { |
| 149 | + console.log('start') |
| 150 | + }) |
| 151 | + } |
| 152 | + } |
| 153 | + `, |
| 154 | + parserOptions, |
| 155 | + errors: [{ |
| 156 | + message: 'The Events api `$on`, `$off` `$once` is deprecated. Using external library instead, for example mitt.', |
| 157 | + line: 4 |
| 158 | + }] |
| 159 | + } |
| 160 | + ] |
| 161 | +}) |
0 commit comments