From 0e5f58d10b89da9c76fd969afcf4918b9dff2c40 Mon Sep 17 00:00:00 2001 From: zhiyuanzmj <32807958+zhiyuanzmj@users.noreply.github.com> Date: Sun, 21 Jul 2024 02:24:03 +0800 Subject: [PATCH] feat(language-core): support default prop when using __typeProps (#4602) --- .../lib/codegen/script/component.ts | 2 +- pnpm-lock.yaml | 109 +++++++++--------- test-workspace/package.json | 2 +- test-workspace/tsc/vue3.5/components/main.vue | 18 ++- test-workspace/tsc/vue3/withDefaults/main.vue | 18 +++ 5 files changed, 88 insertions(+), 61 deletions(-) create mode 100644 test-workspace/tsc/vue3/withDefaults/main.vue diff --git a/packages/language-core/lib/codegen/script/component.ts b/packages/language-core/lib/codegen/script/component.ts index 8e76cefc88..db9305102c 100644 --- a/packages/language-core/lib/codegen/script/component.ts +++ b/packages/language-core/lib/codegen/script/component.ts @@ -80,7 +80,7 @@ export function* generatePropsOption( if (options.vueCompilerOptions.target >= 3.5 && ctx.generatedPropsType) { yield `__typeProps: {} as __VLS_PublicProps,${newLine}`; } - else { + if (options.vueCompilerOptions.target < 3.5 || !ctx.generatedPropsType || scriptSetupRanges.props.withDefaults) { const codegens: (() => Generator)[] = []; if (ctx.generatedPropsType) { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 775b2df64b..fe06da4c51 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -311,8 +311,8 @@ importers: specifier: npm:vue@3.3.13 version: vue@3.3.13(typescript@5.5.3) vue3.5: - specifier: npm:vue@alpha - version: vue@3.5.0-alpha.2(typescript@5.5.3) + specifier: npm:vue@3.5.0-alpha.3 + version: vue@3.5.0-alpha.3(typescript@5.5.3) packages: @@ -898,8 +898,8 @@ packages: '@vue/compiler-core@3.4.31': resolution: {integrity: sha512-skOiodXWTV3DxfDhB4rOf3OGalpITLlgCeOwb+Y9GJpfQ8ErigdBUHomBzvG78JoVE8MJoQsb+qhZiHfKeNeEg==} - '@vue/compiler-core@3.5.0-alpha.2': - resolution: {integrity: sha512-BWyn6mNsYytzNUJQdDXP4ftK/AjvD4+5bE7o08xQwsvbYnFwRF80Zi3G8LSe/0AiC68PLqz2iBqZQhdMfSGyww==} + '@vue/compiler-core@3.5.0-alpha.3': + resolution: {integrity: sha512-du7iRe9SE5HJ8XGaVtN4OnVsBuslCOINZUiRYykJjDvJERdjDcJglJ4YTfi0gmG80p6TmzQSXxPuBVT/JlqpuQ==} '@vue/compiler-dom@3.3.13': resolution: {integrity: sha512-EYRDpbLadGtNL0Gph+HoKiYqXLqZ0xSSpR5Dvnu/Ep7ggaCbjRDIus1MMxTS2Qm0koXED4xSlvTZaTnI8cYAsw==} @@ -907,8 +907,8 @@ packages: '@vue/compiler-dom@3.4.31': resolution: {integrity: sha512-wK424WMXsG1IGMyDGyLqB+TbmEBFM78hIsOJ9QwUVLGrcSk0ak6zYty7Pj8ftm7nEtdU/DGQxAXp0/lM/2cEpQ==} - '@vue/compiler-dom@3.5.0-alpha.2': - resolution: {integrity: sha512-2CoonkvK32tvz9n988sE3oXPoBKYtCO0jrjno9C4gemJ7g5q8D0VJyE5AzGYFKM8ZS7bjgp2T67PAmaOOgsu0Q==} + '@vue/compiler-dom@3.5.0-alpha.3': + resolution: {integrity: sha512-SL/lSqZ/LgiFEyix1Z3SqvRvd1hfZqQ0RfaH0wRDLv/2lkqswl4qxnqFBEChoWS0BpMBIMkAmrAha7xIhELKug==} '@vue/compiler-sfc@2.7.16': resolution: {integrity: sha512-KWhJ9k5nXuNtygPU7+t1rX6baZeqOYLEforUPjgNDBnLicfHCoi48H87Q8XyLZOrNNsmhuwKqtpDQWjEFe6Ekg==} @@ -919,8 +919,8 @@ packages: '@vue/compiler-sfc@3.4.31': resolution: {integrity: sha512-einJxqEw8IIJxzmnxmJBuK2usI+lJonl53foq+9etB2HAzlPjAS/wa7r0uUpXw5ByX3/0uswVSrjNb17vJm1kQ==} - '@vue/compiler-sfc@3.5.0-alpha.2': - resolution: {integrity: sha512-tdTA3AseuL2PcT9xSEBr/3mRFiGEb8BMNydcNWgGwjQHUnVS/iJDkD3ob+QDTc4gdXENu0khQbrSb2cSvNXvTA==} + '@vue/compiler-sfc@3.5.0-alpha.3': + resolution: {integrity: sha512-UVs18nFL365hea2P9iOHvQDBB8eZxymhGmEol6YMbwB3DjhOcGyzscVixRin/kNHyPr5dEKjRUKcfxwZ03a+9A==} '@vue/compiler-ssr@3.3.13': resolution: {integrity: sha512-d/P3bCeUGmkJNS1QUZSAvoCIW4fkOKK3l2deE7zrp0ypJEy+En2AcypIkqvcFQOcw3F0zt2VfMvNsA9JmExTaw==} @@ -928,8 +928,8 @@ packages: '@vue/compiler-ssr@3.4.31': resolution: {integrity: sha512-RtefmITAje3fJ8FSg1gwgDhdKhZVntIVbwupdyZDSifZTRMiWxWehAOTCc8/KZDnBOcYQ4/9VWxsTbd3wT0hAA==} - '@vue/compiler-ssr@3.5.0-alpha.2': - resolution: {integrity: sha512-wyjfDj3jHQI8Ad9cUWPml6dpO8cm3bsEdXOfiIsBSpDOKAlbdvBQIOpb9CQE09JLPK3j8hFtAEGPtq1COcL2kw==} + '@vue/compiler-ssr@3.5.0-alpha.3': + resolution: {integrity: sha512-cATOOLUjg9W86ZMT87DPsi3W4BZUZIIrIGZEUrRcC91lWs00PTtXiWxhQF4QGpxKpGLPr9GK3E2z5SeQWlk5Zw==} '@vue/reactivity-transform@3.3.13': resolution: {integrity: sha512-oWnydGH0bBauhXvh5KXUy61xr9gKaMbtsMHk40IK9M4gMuKPJ342tKFarY0eQ6jef8906m35q37wwA8DMZOm5Q==} @@ -940,8 +940,8 @@ packages: '@vue/reactivity@3.4.31': resolution: {integrity: sha512-VGkTani8SOoVkZNds1PfJ/T1SlAIOf8E58PGAhIOUDYPC4GAmFA2u/E14TDAFcf3vVDKunc4QqCe/SHr8xC65Q==} - '@vue/reactivity@3.5.0-alpha.2': - resolution: {integrity: sha512-4UG565XKudWMqijfrIY296XTJLBh3m/nZa+mLhsK5uMq0xBDFsJsrVskAmFlLNTlOd6c5FiktZufB9AB7m6VKw==} + '@vue/reactivity@3.5.0-alpha.3': + resolution: {integrity: sha512-Ju0okU4JHdBsxxu7RR3qOMssg4G11KX5vhmlyOwpH3bssp1fYZWQ9qyz+iadtOVsomc+b3WG0Jdx1KFgrGMHcg==} '@vue/runtime-core@3.3.13': resolution: {integrity: sha512-1TzA5TvGuh2zUwMJgdfvrBABWZ7y8kBwBhm7BXk8rvdx2SsgcGfz2ruv2GzuGZNvL1aKnK8CQMV/jFOrxNQUMA==} @@ -949,8 +949,8 @@ packages: '@vue/runtime-core@3.4.31': resolution: {integrity: sha512-LDkztxeUPazxG/p8c5JDDKPfkCDBkkiNLVNf7XZIUnJ+66GVGkP+TIh34+8LtPisZ+HMWl2zqhIw0xN5MwU1cw==} - '@vue/runtime-core@3.5.0-alpha.2': - resolution: {integrity: sha512-59R12UzEhqEOlItt6yKOoTyQ+Hx2hqFPKmdYPT9VAYmLH+ABngjrvw9KuJT9OsR+f4q/NwmCAAvjXV4gC56/cg==} + '@vue/runtime-core@3.5.0-alpha.3': + resolution: {integrity: sha512-RWo7KGycr0IIj1hjJ6xe5gsTeEMkhsW9wWc+lU4r8t7Tbg90F/wjBOcIXzFKhqI5+o9dExgozRhTYwrchbx/6g==} '@vue/runtime-dom@3.3.13': resolution: {integrity: sha512-JJkpE8R/hJKXqVTgUoODwS5wqKtOsmJPEqmp90PDVGygtJ4C0PtOkcEYXwhiVEmef6xeXcIlrT3Yo5aQ4qkHhQ==} @@ -958,8 +958,8 @@ packages: '@vue/runtime-dom@3.4.31': resolution: {integrity: sha512-2Auws3mB7+lHhTFCg8E9ZWopA6Q6L455EcU7bzcQ4x6Dn4cCPuqj6S2oBZgN2a8vJRS/LSYYxwFFq2Hlx3Fsaw==} - '@vue/runtime-dom@3.5.0-alpha.2': - resolution: {integrity: sha512-3ShrCHo22tPMblLl0K6VXTv6MB95D9dBZH/YyJO+aLcOdEQXrLtVp8sXG6oPIwapBVlNkOZX7lUQbnzpdZgosQ==} + '@vue/runtime-dom@3.5.0-alpha.3': + resolution: {integrity: sha512-IOM5wRRnEPYU2USpLtG1VbTyMkyj9Dk2xGnHKnqtjy/4Wdnp1pXifCTfa37rnFPZFAbKeXRJT74dLMdRIyH9Kw==} '@vue/server-renderer@3.3.13': resolution: {integrity: sha512-vSnN+nuf6iSqTL3Qgx/9A+BT+0Zf/VJOgF5uMZrKjYPs38GMYyAU1coDyBNHauehXDaP+zl73VhwWv0vBRBHcg==} @@ -971,10 +971,10 @@ packages: peerDependencies: vue: 3.4.31 - '@vue/server-renderer@3.5.0-alpha.2': - resolution: {integrity: sha512-I9Mq56+B3XUhcKSEr0T9xTqOy04ORTrFANazPR871ITXAHO5viQXrdqOgh2nvBrz6L5c8vpDZgKDhKn+H7G9jg==} + '@vue/server-renderer@3.5.0-alpha.3': + resolution: {integrity: sha512-w2ZOu7Q64BSotuyKReFEq5p/G0U1txTVgOsKq6+bTo3kPcUBYZV9yVCaePVf9kcU5tqjZoje5UBzNExe5DzZ3A==} peerDependencies: - vue: 3.5.0-alpha.2 + vue: 3.5.0-alpha.3 '@vue/shared@3.3.13': resolution: {integrity: sha512-/zYUwiHD8j7gKx2argXEMCUXVST6q/21DFU0sTfNX0URJroCe3b1UF6vLJ3lQDfLNIiiRl2ONp7Nh5UVWS6QnA==} @@ -982,8 +982,8 @@ packages: '@vue/shared@3.4.31': resolution: {integrity: sha512-Yp3wtJk//8cO4NItOPpi3QkLExAr/aLBGZMmTtW9WpdwBCJpRM6zj9WgWktXAl8IDIozwNMByT45JP3tO3ACWA==} - '@vue/shared@3.5.0-alpha.2': - resolution: {integrity: sha512-PMOD/qZfwNGE1xpz92Q7YtcGzz2JtvULSb4HIvv3N0idMAlIKmnzkIvsB0EhosPnJWe5O+xDe+DKmr6fIKu7HQ==} + '@vue/shared@3.5.0-alpha.3': + resolution: {integrity: sha512-TOd0zMchrmWoSWNddkx30wHTVRBkzemhgmbXMBUURv6HuyuddlVyqCMLDmYiklEurers3ILRp+e3b5QN838+kg==} JSONStream@1.3.5: resolution: {integrity: sha512-E+iruNOY8VV9s4JEbe1aNEm6MiszPRr/UfcHMz0TQh1BXSxHK+ASV1R6W4HpjBhSeS+54PIsAMCBmwD06LLsqQ==} @@ -3029,8 +3029,8 @@ packages: typescript: optional: true - vue@3.5.0-alpha.2: - resolution: {integrity: sha512-dyhWA95ko++OlThqFNptBXiVFihKPfZ2487FKSYiB1X7LK1DX6AOjEfxbJdzupoNSOuHDVJIzOvcrBeyChX4vA==} + vue@3.5.0-alpha.3: + resolution: {integrity: sha512-VrCHKUgxsiGMO1SCSrS9yHLwJYAwRrbKqrHdd32ruAWnMC5GVnbrjcrcMuvw5RnrfUc+9y7pBXpVbiO6+WsyMQ==} peerDependencies: typescript: '*' peerDependenciesMeta: @@ -3822,10 +3822,10 @@ snapshots: estree-walker: 2.0.2 source-map-js: 1.2.0 - '@vue/compiler-core@3.5.0-alpha.2': + '@vue/compiler-core@3.5.0-alpha.3': dependencies: '@babel/parser': 7.24.7 - '@vue/shared': 3.5.0-alpha.2 + '@vue/shared': 3.5.0-alpha.3 entities: 4.5.0 estree-walker: 2.0.2 source-map-js: 1.2.0 @@ -3840,10 +3840,10 @@ snapshots: '@vue/compiler-core': 3.4.31 '@vue/shared': 3.4.31 - '@vue/compiler-dom@3.5.0-alpha.2': + '@vue/compiler-dom@3.5.0-alpha.3': dependencies: - '@vue/compiler-core': 3.5.0-alpha.2 - '@vue/shared': 3.5.0-alpha.2 + '@vue/compiler-core': 3.5.0-alpha.3 + '@vue/shared': 3.5.0-alpha.3 '@vue/compiler-sfc@2.7.16': dependencies: @@ -3878,13 +3878,13 @@ snapshots: postcss: 8.4.39 source-map-js: 1.2.0 - '@vue/compiler-sfc@3.5.0-alpha.2': + '@vue/compiler-sfc@3.5.0-alpha.3': dependencies: '@babel/parser': 7.24.7 - '@vue/compiler-core': 3.5.0-alpha.2 - '@vue/compiler-dom': 3.5.0-alpha.2 - '@vue/compiler-ssr': 3.5.0-alpha.2 - '@vue/shared': 3.5.0-alpha.2 + '@vue/compiler-core': 3.5.0-alpha.3 + '@vue/compiler-dom': 3.5.0-alpha.3 + '@vue/compiler-ssr': 3.5.0-alpha.3 + '@vue/shared': 3.5.0-alpha.3 estree-walker: 2.0.2 magic-string: 0.30.10 postcss: 8.4.39 @@ -3900,10 +3900,10 @@ snapshots: '@vue/compiler-dom': 3.4.31 '@vue/shared': 3.4.31 - '@vue/compiler-ssr@3.5.0-alpha.2': + '@vue/compiler-ssr@3.5.0-alpha.3': dependencies: - '@vue/compiler-dom': 3.5.0-alpha.2 - '@vue/shared': 3.5.0-alpha.2 + '@vue/compiler-dom': 3.5.0-alpha.3 + '@vue/shared': 3.5.0-alpha.3 '@vue/reactivity-transform@3.3.13': dependencies: @@ -3921,9 +3921,9 @@ snapshots: dependencies: '@vue/shared': 3.4.31 - '@vue/reactivity@3.5.0-alpha.2': + '@vue/reactivity@3.5.0-alpha.3': dependencies: - '@vue/shared': 3.5.0-alpha.2 + '@vue/shared': 3.5.0-alpha.3 '@vue/runtime-core@3.3.13': dependencies: @@ -3935,10 +3935,10 @@ snapshots: '@vue/reactivity': 3.4.31 '@vue/shared': 3.4.31 - '@vue/runtime-core@3.5.0-alpha.2': + '@vue/runtime-core@3.5.0-alpha.3': dependencies: - '@vue/reactivity': 3.5.0-alpha.2 - '@vue/shared': 3.5.0-alpha.2 + '@vue/reactivity': 3.5.0-alpha.3 + '@vue/shared': 3.5.0-alpha.3 '@vue/runtime-dom@3.3.13': dependencies: @@ -3953,10 +3953,11 @@ snapshots: '@vue/shared': 3.4.31 csstype: 3.1.3 - '@vue/runtime-dom@3.5.0-alpha.2': + '@vue/runtime-dom@3.5.0-alpha.3': dependencies: - '@vue/runtime-core': 3.5.0-alpha.2 - '@vue/shared': 3.5.0-alpha.2 + '@vue/reactivity': 3.5.0-alpha.3 + '@vue/runtime-core': 3.5.0-alpha.3 + '@vue/shared': 3.5.0-alpha.3 csstype: 3.1.3 '@vue/server-renderer@3.3.13(vue@3.4.31(typescript@5.5.3))': @@ -3971,17 +3972,17 @@ snapshots: '@vue/shared': 3.4.31 vue: 3.4.31(typescript@5.5.3) - '@vue/server-renderer@3.5.0-alpha.2(vue@3.4.31(typescript@5.5.3))': + '@vue/server-renderer@3.5.0-alpha.3(vue@3.4.31(typescript@5.5.3))': dependencies: - '@vue/compiler-ssr': 3.5.0-alpha.2 - '@vue/shared': 3.5.0-alpha.2 + '@vue/compiler-ssr': 3.5.0-alpha.3 + '@vue/shared': 3.5.0-alpha.3 vue: 3.4.31(typescript@5.5.3) '@vue/shared@3.3.13': {} '@vue/shared@3.4.31': {} - '@vue/shared@3.5.0-alpha.2': {} + '@vue/shared@3.5.0-alpha.3': {} JSONStream@1.3.5: dependencies: @@ -6127,13 +6128,13 @@ snapshots: optionalDependencies: typescript: 5.5.3 - vue@3.5.0-alpha.2(typescript@5.5.3): + vue@3.5.0-alpha.3(typescript@5.5.3): dependencies: - '@vue/compiler-dom': 3.5.0-alpha.2 - '@vue/compiler-sfc': 3.5.0-alpha.2 - '@vue/runtime-dom': 3.5.0-alpha.2 - '@vue/server-renderer': 3.5.0-alpha.2(vue@3.4.31(typescript@5.5.3)) - '@vue/shared': 3.5.0-alpha.2 + '@vue/compiler-dom': 3.5.0-alpha.3 + '@vue/compiler-sfc': 3.5.0-alpha.3 + '@vue/runtime-dom': 3.5.0-alpha.3 + '@vue/server-renderer': 3.5.0-alpha.3(vue@3.4.31(typescript@5.5.3)) + '@vue/shared': 3.5.0-alpha.3 optionalDependencies: typescript: 5.5.3 diff --git a/test-workspace/package.json b/test-workspace/package.json index 3ac1eb906a..6508a4ac06 100644 --- a/test-workspace/package.json +++ b/test-workspace/package.json @@ -6,6 +6,6 @@ "vue-component-type-helpers": "2.0.26", "vue2": "npm:vue@2.7.16", "vue3.3": "npm:vue@3.3.13", - "vue3.5": "npm:vue@alpha" + "vue3.5": "npm:vue@3.5.0-alpha.3" } } diff --git a/test-workspace/tsc/vue3.5/components/main.vue b/test-workspace/tsc/vue3.5/components/main.vue index ab5fdd188e..4bc49803d7 100644 --- a/test-workspace/tsc/vue3.5/components/main.vue +++ b/test-workspace/tsc/vue3.5/components/main.vue @@ -35,11 +35,11 @@ const ScriptSetupExposeExact = defineComponent({ const ScriptSetupTypeOnlyExact = defineComponent({ __typeProps: {} as { foo: string, - bar?: number + bar?: number; }, __typeEmits: {} as { - (e: 'change', id: number): void - (e: 'update', value: string): void + (e: 'change', id: number): void; + (e: 'update', value: string): void; }, setup() { return {}; @@ -48,8 +48,16 @@ const ScriptSetupTypeOnlyExact = defineComponent({ // https://vuejs.org/api/sfc-script-setup.html#default-props-values-when-using-type-declaration const ScriptSetupDefaultPropsExact = defineComponent({ __typeProps: {} as { - msg?: string - labels?: string[] + msg?: string; + labels?: string[]; + }, + props: { + msg: { + default: 'hello' + }, + labels: { + default: () => ['one', 'two'] + } }, setup() { return {}; diff --git a/test-workspace/tsc/vue3/withDefaults/main.vue b/test-workspace/tsc/vue3/withDefaults/main.vue new file mode 100644 index 0000000000..35c5183537 --- /dev/null +++ b/test-workspace/tsc/vue3/withDefaults/main.vue @@ -0,0 +1,18 @@ + + +