forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflow_ast_utils.ml
231 lines (206 loc) · 6.19 KB
/
flow_ast_utils.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
(*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*)
open Flow_ast
type 'loc binding = 'loc * string
type 'loc ident = 'loc * string
type 'loc source = 'loc * string
let rec fold_bindings_of_pattern =
let open Pattern in
let property f acc =
let open Object in
function
| Property (_, { Property.pattern = p; _ })
| RestElement (_, { RestElement.argument = p; comments = _ }) ->
fold_bindings_of_pattern f acc p
in
let element f acc =
let open Array in
function
| Hole _ -> acc
| Element (_, { Element.argument = p; default = _ })
| RestElement (_, { RestElement.argument = p; comments = _ }) ->
fold_bindings_of_pattern f acc p
in
fun f acc -> function
| (_, Identifier { Identifier.name; annot; _ }) -> f acc name annot
| (_, Object { Object.properties; _ }) -> List.fold_left (property f) acc properties
| (_, Array { Array.elements; _ }) -> List.fold_left (element f) acc elements
| (_, Expression _) -> acc
let fold_bindings_of_variable_declarations f acc declarations =
let open Flow_ast.Statement.VariableDeclaration in
List.fold_left
(fun acc -> function
| (_, { Declarator.id = pattern; _ }) -> fold_bindings_of_pattern f acc pattern)
acc
declarations
let partition_directives statements =
let open Flow_ast.Statement in
let rec helper directives = function
| ((_, Expression { Expression.directive = Some _; _ }) as directive) :: rest ->
helper (directive :: directives) rest
| rest -> (List.rev directives, rest)
in
helper [] statements
let negate_number_literal (value, raw) =
let raw_len = String.length raw in
let raw =
if raw_len > 0 && raw.[0] = '-' then
String.sub raw 1 (raw_len - 1)
else
"-" ^ raw
in
(-.value, raw)
let loc_of_statement = fst
let loc_of_expression = fst
let loc_of_pattern = fst
let loc_of_ident = fst
let name_of_ident (_, { Identifier.name; comments = _ }) = name
let source_of_ident (loc, { Identifier.name; comments = _ }) = (loc, name)
let ident_of_source ?comments (loc, name) = (loc, { Identifier.name; comments })
let mk_comments ?(leading = []) ?(trailing = []) a = { Syntax.leading; trailing; internal = a }
let mk_comments_opt ?(leading = []) ?(trailing = []) () =
match (leading, trailing) with
| ([], []) -> None
| (_, _) -> Some (mk_comments ~leading ~trailing ())
let mk_comments_with_internal_opt ?(leading = []) ?(trailing = []) ~internal () =
match (leading, trailing, internal) with
| ([], [], []) -> None
| _ -> Some (mk_comments ~leading ~trailing internal)
let merge_comments ~inner ~outer =
let open Syntax in
match (inner, outer) with
| (None, c)
| (c, None) ->
c
| (Some inner, Some outer) ->
mk_comments_opt
~leading:(outer.leading @ inner.leading)
~trailing:(inner.trailing @ outer.trailing)
()
let merge_comments_with_internal ~inner ~outer =
match (inner, outer) with
| (inner, None) -> inner
| (None, Some { Syntax.leading; trailing; _ }) ->
mk_comments_with_internal_opt ~leading ~trailing ~internal:[] ()
| ( Some { Syntax.leading = inner_leading; trailing = inner_trailing; internal },
Some { Syntax.leading = outer_leading; trailing = outer_trailing; _ } ) ->
mk_comments_with_internal_opt
~leading:(outer_leading @ inner_leading)
~trailing:(inner_trailing @ outer_trailing)
~internal
()
let split_comments comments =
match comments with
| None -> (None, None)
| Some { Syntax.leading; trailing; _ } ->
(mk_comments_opt ~leading (), mk_comments_opt ~trailing ())
let string_of_assignment_operator op =
let open Flow_ast.Expression.Assignment in
match op with
| PlusAssign -> "+="
| MinusAssign -> "-="
| MultAssign -> "*="
| ExpAssign -> "**="
| DivAssign -> "/="
| ModAssign -> "%="
| LShiftAssign -> "<<="
| RShiftAssign -> ">>="
| RShift3Assign -> ">>>="
| BitOrAssign -> "|="
| BitXorAssign -> "^="
| BitAndAssign -> "&="
let string_of_binary_operator op =
let open Flow_ast.Expression.Binary in
match op with
| Equal -> "=="
| NotEqual -> "!="
| StrictEqual -> "==="
| StrictNotEqual -> "!=="
| LessThan -> "<"
| LessThanEqual -> "<="
| GreaterThan -> ">"
| GreaterThanEqual -> ">="
| LShift -> "<<"
| RShift -> ">>"
| RShift3 -> ">>>"
| Plus -> "+"
| Minus -> "-"
| Mult -> "*"
| Exp -> "**"
| Div -> "/"
| Mod -> "%"
| BitOr -> "|"
| Xor -> "^"
| BitAnd -> "&"
| In -> "in"
| Instanceof -> "instanceof"
module ExpressionSort = struct
type t =
| Array
| ArrowFunction
| Assignment
| Binary
| Call
| Class
| Comprehension
| Conditional
| Function
| Generator
| Identifier
| Import
| JSXElement
| JSXFragment
| Literal
| Logical
| Member
| MetaProperty
| New
| Object
| OptionalCall
| OptionalMember
| Sequence
| Super
| TaggedTemplate
| TemplateLiteral
| This
| TypeCast
| Unary
| Update
| Yield
let to_string = function
| Array -> "array"
| ArrowFunction -> "arrow function"
| Assignment -> "assignment expression"
| Binary -> "binary expression"
| Call -> "call expression"
| Class -> "class"
| Comprehension -> "comprehension expression"
| Conditional -> "conditional expression"
| Function -> "function"
| Generator -> "generator"
| Identifier -> "identifier"
| Import -> "import expression"
| JSXElement -> "JSX element"
| JSXFragment -> "JSX fragment"
| Literal -> "literal"
| Logical -> "logical expression"
| Member -> "member expression"
| MetaProperty -> "metaproperty expression"
| New -> "new expression"
| Object -> "object"
| OptionalCall -> "optional call expression"
| OptionalMember -> "optional member expression"
| Sequence -> "sequence"
| Super -> "`super` reference"
| TaggedTemplate -> "tagged template expression"
| TemplateLiteral -> "template literal"
| This -> "`this` reference"
| TypeCast -> "type cast"
| Unary -> "unary expression"
| Update -> "update expression"
| Yield -> "yield expression"
end