-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
fix(vue): vue regular script block exports not being recognized inside editor #8998
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
d16a16d
a0d1d9b
a491105
df12478
ab1b88a
b450334
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,23 +20,27 @@ export function toTSX(code: string, className: string): string { | |
|
|
||
| // Vue supports 2 type of script blocks: setup and non-setup | ||
| const regularScriptBlockContent = parsedResult.descriptor.script?.content ?? ''; | ||
| const { scriptSetup } = parsedResult.descriptor; | ||
|
|
||
| if (parsedResult.descriptor.scriptSetup) { | ||
| const definePropsType = | ||
| parsedResult.descriptor.scriptSetup.content.match(/defineProps<([\s\S]+)>/m); | ||
| if (scriptSetup) { | ||
| const definePropsType = scriptSetup.content.match(/defineProps<([\S\s]+?)>\s?\(\)/m); | ||
| const propsGeneric = scriptSetup.attrs.generic; | ||
| const propsGenericType = propsGeneric ? `<${propsGeneric}>` : ''; | ||
|
|
||
| if (definePropsType) { | ||
| result = ` | ||
| ${regularScriptBlockContent} | ||
| ${parsedResult.descriptor.scriptSetup.content} | ||
| ${scriptSetup.content} | ||
|
|
||
| export default function ${className}__AstroComponent_(_props: ${definePropsType[1]}): any { | ||
| export default function ${className}__AstroComponent_${propsGenericType}(_props: ${definePropsType[1]}): any { | ||
| <div></div> | ||
| } | ||
| `; | ||
| } else { | ||
| const defineProps = | ||
| parsedResult.descriptor.scriptSetup.content.match(/defineProps\([\s\S]+\)/m); | ||
| // TODO. Find a way to support generics when using defineProps without passing explicit types. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ultimately, this module shouldn't use a regex to get the types, it should use something like https://github.com/vuejs/language-tools/tree/master/packages/component-meta and https://github.com/vuejs/language-tools/tree/master/packages/component-type-helpers which should support everything (or at least, if it doesn't, would be a bug upstream that's not in our hands)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree, I have looked briefly into these packages after I opened this PR and its not so far I haven't found an easy way. component-meta gives narrows prop types and output loses generic information, so component-type-helpers needs a component instance types, to import which requires vue language server I think and still it loses all generic information and narrows types. I am not sure how would that work with virtual file. Current approach is not sustainable and prone to fail, right now astro doesn't account for |
||
| // Right now something like this `defineProps({ prop: { type: Array as PropType<T[]> } })` | ||
| // won't be correctly typed in Astro. | ||
| const defineProps = scriptSetup.content.match(/defineProps\([\s\S]+\)/m); | ||
|
|
||
| if (defineProps) { | ||
| result = ` | ||
|
|
@@ -46,7 +50,7 @@ export function toTSX(code: string, className: string): string { | |
|
|
||
| const Props = ${defineProps[0]} | ||
|
|
||
| export default function ${className}__AstroComponent_(_props: typeof Props): any { | ||
| export default function ${className}__AstroComponent_${propsGenericType}(_props: typeof Props): any { | ||
| <div></div> | ||
| } | ||
| `; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.