vue3-core/packages/compiler-sfc/__tests__/utils.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

43 lines
932 B
TypeScript
Raw Normal View History

import {
type SFCParseOptions,
2024-09-10 16:46:19 +08:00
type SFCScriptBlock,
type SFCScriptCompileOptions,
compileScript,
parse,
} from '../src'
import { parse as babelParse } from '@babel/parser'
2020-11-18 02:03:47 +08:00
export const mockId = 'xxxxxxxx'
export function compileSFCScript(
src: string,
options?: Partial<SFCScriptCompileOptions>,
parseOptions?: SFCParseOptions,
2024-09-10 16:46:19 +08:00
): SFCScriptBlock {
const { descriptor, errors } = parse(src, parseOptions)
if (errors.length) {
console.warn(errors[0])
}
return compileScript(descriptor, {
...options,
2020-11-18 02:03:47 +08:00
id: mockId,
})
}
2024-09-10 16:46:19 +08:00
export function assertCode(code: string): void {
// parse the generated code to make sure it is valid
try {
babelParse(code, {
sourceType: 'module',
plugins: [
'typescript',
['importAttributes', { deprecatedAssertSyntax: true }],
],
})
} catch (e: any) {
console.log(code)
throw e
}
expect(code).toMatchSnapshot()
}