Skip to content

Preserve @as decorator on record fields when creating interface #7779

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 4 commits into from
Aug 21, 2025

Conversation

mediremi
Copy link
Contributor

Closes #7611

@mediremi mediremi marked this pull request as ready for review August 21, 2025 08:58
@zth zth requested review from cristianoc and Copilot August 21, 2025 09:00
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes an issue where the @as decorator on record fields was not being preserved when creating interface files. The change ensures that type declarations with attributes are copied verbatim to maintain their decorators.

  • Added logic to detect and preserve attributes (like @as) on type declarations
  • Enhanced the AttributesUtils module with an isEmpty function
  • Updated the interface creation process to copy type declarations with attributes verbatim

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
analysis/src/CreateInterface.ml Core logic to detect attributes and preserve type declarations verbatim
tests/analysis_tests/tests/src/CreateInterface.res Test case with external function and record type using @as decorator
tests/analysis_tests/tests/src/expected/CreateInterface.res.txt Expected output showing preserved @as decorators
CHANGELOG.md Documentation of the bug fix

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

sigItemToString
(Printtyp.tree_of_type_declaration id typeDecl resStatus)
in
Buffer.add_string buf (indent ^ newItemStr ^ "\n"));
Copy link
Preview

Copilot AI Aug 21, 2025

Choose a reason for hiding this comment

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

[nitpick] The conditional logic should be inverted to reduce nesting. Consider using early return pattern: if AttributesUtils.isEmpty attributes then (* normal processing *) else (* verbatim copy *).

Copilot uses AI. Check for mistakes.


(if not (AttributesUtils.isEmpty attributes) then
(* Copy the type declaration verbatim to preserve attributes *)
Buffer.add_string buf ((lines |> String.concat "\n") ^ "\n")
Copy link
Preview

Copilot AI Aug 21, 2025

Choose a reason for hiding this comment

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

String concatenation with ^ creates intermediate strings. Consider using Buffer.add_string for each line separately or String.concat "\n" without the additional concatenation.

Suggested change
Buffer.add_string buf ((lines |> String.concat "\n") ^ "\n")
Array.iter (fun line -> Buffer.add_string buf line; Buffer.add_char buf '\n') lines

Copilot uses AI. Check for mistakes.

Copy link

pkg-pr-new bot commented Aug 21, 2025

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript-lang/rescript@7779

@rescript/darwin-arm64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/darwin-arm64@7779

@rescript/darwin-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/darwin-x64@7779

@rescript/linux-arm64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/linux-arm64@7779

@rescript/linux-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/linux-x64@7779

@rescript/win32-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/win32-x64@7779

commit: 7a4127f

let attributes = AttributesUtils.make lines in

(if not (AttributesUtils.isEmpty attributes) then
(* Copy the type declaration verbatim to preserve attributes *)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does this work in the else branch too? Can we always copy verbatim? Or what's the tradeoff here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've removed the else branch since every type declaration should be valid in an interface file when copied verbatim 👍 The only tradeoff I can think of is that we'll be copying any non-doc comments within the declaration

@mediremi mediremi merged commit 9efa7e2 into rescript-lang:master Aug 21, 2025
27 checks passed
@mediremi mediremi deleted the as-decorator-create-interface branch August 21, 2025 13:56
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.

CreateInterface.command doesn't copy over @as decorators in records
2 participants