2022-05-10 09:16:28 +08:00
|
|
|
import {
|
|
|
|
parse,
|
|
|
|
SFCScriptCompileOptions,
|
|
|
|
compileScript,
|
|
|
|
SFCParseOptions
|
|
|
|
} from '../src'
|
2020-11-17 07:27:15 +08:00
|
|
|
import { parse as babelParse } from '@babel/parser'
|
|
|
|
|
2020-11-18 02:03:47 +08:00
|
|
|
export const mockId = 'xxxxxxxx'
|
|
|
|
|
2020-11-17 07:27:15 +08:00
|
|
|
export function compileSFCScript(
|
|
|
|
src: string,
|
2022-05-10 09:16:28 +08:00
|
|
|
options?: Partial<SFCScriptCompileOptions>,
|
|
|
|
parseOptions?: SFCParseOptions
|
2020-11-17 07:27:15 +08:00
|
|
|
) {
|
2022-05-10 09:16:28 +08:00
|
|
|
const { descriptor } = parse(src, parseOptions)
|
2020-11-17 07:27:15 +08:00
|
|
|
return compileScript(descriptor, {
|
|
|
|
...options,
|
2020-11-18 02:03:47 +08:00
|
|
|
id: mockId
|
2020-11-17 07:27:15 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export function assertCode(code: string) {
|
|
|
|
// parse the generated code to make sure it is valid
|
|
|
|
try {
|
|
|
|
babelParse(code, {
|
|
|
|
sourceType: 'module',
|
2021-10-08 07:32:59 +08:00
|
|
|
plugins: ['typescript']
|
2020-11-17 07:27:15 +08:00
|
|
|
})
|
2021-09-02 21:53:57 +08:00
|
|
|
} catch (e: any) {
|
2020-11-17 07:27:15 +08:00
|
|
|
console.log(code)
|
|
|
|
throw e
|
|
|
|
}
|
|
|
|
expect(code).toMatchSnapshot()
|
|
|
|
}
|