parse.spec.ts 10 KB

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