Skip to content

Commit

Permalink
fix(volar): compatible with volar@1.8.14 (#520)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhiyuanzmj committed Oct 10, 2023
1 parent 027b1c7 commit bd04828
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/slow-rabbits-yell.md
@@ -0,0 +1,5 @@
---
'@vue-macros/volar': patch
---

compatible with volar@1.8.14
13 changes: 5 additions & 8 deletions packages/volar/src/common.ts
Expand Up @@ -2,7 +2,7 @@ import {
type Segment,
type Sfc,
type VueCompilerOptions,
replace,
replaceAll,
} from '@vue/language-core'
import { type FileRangeCapabilities } from '@volar/language-core'
import { type VolarOptions } from '..'
Expand All @@ -16,9 +16,9 @@ export function addProps(
decl: Segment<FileRangeCapabilities>[],
vueLibName: string
) {
replace(
replaceAll(
content,
/setup\(\) {/,
/setup\(\) {/g,
'props: ({} as ',
...decl,
'),\n',
Expand All @@ -35,12 +35,9 @@ export function addEmits(
content: Segment<FileRangeCapabilities>[],
decl: Segment<FileRangeCapabilities>[]
) {
const idx = content.indexOf('setup() {\n')
if (idx === -1) return false

replace(
replaceAll(
content,
/setup\(\) {/,
/setup\(\) {/g,
'emits: ({} as ',
...decl,
'),\n',
Expand Down
23 changes: 15 additions & 8 deletions packages/volar/src/define-models.ts
Expand Up @@ -54,20 +54,27 @@ function transformDefineModels({
)

function mergeProps() {
const idx = codes.indexOf('__VLS_TypePropsToRuntimeProps<')
if (idx === -1) return false
const indexes = codes.reduce((res: number[], code, index) => {
if (code === '__VLS_TypePropsToRuntimeProps<') res.unshift(index)
return res
}, [])
if (indexes.length === 0) return false

codes.splice(idx + 2, 0, ' & __VLS_ModelToProps<', seg, '>')
for (const idx of indexes)
codes.splice(idx + 2, 0, ' & __VLS_ModelToProps<', seg, '>')
return true
}

function mergeEmits() {
const idx = codes.indexOf(
'emits: ({} as __VLS_UnionToIntersection<__VLS_ConstructorOverloads<'
)
if (idx === -1) return false
const indexes = codes.reduce((res: number[], code, index) => {
if (code === 'emits: ({} as __VLS_NormalizeEmits<typeof ')
res.unshift(index)
return res
}, [])
if (indexes.length === 0) return false

codes.splice(idx + 2, 1, '>> & __VLS_ModelToEmits<', seg, '>),\n')
for (const idx of indexes)
codes.splice(idx + 2, 1, ' & __VLS_ModelToEmits<', seg, '>>),\n')
return true
}
}
Expand Down
6 changes: 3 additions & 3 deletions packages/volar/src/export-expose.ts
Expand Up @@ -5,7 +5,7 @@ import {
type Sfc,
type VueEmbeddedFile,
type VueLanguagePlugin,
replace,
replaceAll,
replaceSourceRange,
} from '@vue/language-core'
import { createFilter } from '@rollup/pluginutils'
Expand Down Expand Up @@ -62,9 +62,9 @@ function transform({
',\n',
])

replace(
replaceAll(
file.content,
'return {\n',
/return {\n/g,
'return {\n...{ ',
...exposedStrings,
' },\n'
Expand Down

0 comments on commit bd04828

Please sign in to comment.