Skip to content

Add reprint ReScript source command #6682

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

Merged
merged 3 commits into from
Mar 22, 2024
Merged

Conversation

zth
Copy link
Collaborator

@zth zth commented Mar 17, 2024

Add simple command for transforming a ReScript file using the provided PPXes, and then print the transformed source back to stdout.

Example command:

~/OSS/rescript-compiler/bsc -ppx 'node_modules/@baransu/graphql_ppx_re/ppx6 -lean-parse' -reprint-source src/SomeReScriptFile.res

Useful when you want to "eject" from PPXes, or otherwise just see what source the PPXes actually produce.

Could for example be used to "eject" from old PPXes that won't support uncurried mode. Ejecting to source can fix a lot of uncurried mode issues given that uncurried is done at the syntax level, which we then get access to thanks to the code produced by the PPX now being actual ReScript code and not an AST transformation.

Could also be used to power an editor integration for seeing the expanded PPX source. And so on.

@zth zth requested a review from cristianoc March 17, 2024 17:12
@mununki
Copy link
Member

mununki commented Mar 18, 2024

Very nice to have! Thanks!

exit 1);
parseResult.parsetree
|> Js_implementation.implementationForReprinting
|> Res_printer.printImplementation ~width:80 ~comments:parseResult.comments
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like width is only specified in 2 places: here and in the playground.
Everywhere else it defaults to 100.

Wondering whether we should even have a width parameter, or have it always be an optional arg which is never called.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that's a good point. I should change it to 100 regardless here or the results might not be 100% correct.

@@ -172,6 +172,20 @@ let after_parsing_impl ppf outputprefix (ast : Parsetree.structure) =
Lam_compile_main.lambda_as_module js_program outputprefix);
process_with_gentype (outputprefix ^ ".cmt"))

let implementationForReprinting structure =
Copy link
Collaborator

@cristianoc cristianoc Mar 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do these functions need to be here because of dependencies?
Asking as since there's a single user, then perhaps they could go directly there.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I see similar code is used in this file, except for init_path, so perhaps they can be refactored.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or perhaps not worth it. Just ignore.

@zth zth force-pushed the add-reprint-source-command branch from 62dca4e to 5d7f38e Compare March 22, 2024 15:27
@zth zth requested a review from cristianoc March 22, 2024 15:27
@zth
Copy link
Collaborator Author

zth commented Mar 22, 2024

@cristianoc I simplified a lot, have another look please.

@zth zth enabled auto-merge (squash) March 22, 2024 15:38
@zth zth merged commit aad058e into 11.0_release Mar 22, 2024
@zth zth deleted the add-reprint-source-command branch March 22, 2024 16:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants