|
1 | | -import { visit, walk, NodePath } from '../src'; |
2 | 1 | import type { Visitor, Walker } from '../src'; |
| 2 | +import { NodePath,visit, walk } from '../src'; |
3 | 3 |
|
4 | 4 | describe('traverse', () => { |
5 | 5 | it('should visit SelectStmt nodes with new walk API', () => { |
@@ -122,13 +122,13 @@ describe('traverse', () => { |
122 | 122 | const visitedNodes: string[] = []; |
123 | 123 |
|
124 | 124 | const visitor: Visitor = { |
125 | | - A_Expr: (path: NodePath) => { |
| 125 | + A_Expr: (_path: NodePath) => { |
126 | 126 | visitedNodes.push('A_Expr'); |
127 | 127 | }, |
128 | | - ColumnRef: (path: NodePath) => { |
| 128 | + ColumnRef: (_path: NodePath) => { |
129 | 129 | visitedNodes.push('ColumnRef'); |
130 | 130 | }, |
131 | | - A_Const: (path: NodePath) => { |
| 131 | + A_Const: (_path: NodePath) => { |
132 | 132 | visitedNodes.push('A_Const'); |
133 | 133 | } |
134 | 134 | }; |
@@ -390,4 +390,100 @@ describe('traverse', () => { |
390 | 390 | expect(targetListVisited[0].ctx.path).toEqual(['targetList', 0]); |
391 | 391 | expect(targetListVisited[1].ctx.path).toEqual(['targetList', 1]); |
392 | 392 | }); |
| 393 | + |
| 394 | + it('should traverse WithClause nodes', () => { |
| 395 | + const visitedNodes: string[] = []; |
| 396 | + |
| 397 | + const walker: Walker = (path: NodePath) => { |
| 398 | + visitedNodes.push(path.tag); |
| 399 | + }; |
| 400 | + |
| 401 | + const ast = { |
| 402 | + SelectStmt: { |
| 403 | + withClause: { |
| 404 | + WithClause: { |
| 405 | + ctes: [ |
| 406 | + { |
| 407 | + CommonTableExpr: { |
| 408 | + ctename: 'cte1', |
| 409 | + ctequery: { |
| 410 | + SelectStmt: { |
| 411 | + targetList: [] as any[], |
| 412 | + limitOption: 'LIMIT_OPTION_DEFAULT', |
| 413 | + op: 'SETOP_NONE' |
| 414 | + } |
| 415 | + } |
| 416 | + } |
| 417 | + } |
| 418 | + ] |
| 419 | + } |
| 420 | + }, |
| 421 | + targetList: [] as any[], |
| 422 | + limitOption: 'LIMIT_OPTION_DEFAULT', |
| 423 | + op: 'SETOP_NONE' |
| 424 | + } |
| 425 | + }; |
| 426 | + |
| 427 | + walk(ast, walker); |
| 428 | + |
| 429 | + expect(visitedNodes).toContain('SelectStmt'); |
| 430 | + expect(visitedNodes).toContain('WithClause'); |
| 431 | + expect(visitedNodes).toContain('CommonTableExpr'); |
| 432 | + expect(visitedNodes.filter(n => n === 'SelectStmt')).toHaveLength(2); |
| 433 | + }); |
| 434 | + |
| 435 | + it('should traverse union larg and rarg nodes', () => { |
| 436 | + const visitedNodes: string[] = []; |
| 437 | + |
| 438 | + const walker: Walker = (path: NodePath) => { |
| 439 | + visitedNodes.push(path.tag); |
| 440 | + }; |
| 441 | + |
| 442 | + const ast = { |
| 443 | + SelectStmt: { |
| 444 | + larg: { |
| 445 | + SelectStmt: { |
| 446 | + targetList: [ |
| 447 | + { |
| 448 | + ResTarget: { |
| 449 | + val: { |
| 450 | + A_Const: { |
| 451 | + ival: { Integer: { ival: 1 } } |
| 452 | + } |
| 453 | + } |
| 454 | + } |
| 455 | + } |
| 456 | + ], |
| 457 | + limitOption: 'LIMIT_OPTION_DEFAULT', |
| 458 | + op: 'SETOP_NONE' |
| 459 | + } |
| 460 | + }, |
| 461 | + op: 'SETOP_UNION', |
| 462 | + rarg: { |
| 463 | + SelectStmt: { |
| 464 | + targetList: [ |
| 465 | + { |
| 466 | + ResTarget: { |
| 467 | + val: { |
| 468 | + A_Const: { |
| 469 | + ival: { Integer: { ival: 2 } } |
| 470 | + } |
| 471 | + } |
| 472 | + } |
| 473 | + } |
| 474 | + ], |
| 475 | + limitOption: 'LIMIT_OPTION_DEFAULT', |
| 476 | + op: 'SETOP_NONE' |
| 477 | + } |
| 478 | + } |
| 479 | + } |
| 480 | + }; |
| 481 | + |
| 482 | + walk(ast, walker); |
| 483 | + |
| 484 | + expect(visitedNodes).toContain('SelectStmt'); |
| 485 | + expect(visitedNodes.filter(n => n === 'SelectStmt')).toHaveLength(3); |
| 486 | + expect(visitedNodes).toContain('ResTarget'); |
| 487 | + expect(visitedNodes).toContain('A_Const'); |
| 488 | + }); |
393 | 489 | }); |
0 commit comments