-
Notifications
You must be signed in to change notification settings - Fork 8
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
Future Admin Generator: parse config using ts-morph to support inline functions and direct imports #3297
base: next
Are you sure you want to change the base?
Conversation
@@ -422,14 +421,14 @@ export function ProductForm({ id }: FormProps): React.ReactElement { | |||
name="priceList" | |||
label={<FormattedMessage id="product.priceList" defaultMessage="Price List" />} | |||
variant="horizontal" | |||
maxFileSize={4194304} | |||
maxFileSize={1024 * 1024 * 4} |
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.
here we also can keep the non-evaluated version (good)
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 believe the advantages of this approach outweigh the disadvantages. I'm not sure if devs ever want to code in config files. However, it's confusing that the file is a TS file, but operations won't work...
Maybe we could find a way to use both ts-morph and execute the file? I'm think of mocking/replacing the imports with stubs, like jest.mock
does.
2e53ac9
to
9848727
Compare
this fixes generation of ProductsGrid with typeValues variable
…s correctly everywhere using convertConfigImport helper
05e552b
to
04b25ea
Compare
3835e55
to
81b5033
Compare
…r it in generator code by adding an hardcoded array
a086117
to
651e400
Compare
import { type GQLProductsGridFutureFragment } from "./future/generated/ProductsGrid.generated"; | ||
import { type GQLProductsListManualFragment } from "./ProductsGrid.generated"; | ||
|
||
type Props = GridCellParams<GQLProductsListManualFragment | GQLProductsGridFutureFragment>; |
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.
Why is this change necessary? Was the usage incorrect?
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.
yes, the first param is the row type: https://github.com/mui/mui-x/blob/v7.x/packages/x-data-grid/src/models/params/gridCellParams.ts#L18, the second is the value.
(this was swapped in mui-x)
}); | ||
if ("import" in field.block) { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
imports.push(convertConfigImport(field.block as any)); // TODO: improve typing, generator runtime vs. config mismatch |
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.
When do you plan to do this?
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 don't have an idea yet how this could be solved. We can talk about it today.
| SingleFileFormFieldConfig | ||
| MultiFileFormFieldConfig | ||
) & { | ||
name: keyof T; | ||
label?: string; | ||
required?: boolean; | ||
virtual?: boolean; | ||
validate?: ImportReference; | ||
validate?: (value: unknown) => ReactNode | undefined; |
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.
Do we still support imports here? Might come in handy if we want to reuse the same validation for multiple forms.
Also, you can use the FieldValidator
type here, which includes allValues and meta in the function declaration.
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.
Do we still support imports here?
yes, we do. It's just not visible anymore here in the types.
return tsSource; | ||
} | ||
|
||
const supportedImportPaths = [ |
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 think this is a good solution to the problem 👍🏼
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.
yes, it is pretty simple. But it doesn't support recursions (eg. for fieldsets)
… runtime vs. configtime types mismatch
5fbd19a
to
a4bcf14
Compare
The Problem
Currently the .cometGen.ts config files are evaluated as code. This has one major drawback: we can't parse inline code, as it will be executed, or, if it's a function definition, we don't know if it's inline or imported.
The Proposed Solution
Instead we parse the config file using ts-morph and iterate the resulting AST to get a json structure similar to before, but with the flexibility to parse for imports or inline functions.
Example, inline function (validate)
config with inline function:
generated code:
Example, imported block
Disadvantages
Any other "a bit" advanced definition of config have to be explicitly implemented, as the config is not evaluated anymore. This includes any factories, helpers etc. Problem is that we can't use typescript to forbid them.
For example, the following needs custom (ast "walking") code to be supported:
For example this would not be supported:
Breaking change
The old import is not supported anymore, change:
to:
TODO
<FormattedMessage
in validateTask: https://vivid-planet.atlassian.net/browse/COM-1626