Skip to content

Commit 9eca8c6

Browse files
committed
Various refactorings
1 parent ef0f176 commit 9eca8c6

1 file changed

Lines changed: 49 additions & 76 deletions

File tree

src/FSharp.CosmosDb.Analyzer/CosmosCodeAnalysis.fs

Lines changed: 49 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -5,48 +5,27 @@ open FSharp.Compiler.Range
55
open FSharp.Compiler.SyntaxTree
66

77
module CosmosCodeAnalysis =
8-
let (|Apply|_|) =
9-
function
10-
| SynExpr.TypeApp (funcExpr, lessRange, typeNames, commasRange, greaterRange, typeArgsRange, range) ->
11-
match funcExpr with
12-
| SynExpr.Ident ident -> Some(ident.idText, funcExpr, funcExpr.Range, range)
13-
| SynExpr.LongIdent (isOptional, longDotId, altName, identRange) ->
14-
match longDotId with
15-
| LongIdentWithDots (listOfIds, ranges) ->
16-
let fullName =
17-
listOfIds
18-
|> List.map (fun id -> id.idText)
19-
|> String.concat "."
20-
21-
Some(fullName, funcExpr, funcExpr.Range, range)
22-
| _ -> None
23-
| SynExpr.App (atomicFlag, isInfix, funcExpr, argExpr, applicationRange) ->
24-
match funcExpr with
25-
| SynExpr.Ident ident -> Some(ident.idText, argExpr, funcExpr.Range, applicationRange)
26-
| SynExpr.LongIdent (isOptional, longDotId, altName, identRange) ->
27-
match longDotId with
28-
| LongIdentWithDots (listOfIds, ranges) ->
29-
let fullName =
30-
listOfIds
31-
|> List.map (fun id -> id.idText)
32-
|> String.concat "."
33-
34-
Some(fullName, argExpr, funcExpr.Range, applicationRange)
35-
| _ -> None
8+
let dotConcat = List.map(fun (id:Ident) -> id.idText) >> String.concat "."
9+
let (|Apply|_|) synExpr =
10+
match synExpr with
11+
| SynExpr.TypeApp (funcExpr, _, _, _, _, _, range) -> Some (funcExpr, funcExpr, range)
12+
| SynExpr.App (_, _, funcExpr, argExpr, range) -> Some (funcExpr, argExpr, range)
3613
| _ -> None
14+
|> Option.bind(fun (funcExpr, argExpr, range) ->
15+
match funcExpr with
16+
| SynExpr.Ident ident ->
17+
Some(ident.idText, argExpr, funcExpr.Range, range)
18+
| SynExpr.LongIdent (_, LongIdentWithDots (listOfIds, _), _, _) ->
19+
Some(dotConcat listOfIds, argExpr, funcExpr.Range, range)
20+
| _ ->
21+
None)
3722

3823
let (|LongIdent|_|) =
3924
function
40-
| SynExpr.LongIdent (isOptional, longDotId, altName, identRange) ->
41-
match longDotId with
42-
| LongIdentWithDots (listOfIds, ranges) ->
43-
let fullName =
44-
listOfIds
45-
|> List.map (fun id -> id.idText)
46-
|> String.concat "."
47-
48-
Some(fullName, range)
49-
| _ -> None
25+
| SynExpr.LongIdent (isOptional, LongIdentWithDots (listOfIds, ranges), altName, identRange) ->
26+
Some(dotConcat listOfIds, range)
27+
| _ ->
28+
None
5029

5130
let (|Query|_|) =
5231
function
@@ -58,44 +37,38 @@ module CosmosCodeAnalysis =
5837
| Apply ("Cosmos.query", SynExpr.Ident (identifier), funcRange, appRange) -> Some(identifier.idText, funcRange)
5938
| _ -> None
6039

61-
let (|TypedQuery|_|) =
62-
function
63-
| SynExpr.App (exprAtomic,
64-
isInfix,
65-
(SynExpr.TypeApp (funcExpr, lessRange, typeNames, commasRange, greaterRange, typeArgsRange, typeAppRange)),
66-
SynExpr.Const (SynConst.String (query, queryRange), constRange),
67-
appRange) ->
68-
match funcExpr with
69-
| SynExpr.LongIdent (isOptional, longDotId, altName, identRange) ->
70-
match longDotId with
71-
| LongIdentWithDots (listOfIds, ranges) ->
72-
let fullName =
73-
listOfIds
74-
|> List.map (fun id -> id.idText)
75-
|> String.concat "."
76-
77-
match fullName with
78-
| "Cosmos.query" ->
79-
let names =
80-
typeNames
81-
|> List.filter (fun typeName ->
82-
match typeName with
83-
| SynType.LongIdent (_) -> true
84-
| _ -> false)
85-
|> List.map (fun typeName ->
86-
match typeName with
87-
| SynType.LongIdent (dots) ->
88-
match dots with
89-
| LongIdentWithDots (listOfIds, _) ->
90-
listOfIds
91-
|> List.map (fun id -> id.idText)
92-
|> String.concat "."
93-
| _ -> "")
94-
95-
Some(names, query, typeAppRange)
96-
| _ -> None
97-
| _ -> None
98-
| _ -> None
40+
let (|TypedQuery|_|) synExpr =
41+
match synExpr with
42+
| SynExpr.App (_, _, (SynExpr.TypeApp (funcExpr, _, typeNames, _, _, _, typeAppRange)), SynExpr.Const (SynConst.String (query, _), _), _) ->
43+
Some {| FuncExpr = funcExpr; TypeNames = typeNames; Range = typeAppRange; Query = query |}
44+
| _ ->
45+
None
46+
|> Option.bind(fun args ->
47+
match args.FuncExpr with
48+
| SynExpr.LongIdent (_, LongIdentWithDots (listOfIds, _), _, _) ->
49+
Some {| args with Ids = listOfIds |}
50+
| _ ->
51+
None)
52+
|> Option.bind(fun args ->
53+
let fullName =
54+
args.Ids
55+
|> List.map (fun id -> id.idText)
56+
|> String.concat "."
57+
58+
match fullName with
59+
| "Cosmos.query" ->
60+
let names =
61+
args.TypeNames
62+
|> List.choose (fun typeName ->
63+
match typeName with
64+
| SynType.LongIdent (LongIdentWithDots (listOfIds, _)) ->
65+
dotConcat listOfIds |> Some
66+
| _ ->
67+
None)
68+
69+
Some(names, args.Query, args.Range)
70+
| _ ->
71+
None)
9972

10073
let (|Database|_|) =
10174
function

0 commit comments

Comments
 (0)