parse.spec.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 keep template nodes with no content', () => {
  101. const { descriptor } = parse(`<template/>`)
  102. expect(descriptor.template).toBeTruthy()
  103. expect(descriptor.template!.content).toBeFalsy()
  104. })
  105. test('should ignore other nodes with no content', () => {
  106. expect(parse(`<script/>`).descriptor.script).toBe(null)
  107. expect(parse(`<style/>`).descriptor.styles.length).toBe(0)
  108. expect(parse(`<custom/>`).descriptor.customBlocks.length).toBe(0)
  109. })
  110. test('handle empty nodes with src attribute', () => {
  111. const { descriptor } = parse(`<script src="com"/>`)
  112. expect(descriptor.script).toBeTruthy()
  113. expect(descriptor.script!.content).toBeFalsy()
  114. expect(descriptor.script!.attrs['src']).toBe('com')
  115. })
  116. test('nested templates', () => {
  117. const content = `
  118. <template v-if="ok">ok</template>
  119. <div><div></div></div>
  120. `
  121. const { descriptor } = parse(`<template>${content}</template>`)
  122. expect(descriptor.template!.content).toBe(content)
  123. })
  124. // #1120
  125. test('alternative template lang should be treated as plain text', () => {
  126. const content = `p(v-if="1 < 2") test`
  127. const { descriptor, errors } = parse(
  128. `<template lang="pug">` + content + `</template>`
  129. )
  130. expect(errors.length).toBe(0)
  131. expect(descriptor.template!.content).toBe(content)
  132. })
  133. //#2566
  134. test('div lang should not be treated as plain text', () => {
  135. const { errors } = parse(`
  136. <template lang="pug">
  137. <div lang="">
  138. <div></div>
  139. </div>
  140. </template>
  141. `)
  142. expect(errors.length).toBe(0)
  143. })
  144. test('error tolerance', () => {
  145. const { errors } = parse(`<template>`)
  146. expect(errors.length).toBe(1)
  147. })
  148. test('should parse as DOM by default', () => {
  149. const { errors } = parse(`<template><input></template>`)
  150. expect(errors.length).toBe(0)
  151. })
  152. test('custom compiler', () => {
  153. const { errors } = parse(`<template><input></template>`, {
  154. compiler: {
  155. parse: baseParse,
  156. compile: baseCompile
  157. }
  158. })
  159. expect(errors.length).toBe(1)
  160. })
  161. test('treat custom blocks as raw text', () => {
  162. const { errors, descriptor } = parse(`<foo> <-& </foo>`)
  163. expect(errors.length).toBe(0)
  164. expect(descriptor.customBlocks[0].content).toBe(` <-& `)
  165. })
  166. describe('warnings', () => {
  167. function assertWarning(errors: Error[], msg: string) {
  168. expect(errors.some(e => e.message.match(msg))).toBe(true)
  169. }
  170. test('should only allow single template element', () => {
  171. assertWarning(
  172. parse(`<template><div/></template><template><div/></template>`).errors,
  173. `Single file component can contain only one <template> element`
  174. )
  175. })
  176. test('should only allow single script element', () => {
  177. assertWarning(
  178. parse(`<script>console.log(1)</script><script>console.log(1)</script>`)
  179. .errors,
  180. `Single file component can contain only one <script> element`
  181. )
  182. })
  183. test('should only allow single script setup element', () => {
  184. assertWarning(
  185. parse(
  186. `<script setup>console.log(1)</script><script setup>console.log(1)</script>`
  187. ).errors,
  188. `Single file component can contain only one <script setup> element`
  189. )
  190. })
  191. test('should not warn script & script setup', () => {
  192. expect(
  193. parse(
  194. `<script setup>console.log(1)</script><script>console.log(1)</script>`
  195. ).errors.length
  196. ).toBe(0)
  197. })
  198. })
  199. })