-
-
Notifications
You must be signed in to change notification settings - Fork 428
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
feat(component-meta): extend component meta #5169
base: master
Are you sure you want to change the base?
Conversation
if (!ts.isPropertyAssignment(property)) return | ||
const key = property.name.getText(); | ||
const valueNode = resolveDefaultOptionExpression(property.initializer, ts); | ||
meta[key] = printer?.printNode(ts.EmitHint.Expression, valueNode, ast) ?? valueNode.getText(ast) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@KazariEX I'm wondering if it's a good idea to parse nested objects and arrays by traversing the rest of the AST here. I tried to match the parser for prop default values.
On one side it would be nice for end users to receive plain objects / arrays, but might be a bit heavy on performances.
}); | ||
} | ||
} | ||
return ts.forEachChild(node, traverse) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bit aggressive since it'll walk the entire AST. I'll look into restraining it to the top level of <script setup>
and defineComponent
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if we can parse defineComponentMeta
at language-core
so that we can read the node directly without repeat traversal, since it's not useful for tsc and IDE.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll look into it!
let meta: Record<string, any> = {}; | ||
const snapshot = language.scripts.get(componentPath)?.snapshot!; | ||
const fileText = snapshot.getText(0, snapshot.getLength()); | ||
const ast = ts.createSourceFile('/temp', fileText, ts.ScriptTarget.Latest, true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See:
language-tools/packages/component-meta/lib/base.ts
Lines 334 to 344 in 317b0cc
const vueFile = sourceScript.generated?.root; | |
const vueDefaults = vueFile && exportName === 'default' | |
? (vueFile instanceof vue.VueVirtualCode ? readVueComponentDefaultProps(vueFile, printer, ts) : {}) | |
: {}; | |
const tsDefaults = !vueFile ? readTsComponentDefaultProps( | |
componentPath.slice(componentPath.lastIndexOf('.') + 1), // ts | js | tsx | jsx | |
snapshot.getText(0, snapshot.getLength()), | |
exportName, | |
printer, | |
ts | |
) : {}; |
We can read ast from sourceFile.generated.root._sfc
if it's a .vue
file, or try sharing parsed ast if it's a .ts
file.
Resolves #5168
Introduces a new macro
defineComponentMeta
to extend component meta with arbitrary values.Results in the following meta: