Skip to content

Commit ce44277

Browse files
committed
track all typed patterns and expressions
1 parent 17e47c1 commit ce44277

17 files changed

+116
-284
lines changed

analysis/src/Commands.ml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ let hover ~path ~pos ~debug ~supportsMarkdownLinks =
6868
let isModule =
6969
match locItem.locType with
7070
| LModule _ | TopLevelModule _ -> true
71-
| TypeDefinition _ | Typed _ | Constant _ -> false
71+
| TypeDefinition _ | Typed _ | Constant _ | OtherExpression _
72+
| OtherPattern _ ->
73+
false
7274
in
7375
let uriLocOpt = References.definitionForLocItem ~full locItem in
7476
let skipZero =
@@ -126,7 +128,9 @@ let definition ~path ~pos ~debug =
126128
let isModule =
127129
match locItem.locType with
128130
| LModule _ | TopLevelModule _ -> true
129-
| TypeDefinition _ | Typed _ | Constant _ -> false
131+
| TypeDefinition _ | Typed _ | Constant _ | OtherExpression _
132+
| OtherPattern _ ->
133+
false
130134
in
131135
let skipLoc =
132136
(not isModule) && (not isInterface) && posIsZero loc.loc_start

analysis/src/Hover.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,8 @@ let newHover ~full:{file; package} ~supportsMarkdownLinks locItem =
244244
showModule ~docstring:file.structure.docstring ~name:file.moduleName ~file
245245
~package None)
246246
| Typed (_, _, Definition (_, (Field _ | Constructor _))) -> None
247+
| OtherExpression t | OtherPattern t ->
248+
Some (Markdown.codeBlock (Shared.typeToString t))
247249
| Constant t ->
248250
Some
249251
(Markdown.codeBlock

analysis/src/ProcessExtra.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ let pat ~(file : File.t) ~env ~extra (iter : Tast_iterator.iterator)
383383
(* Log.log("Pattern " ++ name.txt); *)
384384
let stamp = Ident.binding_time ident in
385385
addForPattern stamp name
386-
| _ -> ());
386+
| _ -> addLocItem extra pattern.pat_loc (OtherPattern pattern.pat_type));
387387
Tast_iterator.default_iterator.pat iter pattern
388388

389389
let expr ~env ~(extra : extra) (iter : Tast_iterator.iterator)
@@ -409,7 +409,8 @@ let expr ~env ~(extra : extra) (iter : Tast_iterator.iterator)
409409
| Texp_field (inner, lident, _label_description) ->
410410
addForField ~env ~extra ~recordType:inner.exp_type
411411
~fieldType:expression.exp_type lident
412-
| _ -> ());
412+
| _ ->
413+
addLocItem extra expression.exp_loc (OtherExpression expression.exp_type));
413414
Tast_iterator.default_iterator.expr iter expression
414415

415416
let getExtra ~file ~infos =

