Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/traverse.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Dependencies:
import { Node, SyntaxKind } from 'typescript';
import { JSDoc, Node, SyntaxKind } from 'typescript';
import { syntaxKindName } from './syntax-kind';
import { TSQueryNode, TSQueryTraverseOptions } from './tsquery-types';

Expand Down Expand Up @@ -27,6 +27,9 @@ const PARSERS: { [key: number]: (node: TSQueryNode) => any } = {
export function traverse (node: Node | TSQueryNode, options: TSQueryTraverseOptions): void {
addProperties(node as TSQueryNode);
options.enter(node as TSQueryNode, node.parent as TSQueryNode || null);
if ((node as any).jsDoc) {
((node as any).jsDoc as Array<JSDoc>).forEach(child => traverse(child, options));
}
node.forEachChild(child => traverse(child as TSQueryNode, options));
options.leave(node as TSQueryNode, node.parent as TSQueryNode || null);
}
Expand Down
5 changes: 3 additions & 2 deletions test/attribute.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { expect } from './index';

// Dependencies:
import { BinaryExpression, Block, CallExpression, ExpressionStatement, FunctionDeclaration, IfStatement, VariableDeclaration, VariableStatement } from 'typescript';
import { BinaryExpression, Block, CallExpression, ExpressionStatement, FunctionDeclaration, IfStatement, VariableDeclaration, VariableStatement, JSDoc, JSDocParameterTag } from 'typescript';
import { conditional, simpleFunction, simpleProgram } from './fixtures';

// Under test:
Expand Down Expand Up @@ -69,9 +69,10 @@ describe('tsquery:', () => {
const result = tsquery(ast, '[name=/x|foo/]');

expect(result).to.deep.equal([
(((ast.statements[0] as any).jsDoc as JSDoc[])[0].tags![0] as JSDocParameterTag).name,
(ast.statements[0] as FunctionDeclaration).name,
(ast.statements[0] as FunctionDeclaration).parameters[0].name,
(((((ast.statements[0] as FunctionDeclaration).body as Block).statements[0] as VariableStatement).declarationList.declarations[0] as VariableDeclaration).initializer as BinaryExpression).left
(((((ast.statements[0] as FunctionDeclaration).body as Block).statements[0] as VariableStatement).declarationList.declarations[0] as VariableDeclaration).initializer as BinaryExpression).left,
]);
});

Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/simple-function.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
export const simpleFunction = `

/**
* @param x - the x
* @param y - the y
*/
function foo (x, y) {
var z = x + y;
z++;
Expand Down
2 changes: 1 addition & 1 deletion test/wildcard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('tsquery:', () => {
it('should find all nodes (simple function)', () => {
const result = tsquery(tsquery.ast(simpleFunction), '*');

expect(result.length).to.equal(22);
expect(result.length).to.equal(27);
});

it('should find all nodes (simple program)', () => {
Expand Down