1
1
import { SyntaxNode } from "web-tree-sitter" ;
2
- import { TextEditor } from "vscode" ;
3
2
import { getPojoMatchers } from "./getPojoMatchers" ;
4
3
import {
5
4
cascadingMatcher ,
6
- delimitedMatcher ,
7
- hasType ,
8
- possiblyWrappedNode ,
9
- simpleSelectionExtractor ,
10
- getNodeWithLeadingDelimiter ,
11
- childNodeMatcher ,
5
+ composedMatcher ,
6
+ matcher ,
7
+ typeMatcher ,
12
8
} from "../nodeMatchers" ;
13
- import { NodeMatcher , ScopeType } from "../Types" ;
14
- import { getDeclarationNode , getValueNode } from "../treeSitterUtils" ;
9
+ import { NodeMatcher , ScopeType , NodeFinder } from "../Types" ;
10
+ import {
11
+ getDeclarationNode ,
12
+ getNameNode ,
13
+ getValueNode ,
14
+ } from "../treeSitterUtils" ;
15
+ import {
16
+ findNode ,
17
+ findNodeOfType ,
18
+ findPossiblyWrappedNode ,
19
+ } from "../nodeFinders" ;
20
+ import { selectDelimited , selectWithLeadingDelimiter } from "../nodeSelectors" ;
15
21
16
22
// TODO figure out how to properly use super types
17
23
// Generated by the following command:
@@ -101,28 +107,34 @@ const STATEMENT_TYPES = [
101
107
"with_statement" ,
102
108
] ;
103
109
104
- function possiblyExportedDeclaration ( ...typeNames : string [ ] ) : NodeMatcher {
105
- return possiblyWrappedNode (
106
- ( node ) => node . type === "export_statement" ,
107
- ( node ) => typeNames . includes ( node . type ) ,
110
+ function possiblyExportedDeclaration ( ...typeNames : string [ ] ) : NodeFinder {
111
+ return findPossiblyWrappedNode (
112
+ findNodeOfType ( "export_statement" ) ,
113
+ findNodeOfType ( ... typeNames ) ,
108
114
( node ) => [ getDeclarationNode ( node ) , getValueNode ( node ) ]
109
115
) ;
110
116
}
111
117
112
- const isNamedArrowFunction = ( node : SyntaxNode ) => {
118
+ const findNamedArrowFunction = ( node : SyntaxNode ) => {
113
119
if ( node . type !== "lexical_declaration" || node . namedChildCount !== 1 ) {
114
- return false ;
120
+ return null ;
115
121
}
116
122
117
123
const child = node . firstNamedChild ! ;
118
124
119
- return (
120
- child . type === "variable_declarator" &&
125
+ return child . type === "variable_declarator" &&
121
126
getValueNode ( child ) ! . type === "arrow_function"
122
- ) ;
127
+ ? node
128
+ : null ;
123
129
} ;
124
130
125
- export const getTypeNode = ( node : SyntaxNode ) => {
131
+ const findClassPropertyArrowFunction = ( node : SyntaxNode ) =>
132
+ node . type === "public_field_definition" &&
133
+ getValueNode ( node ) ! . type === "arrow_function"
134
+ ? node
135
+ : null ;
136
+
137
+ export const findTypeNode = ( node : SyntaxNode ) => {
126
138
const typeAnnotationNode = node . children . find ( ( child ) =>
127
139
[ "type_annotation" , "opting_type_annotation" ] . includes ( child . type )
128
140
) ;
@@ -135,55 +147,68 @@ const nodeMatchers: Record<ScopeType, NodeMatcher> = {
135
147
[ "array" ] ,
136
148
( node ) => isExpression ( node ) || node . type === "spread_element"
137
149
) ,
138
- ifStatement : hasType ( "if_statement" ) ,
139
- class : possiblyExportedDeclaration ( "class_declaration" , "class" ) ,
140
- statement : possiblyExportedDeclaration ( ...STATEMENT_TYPES ) ,
141
- arrowFunction : hasType ( "arrow_function" ) ,
142
- functionCall : hasType ( "call_expression" , "new_expression" ) ,
150
+ ifStatement : typeMatcher ( "if_statement" ) ,
151
+ class : matcher ( possiblyExportedDeclaration ( "class_declaration" , "class" ) ) ,
152
+ statement : matcher ( possiblyExportedDeclaration ( ...STATEMENT_TYPES ) ) ,
153
+ arrowFunction : typeMatcher ( "arrow_function" ) ,
154
+ functionCall : typeMatcher ( "call_expression" , "new_expression" ) ,
155
+ functionName : cascadingMatcher (
156
+ composedMatcher ( [
157
+ findNodeOfType ( "function_declaration" , "method_definition" ) ,
158
+ getNameNode ,
159
+ ] ) ,
160
+ composedMatcher ( [ findClassPropertyArrowFunction , getNameNode ] ) ,
161
+ composedMatcher ( [ findNamedArrowFunction , getNameNode ] )
162
+ ) ,
143
163
type : cascadingMatcher (
144
164
// Typed parameters, properties, and functions
145
- childNodeMatcher ( getTypeNode , getNodeWithLeadingDelimiter ) ,
146
-
165
+ matcher ( findTypeNode , selectWithLeadingDelimiter ) ,
147
166
// Type alias/interface declarations
148
- possiblyExportedDeclaration (
149
- "type_alias_declaration" ,
150
- "interface_declaration"
167
+ matcher (
168
+ possiblyExportedDeclaration (
169
+ "type_alias_declaration" ,
170
+ "interface_declaration"
171
+ )
151
172
)
152
173
) ,
153
- argumentOrParameter : delimitedMatcher (
154
- ( node ) =>
155
- ( node . parent ?. type === "arguments" &&
156
- ( isExpression ( node ) || node . type === "spread_element" ) ) ||
157
- node . type === "optional_parameter" ||
158
- node . type === "required_parameter" ,
159
- ( node ) => node . type === "," || node . type === "(" || node . type === ")" ,
160
- ", "
174
+ argumentOrParameter : matcher (
175
+ findNode (
176
+ ( node ) =>
177
+ ( node . parent ?. type === "arguments" &&
178
+ ( isExpression ( node ) || node . type === "spread_element" ) ) ||
179
+ node . type === "optional_parameter" ||
180
+ node . type === "required_parameter"
181
+ ) ,
182
+ selectDelimited (
183
+ ( node ) => node . type === "," || node . type === "(" || node . type === ")" ,
184
+ ", "
185
+ )
161
186
) ,
162
187
namedFunction : cascadingMatcher (
163
188
// Simple case, eg
164
189
// function foo() {}
165
- possiblyExportedDeclaration ( "function_declaration" , "method_definition" ) ,
190
+ matcher (
191
+ possiblyExportedDeclaration ( "function_declaration" , "method_definition" )
192
+ ) ,
166
193
167
194
// Class property defined as field definition with arrow
168
195
// eg:
169
196
// class Foo {
170
197
// bar = () => "hello";
171
198
// }
172
- ( editor : TextEditor , node : SyntaxNode ) =>
173
- node . type === "public_field_definition" &&
174
- getValueNode ( node ) ! . type === "arrow_function"
175
- ? simpleSelectionExtractor ( node )
176
- : null ,
199
+ matcher ( findClassPropertyArrowFunction ) ,
177
200
178
201
// eg:
179
202
// const foo = () => "hello"
180
- possiblyWrappedNode (
181
- ( node ) => node . type === "export_statement" ,
182
- isNamedArrowFunction ,
183
- ( node ) => [ getDeclarationNode ( node ) ]
203
+ matcher (
204
+ findPossiblyWrappedNode (
205
+ findNodeOfType ( "export_statement" ) ,
206
+ findNamedArrowFunction ,
207
+ ( node ) => [ getDeclarationNode ( node ) ]
208
+ )
184
209
)
185
210
) ,
186
- comment : hasType ( "comment" ) ,
211
+ comment : matcher ( findNodeOfType ( "comment" ) ) ,
187
212
} ;
188
213
189
214
export default nodeMatchers ;
0 commit comments