MCPcopy Create free account
hub / github.com/cognosisai/platform / nodeToFunctionCodeFragment

Function nodeToFunctionCodeFragment

src/activities/application/codegrunt.ts:94–148  ·  view source on GitHub ↗
( node: babel.Node )

Source from the content-addressed store, hash-verified

92
93
94 function nodeToFunctionCodeFragment( node: babel.Node ): FunctionCodeFragment
95 {
96 if ( node.type == 'FunctionDeclaration' )
97 {
98
99 let fcf =
100 {
101 type: 'function',
102 name: node.id?.name ?? '',
103 params: new Array< string >(),
104 returnType: '',
105 start: {line: 0, column: 0},
106 end: {line: 0, column: 0},
107 code: '',
108 leadingComment: node.leadingComments?.map( (c) => c.value ).join( '\n' ) ?? '',
109 };
110
111 for ( let param of node.params )
112 {
113 param = <babel.types.Identifier> param;
114 let name_s = param.name
115 let i1 = (<any> (param.loc!.end)).index;
116 let i2 = (<any> (param.loc!.start)).index;
117 let type_s = code.substring( i2, i1 );
118 fcf.params.push( type_s );
119 }
120
121 let returnType = node.returnType!;
122 let i1 = (<any> (returnType.loc!.end)).index;
123 let i2 = (<any> (returnType.loc!.start)).index;
124 fcf.returnType = code.substring( i2, i1 );
125
126
127 let start = (<any> node.loc).start;
128 let end = (<any> node.loc).end;
129 fcf.code = code.substring( start!.index, end!.index );
130 let fcf2: FunctionCodeFragment = {
131 type: 'function',
132 name: fcf.name,
133 params: fcf.params,
134 returnType: fcf.returnType,
135 start: {line: start!.line, column: start!.column},
136 end: {line: end!.line, column: end!.column},
137 code: fcf.code,
138 leadingComment: fcf.leadingComment,
139 signature: `${node.async ? "async" : ""} function ${fcf.name}(${fcf.params.join( ', ' )}): ${fcf.returnType.replace( /^\s*:\s*/, '') }`,
140 };
141 return fcf2;
142 }
143 else
144 {
145 console.log( `Unexpected node type ${node.type}` );
146 throw new Error( 'PERMANENT - Unexpected node type' );
147 }
148 }
149
150 result?.program.body.forEach( async (node) => {
151 console.log( `${node.type} ${node.range}` )

Callers 1

extractFunctionsFunction · 0.85

Calls 1

logMethod · 0.80

Tested by

no test coverage detected