analysis/src/References.ml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ let definitionForLocItem ~full:{file; package} locItem =
358358
| Typed (_, _, NotFound)
359359
| LModule (NotFound | Definition (_, _))
360360
| TypeDefinition (_, _, _)
361-
| Constant _ ->
361+
| Constant _ | OtherExpression _ | OtherPattern _ ->
362362
None
363363
| TopLevelModule name -> (
364364
maybeLog ("Toplevel " ^ name);
@@ -405,7 +405,9 @@ let digConstructor ~env ~package path =
405405

406406
let typeDefinitionForLocItem ~full:{file; package} locItem =
407407
match locItem.locType with
408-
| Constant _ | TopLevelModule _ | LModule _ -> None
408+
| Constant _ | TopLevelModule _ | LModule _ | OtherExpression _
409+
| OtherPattern _ ->
410+
None
409411
| TypeDefinition _ -> Some (file.uri, locItem.loc)
410412
| Typed (_, typ, _) -> (
411413
let env = QueryEnv.fromFile file in
@@ -546,7 +548,10 @@ let allReferencesForLocItem ~full:({file; package} as full) locItem =
546548
getSrc paths |> List.map moduleSrcToRef
547549
in
548550
List.append targetModuleReferences otherModulesReferences
549-
| Typed (_, _, NotFound) | LModule NotFound | Constant _ -> []
551+
| Typed (_, _, NotFound)
552+
| LModule NotFound
553+
| Constant _ | OtherExpression _ | OtherPattern _ ->
554+
[]
550555
| TypeDefinition (_, _, stamp) -> forLocalStamp ~full stamp Type
551556
| Typed (_, _, (LocalReference (stamp, tip) | Definition (stamp, tip)))
552557
| LModule (LocalReference (stamp, tip) | Definition (stamp, tip)) ->

analysis/src/SharedTypes.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,9 @@ type locType =
460460
| LModule of locKind
461461
| TopLevelModule of string
462462
| TypeDefinition of string * Types.type_declaration * int
463+
(* For all other expressions and patterns that are not tracked by the above. Needed to produce hovers, completions, and what not. *)
464+
| OtherExpression of Types.type_expr
465+
| OtherPattern of Types.type_expr
463466

464467
type locItem = {loc: Location.t; locType: locType}
465468

@@ -534,6 +537,8 @@ let locTypeToString = function
534537
"Typed " ^ name ^ " " ^ Shared.typeToString e ^ " "
535538
^ locKindToString locKind
536539
| Constant _ -> "Constant"
540+
| OtherExpression e -> "OtherExpression " ^ Shared.typeToString e
541+
| OtherPattern e -> "OtherPattern " ^ Shared.typeToString e
537542
| LModule locKind -> "LModule " ^ locKindToString locKind
538543
| TopLevelModule _ -> "TopLevelModule"
539544
| TypeDefinition _ -> "TypeDefinition"

tests/analysis_tests/tests-reanalyze/deadcode/expected/deadcode.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@
417417
addValueDeclaration +make Hooks.res:63:6 path:+Hooks.RenderPropRequiresConversion
418418
addRecordLabelDeclaration name Hooks.res:1:16 path:+Hooks.vehicle
419419
addRecordLabelDeclaration vehicle Hooks.res:4:12 path:+Hooks.props
420-
addValueReference Hooks.res:5:26 --> React.res:134:0
420+
addValueReference Hooks.res:5:26 --> React.res:145:0
421421
addTypeReference Hooks.res:10:29 --> Hooks.res:1:16
422422
addValueReference Hooks.res:10:29 --> Hooks.res:4:12
423423
addValueReference Hooks.res:10:75 --> Hooks.res:5:7
@@ -1805,7 +1805,7 @@ File References
18051805
DeadExn.resi -->>
18061806
DeadRT.res -->>
18071807
DeadRT.resi -->>
1808-
DeadTest.res -->> DeadValueTest.resi, DynamicallyLoadedComponent.res, ImmutableArray.resi, React.res
1808+
DeadTest.res -->> React.res, DeadValueTest.resi, DynamicallyLoadedComponent.res, ImmutableArray.resi
18091809
DeadTestBlacklist.res -->>
18101810
DeadTestWithInterface.res -->>
18111811
DeadTypeTest.res -->>
@@ -1821,7 +1821,7 @@ File References
18211821
FirstClassModules.res -->>
18221822
FirstClassModulesInterface.res -->>
18231823
FirstClassModulesInterface.resi -->> FirstClassModulesInterface.res
1824-
Hooks.res -->> ImportHookDefault.res, ImportHooks.res, React.res
1824+
Hooks.res -->> React.res, ImportHookDefault.res, ImportHooks.res
18251825
IgnoreInterface.res -->>
18261826
IgnoreInterface.resi -->>
18271827
ImmutableArray.res -->>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// ^dv+
2+
let xxxxxxx = [1, 2, 3]
3+
// ^hov
4+
5+
let xxxxxxxx = [1, 2, 3]
6+
// ^hov
7+
8+
let xxxxxxxxx = [1, 2, 3]
9+
// ^hov

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

Lines changed: 22 additions & 34 deletions
Large diffs are not rendered by default.

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

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -966,17 +966,5 @@ Path ReactDOM.Client.Root.
966966
}]
967967

968968
Hover src/CompletionInferValues.res 160:27
969-
Nothing at that position. Now trying to use completion.
970-
posCursor:[160:27] posNoWhite:[160:26] Found expr:[160:25->160:28]
971-
Pexp_ident res:[160:25->160:28]
972-
Completable: Cpath Value[res]
973-
Package opens Stdlib.place holder Pervasives.JsxModules.place holder
974-
Resolved opens 1 Stdlib
975-
ContextPath Value[res]
976-
Path res
977-
Package opens Stdlib.place holder Pervasives.JsxModules.place holder
978-
Resolved opens 1 Stdlib
979-
ContextPath Value[res]
980-
Path res
981-
{"contents": {"kind": "markdown", "value": "```rescript\nint\n```"}}
969+
null
982970

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
Hover src/EnhanceHover.res 1:4
3+
{"contents": {"kind": "markdown", "value": "```rescript\narray<int>\n```"}}
4+
5+
Hover src/EnhanceHover.res 4:15
6+
{"contents": {"kind": "markdown", "value": "```rescript\narray<int>\n```"}}
7+
8+
Hover src/EnhanceHover.res 7:17
9+
{"contents": {"kind": "markdown", "value": "```rescript\nint\n```"}}
10+

0 commit comments

Comments
 (0)