Skip to content

Commit 880b8af

Browse files
committed
Traverse template interpolations in analysis
Signed-off-by: Christoph Knittel <ck@cca.io>
1 parent 0abde6d commit 880b8af

3 files changed

Lines changed: 85 additions & 51 deletions

File tree

analysis/src/process_cmt.ml

Lines changed: 15 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -688,57 +688,21 @@ and for_module ~env mod_desc module_name =
688688
scope lookups match precisely.
689689
*)
690690
and scan_let_modules ~env (e : Typedtree.expression) =
691-
match e.exp_desc with
692-
| Texp_letmodule (id, name, mexpr, body) ->
693-
let stamp = Ident.binding_time id in
694-
let item = for_module ~env mexpr.mod_desc name.txt in
695-
let declared =
696-
Process_attributes.new_declared ~item ~extent:name.loc ~name ~stamp
697-
~module_path:NotVisible false []
698-
in
699-
Stamps.add_module env.stamps stamp declared;
700-
scan_let_modules ~env body
701-
| Texp_let (_rf, bindings, body) ->
702-
List.iter
703-
(fun {Typedtree.vb_expr} -> scan_let_modules ~env vb_expr)
704-
bindings;
705-
scan_let_modules ~env body
706-
| Texp_apply {funct; args; _} ->
707-
scan_let_modules ~env funct;
708-
args
709-
|> List.iter (function
710-
| _, Some e -> scan_let_modules ~env e
711-
| _, None -> ())
712-
| Texp_tuple exprs -> List.iter (scan_let_modules ~env) exprs
713-
| Texp_sequence (e1, e2) ->
714-
scan_let_modules ~env e1;
715-
scan_let_modules ~env e2
716-
| Texp_match (e, cases, exn_cases, _) ->
717-
scan_let_modules ~env e;
718-
let scan_case {Typedtree.c_lhs = _; c_guard; c_rhs} =
719-
(match c_guard with
720-
| Some g -> scan_let_modules ~env g
721-
| None -> ());
722-
scan_let_modules ~env c_rhs
723-
in
724-
List.iter scan_case cases;
725-
List.iter scan_case exn_cases
726-
| Texp_function {body; _} -> scan_let_modules ~env body
727-
| Texp_try (e, cases) ->
728-
scan_let_modules ~env e;
729-
cases
730-
|> List.iter (fun {Typedtree.c_lhs = _; c_guard; c_rhs} ->
731-
(match c_guard with
732-
| Some g -> scan_let_modules ~env g
733-
| None -> ());
734-
scan_let_modules ~env c_rhs)
735-
| Texp_ifthenelse (e1, e2, e3_opt) -> (
736-
scan_let_modules ~env e1;
737-
scan_let_modules ~env e2;
738-
match e3_opt with
739-
| Some e3 -> scan_let_modules ~env e3
740-
| None -> ())
741-
| _ -> ()
691+
let expr iterator (expression : Typedtree.expression) =
692+
(match expression.exp_desc with
693+
| Texp_letmodule (id, name, mexpr, _body) ->
694+
let stamp = Ident.binding_time id in
695+
let item = for_module ~env mexpr.mod_desc name.txt in
696+
let declared =
697+
Process_attributes.new_declared ~item ~extent:name.loc ~name ~stamp
698+
~module_path:NotVisible false []
699+
in
700+
Stamps.add_module env.stamps stamp declared
701+
| _ -> ());
702+
Tast_iterator.default_iterator.expr iterator expression
703+
in
704+
let iterator = {Tast_iterator.default_iterator with expr} in
705+
iterator.expr iterator e
742706

743707
and for_structure ~name ~env str_items =
744708
let exported = Exported.init () in

tests/analysis_tests/tests/src/CompletionTaggedTemplate.res

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,17 @@ let w = meh``
1616

1717
// let x = meh`foo`.
1818
// ^com
19+
20+
let ordinaryInterpolation = `value: ${{
21+
module LocalOrdinary = M
22+
// LocalOrdinary.
23+
// ^com
24+
LocalOrdinary.b(w)
25+
}}`
26+
27+
let taggedInterpolation = meh`value: ${{
28+
module LocalTagged = M
29+
// LocalTagged.
30+
// ^com
31+
LocalTagged.b(w)
32+
}}`

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,59 @@ Path
137137
}
138138
]
139139

140+
Complete src/CompletionTaggedTemplate.res 21:19
141+
posCursor:[21:19] posNoWhite:[21:18] Found expr:[19:28->24:3]
142+
posCursor:[21:19] posNoWhite:[21:18] Found expr:[20:2->23:20]
143+
posCursor:[21:19] posNoWhite:[21:18] Found expr:[21:5->23:20]
144+
Pexp_apply ...[21:5->23:17] (...[23:18->23:19])
145+
posCursor:[21:19] posNoWhite:[21:18] Found expr:[21:5->23:17]
146+
Pexp_ident LocalOrdinary.:[21:5->23:17]
147+
Completable: Cpath Value[LocalOrdinary, ""]
148+
Package opens Stdlib.place holder Pervasives.JsxModules.place holder
149+
Resolved opens 1 Stdlib
150+
ContextPath Value[LocalOrdinary, ""]
151+
Path LocalOrdinary.
152+
[
153+
{
154+
"detail": "type t",
155+
"documentation": {
156+
"kind": "markdown",
157+
"value": "```rescript\ntype t = promise<string>\n```"
158+
},
159+
"kind": 22,
160+
"label": "t",
161+
"tags": []
162+
},
163+
{ "detail": "t => int", "kind": 12, "label": "a", "tags": [] },
164+
{ "detail": "t => string", "kind": 12, "label": "b", "tags": [] },
165+
{ "detail": "(t, int) => int", "kind": 12, "label": "xyz", "tags": [] }
166+
]
167+
168+
Complete src/CompletionTaggedTemplate.res 28:17
169+
posCursor:[28:17] posNoWhite:[28:16] Found expr:[26:26->31:3]
170+
posCursor:[28:17] posNoWhite:[28:16] Found expr:[27:2->30:18]
171+
posCursor:[28:17] posNoWhite:[28:16] Found expr:[28:5->30:18]
172+
Pexp_apply ...[28:5->30:15] (...[30:16->30:17])
173+
posCursor:[28:17] posNoWhite:[28:16] Found expr:[28:5->30:15]
174+
Pexp_ident LocalTagged.:[28:5->30:15]
175+
Completable: Cpath Value[LocalTagged, ""]
176+
Package opens Stdlib.place holder Pervasives.JsxModules.place holder
177+
Resolved opens 1 Stdlib
178+
ContextPath Value[LocalTagged, ""]
179+
Path LocalTagged.
180+
[
181+
{
182+
"detail": "type t",
183+
"documentation": {
184+
"kind": "markdown",
185+
"value": "```rescript\ntype t = promise<string>\n```"
186+
},
187+
"kind": 22,
188+
"label": "t",
189+
"tags": []
190+
},
191+
{ "detail": "t => int", "kind": 12, "label": "a", "tags": [] },
192+
{ "detail": "t => string", "kind": 12, "label": "b", "tags": [] },
193+
{ "detail": "(t, int) => int", "kind": 12, "label": "xyz", "tags": [] }
194+
]
195+

0 commit comments

Comments
 (0)