-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
common.ts
55 lines (50 loc) · 1.33 KB
/
common.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import {
type Segment,
type VueCompilerOptions,
replace,
} from '@volar/vue-language-core'
import { type FileRangeCapabilities } from '@volar/language-core'
import { type VolarOptions } from '..'
export function getVueLibraryName(vueVersion: number) {
return vueVersion < 2.7 ? '@vue/runtime-dom' : 'vue'
}
export function addProps(
content: Segment<FileRangeCapabilities>[],
decl: Segment<FileRangeCapabilities>[],
vueLibName: string
) {
replace(
content,
/setup\(\) {/,
'props: ({} as ',
...decl,
'),\n',
'setup() {'
)
content.push(
`type __VLS_NonUndefinedable<T> = T extends undefined ? never : T;\n`,
`type __VLS_TypePropsToRuntimeProps<T> = { [K in keyof T]-?: {} extends Pick<T, K> ? { type: import('${vueLibName}').PropType<__VLS_NonUndefinedable<T[K]>> } : { type: import('${vueLibName}').PropType<T[K]>, required: true } };\n`
)
return true
}
export function addEmits(
content: Segment<FileRangeCapabilities>[],
decl: Segment<FileRangeCapabilities>[]
) {
const idx = content.indexOf('setup() {\n')
if (idx === -1) return false
replace(
content,
/setup\(\) {/,
'emits: ({} as ',
...decl,
'),\n',
'setup() {'
)
return true
}
export function getVolarOptions(
vueCompilerOptions: VueCompilerOptions
): VolarOptions {
return vueCompilerOptions.vueMacros
}