utils.ts 932 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import {
  2. type SFCParseOptions,
  3. type SFCScriptBlock,
  4. type SFCScriptCompileOptions,
  5. compileScript,
  6. parse,
  7. } from '../src'
  8. import { parse as babelParse } from '@babel/parser'
  9. export const mockId = 'xxxxxxxx'
  10. export function compileSFCScript(
  11. src: string,
  12. options?: Partial<SFCScriptCompileOptions>,
  13. parseOptions?: SFCParseOptions,
  14. ): SFCScriptBlock {
  15. const { descriptor, errors } = parse(src, parseOptions)
  16. if (errors.length) {
  17. console.warn(errors[0])
  18. }
  19. return compileScript(descriptor, {
  20. ...options,
  21. id: mockId,
  22. })
  23. }
  24. export function assertCode(code: string): void {
  25. // parse the generated code to make sure it is valid
  26. try {
  27. babelParse(code, {
  28. sourceType: 'module',
  29. plugins: [
  30. 'typescript',
  31. ['importAttributes', { deprecatedAssertSyntax: true }],
  32. ],
  33. })
  34. } catch (e: any) {
  35. console.log(code)
  36. throw e
  37. }
  38. expect(code).toMatchSnapshot()
  39. }