frameworks.test.ts 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743
  1. import { describe, it, expect } from 'vitest';
  2. import type { FrameworkResolver, UnresolvedRef } from '../src/resolution/types';
  3. import type { Node } from '../src/types';
  4. describe('FrameworkResolver.extract interface', () => {
  5. it('extract() returns { nodes, references }', () => {
  6. const resolver: FrameworkResolver = {
  7. name: 'fake',
  8. detect: () => true,
  9. resolve: () => null,
  10. languages: ['python'],
  11. extract: (_filePath: string, _content: string) => ({
  12. nodes: [] as Node[],
  13. references: [] as UnresolvedRef[],
  14. }),
  15. };
  16. const result = resolver.extract!('foo.py', '');
  17. expect(result).toEqual({ nodes: [], references: [] });
  18. });
  19. });
  20. import { getApplicableFrameworks } from '../src/resolution/frameworks';
  21. import type { FrameworkResolver } from '../src/resolution/types';
  22. describe('getApplicableFrameworks', () => {
  23. const pyFw: FrameworkResolver = { name: 'py', languages: ['python'], detect: () => true, resolve: () => null };
  24. const jsFw: FrameworkResolver = { name: 'js', languages: ['javascript', 'typescript'], detect: () => true, resolve: () => null };
  25. const anyFw: FrameworkResolver = { name: 'any', detect: () => true, resolve: () => null };
  26. it('filters by language', () => {
  27. const result = getApplicableFrameworks([pyFw, jsFw, anyFw], 'python');
  28. expect(result.map(r => r.name)).toEqual(['py', 'any']);
  29. });
  30. it('returns anyFw-only when language has no matches', () => {
  31. const result = getApplicableFrameworks([pyFw, jsFw, anyFw], 'rust');
  32. expect(result.map(r => r.name)).toEqual(['any']);
  33. });
  34. });
  35. import { djangoResolver } from '../src/resolution/frameworks/python';
  36. describe('djangoResolver.extract', () => {
  37. it('extracts route node and reference for path() with CBV.as_view()', () => {
  38. const src = `
  39. from django.urls import path
  40. from users.views import UserListView
  41. urlpatterns = [
  42. path('users/', UserListView.as_view(), name='user-list'),
  43. ]
  44. `;
  45. const { nodes, references } = djangoResolver.extract!('users/urls.py', src);
  46. expect(nodes).toHaveLength(1);
  47. expect(nodes[0].kind).toBe('route');
  48. expect(nodes[0].name).toBe('users/');
  49. expect(references).toHaveLength(1);
  50. expect(references[0].referenceName).toBe('UserListView');
  51. expect(references[0].referenceKind).toBe('references');
  52. expect(references[0].fromNodeId).toBe(nodes[0].id);
  53. });
  54. it('extracts route for path() with dotted module.Class.as_view()', () => {
  55. const src = `from django.urls import path\nfrom api.v1 import views as api_v1_views\nurlpatterns = [path('api/', api_v1_views.UserListView.as_view())]\n`;
  56. const { nodes, references } = djangoResolver.extract!('api/urls.py', src);
  57. expect(nodes).toHaveLength(1);
  58. expect(references[0].referenceName).toBe('UserListView');
  59. });
  60. it('extracts route for path() with bare function view', () => {
  61. const src = `from django.urls import path\nurlpatterns = [path('home/', home_view, name='home')]\n`;
  62. const { nodes, references } = djangoResolver.extract!('home/urls.py', src);
  63. expect(references[0].referenceName).toBe('home_view');
  64. });
  65. it('extracts route for path() with include()', () => {
  66. const src = `from django.urls import path, include\nurlpatterns = [path('api/', include('api.urls'))]\n`;
  67. const { nodes, references } = djangoResolver.extract!('root/urls.py', src);
  68. expect(nodes).toHaveLength(1);
  69. expect(nodes[0].kind).toBe('route');
  70. expect(references[0].referenceName).toBe('api.urls');
  71. expect(references[0].referenceKind).toBe('imports');
  72. });
  73. it('extracts routes for re_path and url', () => {
  74. const src = `from django.urls import re_path, url\nurlpatterns = [re_path(r'^users/$', UserView), url(r'^old/$', OldView)]\n`;
  75. const { nodes } = djangoResolver.extract!('legacy/urls.py', src);
  76. expect(nodes).toHaveLength(2);
  77. expect(nodes.map(n => n.name)).toEqual(['^users/$', '^old/$']);
  78. });
  79. it('returns empty result for a non-urls.py python file', () => {
  80. const src = `def foo(): return 1\n`;
  81. const { nodes, references } = djangoResolver.extract!('views.py', src);
  82. expect(nodes).toEqual([]);
  83. expect(references).toEqual([]);
  84. });
  85. });
  86. import { flaskResolver, fastapiResolver } from '../src/resolution/frameworks/python';
  87. describe('flaskResolver.extract', () => {
  88. it('extracts route and reference from @app.route', () => {
  89. const src = `
  90. @app.route('/users')
  91. def list_users():
  92. return []
  93. `;
  94. const { nodes, references } = flaskResolver.extract!('app.py', src);
  95. expect(nodes).toHaveLength(1);
  96. expect(nodes[0].kind).toBe('route');
  97. expect(nodes[0].name).toBe('GET /users');
  98. expect(references[0].referenceName).toBe('list_users');
  99. });
  100. it('extracts blueprint routes', () => {
  101. const src = `
  102. @users_bp.route('/<id>', methods=['POST'])
  103. def create_user(id):
  104. pass
  105. `;
  106. const { nodes, references } = flaskResolver.extract!('routes.py', src);
  107. expect(nodes[0].name).toBe('POST /<id>');
  108. expect(references[0].referenceName).toBe('create_user');
  109. });
  110. it('resolves the handler across an intervening decorator (@login_required)', () => {
  111. const src = `
  112. @bp.route('/profile')
  113. @login_required
  114. def profile():
  115. return render_template('profile.html')
  116. `;
  117. const { nodes, references } = flaskResolver.extract!('routes.py', src);
  118. expect(nodes[0].name).toBe('GET /profile');
  119. expect(references[0].referenceName).toBe('profile');
  120. });
  121. it('extracts stacked @x.route decorators bound to one view', () => {
  122. const src = `
  123. @bp.route('/', methods=['GET', 'POST'])
  124. @bp.route('/index', methods=['GET', 'POST'])
  125. @login_required
  126. def index():
  127. return render_template('index.html')
  128. `;
  129. const { nodes, references } = flaskResolver.extract!('routes.py', src);
  130. expect(nodes.map((n) => n.name)).toEqual(['GET /', 'GET /index']);
  131. expect(references.map((r) => r.referenceName)).toEqual(['index', 'index']);
  132. });
  133. it('extracts the method from a tuple methods=(...) (not just a list)', () => {
  134. const src = `
  135. @blueprint.route('/api/articles', methods=('POST',))
  136. def make_article():
  137. pass
  138. `;
  139. const { nodes, references } = flaskResolver.extract!('views.py', src);
  140. expect(nodes[0].name).toBe('POST /api/articles');
  141. expect(references[0].referenceName).toBe('make_article');
  142. });
  143. it('extracts Flask-RESTful api.add_resource(Resource, paths) → the Resource class', () => {
  144. const src = `
  145. api.add_resource(TodoResource, '/todos/<id>')
  146. api.add_org_resource(AlertResource, '/api/alerts/<id>', endpoint='alert')
  147. `;
  148. const { nodes, references } = flaskResolver.extract!('api.py', src);
  149. expect(nodes.map((n) => n.name)).toEqual(['ANY /todos/<id>', 'ANY /api/alerts/<id>']);
  150. expect(references.map((r) => r.referenceName)).toEqual(['TodoResource', 'AlertResource']);
  151. });
  152. });
  153. describe('fastapiResolver.extract', () => {
  154. it('extracts route and reference from @app.get', () => {
  155. const src = `
  156. @app.get('/users')
  157. async def list_users():
  158. return []
  159. `;
  160. const { nodes, references } = fastapiResolver.extract!('main.py', src);
  161. expect(nodes[0].name).toBe('GET /users');
  162. expect(references[0].referenceName).toBe('list_users');
  163. });
  164. it('extracts route from router.post', () => {
  165. const src = `
  166. @router.post('/items')
  167. def create_item(item: Item):
  168. pass
  169. `;
  170. const { nodes, references } = fastapiResolver.extract!('items.py', src);
  171. expect(nodes[0].name).toBe('POST /items');
  172. expect(references[0].referenceName).toBe('create_item');
  173. });
  174. it('extracts a route mounted at the router/prefix root (empty path)', () => {
  175. const src = `
  176. @router.get("", response_model=ListOfArticles, name="articles:list")
  177. async def list_articles():
  178. return []
  179. `;
  180. const { nodes, references } = fastapiResolver.extract!('articles.py', src);
  181. expect(nodes[0].name).toBe('GET /');
  182. expect(references[0].referenceName).toBe('list_articles');
  183. });
  184. it('extracts a multi-line decorator with an empty path', () => {
  185. const src = `
  186. @router.post(
  187. "",
  188. status_code=201,
  189. response_model=ArticleInResponse,
  190. )
  191. async def create_article():
  192. pass
  193. `;
  194. const { nodes, references } = fastapiResolver.extract!('articles.py', src);
  195. expect(nodes[0].name).toBe('POST /');
  196. expect(references[0].referenceName).toBe('create_article');
  197. });
  198. });
  199. import { expressResolver } from '../src/resolution/frameworks/express';
  200. describe('expressResolver.extract', () => {
  201. it('extracts route with inline handler reference', () => {
  202. const src = `app.get('/users', listUsers);\n`;
  203. const { nodes, references } = expressResolver.extract!('routes.ts', src);
  204. expect(nodes).toHaveLength(1);
  205. expect(nodes[0].name).toBe('GET /users');
  206. expect(references[0].referenceName).toBe('listUsers');
  207. });
  208. it('extracts route with router.post and middleware chain', () => {
  209. const src = `router.post('/items', auth, createItem);\n`;
  210. const { nodes, references } = expressResolver.extract!('items.ts', src);
  211. expect(nodes[0].name).toBe('POST /items');
  212. // Multiple handlers: prefer the LAST one (convention: middleware first, handler last)
  213. expect(references[0].referenceName).toBe('createItem');
  214. });
  215. it('extracts route with controller method reference', () => {
  216. const src = `app.get('/x', userController.list);\n`;
  217. const { nodes, references } = expressResolver.extract!('routes.ts', src);
  218. expect(references[0].referenceName).toBe('list');
  219. });
  220. });
  221. import { nestjsResolver } from '../src/resolution/frameworks/nestjs';
  222. describe('nestjsResolver.extract — HTTP', () => {
  223. it('joins @Controller prefix with @Get and links the handler', () => {
  224. const src = `
  225. @Controller('users')
  226. export class UsersController {
  227. @Get()
  228. findAll() { return []; }
  229. }
  230. `;
  231. const { nodes, references } = nestjsResolver.extract!('users.controller.ts', src);
  232. expect(nodes).toHaveLength(1);
  233. expect(nodes[0].kind).toBe('route');
  234. expect(nodes[0].name).toBe('GET /users');
  235. expect(references[0].referenceName).toBe('findAll');
  236. expect(references[0].referenceKind).toBe('references');
  237. expect(references[0].fromNodeId).toBe(nodes[0].id);
  238. });
  239. it('joins controller prefix with a method-level path param', () => {
  240. const src = `
  241. @Controller('cats')
  242. export class CatsController {
  243. @Get(':id')
  244. findOne(@Param('id') id: string) { return id; }
  245. }
  246. `;
  247. const { nodes, references } = nestjsResolver.extract!('cats.controller.ts', src);
  248. expect(nodes[0].name).toBe('GET /cats/:id');
  249. expect(references[0].referenceName).toBe('findOne');
  250. });
  251. it('handles an empty @Controller() and empty @Post()', () => {
  252. const src = `
  253. @Controller()
  254. export class AppController {
  255. @Post()
  256. create() {}
  257. }
  258. `;
  259. const { nodes, references } = nestjsResolver.extract!('app.controller.ts', src);
  260. expect(nodes[0].name).toBe('POST /');
  261. expect(references[0].referenceName).toBe('create');
  262. });
  263. it('covers HTTP verbs and skips intervening method decorators', () => {
  264. const src = `
  265. @Controller('todos')
  266. export class TodosController {
  267. @Put(':id')
  268. @UseGuards(AuthGuard)
  269. update(@Param('id') id: string) {}
  270. @Delete(':id')
  271. async remove(@Param('id') id: string) {}
  272. }
  273. `;
  274. const { nodes, references } = nestjsResolver.extract!('todos.controller.ts', src);
  275. expect(nodes.map((n) => n.name)).toEqual(['PUT /todos/:id', 'DELETE /todos/:id']);
  276. expect(references.map((r) => r.referenceName)).toEqual(['update', 'remove']);
  277. });
  278. it('attributes methods to the right controller when a file has two', () => {
  279. const src = `
  280. @Controller('a')
  281. export class AController {
  282. @Get('x')
  283. ax() {}
  284. }
  285. @Controller('b')
  286. export class BController {
  287. @Get('y')
  288. by() {}
  289. }
  290. `;
  291. const { nodes } = nestjsResolver.extract!('multi.controller.ts', src);
  292. expect(nodes.map((n) => n.name)).toEqual(['GET /a/x', 'GET /b/y']);
  293. });
  294. });
  295. describe('nestjsResolver.extract — GraphQL', () => {
  296. it('emits QUERY/MUTATION nodes from a resolver, defaulting to the method name', () => {
  297. const src = `
  298. @Resolver(() => User)
  299. export class UsersResolver {
  300. @Query(() => [User])
  301. users() { return []; }
  302. @Mutation(() => User)
  303. createUser(@Args('input') input: CreateUserInput) {}
  304. }
  305. `;
  306. const { nodes, references } = nestjsResolver.extract!('users.resolver.ts', src);
  307. expect(nodes.map((n) => n.name)).toEqual(['QUERY users', 'MUTATION createUser']);
  308. expect(references.map((r) => r.referenceName)).toEqual(['users', 'createUser']);
  309. });
  310. it('uses an explicit operation name when given', () => {
  311. const src = `
  312. @Resolver()
  313. export class CatsResolver {
  314. @Query(() => Cat, { name: 'cat' })
  315. getCat() {}
  316. }
  317. `;
  318. const { nodes } = nestjsResolver.extract!('cats.resolver.ts', src);
  319. expect(nodes[0].name).toBe('QUERY cat');
  320. });
  321. it('does NOT treat the REST @Query() parameter decorator as a GraphQL op', () => {
  322. const src = `
  323. @Controller('search')
  324. export class SearchController {
  325. @Get()
  326. search(@Query() query: SearchDto) { return query; }
  327. }
  328. `;
  329. const { nodes } = nestjsResolver.extract!('search.controller.ts', src);
  330. // Only the HTTP route — the @Query() param decorator must be ignored.
  331. expect(nodes.map((n) => n.name)).toEqual(['GET /search']);
  332. });
  333. });
  334. describe('nestjsResolver.extract — microservices & websockets', () => {
  335. it('extracts @MessagePattern and @EventPattern handlers', () => {
  336. const src = `
  337. @Controller()
  338. export class MathController {
  339. @MessagePattern({ cmd: 'sum' })
  340. accumulate(data: number[]) {}
  341. @EventPattern('user.created')
  342. handleUserCreated(data: any) {}
  343. }
  344. `;
  345. const { nodes, references } = nestjsResolver.extract!('math.controller.ts', src);
  346. expect(nodes.map((n) => n.name)).toEqual(['MESSAGE sum', 'EVENT user.created']);
  347. expect(references.map((r) => r.referenceName)).toEqual(['accumulate', 'handleUserCreated']);
  348. });
  349. it('extracts @SubscribeMessage handlers with the gateway namespace', () => {
  350. const src = `
  351. @WebSocketGateway({ namespace: 'chat' })
  352. export class ChatGateway {
  353. @SubscribeMessage('message')
  354. handleMessage(@MessageBody() data: string) {}
  355. }
  356. `;
  357. const { nodes, references } = nestjsResolver.extract!('chat.gateway.ts', src);
  358. expect(nodes[0].name).toBe('WS chat:message');
  359. expect(references[0].referenceName).toBe('handleMessage');
  360. });
  361. it('extracts @SubscribeMessage without a namespace', () => {
  362. const src = `
  363. @WebSocketGateway()
  364. export class EventsGateway {
  365. @SubscribeMessage('events')
  366. onEvent() {}
  367. }
  368. `;
  369. const { nodes } = nestjsResolver.extract!('events.gateway.ts', src);
  370. expect(nodes[0].name).toBe('WS events');
  371. });
  372. it('returns empty for a non-JS/TS file', () => {
  373. const { nodes, references } = nestjsResolver.extract!('thing.py', '@Controller("x")');
  374. expect(nodes).toEqual([]);
  375. expect(references).toEqual([]);
  376. });
  377. });
  378. describe('nestjsResolver.detect', () => {
  379. const baseContext = {
  380. getNodesInFile: () => [],
  381. getNodesByName: () => [],
  382. getNodesByQualifiedName: () => [],
  383. getNodesByKind: () => [],
  384. fileExists: () => false,
  385. getProjectRoot: () => '/test',
  386. getAllFiles: () => [],
  387. getNodesByLowerName: () => [],
  388. getImportMappings: () => [],
  389. };
  390. it('detects @nestjs/* in package.json', () => {
  391. const context = {
  392. ...baseContext,
  393. readFile: (p: string) =>
  394. p === 'package.json'
  395. ? JSON.stringify({ dependencies: { '@nestjs/common': '^10.0.0' } })
  396. : null,
  397. };
  398. expect(nestjsResolver.detect(context as any)).toBe(true);
  399. });
  400. it('detects @Controller in a *.controller.ts file when package.json is absent', () => {
  401. const context = {
  402. ...baseContext,
  403. getAllFiles: () => ['src/users.controller.ts'],
  404. readFile: (p: string) =>
  405. p === 'src/users.controller.ts'
  406. ? `@Controller('users')\nexport class UsersController {}`
  407. : null,
  408. };
  409. expect(nestjsResolver.detect(context as any)).toBe(true);
  410. });
  411. it('returns false for a non-Nest project', () => {
  412. const context = {
  413. ...baseContext,
  414. readFile: (p: string) =>
  415. p === 'package.json' ? JSON.stringify({ dependencies: { express: '^4' } }) : null,
  416. };
  417. expect(nestjsResolver.detect(context as any)).toBe(false);
  418. });
  419. });
  420. describe('nestjsResolver.resolve', () => {
  421. const baseContext = {
  422. getNodesInFile: () => [],
  423. getNodesByName: () => [],
  424. getNodesByQualifiedName: () => [],
  425. getNodesByKind: () => [],
  426. fileExists: () => false,
  427. readFile: () => null,
  428. getProjectRoot: () => '/test',
  429. getAllFiles: () => [],
  430. getNodesByLowerName: () => [],
  431. getImportMappings: () => [],
  432. };
  433. it('resolves an injected *Service reference to the class in a *.service.ts file', () => {
  434. const svcNode: Node = {
  435. id: 'class:src/users/users.service.ts:UsersService:3',
  436. kind: 'class',
  437. name: 'UsersService',
  438. qualifiedName: 'src/users/users.service.ts::UsersService',
  439. filePath: 'src/users/users.service.ts',
  440. language: 'typescript',
  441. startLine: 3,
  442. endLine: 3,
  443. startColumn: 0,
  444. endColumn: 0,
  445. updatedAt: Date.now(),
  446. };
  447. const context = {
  448. ...baseContext,
  449. getNodesByName: (n: string) => (n === 'UsersService' ? [svcNode] : []),
  450. };
  451. const ref = {
  452. fromNodeId: 'class:src/users/users.controller.ts:UsersController:5',
  453. referenceName: 'UsersService',
  454. referenceKind: 'references' as const,
  455. line: 6,
  456. column: 4,
  457. filePath: 'src/users/users.controller.ts',
  458. language: 'typescript' as const,
  459. };
  460. const result = nestjsResolver.resolve(ref, context as any);
  461. expect(result?.targetNodeId).toBe(svcNode.id);
  462. expect(result?.resolvedBy).toBe('framework');
  463. expect(result?.confidence).toBeGreaterThanOrEqual(0.85);
  464. });
  465. it('returns null for a name without a provider suffix', () => {
  466. const ref = {
  467. fromNodeId: 'x',
  468. referenceName: 'doThing',
  469. referenceKind: 'references' as const,
  470. line: 1,
  471. column: 1,
  472. filePath: 'a.ts',
  473. language: 'typescript' as const,
  474. };
  475. expect(nestjsResolver.resolve(ref, baseContext as any)).toBeNull();
  476. });
  477. });
  478. describe('nestjsResolver.postExtract — RouterModule', () => {
  479. function mkClass(name: string, filePath: string, startLine: number, endLine: number): Node {
  480. return {
  481. id: `class:${filePath}:${startLine}:${name}`,
  482. kind: 'class',
  483. name,
  484. qualifiedName: `${filePath}::${name}`,
  485. filePath,
  486. language: 'typescript',
  487. startLine,
  488. endLine,
  489. startColumn: 0,
  490. endColumn: 0,
  491. updatedAt: 0,
  492. };
  493. }
  494. function mkRoute(
  495. filePath: string,
  496. line: number,
  497. method: string,
  498. path: string,
  499. nameOverride?: string
  500. ): Node {
  501. return {
  502. id: `route:${filePath}:${line}:${method}:${path}`,
  503. kind: 'route',
  504. name: nameOverride ?? `${method} ${path}`,
  505. qualifiedName: `${filePath}::${method}:${path}`,
  506. filePath,
  507. language: 'typescript',
  508. startLine: line,
  509. endLine: line,
  510. startColumn: 0,
  511. endColumn: 0,
  512. updatedAt: 0,
  513. };
  514. }
  515. function makeContext(opts: {
  516. files?: Record<string, string>;
  517. nodes?: Node[];
  518. }) {
  519. const files = opts.files ?? {};
  520. const all = opts.nodes ?? [];
  521. return {
  522. getNodesInFile: (fp: string) => all.filter((n) => n.filePath === fp),
  523. getNodesByName: (name: string) => all.filter((n) => n.name === name),
  524. getNodesByQualifiedName: () => [],
  525. getNodesByKind: (kind: Node['kind']) => all.filter((n) => n.kind === kind),
  526. fileExists: (fp: string) => files[fp] !== undefined,
  527. readFile: (fp: string) => files[fp] ?? null,
  528. getProjectRoot: () => '/test',
  529. getAllFiles: () => Object.keys(files),
  530. getNodesByLowerName: () => [],
  531. getImportMappings: () => [],
  532. } as any;
  533. }
  534. it('prepends RouterModule prefix to a controller route (top-level register)', () => {
  535. const ctx = makeContext({
  536. files: {
  537. 'src/app.module.ts': `
  538. @Module({
  539. imports: [
  540. RouterModule.register([
  541. { path: 'admin', module: AdminModule },
  542. ]),
  543. ],
  544. })
  545. export class AppModule {}
  546. @Module({ controllers: [AdminController] })
  547. export class AdminModule {}
  548. `,
  549. },
  550. nodes: [
  551. mkClass('AdminController', 'src/admin/admin.controller.ts', 1, 10),
  552. mkRoute('src/admin/admin.controller.ts', 3, 'GET', '/'),
  553. ],
  554. });
  555. const updates = nestjsResolver.postExtract!(ctx);
  556. expect(updates).toHaveLength(1);
  557. expect(updates[0]!.name).toBe('GET /admin');
  558. // id and qualifiedName must be preserved so existing route→handler edges
  559. // stay intact and the pass remains idempotent on a second run.
  560. expect(updates[0]!.id).toBe('route:src/admin/admin.controller.ts:3:GET:/');
  561. expect(updates[0]!.qualifiedName).toBe('src/admin/admin.controller.ts::GET:/');
  562. });
  563. it('resolves nested children — the issue #459 example', () => {
  564. const ctx = makeContext({
  565. files: {
  566. 'src/app.module.ts': `
  567. @Module({
  568. imports: [
  569. AdminModule,
  570. UsersModule,
  571. RouterModule.register([
  572. {
  573. path: 'admin',
  574. module: AdminModule,
  575. children: [
  576. { path: 'users', module: UsersModule },
  577. ],
  578. },
  579. ]),
  580. ],
  581. })
  582. export class AppModule {}
  583. `,
  584. 'src/users/users.module.ts': `
  585. @Module({ controllers: [UsersController] })
  586. export class UsersModule {}
  587. `,
  588. },
  589. nodes: [
  590. mkClass('UsersController', 'src/users/users.controller.ts', 1, 10),
  591. mkRoute('src/users/users.controller.ts', 3, 'GET', '/'),
  592. ],
  593. });
  594. const updates = nestjsResolver.postExtract!(ctx);
  595. expect(updates).toHaveLength(1);
  596. expect(updates[0]!.name).toBe('GET /admin/users');
  597. });
  598. it('joins module prefix with a non-empty @Controller path and method params', () => {
  599. const ctx = makeContext({
  600. files: {
  601. 'src/app.module.ts': `
  602. RouterModule.register([{ path: 'admin', module: UsersModule }])
  603. @Module({ controllers: [UsersController] })
  604. export class UsersModule {}
  605. `,
  606. },
  607. nodes: [
  608. mkClass('UsersController', 'src/users.controller.ts', 1, 10),
  609. // Existing extract emitted GET /users/:id from @Controller('users') + @Get(':id')
  610. mkRoute('src/users.controller.ts', 3, 'GET', '/users/:id'),
  611. ],
  612. });
  613. const updates = nestjsResolver.postExtract!(ctx);
  614. expect(updates).toHaveLength(1);
  615. expect(updates[0]!.name).toBe('GET /admin/users/:id');
  616. });
  617. it('is idempotent — a second run returns no updates', () => {
  618. // Simulate the state after one round of postExtract: name is already
  619. // 'GET /admin', but qualifiedName still encodes the original 'GET:/'.
  620. const ctx = makeContext({
  621. files: {
  622. 'src/app.module.ts': `
  623. RouterModule.register([{ path: 'admin', module: UsersModule }])
  624. @Module({ controllers: [UsersController] })
  625. export class UsersModule {}
  626. `,
  627. },
  628. nodes: [
  629. mkClass('UsersController', 'src/users.controller.ts', 1, 10),
  630. mkRoute('src/users.controller.ts', 3, 'GET', '/', 'GET /admin'),
  631. ],
  632. });
  633. const updates = nestjsResolver.postExtract!(ctx);
  634. expect(updates).toHaveLength(0);
  635. });
  636. it('is a no-op when the project does not use RouterModule', () => {
  637. const ctx = makeContext({
  638. files: {
  639. 'src/app.module.ts': `
  640. @Module({ controllers: [UsersController] })
  641. export class AppModule {}
  642. `,
  643. },
  644. nodes: [
  645. mkClass('UsersController', 'src/users.controller.ts', 1, 10),
  646. mkRoute('src/users.controller.ts', 3, 'GET', '/'),
  647. ],
  648. });
  649. const updates = nestjsResolver.postExtract!(ctx);
  650. expect(updates).toHaveLength(0);
  651. });
  652. it('attributes routes to the right controller when one file has two', () => {
  653. // Two controllers in one file, declared in two different modules with
  654. // two different module prefixes. The route's startLine has to match the
  655. // class scope, not just the file path.
  656. const ctx = makeContext({
  657. files: {
  658. 'src/app.module.ts': `
  659. RouterModule.register([
  660. { path: 'p1', module: AModule },
  661. { path: 'p2', module: BModule },
  662. ])
  663. @Module({ controllers: [AController] }) export class AModule {}
  664. @Module({ controllers: [BController] }) export class BModule {}
  665. `,
  666. },
  667. nodes: [
  668. mkClass('AController', 'src/multi.controller.ts', 1, 5),
  669. mkClass('BController', 'src/multi.controller.ts', 7, 12),
  670. mkRoute('src/multi.controller.ts', 3, 'GET', '/a/x'),
  671. mkRoute('src/multi.controller.ts', 9, 'GET', '/b/y'),
  672. ],
  673. });
  674. const updates = nestjsResolver.postExtract!(ctx);
  675. expect(updates).toHaveLength(2);
  676. const byId = new Map(updates.map((u) => [u.id, u.name]));
  677. expect(byId.get('route:src/multi.controller.ts:3:GET:/a/x')).toBe('GET /p1/a/x');
  678. expect(byId.get('route:src/multi.controller.ts:9:GET:/b/y')).toBe('GET /p2/b/y');
  679. });
  680. it('merges RouterModule registrations spread across multiple module files', () => {
  681. const ctx = makeContext({
  682. files: {
  683. 'src/app.module.ts': `
  684. RouterModule.register([{ path: 'a', module: AModule }])
  685. @Module({ controllers: [AController] }) export class AModule {}
  686. `,
  687. 'src/feature.module.ts': `
  688. RouterModule.forChild([{ path: 'b', module: BModule }])
  689. @Module({ controllers: [BController] }) export class BModule {}
  690. `,
  691. },
  692. nodes: [
  693. mkClass('AController', 'src/a.controller.ts', 1, 5),
  694. mkClass('BController', 'src/b.controller.ts', 1, 5),
  695. mkRoute('src/a.controller.ts', 3, 'GET', '/'),
  696. mkRoute('src/b.controller.ts', 3, 'GET', '/'),
  697. ],
  698. });
  699. const updates = nestjsResolver.postExtract!(ctx);
  700. expect(updates).toHaveLength(2);
  701. const byId = new Map(updates.map((u) => [u.id, u.name]));
  702. expect(byId.get('route:src/a.controller.ts:3:GET:/')).toBe('GET /a');
  703. expect(byId.get('route:src/b.controller.ts:3:GET:/')).toBe('GET /b');
  704. });
  705. it('silently skips controllers whose class node is not in the graph', () => {
  706. // RouterModule declares a prefix for a module, but the @Module that
  707. // would link it to a controller is missing — common during partial
  708. // re-extraction. Must not throw.
  709. const ctx = makeContext({
  710. files: {
  711. 'src/app.module.ts': `
  712. RouterModule.register([{ path: 'orphans', module: GhostModule }])
  713. @Module({ controllers: [GhostController] }) export class GhostModule {}
  714. `,
  715. },
  716. nodes: [], // no class or route nodes
  717. });
  718. const updates = nestjsResolver.postExtract!(ctx);
  719. expect(updates).toHaveLength(0);
  720. });
  721. });
  722. import { laravelResolver } from '../src/resolution/frameworks/laravel';
  723. describe('laravelResolver.extract', () => {
  724. it('extracts route with controller tuple syntax', () => {
  725. const src = `Route::get('/users', [UserController::class, 'index']);\n`;
  726. const { nodes, references } = laravelResolver.extract!('routes/web.php', src);
  727. expect(nodes[0].name).toBe('GET /users');
  728. expect(references[0].referenceName).toBe('UserController@index');
  729. });
  730. it('extracts route with Controller@action syntax', () => {
  731. const src = `Route::post('/users', 'UserController@store');\n`;
  732. const { nodes, references } = laravelResolver.extract!('routes/web.php', src);
  733. expect(references[0].referenceName).toBe('UserController@store');
  734. });
  735. it('extracts resource route', () => {
  736. const src = `Route::resource('users', UserController::class);\n`;
  737. const { nodes, references } = laravelResolver.extract!('routes/web.php', src);
  738. expect(nodes[0].kind).toBe('route');
  739. expect(references[0].referenceName).toBe('UserController');
  740. });
  741. });
  742. import { railsResolver } from '../src/resolution/frameworks/ruby';
  743. describe('railsResolver.extract', () => {
  744. it('extracts route with controller#action syntax', () => {
  745. const src = `get '/users', to: 'users#index'\n`;
  746. const { nodes, references } = railsResolver.extract!('config/routes.rb', src);
  747. expect(nodes[0].name).toBe('GET /users');
  748. expect(references[0].referenceName).toBe('users#index');
  749. });
  750. it('extracts route without to: keyword', () => {
  751. const src = `post '/items' => 'items#create'\n`;
  752. const { nodes, references } = railsResolver.extract!('config/routes.rb', src);
  753. expect(references[0].referenceName).toBe('items#create');
  754. });
  755. });
  756. import { springResolver } from '../src/resolution/frameworks/java';
  757. describe('springResolver.extract', () => {
  758. it('extracts route with @GetMapping and next method', () => {
  759. const src = `
  760. @GetMapping("/users")
  761. public List<User> listUsers() {
  762. return users;
  763. }
  764. `;
  765. const { nodes, references } = springResolver.extract!('UserController.java', src);
  766. expect(nodes[0].name).toBe('GET /users');
  767. expect(references[0].referenceName).toBe('listUsers');
  768. });
  769. it('extracts a Kotlin @GetMapping with a fun handler', () => {
  770. const src = `
  771. @GetMapping("/vets")
  772. fun showVetList(model: MutableMap<String, Any>): String {
  773. return "vets"
  774. }
  775. `;
  776. const { nodes, references } = springResolver.extract!('VetController.kt', src);
  777. expect(nodes[0].name).toBe('GET /vets');
  778. expect(references[0].referenceName).toBe('showVetList');
  779. expect(nodes[0].language).toBe('kotlin');
  780. });
  781. it('joins a Kotlin class @RequestMapping prefix and skips a stacked annotation', () => {
  782. const src = `
  783. @RestController
  784. @RequestMapping("/owners")
  785. class OwnerController {
  786. @GetMapping("/{ownerId}")
  787. @ResponseBody
  788. fun showOwner(@PathVariable ownerId: Int): String {
  789. return "owner"
  790. }
  791. }
  792. `;
  793. const { nodes, references } = springResolver.extract!('OwnerController.kt', src);
  794. expect(nodes[0].name).toBe('GET /owners/{ownerId}');
  795. expect(references[0].referenceName).toBe('showOwner');
  796. });
  797. });
  798. import { playResolver } from '../src/resolution/frameworks/play';
  799. import { isSourceFile, isPlayRoutesFile } from '../src/extraction/grammars';
  800. describe('playResolver.extract (conf/routes)', () => {
  801. it('extracts METHOD /path Controller.action routes, dropping the package + args', () => {
  802. const src = `# Routes
  803. GET / controllers.Application.index
  804. GET /computers controllers.Application.list(p: Int ?= 0, s: Int ?= 2)
  805. POST /computers controllers.Application.save
  806. -> /v1/posts v1.post.PostRouter
  807. `;
  808. const { nodes, references } = playResolver.extract!('conf/routes', src);
  809. expect(nodes.map((n) => n.name)).toEqual([
  810. 'GET /',
  811. 'GET /computers',
  812. 'POST /computers',
  813. ]); // the `->` include is skipped
  814. expect(references.map((r) => r.referenceName)).toEqual([
  815. 'Application.index',
  816. 'Application.list',
  817. 'Application.save',
  818. ]);
  819. });
  820. it('only runs on Play routes files', () => {
  821. expect(playResolver.extract!('app/Foo.scala', 'GET / controllers.X.y').nodes).toHaveLength(0);
  822. });
  823. });
  824. describe('Play routes file detection', () => {
  825. it('recognizes conf/routes (extensionless) and *.routes as source files', () => {
  826. expect(isPlayRoutesFile('conf/routes')).toBe(true);
  827. expect(isPlayRoutesFile('myapp/conf/routes')).toBe(true);
  828. expect(isPlayRoutesFile('conf/admin.routes')).toBe(true);
  829. expect(isSourceFile('conf/routes')).toBe(true);
  830. expect(isPlayRoutesFile('src/routes.ts')).toBe(false);
  831. });
  832. });
  833. import { goResolver } from '../src/resolution/frameworks/go';
  834. describe('goResolver.extract', () => {
  835. it('extracts route from r.GET', () => {
  836. const src = `r.GET("/users", listUsers)\n`;
  837. const { nodes, references } = goResolver.extract!('main.go', src);
  838. expect(nodes[0].name).toBe('GET /users');
  839. expect(references[0].referenceName).toBe('listUsers');
  840. });
  841. it('extracts route from router.HandleFunc', () => {
  842. const src = `router.HandleFunc("/items", createItem)\n`;
  843. const { nodes, references } = goResolver.extract!('main.go', src);
  844. expect(references[0].referenceName).toBe('createItem');
  845. });
  846. it('extracts gorilla/mux HandleFunc on a subrouter var, ignoring chained .Methods()', () => {
  847. // `s` is a PathPrefix().Subrouter() var — any receiver is matched; the
  848. // trailing .Methods("GET") doesn't break the handler capture.
  849. const src = `s.HandleFunc("/users/{id}", listUsers).Methods("GET")\n`;
  850. const { references } = goResolver.extract!('routes.go', src);
  851. expect(references[0].referenceName).toBe('listUsers');
  852. });
  853. });
  854. import { goframeResolver } from '../src/resolution/frameworks/goframe';
  855. describe('goframeResolver', () => {
  856. it('detects GoFrame from a gogf/gf dependency in go.mod', () => {
  857. const ctx: any = {
  858. readFile: (f: string) =>
  859. f === 'go.mod' ? 'module example.com/app\nrequire github.com/gogf/gf/v2 v2.7.0\n' : null,
  860. };
  861. expect(goframeResolver.detect(ctx)).toBe(true);
  862. const noGf: any = { readFile: (f: string) => (f === 'go.mod' ? 'module example.com/app\n' : null) };
  863. expect(goframeResolver.detect(noGf)).toBe(false);
  864. });
  865. it('extracts a route node from a g.Meta request struct (method upper-cased)', () => {
  866. const src = `package v1
  867. import "github.com/gogf/gf/v2/frame/g"
  868. type SignInReq struct {
  869. g.Meta \`path:"/user/sign-in" method:"post" tags:"User" summary:"Sign in"\`
  870. Passport string
  871. }
  872. type SignInRes struct{}
  873. `;
  874. const { nodes } = goframeResolver.extract!('api/user/v1/user_sign_in.go', src);
  875. expect(nodes).toHaveLength(1);
  876. expect(nodes[0].kind).toBe('route');
  877. expect(nodes[0].name).toBe('POST /user/sign-in');
  878. // The package-qualified request type is encoded for the synthesizer join.
  879. expect(nodes[0].qualifiedName).toContain('::goframe-route:v1.SignInReq');
  880. });
  881. it('is independent of g.Meta tag attribute order', () => {
  882. const src = `type DeptSearchReq struct {
  883. g.Meta \`path:"/dept/list" tags:"Dept" method:"get" summary:"列表"\`
  884. }`;
  885. const { nodes } = goframeResolver.extract!('api/system/dept.go', src);
  886. expect(nodes[0].name).toBe('GET /dept/list');
  887. expect(nodes[0].qualifiedName).toContain('::goframe-route:DeptSearchReq');
  888. });
  889. it('skips a response g.Meta that has no path (mime-only) and other non-route metadata', () => {
  890. const src = `type ListRes struct {
  891. g.Meta \`mime:"application/json"\`
  892. Items []string
  893. }`;
  894. const { nodes } = goframeResolver.extract!('api/x.go', src);
  895. expect(nodes).toHaveLength(0);
  896. });
  897. it('defaults method to ANY when method: is omitted', () => {
  898. const src = `type PingReq struct {
  899. g.Meta \`path:"/ping"\`
  900. }`;
  901. const { nodes } = goframeResolver.extract!('api/ping.go', src);
  902. expect(nodes[0].name).toBe('ANY /ping');
  903. });
  904. it('extracts every request struct in a multi-route api file', () => {
  905. const src = `type DeptListReq struct { g.Meta \`path:"/dept/list" method:"get"\` }
  906. type DeptListRes struct { g.Meta \`mime:"application/json"\` }
  907. type DeptAddReq struct { g.Meta \`path:"/dept/add" method:"post"\` }
  908. type DeptAddRes struct {}
  909. `;
  910. const { nodes } = goframeResolver.extract!('api/dept.go', src);
  911. expect(nodes.map((n) => n.name).sort()).toEqual(['GET /dept/list', 'POST /dept/add']);
  912. });
  913. it('returns nothing for a non-go file or a file without g.Meta', () => {
  914. expect(goframeResolver.extract!('main.ts', 'const x = 1').nodes).toHaveLength(0);
  915. expect(goframeResolver.extract!('main.go', 'package main\nfunc main() {}\n').nodes).toHaveLength(0);
  916. });
  917. });
  918. import { rustResolver } from '../src/resolution/frameworks/rust';
  919. describe('rustResolver.extract', () => {
  920. it('extracts route from axum .route with get()', () => {
  921. const src = `let app = Router::new().route("/users", get(list_users));\n`;
  922. const { nodes, references } = rustResolver.extract!('main.rs', src);
  923. expect(nodes[0].name).toBe('GET /users');
  924. expect(references[0].referenceName).toBe('list_users');
  925. });
  926. it('extracts every method from a chained axum .route (get().put())', () => {
  927. const src = `let app = Router::new().route("/user", get(get_current_user).put(update_user));\n`;
  928. const { nodes, references } = rustResolver.extract!('main.rs', src);
  929. expect(nodes.map((n) => n.name)).toEqual(['GET /user', 'PUT /user']);
  930. expect(references.map((r) => r.referenceName)).toEqual([
  931. 'get_current_user',
  932. 'update_user',
  933. ]);
  934. });
  935. it('extracts a multi-line axum .route with a namespaced handler', () => {
  936. const src = `
  937. let app = Router::new()
  938. .route(
  939. "/articles/feed",
  940. get(listing::feed_articles),
  941. );
  942. `;
  943. const { nodes, references } = rustResolver.extract!('main.rs', src);
  944. expect(nodes[0].name).toBe('GET /articles/feed');
  945. expect(references[0].referenceName).toBe('feed_articles');
  946. });
  947. it('extracts actix web::resource().route(web::METHOD().to(handler))', () => {
  948. const src = `App::new().service(web::resource("/user/{id}").route(web::get().to(get_user)))\n`;
  949. const { nodes, references } = rustResolver.extract!('main.rs', src);
  950. expect(nodes[0].name).toBe('GET /user/{id}');
  951. expect(references[0].referenceName).toBe('get_user');
  952. });
  953. it('extracts actix web::resource("/").to(handler) (all methods)', () => {
  954. const src = `App::new().service(web::resource("/").to(index))\n`;
  955. const { nodes, references } = rustResolver.extract!('main.rs', src);
  956. expect(nodes[0].name).toBe('ANY /');
  957. expect(references[0].referenceName).toBe('index');
  958. });
  959. it('extracts actix App-level .route("/path", web::METHOD().to(handler))', () => {
  960. const src = `App::new().route("/health", web::get().to(health_check))\n`;
  961. const { nodes, references } = rustResolver.extract!('main.rs', src);
  962. expect(nodes[0].name).toBe('GET /health');
  963. expect(references[0].referenceName).toBe('health_check');
  964. });
  965. });
  966. describe('rustResolver.resolve cargo workspace crates', () => {
  967. it('resolves crate name from workspace member lib.rs', () => {
  968. const workspaceCargo = `
  969. [workspace]
  970. members = ["crates/mytool-core", "crates/mytool-fetcher"]
  971. `;
  972. const coreCargo = `
  973. [package]
  974. name = "mytool-core"
  975. version = "0.1.0"
  976. `;
  977. const libNode: Node = {
  978. id: 'module:crates/mytool-core/src/lib.rs:mytool_core:1',
  979. kind: 'module',
  980. name: 'mytool_core',
  981. qualifiedName: 'crates/mytool-core/src/lib.rs::mytool_core',
  982. filePath: 'crates/mytool-core/src/lib.rs',
  983. language: 'rust',
  984. startLine: 1,
  985. endLine: 1,
  986. startColumn: 0,
  987. endColumn: 0,
  988. updatedAt: Date.now(),
  989. };
  990. const context = {
  991. getNodesInFile: (fp: string) => (fp === 'crates/mytool-core/src/lib.rs' ? [libNode] : []),
  992. getNodesByName: () => [],
  993. getNodesByQualifiedName: () => [],
  994. getNodesByKind: () => [],
  995. fileExists: (p: string) => (
  996. p === 'Cargo.toml' ||
  997. p === 'crates/mytool-core/Cargo.toml' ||
  998. p === 'crates/mytool-core/src/lib.rs'
  999. ),
  1000. readFile: (p: string) => {
  1001. if (p === 'Cargo.toml') return workspaceCargo;
  1002. if (p === 'crates/mytool-core/Cargo.toml') return coreCargo;
  1003. return null;
  1004. },
  1005. getProjectRoot: () => '/test',
  1006. getAllFiles: () => [
  1007. 'Cargo.toml',
  1008. 'crates/mytool-core/Cargo.toml',
  1009. 'crates/mytool-core/src/lib.rs',
  1010. ],
  1011. getNodesByLowerName: () => [],
  1012. getImportMappings: () => [],
  1013. };
  1014. const ref = {
  1015. fromNodeId: 'fn:crates/mytool-fetcher/src/main.rs:main:1',
  1016. referenceName: 'mytool_core',
  1017. referenceKind: 'references' as const,
  1018. line: 1,
  1019. column: 1,
  1020. filePath: 'crates/mytool-fetcher/src/main.rs',
  1021. language: 'rust' as const,
  1022. };
  1023. const result = rustResolver.resolve(ref, context);
  1024. expect(result?.targetNodeId).toBe(libNode.id);
  1025. expect(result?.resolvedBy).toBe('framework');
  1026. // Workspace-manifest hits are unambiguous and must beat name-matcher's
  1027. // self-file matches (0.7) so cross-crate `imports` edges materialize.
  1028. expect(result?.confidence).toBeGreaterThanOrEqual(0.9);
  1029. });
  1030. it('resolves crate name from workspace member main.rs when lib.rs is absent', () => {
  1031. const workspaceCargo = `
  1032. [workspace]
  1033. members = [
  1034. "crates/mytool-runner",
  1035. ]
  1036. `;
  1037. const runnerCargo = `
  1038. [package]
  1039. name = "mytool-runner"
  1040. version = "0.1.0"
  1041. `;
  1042. const mainNode: Node = {
  1043. id: 'module:crates/mytool-runner/src/main.rs:mytool_runner:1',
  1044. kind: 'module',
  1045. name: 'mytool_runner',
  1046. qualifiedName: 'crates/mytool-runner/src/main.rs::mytool_runner',
  1047. filePath: 'crates/mytool-runner/src/main.rs',
  1048. language: 'rust',
  1049. startLine: 1,
  1050. endLine: 1,
  1051. startColumn: 0,
  1052. endColumn: 0,
  1053. updatedAt: Date.now(),
  1054. };
  1055. const context = {
  1056. getNodesInFile: (fp: string) => (fp === 'crates/mytool-runner/src/main.rs' ? [mainNode] : []),
  1057. getNodesByName: () => [],
  1058. getNodesByQualifiedName: () => [],
  1059. getNodesByKind: () => [],
  1060. fileExists: (p: string) => (
  1061. p === 'Cargo.toml' ||
  1062. p === 'crates/mytool-runner/Cargo.toml' ||
  1063. p === 'crates/mytool-runner/src/main.rs'
  1064. ),
  1065. readFile: (p: string) => {
  1066. if (p === 'Cargo.toml') return workspaceCargo;
  1067. if (p === 'crates/mytool-runner/Cargo.toml') return runnerCargo;
  1068. return null;
  1069. },
  1070. getProjectRoot: () => '/test',
  1071. getAllFiles: () => [
  1072. 'Cargo.toml',
  1073. 'crates/mytool-runner/Cargo.toml',
  1074. 'crates/mytool-runner/src/main.rs',
  1075. ],
  1076. getNodesByLowerName: () => [],
  1077. getImportMappings: () => [],
  1078. };
  1079. const ref = {
  1080. fromNodeId: 'fn:crates/mytool-runner/src/main.rs:main:1',
  1081. referenceName: 'mytool_runner',
  1082. referenceKind: 'references' as const,
  1083. line: 1,
  1084. column: 1,
  1085. filePath: 'crates/mytool-runner/src/main.rs',
  1086. language: 'rust' as const,
  1087. };
  1088. const result = rustResolver.resolve(ref, context);
  1089. expect(result?.targetNodeId).toBe(mainNode.id);
  1090. expect(result?.resolvedBy).toBe('framework');
  1091. });
  1092. it('resolves crate name when members uses a glob (crates/*)', () => {
  1093. const workspaceCargo = `
  1094. [workspace]
  1095. members = ["crates/*"]
  1096. `;
  1097. const fooCargo = `
  1098. [package]
  1099. name = "mytool-foo"
  1100. version = "0.1.0"
  1101. `;
  1102. const barCargo = `
  1103. [package]
  1104. name = "mytool-bar"
  1105. version = "0.1.0"
  1106. `;
  1107. const fooLib: Node = {
  1108. id: 'module:crates/mytool-foo/src/lib.rs:mytool_foo:1',
  1109. kind: 'module',
  1110. name: 'mytool_foo',
  1111. qualifiedName: 'crates/mytool-foo/src/lib.rs::mytool_foo',
  1112. filePath: 'crates/mytool-foo/src/lib.rs',
  1113. language: 'rust',
  1114. startLine: 1,
  1115. endLine: 1,
  1116. startColumn: 0,
  1117. endColumn: 0,
  1118. updatedAt: Date.now(),
  1119. };
  1120. const barLib: Node = {
  1121. id: 'module:crates/mytool-bar/src/lib.rs:mytool_bar:1',
  1122. kind: 'module',
  1123. name: 'mytool_bar',
  1124. qualifiedName: 'crates/mytool-bar/src/lib.rs::mytool_bar',
  1125. filePath: 'crates/mytool-bar/src/lib.rs',
  1126. language: 'rust',
  1127. startLine: 1,
  1128. endLine: 1,
  1129. startColumn: 0,
  1130. endColumn: 0,
  1131. updatedAt: Date.now(),
  1132. };
  1133. const filesByPath: Record<string, string> = {
  1134. 'Cargo.toml': workspaceCargo,
  1135. 'crates/mytool-foo/Cargo.toml': fooCargo,
  1136. 'crates/mytool-bar/Cargo.toml': barCargo,
  1137. };
  1138. const nodesByFile: Record<string, Node[]> = {
  1139. 'crates/mytool-foo/src/lib.rs': [fooLib],
  1140. 'crates/mytool-bar/src/lib.rs': [barLib],
  1141. };
  1142. const dirsByPath: Record<string, string[]> = {
  1143. '.': ['crates'],
  1144. crates: ['mytool-foo', 'mytool-bar'],
  1145. 'crates/mytool-foo': ['src'],
  1146. 'crates/mytool-bar': ['src'],
  1147. };
  1148. const context = {
  1149. getNodesInFile: (fp: string) => nodesByFile[fp] ?? [],
  1150. getNodesByName: () => [],
  1151. getNodesByQualifiedName: () => [],
  1152. getNodesByKind: () => [],
  1153. fileExists: (p: string) => (
  1154. Object.prototype.hasOwnProperty.call(filesByPath, p) ||
  1155. Object.prototype.hasOwnProperty.call(nodesByFile, p)
  1156. ),
  1157. readFile: (p: string) => filesByPath[p] ?? null,
  1158. getProjectRoot: () => '/test',
  1159. getAllFiles: () => [
  1160. 'Cargo.toml',
  1161. ...Object.keys(filesByPath).filter((p) => p !== 'Cargo.toml'),
  1162. ...Object.keys(nodesByFile),
  1163. ],
  1164. getNodesByLowerName: () => [],
  1165. getImportMappings: () => [],
  1166. listDirectories: (rel: string) => dirsByPath[rel] ?? [],
  1167. };
  1168. const fooRef = {
  1169. fromNodeId: 'fn:crates/mytool-bar/src/lib.rs:other:1',
  1170. referenceName: 'mytool_foo',
  1171. referenceKind: 'references' as const,
  1172. line: 1,
  1173. column: 1,
  1174. filePath: 'crates/mytool-bar/src/lib.rs',
  1175. language: 'rust' as const,
  1176. };
  1177. const barRef = {
  1178. fromNodeId: 'fn:crates/mytool-foo/src/lib.rs:other:1',
  1179. referenceName: 'mytool_bar',
  1180. referenceKind: 'references' as const,
  1181. line: 1,
  1182. column: 1,
  1183. filePath: 'crates/mytool-foo/src/lib.rs',
  1184. language: 'rust' as const,
  1185. };
  1186. expect(rustResolver.resolve(fooRef, context)?.targetNodeId).toBe(fooLib.id);
  1187. expect(rustResolver.resolve(barRef, context)?.targetNodeId).toBe(barLib.id);
  1188. });
  1189. it('resolves crate name when members uses a name glob at root (helix-*)', () => {
  1190. const workspaceCargo = `
  1191. [workspace]
  1192. members = ["helix-*"]
  1193. `;
  1194. const coreCargo = `
  1195. [package]
  1196. name = "helix-core"
  1197. version = "0.1.0"
  1198. `;
  1199. const coreLib: Node = {
  1200. id: 'module:helix-core/src/lib.rs:helix_core:1',
  1201. kind: 'module',
  1202. name: 'helix_core',
  1203. qualifiedName: 'helix-core/src/lib.rs::helix_core',
  1204. filePath: 'helix-core/src/lib.rs',
  1205. language: 'rust',
  1206. startLine: 1,
  1207. endLine: 1,
  1208. startColumn: 0,
  1209. endColumn: 0,
  1210. updatedAt: Date.now(),
  1211. };
  1212. const filesByPath: Record<string, string> = {
  1213. 'Cargo.toml': workspaceCargo,
  1214. 'helix-core/Cargo.toml': coreCargo,
  1215. };
  1216. const nodesByFile: Record<string, Node[]> = {
  1217. 'helix-core/src/lib.rs': [coreLib],
  1218. };
  1219. const dirsByPath: Record<string, string[]> = {
  1220. '.': ['helix-core', 'docs', 'target'],
  1221. 'helix-core': ['src'],
  1222. };
  1223. const context = {
  1224. getNodesInFile: (fp: string) => nodesByFile[fp] ?? [],
  1225. getNodesByName: () => [],
  1226. getNodesByQualifiedName: () => [],
  1227. getNodesByKind: () => [],
  1228. fileExists: (p: string) => (
  1229. Object.prototype.hasOwnProperty.call(filesByPath, p) ||
  1230. Object.prototype.hasOwnProperty.call(nodesByFile, p)
  1231. ),
  1232. readFile: (p: string) => filesByPath[p] ?? null,
  1233. getProjectRoot: () => '/test',
  1234. getAllFiles: () => [
  1235. 'Cargo.toml',
  1236. ...Object.keys(filesByPath).filter((p) => p !== 'Cargo.toml'),
  1237. ...Object.keys(nodesByFile),
  1238. ],
  1239. getNodesByLowerName: () => [],
  1240. getImportMappings: () => [],
  1241. listDirectories: (rel: string) => dirsByPath[rel] ?? [],
  1242. };
  1243. const ref = {
  1244. fromNodeId: 'fn:helix-core/src/lib.rs:other:1',
  1245. referenceName: 'helix_core',
  1246. referenceKind: 'references' as const,
  1247. line: 1,
  1248. column: 1,
  1249. filePath: 'helix-core/src/lib.rs',
  1250. language: 'rust' as const,
  1251. };
  1252. expect(rustResolver.resolve(ref, context)?.targetNodeId).toBe(coreLib.id);
  1253. });
  1254. });
  1255. import { aspnetResolver } from '../src/resolution/frameworks/csharp';
  1256. describe('aspnetResolver.extract', () => {
  1257. it('extracts route from [HttpGet] attribute', () => {
  1258. const src = `
  1259. [HttpGet("/users")]
  1260. public IActionResult ListUsers()
  1261. {
  1262. return Ok();
  1263. }
  1264. `;
  1265. const { nodes, references } = aspnetResolver.extract!('UserController.cs', src);
  1266. expect(nodes[0].name).toBe('GET /users');
  1267. expect(references[0].referenceName).toBe('ListUsers');
  1268. });
  1269. });
  1270. import { vaporResolver } from '../src/resolution/frameworks/swift';
  1271. describe('vaporResolver.extract', () => {
  1272. it('extracts route from app.get with use:', () => {
  1273. const src = `app.get("users", use: listUsers)\n`;
  1274. const { nodes, references } = vaporResolver.extract!('routes.swift', src);
  1275. expect(nodes[0].name).toBe('GET /users');
  1276. expect(references[0].referenceName).toBe('listUsers');
  1277. });
  1278. it('extracts grouped RouteCollection routes with the group prefix and no path arg', () => {
  1279. const src = `
  1280. func boot(routes: RoutesBuilder) throws {
  1281. let todos = routes.grouped("todos")
  1282. todos.get(use: index)
  1283. todos.post(use: create)
  1284. todos.group(":todoID") { todo in
  1285. todo.delete(use: delete)
  1286. }
  1287. }
  1288. `;
  1289. const { nodes, references } = vaporResolver.extract!('TodoController.swift', src);
  1290. expect(nodes.map((n) => n.name).sort()).toEqual([
  1291. 'DELETE /todos/:todoID',
  1292. 'GET /todos',
  1293. 'POST /todos',
  1294. ]);
  1295. expect(references.map((r) => r.referenceName).sort()).toEqual([
  1296. 'create',
  1297. 'delete',
  1298. 'index',
  1299. ]);
  1300. });
  1301. it('handles use: self.handler and non-string path segments', () => {
  1302. const src = `router.get("users", User.parameter, "edit", use: self.editUserHandler)\n`;
  1303. const { nodes, references } = vaporResolver.extract!('UserController.swift', src);
  1304. expect(nodes[0].name).toBe('GET /users/edit');
  1305. expect(references[0].referenceName).toBe('editUserHandler');
  1306. });
  1307. it('ignores non-route .get calls that lack use: (e.g. Environment.get)', () => {
  1308. const src = `let host = Environment.get("DATABASE_HOST") ?? "localhost"\n`;
  1309. const { nodes } = vaporResolver.extract!('configure.swift', src);
  1310. expect(nodes).toHaveLength(0);
  1311. });
  1312. });
  1313. import { reactResolver } from '../src/resolution/frameworks/react';
  1314. import { svelteResolver } from '../src/resolution/frameworks/svelte';
  1315. import { astroResolver } from '../src/resolution/frameworks/astro';
  1316. describe('reactResolver.extract — React Router', () => {
  1317. it('extracts a v6 <Route path element={<Comp/>}>', () => {
  1318. const src = `<Route path="/users" element={<UsersPage/>}/>`;
  1319. const { nodes, references } = reactResolver.extract!('App.tsx', src);
  1320. const route = nodes.find((n) => n.kind === 'route');
  1321. expect(route?.name).toBe('/users');
  1322. expect(references[0]?.referenceName).toBe('UsersPage');
  1323. });
  1324. it('extracts a v5 <Route path component={Comp}> with attributes in any order', () => {
  1325. const src = `<Route exact path="/login" component={Login} />`;
  1326. const { nodes, references } = reactResolver.extract!('App.jsx', src);
  1327. const route = nodes.find((n) => n.kind === 'route');
  1328. expect(route?.name).toBe('/login');
  1329. expect(references[0]?.referenceName).toBe('Login');
  1330. });
  1331. it('does not treat the <Routes> container as a route', () => {
  1332. const src = `<Routes><Route path="/x" element={<X/>}/></Routes>`;
  1333. const routes = reactResolver.extract!('App.tsx', src).nodes.filter((n) => n.kind === 'route');
  1334. expect(routes).toHaveLength(1);
  1335. expect(routes[0]?.name).toBe('/x');
  1336. });
  1337. it('extracts createBrowserRouter object routes ({ path, element/Component })', () => {
  1338. const src = `const router = createBrowserRouter([
  1339. { path: "/dashboard", element: <Dashboard /> },
  1340. { path: "/login", Component: Login },
  1341. ]);`;
  1342. const { nodes, references } = reactResolver.extract!('router.tsx', src);
  1343. const routes = nodes.filter((n) => n.kind === 'route');
  1344. expect(routes.map((n) => n.name).sort()).toEqual(['/dashboard', '/login']);
  1345. expect(references.map((r) => r.referenceName).sort()).toEqual(['Dashboard', 'Login']);
  1346. });
  1347. it('does not treat config files or a nextjs-pages dir as Next.js routes', () => {
  1348. const cfg = reactResolver.extract!('apps/nextjs-pages/next.config.mjs', 'export default {}');
  1349. expect(cfg.nodes.filter((n) => n.kind === 'route')).toHaveLength(0);
  1350. const vite = reactResolver.extract!('src/pages/vite.config.ts', 'export default {}');
  1351. expect(vite.nodes.filter((n) => n.kind === 'route')).toHaveLength(0);
  1352. // a real page still works
  1353. const page = reactResolver.extract!('src/pages/about.tsx', 'export default function About(){return null}');
  1354. expect(page.nodes.filter((n) => n.kind === 'route').map((n) => n.name)).toEqual(['/about']);
  1355. });
  1356. });
  1357. describe('svelteResolver.extract (smoke)', () => {
  1358. it('returns { nodes, references } shape', () => {
  1359. const result = svelteResolver.extract!('+page.svelte', '');
  1360. expect(result).toHaveProperty('nodes');
  1361. expect(result).toHaveProperty('references');
  1362. });
  1363. });
  1364. describe('astroResolver.extract — src/pages file-based routing', () => {
  1365. const routeNames = (filePath: string): string[] =>
  1366. astroResolver.extract!(filePath, '').nodes.filter((n) => n.kind === 'route').map((n) => n.name);
  1367. it('maps index.astro to /', () => {
  1368. expect(routeNames('src/pages/index.astro')).toEqual(['/']);
  1369. });
  1370. it('maps nested index and plain pages', () => {
  1371. expect(routeNames('src/pages/blog/index.astro')).toEqual(['/blog']);
  1372. expect(routeNames('src/pages/about.astro')).toEqual(['/about']);
  1373. });
  1374. it('converts [param] and [...rest] syntax', () => {
  1375. expect(routeNames('src/pages/blog/[slug].astro')).toEqual(['/blog/:slug']);
  1376. expect(routeNames('src/pages/[...path].astro')).toEqual(['/*path']);
  1377. });
  1378. it('maps .ts endpoints under src/pages to routes', () => {
  1379. expect(routeNames('src/pages/api/posts.ts')).toEqual(['/api/posts']);
  1380. expect(routeNames('src/pages/rss.xml.js')).toEqual(['/rss.xml']);
  1381. });
  1382. it('excludes underscore-prefixed segments and config files', () => {
  1383. expect(routeNames('src/pages/_partial.astro')).toEqual([]);
  1384. expect(routeNames('src/pages/blog/_components/Card.astro')).toEqual([]);
  1385. expect(routeNames('src/pages/vite.config.ts')).toEqual([]);
  1386. });
  1387. it('ignores .astro files outside src/pages', () => {
  1388. expect(routeNames('src/components/Button.astro')).toEqual([]);
  1389. expect(routeNames('docs/pages/guide.astro')).toEqual([]);
  1390. });
  1391. });
  1392. describe('astroResolver.resolve — Astro global and virtual modules', () => {
  1393. const ctx = {} as never;
  1394. const baseRef = {
  1395. fromNodeId: 'component:a',
  1396. line: 1,
  1397. column: 0,
  1398. filePath: 'src/pages/index.astro',
  1399. language: 'astro',
  1400. };
  1401. it('claims Astro.* global references as framework-provided', () => {
  1402. const res = astroResolver.resolve(
  1403. { ...baseRef, referenceName: 'Astro.props', referenceKind: 'references' } as never,
  1404. ctx
  1405. );
  1406. expect(res?.resolvedBy).toBe('framework');
  1407. expect(res?.confidence).toBe(1.0);
  1408. });
  1409. it('claims astro:content virtual module imports', () => {
  1410. const res = astroResolver.resolve(
  1411. { ...baseRef, referenceName: 'astro:content', referenceKind: 'imports' } as never,
  1412. ctx
  1413. );
  1414. expect(res?.resolvedBy).toBe('framework');
  1415. });
  1416. it('leaves ordinary names alone', () => {
  1417. const res = astroResolver.resolve(
  1418. { ...baseRef, referenceName: 'astrolabe', referenceKind: 'calls' } as never,
  1419. { getNodesByName: () => [] } as never
  1420. );
  1421. expect(res).toBeNull();
  1422. });
  1423. });
  1424. // Regression tests: commented-out and docstring route examples must NOT
  1425. // surface as phantom route nodes. These would have failed before the
  1426. // strip-comments wiring (the regex would happily scan comments/docstrings).
  1427. describe('framework extractors ignore commented-out routes', () => {
  1428. it('django: skips line-comment and docstring routes', () => {
  1429. const src = `
  1430. # urls.py example:
  1431. # path('/admin/', AdminPanel.as_view())
  1432. """
  1433. Other routing example:
  1434. path('/users/', UserListView.as_view())
  1435. """
  1436. urlpatterns = [path('/real/', RealView.as_view())]
  1437. `;
  1438. const result = djangoResolver.extract!('app/urls.py', src);
  1439. const urls = result.nodes.map((n) => n.name);
  1440. expect(urls).toEqual(['/real/']);
  1441. });
  1442. it('flask: skips commented-out @app.route', () => {
  1443. const src = `
  1444. # @app.route('/fake')
  1445. # def fake_view():
  1446. # return ''
  1447. @app.route('/real')
  1448. def real_view():
  1449. return ''
  1450. `;
  1451. const { nodes, references } = flaskResolver.extract!('app.py', src);
  1452. expect(nodes.map((n) => n.name)).toEqual(['GET /real']);
  1453. expect(references.map((r) => r.referenceName)).toEqual(['real_view']);
  1454. });
  1455. it('fastapi: skips docstring example routes', () => {
  1456. const src = `
  1457. """
  1458. Example:
  1459. @app.get('/in-docstring')
  1460. async def doc():
  1461. pass
  1462. """
  1463. @app.get('/real')
  1464. async def real_handler():
  1465. return {}
  1466. `;
  1467. const { nodes, references } = fastapiResolver.extract!('main.py', src);
  1468. expect(nodes.map((n) => n.name)).toEqual(['GET /real']);
  1469. expect(references.map((r) => r.referenceName)).toEqual(['real_handler']);
  1470. });
  1471. it('express: skips // and /* */ commented routes', () => {
  1472. const src = `
  1473. // app.get('/fake', fakeHandler);
  1474. /* router.post('/also-fake', otherHandler); */
  1475. app.get('/real', realHandler);
  1476. `;
  1477. const { nodes, references } = expressResolver.extract!('routes.ts', src);
  1478. expect(nodes.map((n) => n.name)).toEqual(['GET /real']);
  1479. expect(references.map((r) => r.referenceName)).toEqual(['realHandler']);
  1480. });
  1481. it('laravel: skips // # and /* */ commented Route::* calls', () => {
  1482. const src = `<?php
  1483. // Route::get('/fake', [FakeController::class, 'index']);
  1484. # Route::get('/also-fake', 'FakeController@show');
  1485. /* Route::post('/another-fake', [X::class, 'y']); */
  1486. Route::get('/real', [RealController::class, 'index']);
  1487. `;
  1488. const { nodes, references } = laravelResolver.extract!('routes/web.php', src);
  1489. expect(nodes.map((n) => n.name)).toEqual(['GET /real']);
  1490. expect(references.map((r) => r.referenceName)).toEqual(['RealController@index']);
  1491. });
  1492. it('rails: skips =begin/=end and # commented routes', () => {
  1493. const src = `
  1494. # get '/fake', to: 'fake#index'
  1495. =begin
  1496. get '/also-fake', to: 'fake#show'
  1497. =end
  1498. get '/real', to: 'real#index'
  1499. `;
  1500. const { nodes, references } = railsResolver.extract!('config/routes.rb', src);
  1501. expect(nodes.map((n) => n.name)).toEqual(['GET /real']);
  1502. expect(references.map((r) => r.referenceName)).toEqual(['real#index']);
  1503. });
  1504. it('spring: skips // and /* */ commented @GetMapping', () => {
  1505. const src = `
  1506. // @GetMapping("/fake")
  1507. // public List<X> fake() { return null; }
  1508. /* @PostMapping("/also-fake")
  1509. public void alsoFake() {} */
  1510. @GetMapping("/real")
  1511. public List<User> listUsers() { return users; }
  1512. `;
  1513. const { nodes, references } = springResolver.extract!('UserController.java', src);
  1514. expect(nodes.map((n) => n.name)).toEqual(['GET /real']);
  1515. expect(references.map((r) => r.referenceName)).toEqual(['listUsers']);
  1516. });
  1517. it('go: skips // and /* */ commented router.METHOD calls', () => {
  1518. const src = `
  1519. // r.GET("/fake", fakeHandler)
  1520. /* r.POST("/also-fake", anotherHandler) */
  1521. r.GET("/real", listUsers)
  1522. `;
  1523. const { nodes, references } = goResolver.extract!('main.go', src);
  1524. expect(nodes.map((n) => n.name)).toEqual(['GET /real']);
  1525. expect(references.map((r) => r.referenceName)).toEqual(['listUsers']);
  1526. });
  1527. it('rust: skips // and nested /* */ commented .route() calls', () => {
  1528. const src = `
  1529. // .route("/fake", get(fake_handler))
  1530. /* outer /* inner .route("/inner-fake", get(x)) */ still .route("/outer-fake", get(y)) */
  1531. let app = Router::new().route("/real", get(list_users));
  1532. `;
  1533. const { nodes, references } = rustResolver.extract!('main.rs', src);
  1534. expect(nodes.map((n) => n.name)).toEqual(['GET /real']);
  1535. expect(references.map((r) => r.referenceName)).toEqual(['list_users']);
  1536. });
  1537. it('aspnet: skips // and /* */ commented [HttpGet] attributes', () => {
  1538. const src = `
  1539. // [HttpGet("/fake")]
  1540. // public IActionResult Fake() { return Ok(); }
  1541. /* [HttpPost("/also-fake")]
  1542. public IActionResult AlsoFake() { return Ok(); } */
  1543. [HttpGet("/real")]
  1544. public IActionResult ListUsers() { return Ok(); }
  1545. `;
  1546. const { nodes, references } = aspnetResolver.extract!('UserController.cs', src);
  1547. expect(nodes.map((n) => n.name)).toEqual(['GET /real']);
  1548. expect(references.map((r) => r.referenceName)).toEqual(['ListUsers']);
  1549. });
  1550. it('vapor: skips // and /* */ commented app.METHOD calls', () => {
  1551. const src = `
  1552. // app.get("fake", use: fakeHandler)
  1553. /* app.post("also-fake", use: anotherHandler) */
  1554. app.get("real", use: listUsers)
  1555. `;
  1556. const { nodes, references } = vaporResolver.extract!('routes.swift', src);
  1557. expect(nodes.map((n) => n.name)).toEqual(['GET /real']);
  1558. expect(references.map((r) => r.referenceName)).toEqual(['listUsers']);
  1559. });
  1560. it('nestjs: skips // and /* */ commented decorators', () => {
  1561. const src = `
  1562. @Controller('users')
  1563. export class UsersController {
  1564. // @Get('fake')
  1565. // fake() {}
  1566. /* @Post('also-fake')
  1567. alsoFake() {} */
  1568. @Get('real')
  1569. real() {}
  1570. }
  1571. `;
  1572. const { nodes, references } = nestjsResolver.extract!('users.controller.ts', src);
  1573. expect(nodes.map((n) => n.name)).toEqual(['GET /users/real']);
  1574. expect(references.map((r) => r.referenceName)).toEqual(['real']);
  1575. });
  1576. });