utils.ts 887 B

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