parse.spec.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import { parse } from '../src'
  2. import { baseParse, baseCompile } from '@vue/compiler-core'
  3. import { SourceMapConsumer } from 'source-map'
  4. describe('compiler:sfc', () => {
  5. describe('source map', () => {
  6. test('style block', () => {
  7. // Padding determines how many blank lines will there be before the style block
  8. const padding = Math.round(Math.random() * 10)
  9. const style = parse(
  10. `${'\n'.repeat(padding)}<style>\n.color {\n color: red;\n }\n</style>\n`
  11. ).descriptor.styles[0]
  12. expect(style.map).not.toBeUndefined()
  13. const consumer = new SourceMapConsumer(style.map!)
  14. consumer.eachMapping(mapping => {
  15. expect(mapping.originalLine - mapping.generatedLine).toBe(padding)
  16. })
  17. })
  18. test('script block', () => {
  19. // Padding determines how many blank lines will there be before the style block
  20. const padding = Math.round(Math.random() * 10)
  21. const script = parse(
  22. `${'\n'.repeat(padding)}<script>\nconsole.log(1)\n }\n</script>\n`
  23. ).descriptor.script
  24. expect(script!.map).not.toBeUndefined()
  25. const consumer = new SourceMapConsumer(script!.map!)
  26. consumer.eachMapping(mapping => {
  27. expect(mapping.originalLine - mapping.generatedLine).toBe(padding)
  28. })
  29. })
  30. test('custom block', () => {
  31. const padding = Math.round(Math.random() * 10)
  32. const custom = parse(
  33. `${'\n'.repeat(padding)}<i18n>\n{\n "greeting": "hello"\n}\n</i18n>\n`
  34. ).descriptor.customBlocks[0]
  35. expect(custom!.map).not.toBeUndefined()
  36. const consumer = new SourceMapConsumer(custom!.map!)
  37. consumer.eachMapping(mapping => {
  38. expect(mapping.originalLine - mapping.generatedLine).toBe(padding)
  39. })
  40. })
  41. })
  42. test('pad content', () => {
  43. const content = `
  44. <template>
  45. <div></div>
  46. </template>
  47. <script>
  48. export default {}
  49. </script>
  50. <style>
  51. h1 { color: red }
  52. </style>
  53. <i18n>
  54. { "greeting": "hello" }
  55. </i18n>
  56. `
  57. const padFalse = parse(content.trim(), { pad: false }).descriptor
  58. expect(padFalse.template!.content).toBe('\n<div></div>\n')
  59. expect(padFalse.script!.content).toBe('\nexport default {}\n')
  60. expect(padFalse.styles[0].content).toBe('\nh1 { color: red }\n')
  61. expect(padFalse.customBlocks[0].content).toBe('\n{ "greeting": "hello" }\n')
  62. const padTrue = parse(content.trim(), { pad: true }).descriptor
  63. expect(padTrue.script!.content).toBe(
  64. Array(3 + 1).join('//\n') + '\nexport default {}\n'
  65. )
  66. expect(padTrue.styles[0].content).toBe(
  67. Array(6 + 1).join('\n') + '\nh1 { color: red }\n'
  68. )
  69. expect(padTrue.customBlocks[0].content).toBe(
  70. Array(9 + 1).join('\n') + '\n{ "greeting": "hello" }\n'
  71. )
  72. const padLine = parse(content.trim(), { pad: 'line' }).descriptor
  73. expect(padLine.script!.content).toBe(
  74. Array(3 + 1).join('//\n') + '\nexport default {}\n'
  75. )
  76. expect(padLine.styles[0].content).toBe(
  77. Array(6 + 1).join('\n') + '\nh1 { color: red }\n'
  78. )
  79. expect(padLine.customBlocks[0].content).toBe(
  80. Array(9 + 1).join('\n') + '\n{ "greeting": "hello" }\n'
  81. )
  82. const padSpace = parse(content.trim(), { pad: 'space' }).descriptor
  83. expect(padSpace.script!.content).toBe(
  84. `<template>\n<div></div>\n</template>\n<script>`.replace(/./g, ' ') +
  85. '\nexport default {}\n'
  86. )
  87. expect(padSpace.styles[0].content).toBe(
  88. `<template>\n<div></div>\n</template>\n<script>\nexport default {}\n</script>\n<style>`.replace(
  89. /./g,
  90. ' '
  91. ) + '\nh1 { color: red }\n'
  92. )
  93. expect(padSpace.customBlocks[0].content).toBe(
  94. `<template>\n<div></div>\n</template>\n<script>\nexport default {}\n</script>\n<style>\nh1 { color: red }\n</style>\n<i18n>`.replace(
  95. /./g,
  96. ' '
  97. ) + '\n{ "greeting": "hello" }\n'
  98. )
  99. })
  100. test('should ignore nodes with no content', () => {
  101. expect(parse(`<template/>`).descriptor.template).toBe(null)
  102. expect(parse(`<script/>`).descriptor.script).toBe(null)
  103. expect(parse(`<style/>`).descriptor.styles.length).toBe(0)
  104. expect(parse(`<custom/>`).descriptor.customBlocks.length).toBe(0)
  105. })
  106. test('handle empty nodes with src attribute', () => {
  107. const { descriptor } = parse(`<script src="com"/>`)
  108. expect(descriptor.script).toBeTruthy()
  109. expect(descriptor.script!.content).toBeFalsy()
  110. expect(descriptor.script!.attrs['src']).toBe('com')
  111. })
  112. test('nested templates', () => {
  113. const content = `
  114. <template v-if="ok">ok</template>
  115. <div><div></div></div>
  116. `
  117. const { descriptor } = parse(`<template>${content}</template>`)
  118. expect(descriptor.template!.content).toBe(content)
  119. })
  120. // #1120
  121. test('alternative template lang should be treated as plain text', () => {
  122. const content = `p(v-if="1 < 2") test`
  123. const { descriptor, errors } = parse(
  124. `<template lang="pug">` + content + `</template>`
  125. )
  126. expect(errors.length).toBe(0)
  127. expect(descriptor.template!.content).toBe(content)
  128. })
  129. test('error tolerance', () => {
  130. const { errors } = parse(`<template>`)
  131. expect(errors.length).toBe(1)
  132. })
  133. test('should parse as DOM by default', () => {
  134. const { errors } = parse(`<template><input></template>`)
  135. expect(errors.length).toBe(0)
  136. })
  137. test('custom compiler', () => {
  138. const { errors } = parse(`<template><input></template>`, {
  139. compiler: {
  140. parse: baseParse,
  141. compile: baseCompile
  142. }
  143. })
  144. expect(errors.length).toBe(1)
  145. })
  146. test('treat custom blocks as raw text', () => {
  147. const { errors, descriptor } = parse(`<foo> <-& </foo>`)
  148. expect(errors.length).toBe(0)
  149. expect(descriptor.customBlocks[0].content).toBe(` <-& `)
  150. })
  151. describe('warnings', () => {
  152. function assertWarning(errors: Error[], msg: string) {
  153. expect(errors.some(e => e.message.match(msg))).toBe(true)
  154. }
  155. test('should only allow single template element', () => {
  156. assertWarning(
  157. parse(`<template><div/></template><template><div/></template>`).errors,
  158. `Single file component can contain only one <template> element`
  159. )
  160. })
  161. test('should only allow single script element', () => {
  162. assertWarning(
  163. parse(`<script>console.log(1)</script><script>console.log(1)</script>`)
  164. .errors,
  165. `Single file component can contain only one <script> element`
  166. )
  167. })
  168. test('should only allow single script setup element', () => {
  169. assertWarning(
  170. parse(
  171. `<script setup>console.log(1)</script><script setup>console.log(1)</script>`
  172. ).errors,
  173. `Single file component can contain only one <script setup> element`
  174. )
  175. })
  176. test('should not warn script & script setup', () => {
  177. expect(
  178. parse(
  179. `<script setup>console.log(1)</script><script>console.log(1)</script>`
  180. ).errors.length
  181. ).toBe(0)
  182. })
  183. })
  184. })