Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix scriptKind in query #93

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"typescript.format.semicolons": "insert",
"javascript.format.insertSpaceBeforeFunctionParenthesis": false,
"typescript.format.insertSpaceBeforeFunctionParenthesis": true,
"typescript.format.insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": false
}
2 changes: 1 addition & 1 deletion src/parse.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Dependencies:
import * as esquery from 'esquery';
import esquery from 'esquery';
import { SyntaxKind } from 'typescript';
import { TSQuerySelectorNode } from './tsquery-types';

Expand Down
6 changes: 3 additions & 3 deletions src/query.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Dependencies:
import { Node } from 'typescript';
import { Node, ScriptKind } from 'typescript';
import { createAST } from './ast';
import { match } from './match';
import { parse } from './parse';
import { TSQueryOptions } from './tsquery-types';

export function query <T extends Node = Node> (ast: string | Node, selector: string, options: TSQueryOptions = {}): Array<T> {
export function query<T extends Node = Node> (ast: string | Node, selector: string, options: TSQueryOptions = {}): Array<T> {
if (typeof ast === 'string') {
ast = createAST(ast);
ast = createAST(ast, undefined, options.scriptKind ?? ScriptKind.Unknown);
}
return match<T>(ast, parse(selector), options);
}
143 changes: 72 additions & 71 deletions src/tsquery-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,101 +3,102 @@ import { Node, ScriptKind, SourceFile, SyntaxKind, VisitResult } from 'typescrip

export type TSQueryNodeTransformer = (node: Node) => VisitResult<Node>;
export type TSQueryStringTransformer = (
node: Node
node: Node
) => string | null | undefined;

export type TSQueryApi = {
<T extends Node = Node>(
ast: string | Node,
selector: string,
options?: TSQueryOptions
): Array<T>;
ast(source: string, fileName?: string, scriptKind?: ScriptKind): SourceFile;
map(
ast: SourceFile,
selector: string,
nodeTransformer: TSQueryNodeTransformer,
options?: TSQueryOptions
): SourceFile;
match<T extends Node = Node>(
ast: Node,
selector: TSQuerySelectorNode,
options?: TSQueryOptions
): Array<T>;
parse(selector: string, options?: TSQueryOptions): TSQuerySelectorNode;
project(configFilePath: string): Array<SourceFile>;
projectFiles(configFilePath: string): Array<string>;
query<T extends Node = Node>(
ast: string | Node,
selector: string,
options?: TSQueryOptions
): Array<T>;
replace(
source: string,
selector: string,
stringTransformer: TSQueryStringTransformer,
options?: TSQueryOptions
): string;
syntaxKindName(node: SyntaxKind): string;
<T extends Node = Node> (
ast: string | Node,
selector: string,
options?: TSQueryOptions
): Array<T>;
ast (source: string, fileName?: string, scriptKind?: ScriptKind): SourceFile;
map (
ast: SourceFile,
selector: string,
nodeTransformer: TSQueryNodeTransformer,
options?: TSQueryOptions
): SourceFile;
match<T extends Node = Node> (
ast: Node,
selector: TSQuerySelectorNode,
options?: TSQueryOptions
): Array<T>;
parse (selector: string, options?: TSQueryOptions): TSQuerySelectorNode;
project (configFilePath: string): Array<SourceFile>;
projectFiles (configFilePath: string): Array<string>;
query<T extends Node = Node> (
ast: string | Node,
selector: string,
options?: TSQueryOptions
): Array<T>;
replace (
source: string,
selector: string,
stringTransformer: TSQueryStringTransformer,
options?: TSQueryOptions
): string;
syntaxKindName (node: SyntaxKind): string;
};

export type TSQueryAttributeOperatorType = 'regexp' | 'literal' | 'type';
export type TSQueryAttributeOperator = (
obj: any,
value: any,
type: TSQueryAttributeOperatorType
obj: any,
value: any,
type: TSQueryAttributeOperatorType
) => boolean;
export type TSQueryAttributeOperators = {
[key: string]: TSQueryAttributeOperator;
[key: string]: TSQueryAttributeOperator;
};

export type TSQueryMatcher = (
node: Node,
selector: TSQuerySelectorNode,
ancestry: Array<Node>,
options: TSQueryOptions
node: Node,
selector: TSQuerySelectorNode,
ancestry: Array<Node>,
options: TSQueryOptions
) => boolean;
export type TSQueryMatchers = {
[key: string]: TSQueryMatcher;
[key: string]: TSQueryMatcher;
};

export type TSQueryProperties = {
// We convert the `kind` property to its string name from the `SyntaxKind` enum:
// Some nodes have more that one applicable `SyntaxKind`...
kindName: string;
// We add a 'name' property to `Node`s with `type` `SyntaxKind.Identifier`:
name?: string;
// We automatically call `getText()` so it can be selected on:
text: string;
// We parse the `text` to a `value` for all Literals:
value?: any;
// We convert the `kind` property to its string name from the `SyntaxKind` enum:
// Some nodes have more that one applicable `SyntaxKind`...
kindName: string;
// We add a 'name' property to `Node`s with `type` `SyntaxKind.Identifier`:
name?: string;
// We automatically call `getText()` so it can be selected on:
text: string;
// We parse the `text` to a `value` for all Literals:
value?: any;
};

export type TSQueryOptions = {
visitAllChildren?: boolean;
visitAllChildren?: boolean;
scriptKind?: ScriptKind;
};

export type TSQuerySelectorNode = {
[key: string]:
| TSQuerySelectorNode
| Array<TSQuerySelectorNode>
| RegExp
| boolean
| number
| string;
index: TSQuerySelectorNode;
left: TSQuerySelectorNode;
name: string;
operator: '=' | '!=' | '<=' | '<' | '>=' | '>';
right: TSQuerySelectorNode;
selectors: Array<TSQuerySelectorNode>;
subject: boolean;
type: TSQueryAttributeOperatorType;
value: TSQuerySelectorNode | RegExp | number | string;
[key: string]:
| TSQuerySelectorNode
| Array<TSQuerySelectorNode>
| RegExp
| boolean
| number
| string;
index: TSQuerySelectorNode;
left: TSQuerySelectorNode;
name: string;
operator: '=' | '!=' | '<=' | '<' | '>=' | '>';
right: TSQuerySelectorNode;
selectors: Array<TSQuerySelectorNode>;
subject: boolean;
type: TSQueryAttributeOperatorType;
value: TSQuerySelectorNode | RegExp | number | string;
};

export type TSQueryTraverseOptions = {
enter: (node: Node, parent: Node | null) => void;
leave: (node: Node, parent: Node | null) => void;
visitAllChildren: boolean;
enter: (node: Node, parent: Node | null) => void;
leave: (node: Node, parent: Node | null) => void;
visitAllChildren: boolean;
};
2 changes: 2 additions & 0 deletions test/map.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ console.log('bar');

`.trim()
);
//@ts-ignore
const result = tsquery.map(ast, 'StringLiteral', () => undefined);
const printer = createPrinter(printerOptions);
expect(printer.printFile(result).trim()).toEqual(
Expand All @@ -123,6 +124,7 @@ console.log();
it(`should't visit child nodes when an ancestor has been replaced`, () => {
const ast = tsquery.ast('label1: label2: 1 + 1'.trim());
let count = 0;
//@ts-ignore
tsquery.map(ast, 'LabeledStatement', () => {
++count;
return undefined;
Expand Down