codeframe.spec.js 1.7 KB

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