|
| 1 | +/** |
| 2 | + * @author Yosuke Ota |
| 3 | + */ |
| 4 | +'use strict' |
| 5 | + |
| 6 | +// ------------------------------------------------------------------------------ |
| 7 | +// Requirements |
| 8 | +// ------------------------------------------------------------------------------ |
| 9 | + |
| 10 | +const rule = require('../../../lib/rules/multiline-html-element-content-newline') |
| 11 | +const RuleTester = require('eslint').RuleTester |
| 12 | + |
| 13 | +// ------------------------------------------------------------------------------ |
| 14 | +// Tests |
| 15 | +// ------------------------------------------------------------------------------ |
| 16 | + |
| 17 | +const tester = new RuleTester({ |
| 18 | + parser: 'vue-eslint-parser', |
| 19 | + parserOptions: { |
| 20 | + ecmaVersion: 2015 |
| 21 | + } |
| 22 | +}) |
| 23 | + |
| 24 | +tester.run('multiline-html-element-content-newline', rule, { |
| 25 | + valid: [ |
| 26 | + `<template><div class="panel">content</div></template>`, |
| 27 | + `<template><div class="panel"><div></div></div></template>`, |
| 28 | + `<template><div class="panel"><!-- comment --></div></template>`, |
| 29 | + ` |
| 30 | + <template> |
| 31 | + <div class="panel"> |
| 32 | + content |
| 33 | + </div> |
| 34 | + </template>`, |
| 35 | + ` |
| 36 | + <template> |
| 37 | + <div |
| 38 | + class="panel" |
| 39 | + > |
| 40 | + content |
| 41 | + </div> |
| 42 | + </template>`, |
| 43 | + ` |
| 44 | + <template> |
| 45 | + <div> |
| 46 | + <div> |
| 47 | + content |
| 48 | + content |
| 49 | + </div> |
| 50 | + </div> |
| 51 | + </template>`, |
| 52 | + `<div>multiline end tag</div |
| 53 | + >`, |
| 54 | + // empty |
| 55 | + `<template><div class="panel"></div></template>`, |
| 56 | + ` |
| 57 | + <template> |
| 58 | + <div |
| 59 | + class="panel"> |
| 60 | + </div> |
| 61 | + </template>`, |
| 62 | + // self closing |
| 63 | + ` |
| 64 | + <template> |
| 65 | + <self-closing /> |
| 66 | + </template>`, |
| 67 | + // ignores |
| 68 | + ` |
| 69 | + <template> |
| 70 | + <pre>content</pre> |
| 71 | + <pre |
| 72 | + id="test-pre" |
| 73 | + >content</pre> |
| 74 | + <pre><div |
| 75 | + >content</div></pre> |
| 76 | + <pre>content |
| 77 | + content</pre> |
| 78 | + <textarea>content</textarea> |
| 79 | + <textarea |
| 80 | + id="test-textarea" |
| 81 | + >content</textarea> |
| 82 | + <textarea>content |
| 83 | + content</textarea> |
| 84 | + </template>`, |
| 85 | + { |
| 86 | + code: ` |
| 87 | + <template> |
| 88 | + <ignore-tag>content</ignore-tag> |
| 89 | + <ignore-tag |
| 90 | + id="test-pre" |
| 91 | + >content</ignore-tag> |
| 92 | + <ignore-tag><div |
| 93 | + >content</div></ignore-tag> |
| 94 | + <ignore-tag>>content |
| 95 | + content</ignore-tag> |
| 96 | + </template>`, |
| 97 | + options: [{ |
| 98 | + ignores: ['ignore-tag'] |
| 99 | + }] |
| 100 | + }, |
| 101 | + // Ignore if no closing brackets |
| 102 | + ` |
| 103 | + <template> |
| 104 | + <div |
| 105 | + id= |
| 106 | + "" |
| 107 | + ` |
| 108 | + ], |
| 109 | + invalid: [ |
| 110 | + { |
| 111 | + code: ` |
| 112 | + <template> |
| 113 | + <div |
| 114 | + class="panel" |
| 115 | + >content</div> |
| 116 | + </template> |
| 117 | + `, |
| 118 | + output: ` |
| 119 | + <template> |
| 120 | + <div |
| 121 | + class="panel" |
| 122 | + > |
| 123 | +content |
| 124 | +</div> |
| 125 | + </template> |
| 126 | + `, |
| 127 | + errors: [ |
| 128 | + { |
| 129 | + message: 'Expected 1 line break after opening tag (`<div>`), but no line breaks found.', |
| 130 | + line: 5, |
| 131 | + column: 12, |
| 132 | + nodeType: 'HTMLTagClose', |
| 133 | + endLine: 5, |
| 134 | + endColumn: 12 |
| 135 | + }, |
| 136 | + { |
| 137 | + message: 'Expected 1 line break before closing tag (`</div>`), but no line breaks found.', |
| 138 | + line: 5, |
| 139 | + column: 19, |
| 140 | + nodeType: 'HTMLEndTagOpen', |
| 141 | + endLine: 5, |
| 142 | + endColumn: 19 |
| 143 | + } |
| 144 | + ] |
| 145 | + }, |
| 146 | + // spaces |
| 147 | + { |
| 148 | + code: ` |
| 149 | + <template> |
| 150 | + <div |
| 151 | + class="panel" |
| 152 | + > content</div> |
| 153 | + </template> |
| 154 | + `, |
| 155 | + output: ` |
| 156 | + <template> |
| 157 | + <div |
| 158 | + class="panel" |
| 159 | + > |
| 160 | +content |
| 161 | +</div> |
| 162 | + </template> |
| 163 | + `, |
| 164 | + errors: [ |
| 165 | + { |
| 166 | + message: 'Expected 1 line break after opening tag (`<div>`), but no line breaks found.', |
| 167 | + line: 5, |
| 168 | + column: 12, |
| 169 | + endLine: 5, |
| 170 | + endColumn: 15 |
| 171 | + }, |
| 172 | + { |
| 173 | + message: 'Expected 1 line break before closing tag (`</div>`), but no line breaks found.', |
| 174 | + line: 5, |
| 175 | + column: 22, |
| 176 | + endLine: 5, |
| 177 | + endColumn: 22 |
| 178 | + } |
| 179 | + ] |
| 180 | + }, |
| 181 | + // elements |
| 182 | + { |
| 183 | + code: ` |
| 184 | + <template> |
| 185 | + <div><div></div> |
| 186 | + <div></div></div> |
| 187 | + </template> |
| 188 | + `, |
| 189 | + output: ` |
| 190 | + <template> |
| 191 | + <div> |
| 192 | +<div></div> |
| 193 | + <div></div> |
| 194 | +</div> |
| 195 | + </template> |
| 196 | + `, |
| 197 | + errors: [ |
| 198 | + { |
| 199 | + message: 'Expected 1 line break after opening tag (`<div>`), but no line breaks found.', |
| 200 | + line: 3, |
| 201 | + column: 16, |
| 202 | + endLine: 3, |
| 203 | + endColumn: 16 |
| 204 | + }, |
| 205 | + { |
| 206 | + message: 'Expected 1 line break before closing tag (`</div>`), but no line breaks found.', |
| 207 | + line: 4, |
| 208 | + column: 22, |
| 209 | + endLine: 4, |
| 210 | + endColumn: 22 |
| 211 | + } |
| 212 | + ] |
| 213 | + }, |
| 214 | + // contents |
| 215 | + { |
| 216 | + code: ` |
| 217 | + <template> |
| 218 | + <div>multiline |
| 219 | + content</div> |
| 220 | + </template>`, |
| 221 | + output: ` |
| 222 | + <template> |
| 223 | + <div> |
| 224 | +multiline |
| 225 | + content |
| 226 | +</div> |
| 227 | + </template>`, |
| 228 | + errors: [ |
| 229 | + 'Expected 1 line break after opening tag (`<div>`), but no line breaks found.', |
| 230 | + 'Expected 1 line break before closing tag (`</div>`), but no line breaks found.' |
| 231 | + ] |
| 232 | + }, |
| 233 | + { |
| 234 | + code: ` |
| 235 | + <template> |
| 236 | + <div>multiline content |
| 237 | + </div> |
| 238 | + </template>`, |
| 239 | + output: ` |
| 240 | + <template> |
| 241 | + <div> |
| 242 | +multiline content |
| 243 | + </div> |
| 244 | + </template>`, |
| 245 | + errors: [ |
| 246 | + 'Expected 1 line break after opening tag (`<div>`), but no line breaks found.' |
| 247 | + ] |
| 248 | + }, |
| 249 | + { |
| 250 | + code: ` |
| 251 | + <template> |
| 252 | + <div> |
| 253 | + multiline content</div> |
| 254 | + </template>`, |
| 255 | + output: ` |
| 256 | + <template> |
| 257 | + <div> |
| 258 | + multiline content |
| 259 | +</div> |
| 260 | + </template>`, |
| 261 | + errors: [ |
| 262 | + 'Expected 1 line break before closing tag (`</div>`), but no line breaks found.' |
| 263 | + ] |
| 264 | + }, |
| 265 | + // comments |
| 266 | + { |
| 267 | + code: ` |
| 268 | + <template> |
| 269 | + <div><!--comment--> |
| 270 | + <!--comment--></div> |
| 271 | + </template> |
| 272 | + `, |
| 273 | + output: ` |
| 274 | + <template> |
| 275 | + <div> |
| 276 | +<!--comment--> |
| 277 | + <!--comment--> |
| 278 | +</div> |
| 279 | + </template> |
| 280 | + `, |
| 281 | + errors: [ |
| 282 | + 'Expected 1 line break after opening tag (`<div>`), but no line breaks found.', |
| 283 | + 'Expected 1 line break before closing tag (`</div>`), but no line breaks found.' |
| 284 | + ] |
| 285 | + }, |
| 286 | + { |
| 287 | + code: ` |
| 288 | + <template> |
| 289 | + <div><!--comment |
| 290 | + comment--></div> |
| 291 | + </template> |
| 292 | + `, |
| 293 | + output: ` |
| 294 | + <template> |
| 295 | + <div> |
| 296 | +<!--comment |
| 297 | + comment--> |
| 298 | +</div> |
| 299 | + </template> |
| 300 | + `, |
| 301 | + errors: [ |
| 302 | + 'Expected 1 line break after opening tag (`<div>`), but no line breaks found.', |
| 303 | + 'Expected 1 line break before closing tag (`</div>`), but no line breaks found.' |
| 304 | + ] |
| 305 | + }, |
| 306 | + // one error |
| 307 | + { |
| 308 | + code: ` |
| 309 | + <template> |
| 310 | + <div>content |
| 311 | + content |
| 312 | + </div> |
| 313 | + </template> |
| 314 | + `, |
| 315 | + output: ` |
| 316 | + <template> |
| 317 | + <div> |
| 318 | +content |
| 319 | + content |
| 320 | + </div> |
| 321 | + </template> |
| 322 | + `, |
| 323 | + errors: [ |
| 324 | + 'Expected 1 line break after opening tag (`<div>`), but no line breaks found.' |
| 325 | + ] |
| 326 | + }, |
| 327 | + { |
| 328 | + code: ` |
| 329 | + <template> |
| 330 | + <div> |
| 331 | + content |
| 332 | + content</div> |
| 333 | + </template> |
| 334 | + `, |
| 335 | + output: ` |
| 336 | + <template> |
| 337 | + <div> |
| 338 | + content |
| 339 | + content |
| 340 | +</div> |
| 341 | + </template> |
| 342 | + `, |
| 343 | + errors: [ |
| 344 | + 'Expected 1 line break before closing tag (`</div>`), but no line breaks found.' |
| 345 | + ] |
| 346 | + }, |
| 347 | + // multi |
| 348 | + { |
| 349 | + code: ` |
| 350 | + <template><div>content<div>content |
| 351 | + content</div>content</div></template> |
| 352 | + `, |
| 353 | + output: ` |
| 354 | + <template> |
| 355 | +<div> |
| 356 | +content<div> |
| 357 | +content |
| 358 | + content |
| 359 | +</div>content |
| 360 | +</div> |
| 361 | +</template> |
| 362 | + `, |
| 363 | + errors: [ |
| 364 | + 'Expected 1 line break after opening tag (`<template>`), but no line breaks found.', |
| 365 | + 'Expected 1 line break after opening tag (`<div>`), but no line breaks found.', |
| 366 | + 'Expected 1 line break after opening tag (`<div>`), but no line breaks found.', |
| 367 | + 'Expected 1 line break before closing tag (`</div>`), but no line breaks found.', |
| 368 | + 'Expected 1 line break before closing tag (`</div>`), but no line breaks found.', |
| 369 | + 'Expected 1 line break before closing tag (`</template>`), but no line breaks found.' |
| 370 | + ] |
| 371 | + }, |
| 372 | + // multi line breaks |
| 373 | + { |
| 374 | + code: ` |
| 375 | + <template> |
| 376 | + <div> |
| 377 | +
|
| 378 | + content |
| 379 | + content |
| 380 | +
|
| 381 | + </div> |
| 382 | + </template> |
| 383 | + `, |
| 384 | + output: ` |
| 385 | + <template> |
| 386 | + <div> |
| 387 | +content |
| 388 | + content |
| 389 | +</div> |
| 390 | + </template> |
| 391 | + `, |
| 392 | + errors: [ |
| 393 | + 'Expected 1 line break after opening tag (`<div>`), but 2 line breaks found.', |
| 394 | + 'Expected 1 line break before closing tag (`</div>`), but 2 line breaks found.' |
| 395 | + ] |
| 396 | + }, |
| 397 | + // mustache |
| 398 | + { |
| 399 | + code: ` |
| 400 | + <template> |
| 401 | + <div>{{content}} |
| 402 | + {{content2}}</div> |
| 403 | + </template> |
| 404 | + `, |
| 405 | + output: ` |
| 406 | + <template> |
| 407 | + <div> |
| 408 | +{{content}} |
| 409 | + {{content2}} |
| 410 | +</div> |
| 411 | + </template> |
| 412 | + `, |
| 413 | + errors: [ |
| 414 | + 'Expected 1 line break after opening tag (`<div>`), but no line breaks found.', |
| 415 | + 'Expected 1 line break before closing tag (`</div>`), but no line breaks found.' |
| 416 | + ] |
| 417 | + }, |
| 418 | + // mix |
| 419 | + { |
| 420 | + code: ` |
| 421 | + <template> |
| 422 | + <div>content |
| 423 | + <child></child> |
| 424 | + <!-- comment --></div> |
| 425 | + </template> |
| 426 | + `, |
| 427 | + output: ` |
| 428 | + <template> |
| 429 | + <div> |
| 430 | +content |
| 431 | + <child></child> |
| 432 | + <!-- comment --> |
| 433 | +</div> |
| 434 | + </template> |
| 435 | + `, |
| 436 | + errors: [ |
| 437 | + 'Expected 1 line break after opening tag (`<div>`), but no line breaks found.', |
| 438 | + 'Expected 1 line break before closing tag (`</div>`), but no line breaks found.' |
| 439 | + ] |
| 440 | + }, |
| 441 | + // start tag |
| 442 | + { |
| 443 | + code: ` |
| 444 | + <template> |
| 445 | + <div |
| 446 | + >content</div> |
| 447 | + </template> |
| 448 | + `, |
| 449 | + output: ` |
| 450 | + <template> |
| 451 | + <div |
| 452 | + > |
| 453 | +content |
| 454 | +</div> |
| 455 | + </template> |
| 456 | + `, |
| 457 | + errors: [ |
| 458 | + 'Expected 1 line break after opening tag (`<div>`), but no line breaks found.', |
| 459 | + 'Expected 1 line break before closing tag (`</div>`), but no line breaks found.' |
| 460 | + ] |
| 461 | + }, |
| 462 | + { |
| 463 | + code: ` |
| 464 | + <template> |
| 465 | + <div |
| 466 | + attr>content</div> |
| 467 | + </template> |
| 468 | + `, |
| 469 | + output: ` |
| 470 | + <template> |
| 471 | + <div |
| 472 | + attr> |
| 473 | +content |
| 474 | +</div> |
| 475 | + </template> |
| 476 | + `, |
| 477 | + errors: [ |
| 478 | + 'Expected 1 line break after opening tag (`<div>`), but no line breaks found.', |
| 479 | + 'Expected 1 line break before closing tag (`</div>`), but no line breaks found.' |
| 480 | + ] |
| 481 | + }, |
| 482 | + { |
| 483 | + code: ` |
| 484 | + <template> |
| 485 | + <div |
| 486 | + ></div> |
| 487 | + </template> |
| 488 | + `, |
| 489 | + output: ` |
| 490 | + <template> |
| 491 | + <div |
| 492 | + > |
| 493 | +</div> |
| 494 | + </template> |
| 495 | + `, |
| 496 | + errors: ['Expected 1 line break after opening tag (`<div>`), but no line breaks found.'] |
| 497 | + } |
| 498 | + ] |
| 499 | +}) |
0 commit comments