-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathjavascript.function.scm
181 lines (162 loc) · 3.69 KB
/
javascript.function.scm
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
;; Anonymous functions
[
;;!! function() {}
(function_expression
!name
)
;;!! function *() {}
(generator_function
!name
)
;;!! () => {}
(arrow_function)
] @anonymousFunction
;; If we export an anonymous function as default, it semantically feels like a
;; named function.
(export_statement
[
;;!! export default function() {}
(function_expression
!name
)
;;!! export default function *() {}
(generator_function
!name
)
;;!! export default () => {}
(arrow_function)
]
) @namedFunction
;; Named functions without export
(
[
;;!! function foo() {}
(function_declaration
name: (_) @functionName
)
;;!! function *foo() {}
(generator_function_declaration
name: (_) @functionName
)
;;!! (let | const) foo = () => {}
;;!! (let | const) foo = function() {}
;;!! (let | const) foo = function *() {}
(lexical_declaration
(variable_declarator
name: (_) @functionName
value: [
(function_expression
!name
)
(generator_function
!name
)
(arrow_function)
]
)
)
;;!! var foo = () => {}
;;!! var foo = function() {}
;;!! var foo = function *() {}
;; Note that we can't merge this with the variable declaration above because
;; of https://github.com/tree-sitter/tree-sitter/issues/1442#issuecomment-1584628651
(variable_declaration
(variable_declarator
name: (_) @functionName
value: [
(function_expression
!name
)
(generator_function
!name
)
(arrow_function)
]
)
)
] @namedFunction @functionName.domain
(#not-parent-type? @namedFunction export_statement)
)
;; Exported named functions
(export_statement
[
;;!! export [default] function foo() {}
(function_declaration
name: (_) @functionName
)
;;!! export [default] function *foo() {}
(generator_function_declaration
name: (_) @functionName
)
;;!! export [default] (let | const | var) foo = () => {}
;;!! export [default] (let | const | var) foo = function() {}
;;!! export [default] (let | const | var) foo = function *() {}
(_
(variable_declarator
name: (_) @functionName
value: [
(function_expression
!name
)
(generator_function
!name
)
(arrow_function)
]
)
)
]
) @namedFunction @functionName.domain
;; Note that there are a few Typescript-specific function declarations that we
;; don't handle here; see typescript.scm.
;; We also don't handle function declarations that only exist in Javascript;
;; see javascript.scm.
[
;;!! (function foo() {})
(function_expression
name: (_) @functionName
)
;;!! (function *foo() {})
(generator_function
name: (_) @functionName
)
;;!! class Foo { foo() {} }
;;! ^^^^^^^^
(method_definition
name: (_) @functionName
)
;;!! foo = () => {};
;;!! foo = function() {};
;;!! foo = function *() {};
(assignment_expression
left: (_) @functionName
right: [
(function_expression
!name
)
(generator_function
!name
)
(arrow_function)
]
)
] @namedFunction @functionName.domain
[
(program)
(class_declaration)
(object
(method_definition)
)
] @namedFunction.iteration @functionName.iteration
;;!! { funk: function() { } }
;;! ^^^^
(pair
key: (_) @functionName @name
value: (function_expression)
) @_.domain
;;!! { funk: () => { } }
;;! ^^^^
(pair
key: (_) @functionName @name
value: (arrow_function)
) @_.domain