Skip to content

Commit 9efa7e2

Browse files
authored
Preserve @as decorator on record fields when creating interface (#7779)
* Preserve @as decorator on record fields when creating interface * Add CHANGELOG entry * Always copy type declarations verbatim * Remove unused helpers
1 parent 271a112 commit 9efa7e2

File tree

4 files changed

+20
-5
lines changed

4 files changed

+20
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
#### :bug: Bug fix
2525

26+
- Preserve `@as(...)` decorator on record fields when creating interface. https://github.com/rescript-lang/rescript/pull/7779
27+
2628
#### :memo: Documentation
2729

2830
#### :nail_care: Polish

analysis/src/CreateInterface.ml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -249,12 +249,13 @@ let printSignature ~extractor ~signature =
249249
in
250250
Buffer.add_string buf (indent ^ newItemStr ^ "\n");
251251
processSignature ~indent items
252-
| Sig_type (id, typeDecl, resStatus) :: items ->
253-
let newItemStr =
254-
sigItemToString
255-
(Printtyp.tree_of_type_declaration id typeDecl resStatus)
252+
| Sig_type (_id, typeDecl, _recStatus) :: items ->
253+
let lines =
254+
let posStart, posEnd = Loc.range typeDecl.type_loc in
255+
extractor |> SourceFileExtractor.extract ~posStart ~posEnd
256256
in
257-
Buffer.add_string buf (indent ^ newItemStr ^ "\n");
257+
(* Copy the type declaration verbatim to preserve attributes *)
258+
Buffer.add_string buf ((lines |> String.concat "\n") ^ "\n");
258259
processSignature ~indent items
259260
| Sig_typext (id, extConstr, extStatus) :: items ->
260261
let newItemStr =

tests/analysis_tests/tests/src/CreateInterface.res

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,10 @@ module Memo = {
143143

144144
let make = React.memo(make)
145145
}
146+
147+
external x: (@as("bar") ~foo: int) => unit = "myexternal"
148+
149+
type record = {
150+
@as("foo_bar")
151+
fooBar: int
152+
}

tests/analysis_tests/tests/src/expected/CreateInterface.res.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,9 @@ module Memo: {
109109
@react.component
110110
let make: (~name: string) => React.element
111111
}
112+
external x: (@as("bar") ~foo: int) => unit = "myexternal"
113+
type record = {
114+
@as("foo_bar")
115+
fooBar: int
116+
}
112117

0 commit comments

Comments
 (0)