diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c1dac27e0..bf61f0caab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,8 @@ #### :bug: Bug fix +- Preserve `@as(...)` decorator on record fields when creating interface. https://github.com/rescript-lang/rescript/pull/7779 + #### :memo: Documentation #### :nail_care: Polish diff --git a/analysis/src/CreateInterface.ml b/analysis/src/CreateInterface.ml index 1bbb3632fe..107fa5973d 100644 --- a/analysis/src/CreateInterface.ml +++ b/analysis/src/CreateInterface.ml @@ -249,12 +249,13 @@ let printSignature ~extractor ~signature = in Buffer.add_string buf (indent ^ newItemStr ^ "\n"); processSignature ~indent items - | Sig_type (id, typeDecl, resStatus) :: items -> - let newItemStr = - sigItemToString - (Printtyp.tree_of_type_declaration id typeDecl resStatus) + | Sig_type (_id, typeDecl, _recStatus) :: items -> + let lines = + let posStart, posEnd = Loc.range typeDecl.type_loc in + extractor |> SourceFileExtractor.extract ~posStart ~posEnd in - Buffer.add_string buf (indent ^ newItemStr ^ "\n"); + (* Copy the type declaration verbatim to preserve attributes *) + Buffer.add_string buf ((lines |> String.concat "\n") ^ "\n"); processSignature ~indent items | Sig_typext (id, extConstr, extStatus) :: items -> let newItemStr = diff --git a/tests/analysis_tests/tests/src/CreateInterface.res b/tests/analysis_tests/tests/src/CreateInterface.res index 7bc514cc27..3f7ab03d48 100644 --- a/tests/analysis_tests/tests/src/CreateInterface.res +++ b/tests/analysis_tests/tests/src/CreateInterface.res @@ -143,3 +143,10 @@ module Memo = { let make = React.memo(make) } + +external x: (@as("bar") ~foo: int) => unit = "myexternal" + +type record = { + @as("foo_bar") + fooBar: int +} diff --git a/tests/analysis_tests/tests/src/expected/CreateInterface.res.txt b/tests/analysis_tests/tests/src/expected/CreateInterface.res.txt index 4e12129485..700f3a599d 100644 --- a/tests/analysis_tests/tests/src/expected/CreateInterface.res.txt +++ b/tests/analysis_tests/tests/src/expected/CreateInterface.res.txt @@ -109,4 +109,9 @@ module Memo: { @react.component let make: (~name: string) => React.element } +external x: (@as("bar") ~foo: int) => unit = "myexternal" +type record = { + @as("foo_bar") + fooBar: int +}