@@ -12,6 +12,12 @@ import {
1212 isTemplateHead ,
1313 isTemplateMiddle ,
1414 isTemplateTail ,
15+ isFunctionDeclaration ,
16+ isExpressionStatement ,
17+ isCallExpression ,
18+ isPropertyAccessExpression ,
19+ isIdentifier ,
20+ isStringLiteral ,
1521} from "@typescript/ast" ;
1622import assert from "node:assert" ;
1723import {
@@ -177,6 +183,45 @@ test("Dispose", () => {
177183 } ) ;
178184} ) ;
179185
186+ test ( "Function" , ( ) => {
187+ const currentFiles = {
188+ "/tsconfig.json" : "{}" ,
189+ "/src/index.ts" : `function foo() {
190+ console.log("hello", "world")
191+ }` ,
192+ } ;
193+
194+ let api = spawnAPI ( currentFiles ) ;
195+ const project = api . loadProject ( "/tsconfig.json" ) ;
196+ const sourceFile = project . getSourceFile ( "/src/index.ts" ) ;
197+
198+ let func = sourceFile ?. statements [ 0 ] ! ;
199+ assert . ok ( isFunctionDeclaration ( func ) ) ;
200+
201+ let body = func . body ! ;
202+ let expr = body . statements [ 0 ] ! ;
203+ assert . ok ( isExpressionStatement ( expr ) ) ;
204+ let call_expr = expr . expression ;
205+ assert . ok ( isCallExpression ( call_expr ) ) ;
206+
207+ let callee = call_expr . expression ;
208+ assert . ok ( isPropertyAccessExpression ( callee ) ) ;
209+ let left = callee . expression ;
210+ let right = callee . name ;
211+ assert . ok ( isIdentifier ( left ) ) ;
212+ assert . ok ( isIdentifier ( right ) ) ;
213+ assert . equal ( left . text , "console" ) ;
214+ assert . equal ( right . text , "log" ) ;
215+
216+ let args = call_expr . arguments ;
217+ let arg0 = args [ 0 ] ;
218+ let arg1 = args [ 1 ] ;
219+ assert . ok ( isStringLiteral ( arg0 ) ) ;
220+ assert . ok ( isStringLiteral ( arg1 ) ) ;
221+ assert . equal ( arg0 . text , "hello" ) ;
222+ assert . equal ( arg1 . text , "world" ) ;
223+ } ) ;
224+
180225test ( "Benchmarks" , async ( ) => {
181226 await runBenchmarks ( /*singleIteration*/ true ) ;
182227} ) ;
0 commit comments