1
1
import { SyntaxNode } from "web-tree-sitter" ;
2
2
import { getPojoMatchers } from "./getPojoMatchers" ;
3
3
import {
4
- childNodeMatcher ,
5
- delimitedMatcher ,
6
- getNodeWithLeadingDelimiter ,
7
- hasType ,
8
- possiblyWrappedNode ,
4
+ cascadingMatcher ,
5
+ composedMatcher ,
6
+ matcher ,
7
+ notSupported ,
8
+ typeMatcher ,
9
9
} from "../nodeMatchers" ;
10
- import { NodeMatcher , ScopeType } from "../Types" ;
11
- import { getDefinitionNode } from "../treeSitterUtils" ;
10
+ import { NodeFinder , NodeMatcher , ScopeType } from "../Types" ;
11
+ import {
12
+ getDefinitionNode ,
13
+ getLeftNode ,
14
+ getNameNode ,
15
+ } from "../treeSitterUtils" ;
16
+ import {
17
+ nodeFinder ,
18
+ typedNodeFinder ,
19
+ findPossiblyWrappedNode ,
20
+ } from "../nodeFinders" ;
21
+ import {
22
+ delimitedSelector ,
23
+ selectWithLeadingDelimiter ,
24
+ } from "../nodeSelectors" ;
12
25
13
26
// TODO figure out how to properly use super types
14
27
// Generated by the following command:
@@ -113,10 +126,10 @@ const ARGUMENT_TYPES = [
113
126
"keyword_argument" ,
114
127
] ;
115
128
116
- function possiblyDecoratedDefinition ( ...typeNames : string [ ] ) : NodeMatcher {
117
- return possiblyWrappedNode (
118
- ( node ) => node . type === "decorated_definition" ,
119
- ( node ) => typeNames . includes ( node . type ) ,
129
+ function possiblyDecoratedDefinition ( ...typeNames : string [ ] ) : NodeFinder {
130
+ return findPossiblyWrappedNode (
131
+ typedNodeFinder ( "decorated_definition" ) ,
132
+ typedNodeFinder ( ... typeNames ) ,
120
133
( node ) => [ getDefinitionNode ( node ) ]
121
134
) ;
122
135
}
@@ -130,23 +143,39 @@ const nodeMatchers: Record<ScopeType, NodeMatcher> = {
130
143
[ "list" , "list_comprehension" ] ,
131
144
( node ) => LIST_ELEMENT_TYPES . includes ( node . type )
132
145
) ,
133
- ifStatement : hasType ( "if_statement" ) ,
134
- class : possiblyDecoratedDefinition ( "class_definition" ) ,
135
- statement : hasType ( ...STATEMENT_TYPES ) ,
136
- arrowFunction : hasType ( "lambda" ) ,
137
- functionCall : hasType ( "call" ) ,
138
- argumentOrParameter : delimitedMatcher (
139
- ( node ) =>
140
- ( node . parent ?. type === "argument_list" &&
141
- ARGUMENT_TYPES . includes ( node . type ) ) ||
142
- ( PARAMETER_LIST_TYPES . includes ( node . parent ?. type ?? "" ) &&
143
- PARAMETER_TYPES . includes ( node . type ) ) ,
144
- ( node ) => node . type === "," || node . type === "(" || node . type === ")" ,
145
- ", "
146
+ ifStatement : typeMatcher ( "if_statement" ) ,
147
+ class : matcher ( possiblyDecoratedDefinition ( "class_definition" ) ) ,
148
+ statement : typeMatcher ( ...STATEMENT_TYPES ) ,
149
+ name : cascadingMatcher (
150
+ matcher ( getNameNode ) ,
151
+ matcher ( ( node ) => ( node . type === "assignment" ? getLeftNode ( node ) : null ) )
152
+ ) ,
153
+ functionName : composedMatcher ( [
154
+ typedNodeFinder ( "function_definition" ) ,
155
+ getNameNode ,
156
+ ] ) ,
157
+ className : composedMatcher ( [
158
+ typedNodeFinder ( "class_definition" ) ,
159
+ getNameNode ,
160
+ ] ) ,
161
+ arrowFunction : typeMatcher ( "lambda" ) ,
162
+ functionCall : typeMatcher ( "call" ) ,
163
+ argumentOrParameter : matcher (
164
+ nodeFinder (
165
+ ( node ) =>
166
+ ( node . parent ?. type === "argument_list" &&
167
+ ARGUMENT_TYPES . includes ( node . type ) ) ||
168
+ ( PARAMETER_LIST_TYPES . includes ( node . parent ?. type ?? "" ) &&
169
+ PARAMETER_TYPES . includes ( node . type ) )
170
+ ) ,
171
+ delimitedSelector (
172
+ ( node ) => node . type === "," || node . type === "(" || node . type === ")" ,
173
+ ", "
174
+ )
146
175
) ,
147
- namedFunction : possiblyDecoratedDefinition ( "function_definition" ) ,
148
- comment : hasType ( "comment" ) ,
149
- type : childNodeMatcher ( getTypeNode , getNodeWithLeadingDelimiter ) ,
176
+ namedFunction : matcher ( possiblyDecoratedDefinition ( "function_definition" ) ) ,
177
+ comment : typeMatcher ( "comment" ) ,
178
+ type : matcher ( getTypeNode , selectWithLeadingDelimiter ) ,
150
179
} ;
151
180
152
181
export default nodeMatchers ;
0 commit comments