@@ -37,7 +37,7 @@ export async function findFunctionRanges(filepath: string, node: Parser.SyntaxNo
3737 if ( ! querySource ) {
3838 return [ ] ;
3939 }
40-
40+
4141 const extension = filepath . split ( '.' ) . pop ( ) || '' ;
4242 let query : Parser . Query | undefined = functionCache . get ( extension ) ;
4343 if ( ! query ) {
@@ -46,17 +46,18 @@ export async function findFunctionRanges(filepath: string, node: Parser.SyntaxNo
4646 }
4747 const matches = query ?. matches ( node ) ;
4848
49- return (
50- matches ?. flatMap ( ( match ) => {
49+ const functionRanges : FunctionRange [ ] = [ ] ;
50+ if ( matches ) {
51+ for ( const match of matches ) {
5152 // find functionNode through tag name
5253 const functionNode = match . captures . find ( ( capture ) => capture . name === "function" ) ?. node ;
5354 const bodyNode = match . captures . find ( ( capture ) => capture . name === "function.body" ) ?. node ;
5455 const nameNode = match . captures . find ( ( capture ) => capture . name === "function.name" ) ?. node ;
55- if ( ! functionNode || ! bodyNode ) {
56- return [ ] ;
56+ if ( ! functionNode || ! bodyNode ) {
57+ continue ;
5758 }
5859
59- const results = {
60+ const functionRange : FunctionRange = {
6061 define : {
6162 start : {
6263 row : functionNode . startPosition . row ,
@@ -77,11 +78,26 @@ export async function findFunctionRanges(filepath: string, node: Parser.SyntaxNo
7778 column : bodyNode . endPosition . column ,
7879 } ,
7980 } ,
80- name : nameNode ?. text ?? "" ,
81+ name : nameNode ?. text ?? "" ,
8182 } ;
82- return results ;
83- } ) ?? [ ]
84- ) ;
83+
84+ // Check if this function range is not fully contained within another function range
85+ const isContained = functionRanges . some ( range => {
86+ return (
87+ range . define . start . row <= functionRange . define . start . row &&
88+ range . define . end . row >= functionRange . define . end . row &&
89+ range . body . start . row <= functionRange . body . start . row &&
90+ range . body . end . row >= functionRange . body . end . row
91+ ) ;
92+ } ) ;
93+
94+ if ( ! isContained ) {
95+ functionRanges . push ( functionRange ) ;
96+ }
97+ }
98+ }
99+
100+ return functionRanges ;
85101}
86102
87103export async function findFunctionNodes ( filepath : string , node : Parser . SyntaxNode ) : Promise < Parser . SyntaxNode [ ] > {
0 commit comments