diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..a22e38b --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "typescript.format.semicolons": "insert", + "javascript.format.insertSpaceBeforeFunctionParenthesis": false, + "typescript.format.insertSpaceBeforeFunctionParenthesis": true, + "typescript.format.insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": false +} \ No newline at end of file diff --git a/src/parse.ts b/src/parse.ts index b7ce9af..46468f6 100644 --- a/src/parse.ts +++ b/src/parse.ts @@ -1,5 +1,5 @@ // Dependencies: -import * as esquery from 'esquery'; +import esquery from 'esquery'; import { SyntaxKind } from 'typescript'; import { TSQuerySelectorNode } from './tsquery-types'; diff --git a/src/query.ts b/src/query.ts index eb577be..ffbd3cf 100644 --- a/src/query.ts +++ b/src/query.ts @@ -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 (ast: string | Node, selector: string, options: TSQueryOptions = {}): Array { +export function query (ast: string | Node, selector: string, options: TSQueryOptions = {}): Array { if (typeof ast === 'string') { - ast = createAST(ast); + ast = createAST(ast, undefined, options.scriptKind ?? ScriptKind.Unknown); } return match(ast, parse(selector), options); } diff --git a/src/tsquery-types.ts b/src/tsquery-types.ts index 9535fe3..a6420c9 100644 --- a/src/tsquery-types.ts +++ b/src/tsquery-types.ts @@ -3,101 +3,102 @@ import { Node, ScriptKind, SourceFile, SyntaxKind, VisitResult } from 'typescrip export type TSQueryNodeTransformer = (node: Node) => VisitResult; export type TSQueryStringTransformer = ( - node: Node + node: Node ) => string | null | undefined; export type TSQueryApi = { - ( - ast: string | Node, - selector: string, - options?: TSQueryOptions - ): Array; - ast(source: string, fileName?: string, scriptKind?: ScriptKind): SourceFile; - map( - ast: SourceFile, - selector: string, - nodeTransformer: TSQueryNodeTransformer, - options?: TSQueryOptions - ): SourceFile; - match( - ast: Node, - selector: TSQuerySelectorNode, - options?: TSQueryOptions - ): Array; - parse(selector: string, options?: TSQueryOptions): TSQuerySelectorNode; - project(configFilePath: string): Array; - projectFiles(configFilePath: string): Array; - query( - ast: string | Node, - selector: string, - options?: TSQueryOptions - ): Array; - replace( - source: string, - selector: string, - stringTransformer: TSQueryStringTransformer, - options?: TSQueryOptions - ): string; - syntaxKindName(node: SyntaxKind): string; + ( + ast: string | Node, + selector: string, + options?: TSQueryOptions + ): Array; + ast (source: string, fileName?: string, scriptKind?: ScriptKind): SourceFile; + map ( + ast: SourceFile, + selector: string, + nodeTransformer: TSQueryNodeTransformer, + options?: TSQueryOptions + ): SourceFile; + match ( + ast: Node, + selector: TSQuerySelectorNode, + options?: TSQueryOptions + ): Array; + parse (selector: string, options?: TSQueryOptions): TSQuerySelectorNode; + project (configFilePath: string): Array; + projectFiles (configFilePath: string): Array; + query ( + ast: string | Node, + selector: string, + options?: TSQueryOptions + ): Array; + 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, - options: TSQueryOptions + node: Node, + selector: TSQuerySelectorNode, + ancestry: Array, + 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 - | RegExp - | boolean - | number - | string; - index: TSQuerySelectorNode; - left: TSQuerySelectorNode; - name: string; - operator: '=' | '!=' | '<=' | '<' | '>=' | '>'; - right: TSQuerySelectorNode; - selectors: Array; - subject: boolean; - type: TSQueryAttributeOperatorType; - value: TSQuerySelectorNode | RegExp | number | string; + [key: string]: + | TSQuerySelectorNode + | Array + | RegExp + | boolean + | number + | string; + index: TSQuerySelectorNode; + left: TSQuerySelectorNode; + name: string; + operator: '=' | '!=' | '<=' | '<' | '>=' | '>'; + right: TSQuerySelectorNode; + selectors: Array; + 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; }; diff --git a/test/map.spec.ts b/test/map.spec.ts index a44d255..558d284 100644 --- a/test/map.spec.ts +++ b/test/map.spec.ts @@ -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( @@ -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;