|
| 1 | +/** |
| 2 | + * @author Yosuke Ota |
| 3 | + */ |
| 4 | +'use strict' |
| 5 | + |
| 6 | +const RuleTester = require('eslint').RuleTester |
| 7 | +const rule = require('../../../lib/rules/no-lifecycle-after-await') |
| 8 | + |
| 9 | +const tester = new RuleTester({ |
| 10 | + parser: require.resolve('vue-eslint-parser'), |
| 11 | + parserOptions: { ecmaVersion: 2019, sourceType: 'module' } |
| 12 | +}) |
| 13 | + |
| 14 | +tester.run('no-lifecycle-after-await', rule, { |
| 15 | + valid: [ |
| 16 | + { |
| 17 | + filename: 'test.vue', |
| 18 | + code: ` |
| 19 | + <script> |
| 20 | + import {onMounted} from 'vue' |
| 21 | + export default { |
| 22 | + async setup() { |
| 23 | + onMounted(() => { /* ... */ }) // ok |
| 24 | +
|
| 25 | + await doSomething() |
| 26 | + } |
| 27 | + } |
| 28 | + </script> |
| 29 | + ` |
| 30 | + }, { |
| 31 | + filename: 'test.vue', |
| 32 | + code: ` |
| 33 | + <script> |
| 34 | + import {onMounted} from 'vue' |
| 35 | + export default { |
| 36 | + async setup() { |
| 37 | + onMounted(() => { /* ... */ }) |
| 38 | + } |
| 39 | + } |
| 40 | + </script> |
| 41 | + ` |
| 42 | + }, { |
| 43 | + filename: 'test.vue', |
| 44 | + code: ` |
| 45 | + <script> |
| 46 | + import {onBeforeMount, onBeforeUnmount, onBeforeUpdate, onErrorCaptured, onMounted, onRenderTracked, onRenderTriggered, onUnmounted, onUpdated, onActivated, onDeactivated} from 'vue' |
| 47 | + export default { |
| 48 | + async setup() { |
| 49 | + onBeforeMount(() => { /* ... */ }) |
| 50 | + onBeforeUnmount(() => { /* ... */ }) |
| 51 | + onBeforeUpdate(() => { /* ... */ }) |
| 52 | + onErrorCaptured(() => { /* ... */ }) |
| 53 | + onMounted(() => { /* ... */ }) |
| 54 | + onRenderTracked(() => { /* ... */ }) |
| 55 | + onRenderTriggered(() => { /* ... */ }) |
| 56 | + onUnmounted(() => { /* ... */ }) |
| 57 | + onUpdated(() => { /* ... */ }) |
| 58 | + onActivated(() => { /* ... */ }) |
| 59 | + onDeactivated(() => { /* ... */ }) |
| 60 | +
|
| 61 | + await doSomething() |
| 62 | + } |
| 63 | + } |
| 64 | + </script> |
| 65 | + ` |
| 66 | + }, |
| 67 | + { |
| 68 | + filename: 'test.vue', |
| 69 | + code: ` |
| 70 | + <script> |
| 71 | + import {onMounted} from 'vue' |
| 72 | + export default { |
| 73 | + async _setup() { |
| 74 | + await doSomething() |
| 75 | +
|
| 76 | + onMounted(() => { /* ... */ }) // error |
| 77 | + } |
| 78 | + } |
| 79 | + </script> |
| 80 | + ` |
| 81 | + } |
| 82 | + ], |
| 83 | + invalid: [ |
| 84 | + { |
| 85 | + filename: 'test.vue', |
| 86 | + code: ` |
| 87 | + <script> |
| 88 | + import {onMounted} from 'vue' |
| 89 | + export default { |
| 90 | + async setup() { |
| 91 | + await doSomething() |
| 92 | +
|
| 93 | + onMounted(() => { /* ... */ }) // error |
| 94 | + } |
| 95 | + } |
| 96 | + </script> |
| 97 | + `, |
| 98 | + errors: [ |
| 99 | + { |
| 100 | + message: 'The lifecycle hooks after `await` expression are forbidden.', |
| 101 | + line: 8, |
| 102 | + column: 11, |
| 103 | + endLine: 8, |
| 104 | + endColumn: 41 |
| 105 | + } |
| 106 | + ] |
| 107 | + }, |
| 108 | + { |
| 109 | + filename: 'test.vue', |
| 110 | + code: ` |
| 111 | + <script> |
| 112 | + import {onBeforeMount, onBeforeUnmount, onBeforeUpdate, onErrorCaptured, onMounted, onRenderTracked, onRenderTriggered, onUnmounted, onUpdated, onActivated, onDeactivated} from 'vue' |
| 113 | + export default { |
| 114 | + async setup() { |
| 115 | + await doSomething() |
| 116 | +
|
| 117 | + onBeforeMount(() => { /* ... */ }) |
| 118 | + onBeforeUnmount(() => { /* ... */ }) |
| 119 | + onBeforeUpdate(() => { /* ... */ }) |
| 120 | + onErrorCaptured(() => { /* ... */ }) |
| 121 | + onMounted(() => { /* ... */ }) |
| 122 | + onRenderTracked(() => { /* ... */ }) |
| 123 | + onRenderTriggered(() => { /* ... */ }) |
| 124 | + onUnmounted(() => { /* ... */ }) |
| 125 | + onUpdated(() => { /* ... */ }) |
| 126 | + onActivated(() => { /* ... */ }) |
| 127 | + onDeactivated(() => { /* ... */ }) |
| 128 | + } |
| 129 | + } |
| 130 | + </script> |
| 131 | + `, |
| 132 | + errors: [ |
| 133 | + { |
| 134 | + messageId: 'forbidden', |
| 135 | + line: 8 |
| 136 | + }, |
| 137 | + { |
| 138 | + messageId: 'forbidden', |
| 139 | + line: 9 |
| 140 | + }, |
| 141 | + { |
| 142 | + messageId: 'forbidden', |
| 143 | + line: 10 |
| 144 | + }, |
| 145 | + { |
| 146 | + messageId: 'forbidden', |
| 147 | + line: 11 |
| 148 | + }, |
| 149 | + { |
| 150 | + messageId: 'forbidden', |
| 151 | + line: 12 |
| 152 | + }, |
| 153 | + { |
| 154 | + messageId: 'forbidden', |
| 155 | + line: 13 |
| 156 | + }, |
| 157 | + { |
| 158 | + messageId: 'forbidden', |
| 159 | + line: 14 |
| 160 | + }, |
| 161 | + { |
| 162 | + messageId: 'forbidden', |
| 163 | + line: 15 |
| 164 | + }, |
| 165 | + { |
| 166 | + messageId: 'forbidden', |
| 167 | + line: 16 |
| 168 | + }, |
| 169 | + { |
| 170 | + messageId: 'forbidden', |
| 171 | + line: 17 |
| 172 | + }, |
| 173 | + { |
| 174 | + messageId: 'forbidden', |
| 175 | + line: 18 |
| 176 | + } |
| 177 | + ] |
| 178 | + } |
| 179 | + ] |
| 180 | +}) |
0 commit comments