Skip to content

Commit 11e0519

Browse files
fix(deps): update volarjs to v2 (major) (#633)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: gaokefei <260480378@qq.com>
1 parent 46ec4c5 commit 11e0519

28 files changed

+1869
-1734
lines changed

docs/guide/getting-started.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ We assume you are already familiar with the basic usages of Vue before you conti
99
- Node.js 16.14.0 or higher.
1010
- Vue >= 2.7 or Vue >= 3.0.
1111
- Some features need Vue >= 3.2.25.
12-
- VSCode installed the [Vue Language Features (Volar)](https://marketplace.visualstudio.com/items?itemName=Vue.volar) **v1.8.27** (the latest v1) extension.
13-
- ❌ Considering that v2 is not yet stable and some issues remain unresolved, plus adapting to 2.x would mean an almost irreversible upgrade for Vue Macros, we've decided not to support 2.x at this time. In other words, we will continue to support v1.
12+
- VSCode installed the [Vue Language Features (Volar)](https://marketplace.visualstudio.com/items?itemName=Vue.volar) **latest** extension.
1413
- ❌ WebStorm is not supported.
1514

1615
## Creating a Vue Macros Project

docs/zh-CN/guide/getting-started.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ Vue Macros 是一个库,用于实现尚未被 Vue 正式实现的提案或想
99
- Node.js 16.14.0 或更高
1010
- Vue >= 2.7 或 Vue >= 3.0
1111
- 某些功能需要 Vue >= 3.2.25
12-
- VSCode 安装了 [Vue Language Features (Volar)](https://marketplace.visualstudio.com/items?itemName=Vue.volar) **v1.8.27**(最新的 v1)扩展
13-
- ❌ 考虑到 2.x 版本尚未稳定,还有一些问题未被解决。并且适配 2.x 对 Vue Macros 几乎是不可逆的升级。所以我们决定目前不兼容 2.x,換而言之,我们依然支持 1.x。
12+
- VSCode 安装了 [Vue Language Features (Volar)](https://marketplace.visualstudio.com/items?itemName=Vue.volar) **最新的**扩展
1413
- ❌ 不支持 WebStorm
1514

1615
## 搭建第一个 Vue Macros 项目

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
"vite": "^5.2.10",
6666
"vitest": "^1.5.2",
6767
"vue": "^3.4.25",
68-
"vue-tsc": "1.8.27",
68+
"vue-tsc": "2.0.16",
6969
"vue2": "npm:vue@^2.7.16",
7070
"webpack": "^5.91.0"
7171
},

packages/nuxt/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
"devDependencies": {
5757
"@nuxt/devtools": "^1.2.0",
5858
"@nuxt/schema": "^3.11.2",
59-
"@vue/language-core": "1.8.27"
59+
"@vue/language-core": "2.0.16"
6060
},
6161
"engines": {
6262
"node": ">=16.14.0"

packages/volar/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"dev": "tsup --watch"
4141
},
4242
"peerDependencies": {
43-
"vue-tsc": "^1.6.0"
43+
"vue-tsc": "^2.0.0"
4444
},
4545
"peerDependenciesMeta": {
4646
"vue-tsc": {
@@ -53,13 +53,13 @@
5353
"@vue-macros/define-props": "workspace:*",
5454
"@vue-macros/short-bind": "workspace:*",
5555
"@vue-macros/short-vmodel": "workspace:*",
56-
"@vue/language-core": "1.8.27",
56+
"@vue/language-core": "2.0.16",
5757
"@vue/shared": "^3.4.25"
5858
},
5959
"devDependencies": {
6060
"@vue/compiler-dom": "^3.4.25",
6161
"typescript": "~5.4.5",
62-
"vue-tsc": "1.8.27"
62+
"vue-tsc": "2.0.16"
6363
},
6464
"engines": {
6565
"node": ">=16.14.0"

packages/volar/src/common.ts

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {
2-
type FileRangeCapabilities,
3-
type Segment,
2+
type Code,
43
type Sfc,
54
type VueCompilerOptions,
65
replaceAll,
@@ -11,11 +10,7 @@ export function getVueLibraryName(vueVersion: number) {
1110
return vueVersion < 2.7 ? '@vue/runtime-dom' : 'vue'
1211
}
1312

14-
export function addProps(
15-
content: Segment<FileRangeCapabilities>[],
16-
decl: Segment<FileRangeCapabilities>[],
17-
vueLibName: string,
18-
) {
13+
export function addProps(content: Code[], decl: Code[]) {
1914
replaceAll(
2015
content,
2116
/setup\(\) {/g,
@@ -24,17 +19,10 @@ export function addProps(
2419
'),\n',
2520
'setup() {',
2621
)
27-
content.push(
28-
`type __VLS_NonUndefinedable<T> = T extends undefined ? never : T;\n`,
29-
`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`,
30-
)
3122
return true
3223
}
3324

34-
export function addEmits(
35-
content: Segment<FileRangeCapabilities>[],
36-
decl: Segment<FileRangeCapabilities>[],
37-
) {
25+
export function addEmits(content: Code[], decl: Code[]) {
3826
replaceAll(
3927
content,
4028
/setup\(\) {/g,
@@ -52,35 +40,56 @@ export function getVolarOptions(
5240
return vueCompilerOptions.vueMacros
5341
}
5442

55-
export function getImportNames(
56-
ts: typeof import('typescript/lib/tsserverlibrary'),
57-
sfc: Sfc,
58-
) {
43+
export function getImportNames(ts: typeof import('typescript'), sfc: Sfc) {
5944
const names: string[] = []
6045
const sourceFile = sfc.scriptSetup!.ast
61-
sourceFile.forEachChild((node) => {
46+
ts.forEachChild(sourceFile, (node) => {
6247
if (
6348
ts.isImportDeclaration(node) &&
64-
node.assertClause?.elements.some(
49+
node.attributes?.elements.some(
6550
(el) =>
66-
el.name.text === 'type' &&
51+
getText(el.name, { ts, sfc, source: 'scriptSetup' }) === 'type' &&
6752
ts.isStringLiteral(el.value) &&
68-
el.value.text === 'macro',
53+
getText(el.value, { ts, sfc, source: 'scriptSetup' }) === 'macro',
6954
)
7055
) {
71-
const name = node.importClause?.name?.text
56+
const name = node.importClause?.name?.escapedText
7257
if (name) names.push(name)
7358

7459
if (node.importClause?.namedBindings) {
7560
const bindings = node.importClause.namedBindings
7661
if (ts.isNamespaceImport(bindings)) {
77-
names.push(bindings.name.text)
62+
names.push(bindings.name.escapedText!)
7863
} else {
79-
for (const el of bindings.elements) names.push(el.name.text)
64+
for (const el of bindings.elements) names.push(el.name.escapedText!)
8065
}
8166
}
8267
}
8368
})
8469

8570
return names
8671
}
72+
73+
interface Options {
74+
sfc: Sfc
75+
ts: typeof import('typescript')
76+
source?: 'script' | 'scriptSetup'
77+
}
78+
79+
export function getStart(
80+
node: import('typescript').Node,
81+
{ ts, sfc, source = 'scriptSetup' }: Options,
82+
) {
83+
return (ts as any).getTokenPosOfNode(node, sfc[source]!.ast)
84+
}
85+
86+
export function getText(node: import('typescript').Node, options: Options) {
87+
const { sfc, source = 'scriptSetup' } = options
88+
return sfc[source]!.content.slice(getStart(node, options), node.end)
89+
}
90+
91+
export function isJsxExpression(
92+
node?: import('typescript').Node,
93+
): node is import('typescript').JsxExpression {
94+
return node?.kind === 294
95+
}

packages/volar/src/define-models.ts

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { DEFINE_MODELS, DEFINE_MODELS_DOLLAR } from '@vue-macros/common'
22
import {
3-
FileKind,
4-
FileRangeCapabilities,
5-
type Segment,
3+
type Code,
64
type Sfc,
75
type VueLanguagePlugin,
6+
allCodeFeatures,
87
} from '@vue/language-core'
98
import {
109
addEmits,
@@ -17,28 +16,18 @@ function transformDefineModels({
1716
codes,
1817
sfc,
1918
typeArg,
20-
vueLibName,
2119
unified,
2220
}: {
23-
codes: Segment<FileRangeCapabilities>[]
21+
codes: Code[]
2422
sfc: Sfc
25-
typeArg: import('typescript/lib/tsserverlibrary').TypeNode
23+
typeArg: import('typescript').TypeNode
2624
vueLibName: string
2725
unified: boolean
2826
}) {
2927
const source = sfc.scriptSetup!.content.slice(typeArg.pos, typeArg.end)
30-
const seg: Segment<FileRangeCapabilities> = [
31-
source,
32-
'scriptSetup',
33-
typeArg!.pos,
34-
FileRangeCapabilities.full,
35-
]
28+
const seg: Code = [source, 'scriptSetup', typeArg!.pos, allCodeFeatures]
3629
mergeProps() ||
37-
addProps(
38-
codes,
39-
['__VLS_TypePropsToRuntimeProps<__VLS_ModelToProps<', seg, '>>'],
40-
vueLibName,
41-
)
30+
addProps(codes, ['__VLS_TypePropsToOption<__VLS_ModelToProps<', seg, '>>'])
4231
mergeEmits() || addEmits(codes, ['__VLS_ModelToEmits<', seg, '>'])
4332

4433
codes.push(
@@ -56,13 +45,19 @@ function transformDefineModels({
5645

5746
function mergeProps() {
5847
const indexes = codes.reduce((res: number[], code, index) => {
59-
if (code === '__VLS_TypePropsToRuntimeProps<') res.unshift(index)
48+
if (code === '__VLS_TypePropsToOption<') res.unshift(index)
6049
return res
6150
}, [])
6251
if (indexes.length === 0) return false
6352

6453
for (const idx of indexes)
65-
codes.splice(idx + 2, 0, ' & __VLS_ModelToProps<', seg, '>')
54+
codes.splice(
55+
idx + 2,
56+
0,
57+
' & __VLS_TypePropsToOption<__VLS_ModelToProps<',
58+
seg,
59+
'>>',
60+
)
6661
return true
6762
}
6863

@@ -80,16 +75,15 @@ function transformDefineModels({
8075
}
8176
}
8277

83-
function getTypeArg(
84-
ts: typeof import('typescript/lib/tsserverlibrary'),
85-
sfc: Sfc,
86-
) {
87-
function getCallArg(node: import('typescript/lib/tsserverlibrary').Node) {
78+
function getTypeArg(ts: typeof import('typescript'), sfc: Sfc) {
79+
function getCallArg(node: import('typescript').Node) {
8880
if (
8981
!(
9082
ts.isCallExpression(node) &&
9183
ts.isIdentifier(node.expression) &&
92-
[DEFINE_MODELS, DEFINE_MODELS_DOLLAR].includes(node.expression.text) &&
84+
[DEFINE_MODELS, DEFINE_MODELS_DOLLAR].includes(
85+
node.expression.escapedText!,
86+
) &&
9387
node.typeArguments?.length === 1
9488
)
9589
)
@@ -98,11 +92,11 @@ function getTypeArg(
9892
}
9993

10094
const sourceFile = sfc.scriptSetup!.ast
101-
return sourceFile.forEachChild((node) => {
95+
return ts.forEachChild(sourceFile, (node) => {
10296
if (ts.isExpressionStatement(node)) {
10397
return getCallArg(node.expression)
10498
} else if (ts.isVariableStatement(node)) {
105-
return node.declarationList.forEachChild((decl) => {
99+
return ts.forEachChild(node.declarationList, (decl) => {
106100
if (!ts.isVariableDeclaration(decl) || !decl.initializer) return
107101
return getCallArg(decl.initializer)
108102
})
@@ -116,10 +110,10 @@ const plugin: VueLanguagePlugin = ({
116110
}) => {
117111
return {
118112
name: 'vue-macros-define-models',
119-
version: 1,
120-
resolveEmbeddedFile(fileName, sfc, embeddedFile) {
113+
version: 2,
114+
resolveEmbeddedCode(fileName, sfc, embeddedFile) {
121115
if (
122-
embeddedFile.kind !== FileKind.TypeScriptHostFile ||
116+
!['ts', 'tsx'].includes(embeddedFile.lang) ||
123117
!sfc.scriptSetup ||
124118
!sfc.scriptSetup.ast
125119
)

packages/volar/src/define-options.ts

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,45 @@
11
import { DEFINE_OPTIONS } from '@vue-macros/common'
22
import {
3-
FileKind,
4-
FileRangeCapabilities,
5-
type Segment,
3+
type Code,
64
type Sfc,
75
type VueLanguagePlugin,
8-
replace,
6+
allCodeFeatures,
7+
replaceAll,
98
} from '@vue/language-core'
109

1110
function transformDefineOptions({
1211
codes,
1312
sfc,
1413
arg,
1514
}: {
16-
codes: Segment<FileRangeCapabilities>[]
15+
codes: Code[]
1716
sfc: Sfc
18-
arg: import('typescript/lib/tsserverlibrary').Node
17+
arg: import('typescript').Node
1918
}) {
2019
const source = sfc.scriptSetup!.content.slice(arg.pos, arg.end)
21-
const seg: Segment<FileRangeCapabilities> = [
22-
source,
23-
'scriptSetup',
24-
arg!.pos,
25-
FileRangeCapabilities.full,
26-
]
27-
replace(codes, /setup\(\) {/, '...', seg, ',\nsetup() {')
20+
const seg: Code = [source, 'scriptSetup', arg!.pos, allCodeFeatures]
21+
replaceAll(codes, /setup\(\) {/g, '...', seg, ',\nsetup() {')
2822
}
2923

30-
function getArg(ts: typeof import('typescript/lib/tsserverlibrary'), sfc: Sfc) {
31-
function getCallArg(node: import('typescript/lib/tsserverlibrary').Node) {
24+
function getArg(ts: typeof import('typescript'), sfc: Sfc) {
25+
function getCallArg(node: import('typescript').Node) {
3226
if (
3327
!(
3428
ts.isCallExpression(node) &&
3529
ts.isIdentifier(node.expression) &&
36-
node.expression.text === DEFINE_OPTIONS
30+
node.expression.escapedText === DEFINE_OPTIONS
3731
)
3832
)
3933
return undefined
4034
return node.arguments[0]
4135
}
4236

4337
const sourceFile = sfc.scriptSetup!.ast
44-
return sourceFile.forEachChild((node) => {
38+
return ts.forEachChild(sourceFile, (node) => {
4539
if (ts.isExpressionStatement(node)) {
4640
return getCallArg(node.expression)
4741
} else if (ts.isVariableStatement(node)) {
48-
return node.declarationList.forEachChild((decl) => {
42+
return ts.forEachChild(node.declarationList, (decl) => {
4943
if (!ts.isVariableDeclaration(decl) || !decl.initializer) return
5044
return getCallArg(decl.initializer)
5145
})
@@ -56,14 +50,9 @@ function getArg(ts: typeof import('typescript/lib/tsserverlibrary'), sfc: Sfc) {
5650
const plugin: VueLanguagePlugin = ({ modules: { typescript: ts } }) => {
5751
return {
5852
name: 'vue-macros-define-options',
59-
version: 1,
60-
resolveEmbeddedFile(fileName, sfc, embeddedFile) {
61-
if (
62-
embeddedFile.kind !== FileKind.TypeScriptHostFile ||
63-
!sfc.scriptSetup ||
64-
!sfc.scriptSetup.ast
65-
)
66-
return
53+
version: 2,
54+
resolveEmbeddedCode(fileName, sfc, embeddedFile) {
55+
if (!sfc.scriptSetup || !sfc.scriptSetup.ast) return
6756

6857
const arg = getArg(ts, sfc)
6958
if (!arg) return

packages/volar/src/define-props-refs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const plugin: VueLanguagePlugin = ({ vueCompilerOptions }) => {
55

66
return {
77
name: 'vue-macros-define-props-refs',
8-
version: 1,
8+
version: 2,
99
}
1010
}
1111

packages/volar/src/define-props.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const plugin: VueLanguagePlugin = ({ vueCompilerOptions }) => {
55

66
return {
77
name: 'vue-macros-define-props',
8-
version: 1,
8+
version: 2,
99
}
1010
}
1111

0 commit comments

Comments
 (0)