|
| 1 | +/** |
| 2 | + * @author Wayne Zhang |
| 3 | + * See LICENSE file in root directory for full license. |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | + |
| 7 | +const RuleTester = require('../../eslint-compat').RuleTester |
| 8 | +const rule = require('../../../lib/rules/define-props-destructuring') |
| 9 | + |
| 10 | +const tester = new RuleTester({ |
| 11 | + languageOptions: { |
| 12 | + parser: require('vue-eslint-parser'), |
| 13 | + ecmaVersion: 2015, |
| 14 | + sourceType: 'module' |
| 15 | + } |
| 16 | +}) |
| 17 | + |
| 18 | +tester.run('define-props-destructuring', rule, { |
| 19 | + valid: [ |
| 20 | + { |
| 21 | + filename: 'test.vue', |
| 22 | + code: ` |
| 23 | + <script setup> |
| 24 | + const { foo = 'default' } = defineProps(['foo']) |
| 25 | + </script> |
| 26 | + ` |
| 27 | + }, |
| 28 | + { |
| 29 | + filename: 'test.vue', |
| 30 | + code: ` |
| 31 | + <script setup lang="ts"> |
| 32 | + const { foo = 'default' } = defineProps<{ foo?: string }>() |
| 33 | + </script> |
| 34 | + `, |
| 35 | + languageOptions: { |
| 36 | + parserOptions: { parser: require.resolve('@typescript-eslint/parser') } |
| 37 | + } |
| 38 | + }, |
| 39 | + { |
| 40 | + filename: 'test.vue', |
| 41 | + code: ` |
| 42 | + <script setup> |
| 43 | + const props = defineProps(['foo']) |
| 44 | + </script> |
| 45 | + `, |
| 46 | + options: [{ destructure: 'never' }] |
| 47 | + }, |
| 48 | + { |
| 49 | + filename: 'test.vue', |
| 50 | + code: ` |
| 51 | + <script setup> |
| 52 | + const props = withDefaults(defineProps(['foo']), { foo: 'default' }) |
| 53 | + </script> |
| 54 | + `, |
| 55 | + options: [{ destructure: 'never' }] |
| 56 | + }, |
| 57 | + { |
| 58 | + filename: 'test.vue', |
| 59 | + code: ` |
| 60 | + <script setup lang="ts"> |
| 61 | + const props = defineProps<{ foo?: string }>() |
| 62 | + </script> |
| 63 | + `, |
| 64 | + options: [{ destructure: 'never' }], |
| 65 | + languageOptions: { |
| 66 | + parserOptions: { parser: require.resolve('@typescript-eslint/parser') } |
| 67 | + } |
| 68 | + } |
| 69 | + ], |
| 70 | + invalid: [ |
| 71 | + { |
| 72 | + filename: 'test.vue', |
| 73 | + code: ` |
| 74 | + <script setup> |
| 75 | + const props = defineProps(['foo']) |
| 76 | + </script> |
| 77 | + `, |
| 78 | + errors: [ |
| 79 | + { |
| 80 | + messageId: 'preferDestructuring', |
| 81 | + line: 3, |
| 82 | + column: 21 |
| 83 | + } |
| 84 | + ] |
| 85 | + }, |
| 86 | + { |
| 87 | + filename: 'test.vue', |
| 88 | + code: ` |
| 89 | + <script setup> |
| 90 | + const props = withDefaults(defineProps(['foo']), { foo: 'default' }) |
| 91 | + </script> |
| 92 | + `, |
| 93 | + errors: [ |
| 94 | + { |
| 95 | + messageId: 'preferDestructuring', |
| 96 | + line: 3, |
| 97 | + column: 34 |
| 98 | + } |
| 99 | + ] |
| 100 | + }, |
| 101 | + { |
| 102 | + filename: 'test.vue', |
| 103 | + code: ` |
| 104 | + <script setup> |
| 105 | + const { foo } = withDefaults(defineProps(['foo']), { foo: 'default' }) |
| 106 | + </script> |
| 107 | + `, |
| 108 | + errors: [ |
| 109 | + { |
| 110 | + messageId: 'avoidWithDefaults', |
| 111 | + line: 3, |
| 112 | + column: 23 |
| 113 | + } |
| 114 | + ] |
| 115 | + }, |
| 116 | + { |
| 117 | + filename: 'test.vue', |
| 118 | + code: ` |
| 119 | + <script setup lang="ts"> |
| 120 | + const props = withDefaults(defineProps<{ foo?: string }>(), { foo: 'default' }) |
| 121 | + </script> |
| 122 | + `, |
| 123 | + languageOptions: { |
| 124 | + parserOptions: { parser: require.resolve('@typescript-eslint/parser') } |
| 125 | + }, |
| 126 | + errors: [ |
| 127 | + { |
| 128 | + messageId: 'preferDestructuring', |
| 129 | + line: 3, |
| 130 | + column: 34 |
| 131 | + } |
| 132 | + ] |
| 133 | + }, |
| 134 | + { |
| 135 | + filename: 'test.vue', |
| 136 | + code: ` |
| 137 | + <script setup lang="ts"> |
| 138 | + const { foo } = withDefaults(defineProps<{ foo?: string }>(), { foo: 'default' }) |
| 139 | + </script> |
| 140 | + `, |
| 141 | + languageOptions: { |
| 142 | + parserOptions: { parser: require.resolve('@typescript-eslint/parser') } |
| 143 | + }, |
| 144 | + errors: [ |
| 145 | + { |
| 146 | + messageId: 'avoidWithDefaults', |
| 147 | + line: 3, |
| 148 | + column: 23 |
| 149 | + } |
| 150 | + ] |
| 151 | + }, |
| 152 | + { |
| 153 | + filename: 'test.vue', |
| 154 | + code: ` |
| 155 | + <script setup> |
| 156 | + const { foo } = defineProps(['foo']) |
| 157 | + </script> |
| 158 | + `, |
| 159 | + options: [{ destructure: 'never' }], |
| 160 | + errors: [ |
| 161 | + { |
| 162 | + messageId: 'avoidDestructuring', |
| 163 | + line: 3, |
| 164 | + column: 23 |
| 165 | + } |
| 166 | + ] |
| 167 | + }, |
| 168 | + { |
| 169 | + filename: 'test.vue', |
| 170 | + code: ` |
| 171 | + <script setup> |
| 172 | + const { foo } = withDefaults(defineProps(['foo']), { foo: 'default' }) |
| 173 | + </script> |
| 174 | + `, |
| 175 | + options: [{ destructure: 'never' }], |
| 176 | + errors: [ |
| 177 | + { |
| 178 | + messageId: 'avoidDestructuring', |
| 179 | + line: 3, |
| 180 | + column: 36 |
| 181 | + } |
| 182 | + ] |
| 183 | + }, |
| 184 | + { |
| 185 | + filename: 'test.vue', |
| 186 | + code: ` |
| 187 | + <script setup lang="ts"> |
| 188 | + const { foo } = defineProps<{ foo?: string }>() |
| 189 | + </script> |
| 190 | + `, |
| 191 | + options: [{ destructure: 'never' }], |
| 192 | + languageOptions: { |
| 193 | + parserOptions: { parser: require.resolve('@typescript-eslint/parser') } |
| 194 | + }, |
| 195 | + errors: [ |
| 196 | + { |
| 197 | + messageId: 'avoidDestructuring', |
| 198 | + line: 3, |
| 199 | + column: 23 |
| 200 | + } |
| 201 | + ] |
| 202 | + } |
| 203 | + ] |
| 204 | +}) |
0 commit comments