summaryrefslogtreecommitdiff
path: root/packages/integrations/vue/src
diff options
context:
space:
mode:
authorGravatar Erika <3019731+Princesseuh@users.noreply.github.com> 2022-07-08 17:47:01 -0400
committerGravatar GitHub <noreply@github.com> 2022-07-08 17:47:01 -0400
commitf9ed77bb0d71d1644d524547a24963210f4ecaff (patch)
tree5c4c27daf7213d8e483b8a0eaaca547804542ef1 /packages/integrations/vue/src
parentd2f68345f97eb5b55bc323d633017069398727b2 (diff)
downloadastro-f9ed77bb0d71d1644d524547a24963210f4ecaff.tar.gz
astro-f9ed77bb0d71d1644d524547a24963210f4ecaff.tar.zst
astro-f9ed77bb0d71d1644d524547a24963210f4ecaff.zip
Add editor integrations to language integrations (#3864)
Diffstat (limited to 'packages/integrations/vue/src')
-rw-r--r--packages/integrations/vue/src/editor.cts55
1 files changed, 55 insertions, 0 deletions
diff --git a/packages/integrations/vue/src/editor.cts b/packages/integrations/vue/src/editor.cts
new file mode 100644
index 000000000..29adaa09c
--- /dev/null
+++ b/packages/integrations/vue/src/editor.cts
@@ -0,0 +1,55 @@
+import { parse } from '@vue/compiler-sfc';
+
+export function toTSX(code: string, className: string): string {
+ let result = `export default function ${className}__AstroComponent_(_props: Record<string, any>): any {}`;
+
+ // NOTE: As you can expect, using regexes for this is not exactly the most reliable way of doing things
+ // However, I couldn't figure out a way to do it using Vue's compiler, I tried looking at how Volar does it, but I
+ // didn't really understand everything happening there and it seemed to be pretty Volar-specific. I do believe
+ // someone more knowledgable on Vue's internals could figure it out, but since this solution is good enough for most
+ // Vue components (and it's an improvement over, well, nothing), it's alright, I think
+ try {
+ const parsedResult = parse(code);
+
+ if (parsedResult.errors.length > 0) {
+ return `
+ let ${className}__AstroComponent_: Error
+ export default ${className}__AstroComponent_
+ `;
+ }
+
+ if (parsedResult.descriptor.scriptSetup) {
+ const definePropsType =
+ parsedResult.descriptor.scriptSetup.content.match(/defineProps<([\s\S]+)>/m);
+
+ if (definePropsType) {
+ result = `
+ ${parsedResult.descriptor.scriptSetup.content}
+
+ export default function ${className}__AstroComponent_(_props: ${definePropsType[1]}): any {
+ <div></div>
+ }
+ `;
+ } else {
+ const defineProps =
+ parsedResult.descriptor.scriptSetup.content.match(/defineProps\([\s\S]+\)/m);
+
+ if (defineProps) {
+ result = `
+ import { defineProps } from '@vue/runtime-core';
+
+ const Props = ${defineProps[0]}
+
+ export default function ${className}__AstroComponent_(_props: typeof Props): any {
+ <div></div>
+ }
+ `;
+ }
+ }
+ }
+ } catch (e: any) {
+ return result;
+ }
+
+ return result;
+}