|
| 1 | +/** |
| 2 | + * @author kevsommer Kevin Sommer |
| 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/max-props') |
| 9 | + |
| 10 | +const tester = new RuleTester({ |
| 11 | + languageOptions: { |
| 12 | + parser: require('vue-eslint-parser'), |
| 13 | + ecmaVersion: 2020, |
| 14 | + sourceType: 'module' |
| 15 | + } |
| 16 | +}) |
| 17 | + |
| 18 | +tester.run('max-props', rule, { |
| 19 | + valid: [ |
| 20 | + { |
| 21 | + filename: 'test.vue', |
| 22 | + code: ` |
| 23 | + <script setup> |
| 24 | + defineProps({ prop1: '', prop2: '' }) |
| 25 | + </script> |
| 26 | + `, |
| 27 | + options: [{ maxProps: 5 }] |
| 28 | + }, |
| 29 | + { |
| 30 | + filename: 'test.vue', |
| 31 | + code: ` |
| 32 | + <script> |
| 33 | + export default { |
| 34 | + props: { |
| 35 | + prop1: String, |
| 36 | + prop2: String |
| 37 | + } |
| 38 | + } |
| 39 | + </script> |
| 40 | + `, |
| 41 | + options: [{ maxProps: 5 }] |
| 42 | + }, |
| 43 | + { |
| 44 | + filename: 'test.vue', |
| 45 | + code: ` |
| 46 | + <script> |
| 47 | + export default { |
| 48 | + props: { |
| 49 | + prop1: String, |
| 50 | + prop2: String, |
| 51 | + prop3: String, |
| 52 | + prop4: String, |
| 53 | + prop5: String |
| 54 | + } |
| 55 | + } |
| 56 | + </script> |
| 57 | + `, |
| 58 | + options: [{ maxProps: 5 }] |
| 59 | + }, |
| 60 | + { |
| 61 | + filename: 'test.vue', |
| 62 | + code: ` |
| 63 | + <script> |
| 64 | + export default { |
| 65 | + props: {} |
| 66 | + } |
| 67 | + </script> |
| 68 | + `, |
| 69 | + options: [{ maxProps: 5 }] |
| 70 | + }, |
| 71 | + { |
| 72 | + filename: 'test.vue', |
| 73 | + code: ` |
| 74 | + <script setup> |
| 75 | + defineProps({}) |
| 76 | + </script> |
| 77 | + `, |
| 78 | + options: [{ maxProps: 5 }] |
| 79 | + }, |
| 80 | + { |
| 81 | + filename: 'test.vue', |
| 82 | + code: ` |
| 83 | + <script> |
| 84 | + </script> |
| 85 | + `, |
| 86 | + options: [{ maxProps: 5 }] |
| 87 | + }, |
| 88 | + { |
| 89 | + filename: 'test.vue', |
| 90 | + code: ` |
| 91 | + <script setup lang="ts"> |
| 92 | + defineProps<{ prop1: string, prop2: string }>(); |
| 93 | + </script> |
| 94 | + `, |
| 95 | + options: [{ maxProps: 5 }], |
| 96 | + languageOptions: { |
| 97 | + parser: require('vue-eslint-parser'), |
| 98 | + parserOptions: { |
| 99 | + parser: require.resolve('@typescript-eslint/parser') |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + ], |
| 104 | + invalid: [ |
| 105 | + { |
| 106 | + filename: 'test.vue', |
| 107 | + code: ` |
| 108 | + <script setup> |
| 109 | + defineProps({ prop1: '', prop2: '' }) |
| 110 | + </script> |
| 111 | + `, |
| 112 | + options: [{ maxProps: 1 }], |
| 113 | + errors: [ |
| 114 | + { |
| 115 | + message: 'Component has too many props (2). Maximum allowed is 1.', |
| 116 | + line: 3 |
| 117 | + } |
| 118 | + ] |
| 119 | + }, |
| 120 | + { |
| 121 | + filename: 'test.vue', |
| 122 | + code: ` |
| 123 | + <script> |
| 124 | + export default { |
| 125 | + props: { |
| 126 | + prop1: String, |
| 127 | + prop2: String |
| 128 | + } |
| 129 | + } |
| 130 | + </script> |
| 131 | + `, |
| 132 | + options: [{ maxProps: 1 }], |
| 133 | + errors: [ |
| 134 | + { |
| 135 | + message: 'Component has too many props (2). Maximum allowed is 1.', |
| 136 | + line: 5 |
| 137 | + } |
| 138 | + ] |
| 139 | + }, |
| 140 | + { |
| 141 | + filename: 'test.vue', |
| 142 | + code: ` |
| 143 | + <script setup lang="ts"> |
| 144 | + defineProps<{ prop1: string, prop2: string, prop3: string }>(); |
| 145 | + </script> |
| 146 | + `, |
| 147 | + options: [{ maxProps: 2 }], |
| 148 | + languageOptions: { |
| 149 | + parser: require('vue-eslint-parser'), |
| 150 | + parserOptions: { |
| 151 | + parser: require.resolve('@typescript-eslint/parser') |
| 152 | + } |
| 153 | + }, |
| 154 | + errors: [ |
| 155 | + { |
| 156 | + message: 'Component has too many props (3). Maximum allowed is 2.', |
| 157 | + line: 3 |
| 158 | + } |
| 159 | + ] |
| 160 | + } |
| 161 | + ] |
| 162 | +}) |
0 commit comments