|
| 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-template-depth') |
| 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-template-depth', rule, { |
| 19 | + valid: [ |
| 20 | + { |
| 21 | + filename: 'test.vue', |
| 22 | + code: ` |
| 23 | + <template> |
| 24 | + <main-container> |
| 25 | + <child-component> |
| 26 | + <div /> |
| 27 | + </child-component> |
| 28 | + </main-container> |
| 29 | + </template> |
| 30 | + `, |
| 31 | + options: [{ maxDepth: 3 }] |
| 32 | + }, |
| 33 | + { |
| 34 | + filename: 'test.vue', |
| 35 | + code: ` |
| 36 | + <template> |
| 37 | + <main-container> |
| 38 | + <ul> |
| 39 | + <li>Item 1</li> |
| 40 | + </ul> |
| 41 | + </main-container> |
| 42 | + </template> |
| 43 | + `, |
| 44 | + options: [{ maxDepth: 4 }] |
| 45 | + }, |
| 46 | + { |
| 47 | + filename: 'test.vue', |
| 48 | + code: ` |
| 49 | + <template> |
| 50 | + <main-container> |
| 51 | + <child-component> |
| 52 | + <sub-child-component> |
| 53 | + <div /> |
| 54 | + </sub-child-component> |
| 55 | + </child-component> |
| 56 | + </main-container> |
| 57 | + </template> |
| 58 | + `, |
| 59 | + options: [{ maxDepth: 4 }] |
| 60 | + }, |
| 61 | + { |
| 62 | + filename: 'test.vue', |
| 63 | + code: ` |
| 64 | + <template> |
| 65 | + <div> |
| 66 | + </div> |
| 67 | + <main-container> |
| 68 | + <child-component> |
| 69 | + <ul> |
| 70 | + <li /> |
| 71 | + </ul> |
| 72 | + </child-component> |
| 73 | + </main-container> |
| 74 | + </template> |
| 75 | + `, |
| 76 | + options: [{ maxDepth: 4 }] |
| 77 | + } |
| 78 | + ], |
| 79 | + invalid: [ |
| 80 | + { |
| 81 | + filename: 'test.vue', |
| 82 | + code: ` |
| 83 | + <template> |
| 84 | + <div> |
| 85 | + <div> |
| 86 | + <div> |
| 87 | + <div /> |
| 88 | + </div> |
| 89 | + </div> |
| 90 | + </div> |
| 91 | + </template> |
| 92 | + `, |
| 93 | + options: [{ maxDepth: 3 }], |
| 94 | + errors: [ |
| 95 | + { |
| 96 | + message: |
| 97 | + 'Element is nested too deeply (depth of 4, maximum allowed is 3).', |
| 98 | + line: 6 |
| 99 | + } |
| 100 | + ] |
| 101 | + }, |
| 102 | + { |
| 103 | + filename: 'test.vue', |
| 104 | + code: ` |
| 105 | + <template> |
| 106 | + <side-container> |
| 107 | + <child-component /> |
| 108 | + </side-container> |
| 109 | + <main-container> |
| 110 | + <child-component> |
| 111 | + <nested-component> |
| 112 | + <h1 /> |
| 113 | + </nested-component> |
| 114 | + </child-component> |
| 115 | + </main-container> |
| 116 | + </template> |
| 117 | + `, |
| 118 | + options: [{ maxDepth: 3 }], |
| 119 | + errors: [ |
| 120 | + { |
| 121 | + message: |
| 122 | + 'Element is nested too deeply (depth of 4, maximum allowed is 3).', |
| 123 | + line: 9 |
| 124 | + } |
| 125 | + ] |
| 126 | + }, |
| 127 | + { |
| 128 | + filename: 'test.vue', |
| 129 | + code: ` |
| 130 | + <template> |
| 131 | + <main-container> |
| 132 | + <nav-bar> |
| 133 | + <div /> |
| 134 | + </nav-bar> |
| 135 | + <child-component> |
| 136 | + <nested-component> |
| 137 | + <div> |
| 138 | + <div /> |
| 139 | + <div> |
| 140 | + </nested-component> |
| 141 | + </child-component> |
| 142 | + </main-container> |
| 143 | + </template> |
| 144 | + `, |
| 145 | + options: [{ maxDepth: 3 }], |
| 146 | + errors: [ |
| 147 | + { |
| 148 | + message: |
| 149 | + 'Element is nested too deeply (depth of 4, maximum allowed is 3).', |
| 150 | + line: 9, |
| 151 | + endLine: 12 |
| 152 | + }, |
| 153 | + { |
| 154 | + message: |
| 155 | + 'Element is nested too deeply (depth of 5, maximum allowed is 3).', |
| 156 | + line: 10, |
| 157 | + endLine: 10 |
| 158 | + }, |
| 159 | + { |
| 160 | + message: |
| 161 | + 'Element is nested too deeply (depth of 5, maximum allowed is 3).', |
| 162 | + line: 11, |
| 163 | + endLine: 12 |
| 164 | + } |
| 165 | + ] |
| 166 | + } |
| 167 | + ] |
| 168 | +}) |
0 commit comments