Skip to content

Commit

Permalink
refactor: common
Browse files Browse the repository at this point in the history
  • Loading branch information
zhiyuanzmj committed May 12, 2024
1 parent b27e9e9 commit 3c91f7d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
7 changes: 7 additions & 0 deletions packages/volar/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export const REGEX_DEFINE_COMPONENT =
/(?<=(default|__VLS_internalComponent =|__VLS_fnComponent =) \(await import\(\S+\)\)\.defineComponent\({\n)/g

export function addProps(codes: Code[], decl: Code[], vueLibName: string) {
if (codes.includes('__VLS_TypePropsToOption<')) return

replaceAll(codes, REGEX_DEFINE_COMPONENT, 'props: ({} as ', ...decl, '),\n')
codes.push(
`type __VLS_NonUndefinedable<T> = T extends undefined ? never : T;\n`,
Expand All @@ -19,6 +21,11 @@ export function addProps(codes: Code[], decl: Code[], vueLibName: string) {
}

export function addEmits(codes: Code[], decl: Code[]) {
if (
codes.some((code) => code.includes('emits: ({} as __VLS_NormalizeEmits<'))
)
return

replaceAll(codes, REGEX_DEFINE_COMPONENT, 'emits: ({} as ', ...decl, '),\n')
return true
}
Expand Down
11 changes: 2 additions & 9 deletions packages/volar/src/define-models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,7 @@ function transformDefineModels(options: {
`{\n${propStrings.join(',\n')}} & `,
)

codes.includes('__VLS_TypePropsToOption<') ||
addProps(
codes,
['__VLS_TypePropsToOption<__VLS_PublicProps>'],
vueLibName,
)
addProps(codes, ['__VLS_TypePropsToOption<__VLS_PublicProps>'], vueLibName)
}

if (emitStrings.length) {
Expand All @@ -59,9 +54,7 @@ function transformDefineModels(options: {
` & ReturnType<typeof import('${vueLibName}').defineEmits<{\n${emitStrings.join(',\n')}}>>`,
)

codes.some((code) =>
code.toString().includes('emits: ({} as __VLS_NormalizeEmits<'),
) || addEmits(codes, ['__VLS_NormalizeEmits<typeof __VLS_modelEmitsType>'])
addEmits(codes, ['__VLS_NormalizeEmits<typeof __VLS_modelEmitsType>'])
}
}

Expand Down

0 comments on commit 3c91f7d

Please sign in to comment.