-
Notifications
You must be signed in to change notification settings - Fork 52
feat(schema): support DEFAULT_PROPS export for default values #1118
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1516,6 +1516,32 @@ const getReturnFnFunction = async ( | |||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||
| * Searches for `export const DEFAULT_PROPS = { ... }` in the module AST. | ||||||||||||||||||||||||||||||||||||||||||
| * Returns the parsed object value if found, undefined otherwise. | ||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||
| const findExportedDefaultProps = ( | ||||||||||||||||||||||||||||||||||||||||||
| program: ParsedSource, | ||||||||||||||||||||||||||||||||||||||||||
| ): JSONSchema7Type | undefined => { | ||||||||||||||||||||||||||||||||||||||||||
| for (const item of program.program.body) { | ||||||||||||||||||||||||||||||||||||||||||
| if ( | ||||||||||||||||||||||||||||||||||||||||||
| item.type === "ExportDeclaration" && | ||||||||||||||||||||||||||||||||||||||||||
| item.declaration.type === "VariableDeclaration" | ||||||||||||||||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||||||||||||||||
| for (const decl of item.declaration.declarations) { | ||||||||||||||||||||||||||||||||||||||||||
| if ( | ||||||||||||||||||||||||||||||||||||||||||
| decl.id.type === "Identifier" && | ||||||||||||||||||||||||||||||||||||||||||
| decl.id.value === "DEFAULT_PROPS" && | ||||||||||||||||||||||||||||||||||||||||||
| decl.init | ||||||||||||||||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||||||||||||||||
| return generateObject(decl.init); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+1532
to
+1538
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. Validate Right now any initializer is accepted, so Proposed fix if (
decl.id.type === "Identifier" &&
decl.id.value === "DEFAULT_PROPS" &&
decl.init
) {
- return generateObject(decl.init);
+ if (decl.init.type !== "ObjectExpression") {
+ continue;
+ }
+ const parsed = generateObject(decl.init);
+ if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
+ return parsed;
+ }
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| return undefined; | ||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| export const programToBlockRef = async ( | ||||||||||||||||||||||||||||||||||||||||||
| importMapResolver: ImportMapResolver, | ||||||||||||||||||||||||||||||||||||||||||
| mPath: string, | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -1584,7 +1610,9 @@ export const programToBlockRef = async ( | |||||||||||||||||||||||||||||||||||||||||
| references: schemeableReferences, | ||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||
| if (inputSchema.type === "object") { | ||||||||||||||||||||||||||||||||||||||||||
| inputSchema.default = defaultValue; | ||||||||||||||||||||||||||||||||||||||||||
| // Priority: DEFAULT_PROPS export > function parameter default > nothing | ||||||||||||||||||||||||||||||||||||||||||
| const exportedDefaultProps = findExportedDefaultProps(mProgram); | ||||||||||||||||||||||||||||||||||||||||||
|
Contributor
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. P2: Look up Prompt for AI agents
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
| inputSchema.default = exportedDefaultProps ?? defaultValue; | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||||||||||||||||
| ...baseBlockRef, | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.
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.
P2:
DEFAULT_PROPSvalues that contain booleans are serialized asnullbecause this path reuses a parser that does not handleBooleanLiteral.Prompt for AI agents