|
| 1 | +/** |
| 2 | + * @author dev1437 |
| 3 | + * See LICENSE file in root directory for full license. |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | + |
| 7 | +const { RuleTester, ESLint } = require('../../eslint-compat') |
| 8 | +const rule = require('../../../lib/rules/multiline-ternary') |
| 9 | +const semver = require('semver') |
| 10 | + |
| 11 | +const tester = new RuleTester({ |
| 12 | + parser: require.resolve('vue-eslint-parser'), |
| 13 | + parserOptions: { |
| 14 | + ecmaVersion: 2020, |
| 15 | + sourceType: 'module' |
| 16 | + } |
| 17 | +}) |
| 18 | + |
| 19 | +tester.run('multiline-ternary', rule, { |
| 20 | + valid: [ |
| 21 | + { |
| 22 | + filename: 'test.vue', |
| 23 | + code: ` |
| 24 | + <template> |
| 25 | + <div :class="{ |
| 26 | + 'test': someReallyLongCondition ? |
| 27 | + aVeryLongOutput : |
| 28 | + thisCantFitOnASingleLine |
| 29 | + }"> |
| 30 | + </div> |
| 31 | + </template> |
| 32 | + ` |
| 33 | + }, |
| 34 | + { |
| 35 | + // doesn't check ternary statements in <script> block |
| 36 | + filename: 'test.vue', |
| 37 | + code: ` |
| 38 | + <script> |
| 39 | + let test = someReallyLongCondition ? aVeryLongOutput : thisCantFitOnASingleLine |
| 40 | + </script> |
| 41 | + ` |
| 42 | + }, |
| 43 | + { |
| 44 | + filename: 'test.vue', |
| 45 | + code: ` |
| 46 | + <template> |
| 47 | + <div :class="{ |
| 48 | + 'test': someReallyLongCondition ? aVeryLongOutput : thisCantFitOnASingleLine |
| 49 | + }"> |
| 50 | + </div> |
| 51 | + </template> |
| 52 | + `, |
| 53 | + options: ['never'] |
| 54 | + }, |
| 55 | + { |
| 56 | + filename: 'test.vue', |
| 57 | + code: ` |
| 58 | + <template> |
| 59 | + <div class="test"> |
| 60 | + </div> |
| 61 | + </template> |
| 62 | + <style> |
| 63 | + .test { |
| 64 | + color: v-bind('someReallyLongCondition ? aVeryLongOutput : thisCantFitOnASingleLine') |
| 65 | + } |
| 66 | + </style> |
| 67 | + `, |
| 68 | + options: ['never'] |
| 69 | + } |
| 70 | + ], |
| 71 | + invalid: [ |
| 72 | + { |
| 73 | + filename: 'test.vue', |
| 74 | + code: ` |
| 75 | + <template> |
| 76 | + <div :class="{ |
| 77 | + 'test': someReallyLongCondition ? |
| 78 | + aVeryLongOutput : thisCantFitOnASingleLine |
| 79 | + }"> |
| 80 | + </div> |
| 81 | + </template> |
| 82 | + `, |
| 83 | + output: semver.gte(ESLint.version, '7.1.0') |
| 84 | + ? ` |
| 85 | + <template> |
| 86 | + <div :class="{ |
| 87 | + 'test': someReallyLongCondition ? |
| 88 | + aVeryLongOutput |
| 89 | +: thisCantFitOnASingleLine |
| 90 | + }"> |
| 91 | + </div> |
| 92 | + </template> |
| 93 | + ` |
| 94 | + : null, |
| 95 | + errors: [ |
| 96 | + { |
| 97 | + message: |
| 98 | + 'Expected newline between consequent and alternate of ternary expression.', |
| 99 | + line: 5, |
| 100 | + column: 15 |
| 101 | + } |
| 102 | + ], |
| 103 | + options: ['always-multiline'] |
| 104 | + }, |
| 105 | + { |
| 106 | + filename: 'test.vue', |
| 107 | + code: ` |
| 108 | + <template> |
| 109 | + <div :class="{ |
| 110 | + 'test': someReallyLongCondition ? |
| 111 | + aVeryLongOutput : thisCantFitOnASingleLine |
| 112 | + }"> |
| 113 | + </div> |
| 114 | + </template> |
| 115 | + `, |
| 116 | + output: semver.gte(ESLint.version, '7.1.0') |
| 117 | + ? ` |
| 118 | + <template> |
| 119 | + <div :class="{ |
| 120 | + 'test': someReallyLongCondition ?aVeryLongOutput : thisCantFitOnASingleLine |
| 121 | + }"> |
| 122 | + </div> |
| 123 | + </template> |
| 124 | + ` |
| 125 | + : null, |
| 126 | + errors: [ |
| 127 | + { |
| 128 | + message: |
| 129 | + 'Unexpected newline between test and consequent of ternary expression.', |
| 130 | + line: 4, |
| 131 | + column: 21 |
| 132 | + } |
| 133 | + ], |
| 134 | + options: ['never'] |
| 135 | + }, |
| 136 | + { |
| 137 | + filename: 'test.vue', |
| 138 | + code: ` |
| 139 | + <template> |
| 140 | + <div :class="{ |
| 141 | + 'test': someReallyLongCondition ? aVeryLongOutput : thisCantFitOnASingleLine |
| 142 | + }"> |
| 143 | + </div> |
| 144 | + </template> |
| 145 | + `, |
| 146 | + output: semver.gte(ESLint.version, '7.1.0') |
| 147 | + ? ` |
| 148 | + <template> |
| 149 | + <div :class="{ |
| 150 | + 'test': someReallyLongCondition |
| 151 | +? aVeryLongOutput |
| 152 | +: thisCantFitOnASingleLine |
| 153 | + }"> |
| 154 | + </div> |
| 155 | + </template> |
| 156 | + ` |
| 157 | + : null, |
| 158 | + errors: [ |
| 159 | + { |
| 160 | + message: |
| 161 | + 'Expected newline between test and consequent of ternary expression.', |
| 162 | + line: 4, |
| 163 | + column: 21 |
| 164 | + }, |
| 165 | + { |
| 166 | + message: |
| 167 | + 'Expected newline between consequent and alternate of ternary expression.', |
| 168 | + line: 4, |
| 169 | + column: 47 |
| 170 | + } |
| 171 | + ] |
| 172 | + }, |
| 173 | + { |
| 174 | + filename: 'test.vue', |
| 175 | + code: ` |
| 176 | + <template> |
| 177 | + <div :style="{ |
| 178 | + 'test': someReallyLongCondition ? aVeryLongOutput : thisCantFitOnASingleLine |
| 179 | + }"> |
| 180 | + </div> |
| 181 | + </template> |
| 182 | + `, |
| 183 | + output: semver.gte(ESLint.version, '7.1.0') |
| 184 | + ? ` |
| 185 | + <template> |
| 186 | + <div :style="{ |
| 187 | + 'test': someReallyLongCondition |
| 188 | +? aVeryLongOutput |
| 189 | +: thisCantFitOnASingleLine |
| 190 | + }"> |
| 191 | + </div> |
| 192 | + </template> |
| 193 | + ` |
| 194 | + : null, |
| 195 | + errors: [ |
| 196 | + { |
| 197 | + message: |
| 198 | + 'Expected newline between test and consequent of ternary expression.', |
| 199 | + line: 4, |
| 200 | + column: 21 |
| 201 | + }, |
| 202 | + { |
| 203 | + message: |
| 204 | + 'Expected newline between consequent and alternate of ternary expression.', |
| 205 | + line: 4, |
| 206 | + column: 47 |
| 207 | + } |
| 208 | + ] |
| 209 | + }, |
| 210 | + { |
| 211 | + filename: 'test.vue', |
| 212 | + code: ` |
| 213 | + <template> |
| 214 | + <div class="test"> |
| 215 | + </div> |
| 216 | + </template> |
| 217 | + <style> |
| 218 | + .test { |
| 219 | + color: v-bind('someReallyLongCondition ? aVeryLongOutput : thisCantFitOnASingleLine') |
| 220 | + } |
| 221 | + </style> |
| 222 | + `, |
| 223 | + output: semver.gte(ESLint.version, '7.1.0') |
| 224 | + ? ` |
| 225 | + <template> |
| 226 | + <div class="test"> |
| 227 | + </div> |
| 228 | + </template> |
| 229 | + <style> |
| 230 | + .test { |
| 231 | + color: v-bind('someReallyLongCondition |
| 232 | +? aVeryLongOutput |
| 233 | +: thisCantFitOnASingleLine') |
| 234 | + } |
| 235 | + </style> |
| 236 | + ` |
| 237 | + : null, |
| 238 | + errors: [ |
| 239 | + { |
| 240 | + message: |
| 241 | + 'Expected newline between test and consequent of ternary expression.', |
| 242 | + line: 8, |
| 243 | + column: 30 |
| 244 | + }, |
| 245 | + { |
| 246 | + message: |
| 247 | + 'Expected newline between consequent and alternate of ternary expression.', |
| 248 | + line: 8, |
| 249 | + column: 56 |
| 250 | + } |
| 251 | + ] |
| 252 | + }, |
| 253 | + { |
| 254 | + filename: 'test.vue', |
| 255 | + code: ` |
| 256 | + <template> |
| 257 | + <div :class="{ |
| 258 | + 'test': someReallyLongCondition ? aVeryLongOutput : thisCantFitOnASingleLine |
| 259 | + }"> |
| 260 | + </div> |
| 261 | + </template> |
| 262 | + <script> |
| 263 | + let test = someReallyLongCondition ? aVeryLongOutput : thisCantFitOnASingleLine |
| 264 | + </script> |
| 265 | + `, |
| 266 | + output: semver.gte(ESLint.version, '7.1.0') |
| 267 | + ? ` |
| 268 | + <template> |
| 269 | + <div :class="{ |
| 270 | + 'test': someReallyLongCondition |
| 271 | +? aVeryLongOutput |
| 272 | +: thisCantFitOnASingleLine |
| 273 | + }"> |
| 274 | + </div> |
| 275 | + </template> |
| 276 | + <script> |
| 277 | + let test = someReallyLongCondition ? aVeryLongOutput : thisCantFitOnASingleLine |
| 278 | + </script> |
| 279 | + ` |
| 280 | + : null, |
| 281 | + errors: [ |
| 282 | + { |
| 283 | + message: |
| 284 | + 'Expected newline between test and consequent of ternary expression.', |
| 285 | + line: 4, |
| 286 | + column: 19 |
| 287 | + }, |
| 288 | + { |
| 289 | + message: |
| 290 | + 'Expected newline between consequent and alternate of ternary expression.', |
| 291 | + line: 4, |
| 292 | + column: 45 |
| 293 | + } |
| 294 | + ] |
| 295 | + } |
| 296 | + ] |
| 297 | +}) |
0 commit comments