-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add editor integrations to language integrations (#3864)
- Loading branch information
1 parent
d2f6834
commit f9ed77b
Showing
8 changed files
with
131 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@astrojs/svelte': patch | ||
'@astrojs/vue': patch | ||
--- | ||
|
||
Add entrypoints for editor support for Vue and Svelte (destined to be used by our language server) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { svelte2tsx } from 'svelte2tsx'; | ||
|
||
export function toTSX(code: string, className: string): string { | ||
let result = ` | ||
let ${className}__AstroComponent_: Error | ||
export default ${className}__AstroComponent_ | ||
`; | ||
|
||
try { | ||
let tsx = svelte2tsx(code).code; | ||
tsx = 'let Props = render().props;\n' + tsx; | ||
|
||
// Replace Svelte's class export with a function export | ||
result = tsx.replace( | ||
/^export default[\S\s]*/gm, | ||
`export default function ${className}__AstroComponent_(_props: typeof Props): any {}` | ||
); | ||
} catch (e: any) { | ||
return result; | ||
} | ||
|
||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters