parse.spec.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. import { parse } from '../src'
  2. import { baseCompile, createRoot } from '@vue/compiler-core'
  3. import { SourceMapConsumer } from 'source-map-js'
  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 parse correct range for root level self closing tag', () => {
  101. const content = `\n <div/>\n`
  102. const { descriptor } = parse(`<template>${content}</template>`)
  103. expect(descriptor.template).toBeTruthy()
  104. expect(descriptor.template!.content).toBe(content)
  105. expect(descriptor.template!.loc).toMatchObject({
  106. start: { line: 1, column: 11, offset: 10 },
  107. end: {
  108. line: 3,
  109. column: 1,
  110. offset: 10 + content.length
  111. }
  112. })
  113. })
  114. test('should parse correct range for blocks with no content (self closing)', () => {
  115. const { descriptor } = parse(`<template/>`)
  116. expect(descriptor.template).toBeTruthy()
  117. expect(descriptor.template!.content).toBeFalsy()
  118. expect(descriptor.template!.loc).toMatchObject({
  119. start: { line: 1, column: 12, offset: 11 },
  120. end: { line: 1, column: 12, offset: 11 }
  121. })
  122. })
  123. test('should parse correct range for blocks with no content (explicit)', () => {
  124. const { descriptor } = parse(`<template></template>`)
  125. expect(descriptor.template).toBeTruthy()
  126. expect(descriptor.template!.content).toBeFalsy()
  127. expect(descriptor.template!.loc).toMatchObject({
  128. start: { line: 1, column: 11, offset: 10 },
  129. end: { line: 1, column: 11, offset: 10 }
  130. })
  131. })
  132. test('should ignore other nodes with no content', () => {
  133. expect(parse(`<script/>`).descriptor.script).toBe(null)
  134. expect(parse(`<script> \n\t </script>`).descriptor.script).toBe(null)
  135. expect(parse(`<style/>`).descriptor.styles.length).toBe(0)
  136. expect(parse(`<style> \n\t </style>`).descriptor.styles.length).toBe(0)
  137. expect(parse(`<custom/>`).descriptor.customBlocks.length).toBe(0)
  138. expect(
  139. parse(`<custom> \n\t </custom>`).descriptor.customBlocks.length
  140. ).toBe(0)
  141. })
  142. test('handle empty nodes with src attribute', () => {
  143. const { descriptor } = parse(`<script src="com"/>`)
  144. expect(descriptor.script).toBeTruthy()
  145. expect(descriptor.script!.content).toBeFalsy()
  146. expect(descriptor.script!.attrs['src']).toBe('com')
  147. })
  148. test('ignoreEmpty: false', () => {
  149. const { descriptor } = parse(
  150. `<script></script>\n<script setup>\n</script>`,
  151. {
  152. ignoreEmpty: false
  153. }
  154. )
  155. expect(descriptor.script).toBeTruthy()
  156. expect(descriptor.script!.loc).toMatchObject({
  157. start: { line: 1, column: 9, offset: 8 },
  158. end: { line: 1, column: 9, offset: 8 }
  159. })
  160. expect(descriptor.scriptSetup).toBeTruthy()
  161. expect(descriptor.scriptSetup!.loc).toMatchObject({
  162. start: { line: 2, column: 15, offset: 32 },
  163. end: { line: 3, column: 1, offset: 33 }
  164. })
  165. })
  166. test('nested templates', () => {
  167. const content = `
  168. <template v-if="ok">ok</template>
  169. <div><div></div></div>
  170. `
  171. const { descriptor } = parse(`<template>${content}</template>`)
  172. expect(descriptor.template!.content).toBe(content)
  173. })
  174. test('treat empty lang attribute as the html', () => {
  175. const content = `<div><template v-if="ok">ok</template></div>`
  176. const { descriptor, errors } = parse(
  177. `<template lang="">${content}</template>`
  178. )
  179. expect(descriptor.template!.content).toBe(content)
  180. expect(errors.length).toBe(0)
  181. })
  182. // #1120
  183. test('alternative template lang should be treated as plain text', () => {
  184. const content = `p(v-if="1 < 2") test`
  185. const { descriptor, errors } = parse(
  186. `<template lang="pug">` + content + `</template>`
  187. )
  188. expect(errors.length).toBe(0)
  189. expect(descriptor.template!.content).toBe(content)
  190. })
  191. //#2566
  192. test('div lang should not be treated as plain text', () => {
  193. const { errors } = parse(`
  194. <template lang="pug">
  195. <div lang="">
  196. <div></div>
  197. </div>
  198. </template>
  199. `)
  200. expect(errors.length).toBe(0)
  201. })
  202. test('slotted detection', async () => {
  203. expect(parse(`<template>hi</template>`).descriptor.slotted).toBe(false)
  204. expect(
  205. parse(`<template>hi</template><style>h1{color:red;}</style>`).descriptor
  206. .slotted
  207. ).toBe(false)
  208. expect(
  209. parse(
  210. `<template>hi</template><style scoped>:slotted(h1){color:red;}</style>`
  211. ).descriptor.slotted
  212. ).toBe(true)
  213. expect(
  214. parse(
  215. `<template>hi</template><style scoped>::v-slotted(h1){color:red;}</style>`
  216. ).descriptor.slotted
  217. ).toBe(true)
  218. })
  219. test('error tolerance', () => {
  220. const { errors } = parse(`<template>`)
  221. expect(errors.length).toBe(1)
  222. })
  223. test('should parse as DOM by default', () => {
  224. const { errors } = parse(`<template><input></template>`)
  225. expect(errors.length).toBe(0)
  226. })
  227. test('custom compiler', () => {
  228. const { errors } = parse(`<template><input></template>`, {
  229. compiler: {
  230. parse: (_, options) => {
  231. options.onError!(new Error('foo') as any)
  232. return createRoot([])
  233. },
  234. compile: baseCompile
  235. }
  236. })
  237. expect(errors.length).toBe(2)
  238. // error thrown by the custom parse
  239. expect(errors[0].message).toBe('foo')
  240. // error thrown based on the returned root
  241. expect(errors[1].message).toMatch('At least one')
  242. })
  243. test('treat custom blocks as raw text', () => {
  244. const { errors, descriptor } = parse(
  245. `<template><input></template><foo> <-& </foo>`
  246. )
  247. expect(errors.length).toBe(0)
  248. expect(descriptor.customBlocks[0].content).toBe(` <-& `)
  249. })
  250. describe('warnings', () => {
  251. function assertWarning(errors: Error[], msg: string) {
  252. expect(errors.some(e => e.message.match(msg))).toBe(true)
  253. }
  254. test('should only allow single template element', () => {
  255. assertWarning(
  256. parse(`<template><div/></template><template><div/></template>`).errors,
  257. `Single file component can contain only one <template> element`
  258. )
  259. })
  260. test('should only allow single script element', () => {
  261. assertWarning(
  262. parse(`<script>console.log(1)</script><script>console.log(1)</script>`)
  263. .errors,
  264. `Single file component can contain only one <script> element`
  265. )
  266. })
  267. test('should only allow single script setup element', () => {
  268. assertWarning(
  269. parse(
  270. `<script setup>console.log(1)</script><script setup>console.log(1)</script>`
  271. ).errors,
  272. `Single file component can contain only one <script setup> element`
  273. )
  274. })
  275. test('should not warn script & script setup', () => {
  276. expect(
  277. parse(
  278. `<script setup>console.log(1)</script><script>console.log(1)</script>`
  279. ).errors.length
  280. ).toBe(0)
  281. })
  282. // # 6676
  283. test('should throw error if no <template> or <script> is present', () => {
  284. assertWarning(
  285. parse(`import { ref } from 'vue'`).errors,
  286. `At least one <template> or <script> is required in a single file component`
  287. )
  288. })
  289. })
  290. })