codeframe.spec.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { generateCodeFrame } from 'compiler/codeframe'
  2. describe('codeframe', () => {
  3. const source = `
  4. <div>
  5. <template key="one"></template>
  6. <ul>
  7. <li v-for="foobar">hi</li>
  8. </ul>
  9. <template key="two"></template>
  10. </div>
  11. `.trim()
  12. it('line near top', () => {
  13. const keyStart = source.indexOf(`key="one"`)
  14. const keyEnd = keyStart + `key="one"`.length
  15. expect(generateCodeFrame(source, keyStart, keyEnd)).toBe(
  16. `
  17. 1 | <div>
  18. 2 | <template key="one"></template>
  19. | ^^^^^^^^^
  20. 3 | <ul>
  21. 4 | <li v-for="foobar">hi</li>
  22. `.trim()
  23. )
  24. })
  25. it('line in middle', () => {
  26. // should cover 5 lines
  27. const forStart = source.indexOf(`v-for=`)
  28. const forEnd = forStart + `v-for="foobar"`.length
  29. expect(generateCodeFrame(source, forStart, forEnd)).toBe(
  30. `
  31. 2 | <template key="one"></template>
  32. 3 | <ul>
  33. 4 | <li v-for="foobar">hi</li>
  34. | ^^^^^^^^^^^^^^
  35. 5 | </ul>
  36. 6 | <template key="two"></template>
  37. `.trim()
  38. )
  39. })
  40. it('line near bottom', () => {
  41. const keyStart = source.indexOf(`key="two"`)
  42. const keyEnd = keyStart + `key="two"`.length
  43. expect(generateCodeFrame(source, keyStart, keyEnd)).toBe(
  44. `
  45. 4 | <li v-for="foobar">hi</li>
  46. 5 | </ul>
  47. 6 | <template key="two"></template>
  48. | ^^^^^^^^^
  49. 7 | </div>
  50. `.trim()
  51. )
  52. })
  53. it('multi-line highlights', () => {
  54. const source = `
  55. <div attr="some
  56. multiline
  57. attr
  58. ">
  59. </div>
  60. `.trim()
  61. const attrStart = source.indexOf(`attr=`)
  62. const attrEnd = source.indexOf(`">`) + 1
  63. expect(generateCodeFrame(source, attrStart, attrEnd)).toBe(
  64. `
  65. 1 | <div attr="some
  66. | ^^^^^^^^^^
  67. 2 | multiline
  68. | ^^^^^^^^^^^
  69. 3 | attr
  70. | ^^^^
  71. 4 | ">
  72. | ^
  73. `.trim()
  74. )
  75. })
  76. })