From 2de2ea27239829c8fb7e04032000999d74069c64 Mon Sep 17 00:00:00 2001 From: so1ve Date: Tue, 7 Nov 2023 11:09:38 +0800 Subject: [PATCH] feat: [wip] `defineEmits` support --- src/core/language.ts | 57 ++++++++++---- src/core/printer.ts | 6 +- src/core/transform.ts | 76 +++++++++++++++++-- test/__fixtures__/defineEmits/basic-ts.ts | 4 + test/__fixtures__/defineEmits/basic.vue | 5 ++ .../{ => defineProps}/basic-ts.ts | 0 test/__fixtures__/{ => defineProps}/basic.vue | 0 .../{ => defineProps}/core#8286.vue | 0 .../{ => defineProps}/core#8468-props.ts | 0 .../{ => defineProps}/core#8468.vue | 0 .../import-from-vue-vue.exclude.vue | 0 .../{ => defineProps}/import-from-vue.vue | 0 .../fixtures-compiled.test.ts.snap | 27 ++++++- test/__snapshots__/fixtures.test.ts.snap | 17 ++++- test/fixtures-compiled.test.ts | 2 +- test/fixtures.test.ts | 2 +- 16 files changed, 162 insertions(+), 34 deletions(-) create mode 100644 test/__fixtures__/defineEmits/basic-ts.ts create mode 100644 test/__fixtures__/defineEmits/basic.vue rename test/__fixtures__/{ => defineProps}/basic-ts.ts (100%) rename test/__fixtures__/{ => defineProps}/basic.vue (100%) rename test/__fixtures__/{ => defineProps}/core#8286.vue (100%) rename test/__fixtures__/{ => defineProps}/core#8468-props.ts (100%) rename test/__fixtures__/{ => defineProps}/core#8468.vue (100%) rename test/__fixtures__/{ => defineProps}/import-from-vue-vue.exclude.vue (100%) rename test/__fixtures__/{ => defineProps}/import-from-vue.vue (100%) diff --git a/src/core/language.ts b/src/core/language.ts index b5f3ebc..d2febdb 100644 --- a/src/core/language.ts +++ b/src/core/language.ts @@ -114,10 +114,19 @@ function baseCreateLanguageWorker( const program = tsLs.getProgram()!; const typeChecker = program.getTypeChecker(); - function findScriptRangesNode( + function findScriptRangesAndNode( filepath: string, filter: (ranges: vue.ScriptSetupRanges) => vue.TextRange | undefined, - ): { node: ts.Node; range: vue.TextRange } | undefined { + ): + | { + virtualFileNode: ts.Node; + scriptNode: ts.Node; + setupRange: vue.TextRange; + scriptSetupAst: ts.SourceFile; + virtualFileAst: ts.SourceFile; + offset: number; + } + | undefined { filepath = normalizePath(filepath); const sourceFile = core.virtualFiles.getSource(filepath)?.root; if (!(sourceFile instanceof vue.VueFile)) { @@ -128,7 +137,7 @@ function baseCreateLanguageWorker( } const { ast: scriptSetupAst, - startTagEnd, + startTagEnd: offset, lang, } = sourceFile.sfc.scriptSetup; if (lang !== "ts" && lang !== "tsx") { @@ -137,8 +146,8 @@ function baseCreateLanguageWorker( // We get volar's generated virtual ts file because `scriptSetupAst` // is the ast of ` diff --git a/test/__fixtures__/basic-ts.ts b/test/__fixtures__/defineProps/basic-ts.ts similarity index 100% rename from test/__fixtures__/basic-ts.ts rename to test/__fixtures__/defineProps/basic-ts.ts diff --git a/test/__fixtures__/basic.vue b/test/__fixtures__/defineProps/basic.vue similarity index 100% rename from test/__fixtures__/basic.vue rename to test/__fixtures__/defineProps/basic.vue diff --git a/test/__fixtures__/core#8286.vue b/test/__fixtures__/defineProps/core#8286.vue similarity index 100% rename from test/__fixtures__/core#8286.vue rename to test/__fixtures__/defineProps/core#8286.vue diff --git a/test/__fixtures__/core#8468-props.ts b/test/__fixtures__/defineProps/core#8468-props.ts similarity index 100% rename from test/__fixtures__/core#8468-props.ts rename to test/__fixtures__/defineProps/core#8468-props.ts diff --git a/test/__fixtures__/core#8468.vue b/test/__fixtures__/defineProps/core#8468.vue similarity index 100% rename from test/__fixtures__/core#8468.vue rename to test/__fixtures__/defineProps/core#8468.vue diff --git a/test/__fixtures__/import-from-vue-vue.exclude.vue b/test/__fixtures__/defineProps/import-from-vue-vue.exclude.vue similarity index 100% rename from test/__fixtures__/import-from-vue-vue.exclude.vue rename to test/__fixtures__/defineProps/import-from-vue-vue.exclude.vue diff --git a/test/__fixtures__/import-from-vue.vue b/test/__fixtures__/defineProps/import-from-vue.vue similarity index 100% rename from test/__fixtures__/import-from-vue.vue rename to test/__fixtures__/defineProps/import-from-vue.vue diff --git a/test/__snapshots__/fixtures-compiled.test.ts.snap b/test/__snapshots__/fixtures-compiled.test.ts.snap index a1581b9..fc02985 100644 --- a/test/__snapshots__/fixtures-compiled.test.ts.snap +++ b/test/__snapshots__/fixtures-compiled.test.ts.snap @@ -1,6 +1,25 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html -exports[`fixtures compiled > __fixtures__/basic.vue 1`] = ` +exports[`fixtures compiled > __fixtures__/defineEmits/basic.vue 1`] = ` +"import { defineComponent } from 'vue'; +import _export_sfc from '[NULL]/plugin-vue/export-helper'; + +\\"use strict\\"; +var _sfc_main = /* @__PURE__ */ defineComponent({ + __name: \\"basic\\", + setup(__props) { + return () => { + }; + } +}); + +var basic = /* @__PURE__ */ _export_sfc(_sfc_main, [__FILE__]); + +export { basic as default }; +" +`; + +exports[`fixtures compiled > __fixtures__/defineProps/basic.vue 1`] = ` "import { defineComponent } from 'vue'; import _export_sfc from '[NULL]/plugin-vue/export-helper'; @@ -36,7 +55,7 @@ export { basic as default }; " `; -exports[`fixtures compiled > __fixtures__/core#8286.vue 1`] = ` +exports[`fixtures compiled > __fixtures__/defineProps/core#8286.vue 1`] = ` "import { defineComponent } from 'vue'; import _export_sfc from '[NULL]/plugin-vue/export-helper'; @@ -239,7 +258,7 @@ export { core_8286 as default }; " `; -exports[`fixtures compiled > __fixtures__/core#8468.vue 1`] = ` +exports[`fixtures compiled > __fixtures__/defineProps/core#8468.vue 1`] = ` "import { defineComponent } from 'vue'; import _export_sfc from '[NULL]/plugin-vue/export-helper'; @@ -262,7 +281,7 @@ export { core_8468 as default }; " `; -exports[`fixtures compiled > __fixtures__/import-from-vue.vue 1`] = ` +exports[`fixtures compiled > __fixtures__/defineProps/import-from-vue.vue 1`] = ` "import { defineComponent } from 'vue'; import _export_sfc from '[NULL]/plugin-vue/export-helper'; diff --git a/test/__snapshots__/fixtures.test.ts.snap b/test/__snapshots__/fixtures.test.ts.snap index 7db9bbe..b429b42 100644 --- a/test/__snapshots__/fixtures.test.ts.snap +++ b/test/__snapshots__/fixtures.test.ts.snap @@ -1,6 +1,15 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html -exports[`fixtures > __fixtures__/basic.vue 1`] = ` +exports[`fixtures > __fixtures__/defineEmits/basic.vue 1`] = ` +" +" +`; + +exports[`fixtures > __fixtures__/defineProps/basic.vue 1`] = ` "