{"name":"@phenomnomnominal/tsquery","version":"5.0.1","description":"Query TypeScript ASTs with the esquery API!","main":"dist/src/index.js","typings":"dist/src/index.d.ts","author":{"name":"Craig Spence","email":"craigspence0@gmail.com"},"repository":{"type":"git","url":"git+https://github.com/phenomnomnominal/tsquery.git"},"license":"MIT","scripts":{"build":"npm run clean && npm run compile && npm run lint && npm run test","clean":"rimraf dist","compile":"tsc","lint":"npm run lint:src && npm run lint:test","lint:src":"tslint --project ./tsconfig.json -c ./tslint.json 'src/**/*.ts'","lint:test":"tslint --project ./tsconfig.json -c ./tslint.json 'test/**/*.ts'","lint:fix":"npm run lint:src:fix && npm run lint:test","lint:src:fix":"tslint -c tslint.json 'src/**/*.ts' --fix","lint:test:fix":"tslint -c tslint.json 'test/**/*.ts' --fix","test":"jest","test:debug":"node --inspect-brk ./node_modules/jest/bin/jest.js --runInBand","prepublishOnly":"npm run build"},"dependencies":{"esquery":"^1.4.0"},"peerDependencies":{"typescript":"^3 || ^4 || ^5"},"devDependencies":{"@types/jest":"^27.4.1","@types/node":"^17.0.25","jest":"^27.5.1","rimraf":"^3.0.2","ts-jest":"^27.1.4","tslint":"^6.1.3","typescript":"^4.6.3"},"jest":{"collectCoverage":true,"collectCoverageFrom":["<rootDir>/src/**"],"coverageDirectory":"<rootDir>/reports/coverage","transform":{"\\.(ts)$":"ts-jest"},"testRegex":"/test/.*\\.spec\\.ts$"},"gitHead":"957407d06c9d9b05986fde1cadc025a9ea0beb52","bugs":{"url":"https://github.com/phenomnomnominal/tsquery/issues"},"homepage":"https://github.com/phenomnomnominal/tsquery#readme","_id":"@phenomnomnominal/tsquery@5.0.1","_nodeVersion":"18.14.0","_npmVersion":"9.3.1","dist":{"integrity":"sha512-3nVv+e2FQwsW8Aw6qTU6f+1rfcJ3hrcnvH/mu9i8YhxO+9sqbOfpL8m6PbET5+xKOlz/VSbp0RoYWYCtIsnmuA==","shasum":"a2a5abc89f92c01562a32806655817516653a388","tarball":"http://registry-npm.ywwl.com/@phenomnomnominal/tsquery/download/@phenomnomnominal/tsquery-5.0.1.tgz","fileCount":78,"unpackedSize":62682,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCTRKxfjDBDfUW6ARWhBLcwoM67zDnO85ksRjTRlR/yKgIhAMZGFeZDOMx51Q+4PHXGD6pc6LxJhFyWWNMyQOce0VGE"}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJkFSmIACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2Vmo+GA/+MGSP/rxhIQfHYXHBtIOT8Z6x9WwV5yd4/5N1Z49gDS0wY7OG\r\np+m/K0+8pvAOTQLO470B5l91oaWNyVqGfYVAZVaU2vfQLjqBwnjl/NNeQfrF\r\ntA8NlraFqhI8h+OdGHmGj0KyYdDYS1S2ECKQgKkjo6Kn0EGfkg/8MDxCXd8U\r\nb081+2BxsTbszU4q1ykItdUzRC52hHbb0tzPcyqwS0UGuJPz3REpYN4fDn5J\r\nT/CziZAq6fpfCSAFM1xpEtKwVQTTfifc8NC/v9Tlbj8/t9vUrT7+hTLJCo3g\r\n2OtjYcqdVoWDplbBmk9NkyanzA6Xmms9UZn5iF/LAK/Gphg0JRLFKAc1fGIt\r\nGUblKszuVRl+u3AxIWpWY9fccARemrkV7Hih6H05LDJwy1y23pVrgRlzooc2\r\nMVzjqve+TEKniwRru4lz3NI3cfcUGj4lpsq+08Ha9bK9rkXGEyPCl7oTpNCc\r\nG8HJkkrP+WVjHTUNYz7KjFIhQYmUuq8sUakJIk9UaUSvimVrg+gbun2U7MeH\r\nL07Z5Lv25YCKP7CUK8wE3kPPzdH+JMstg3cenUn7ah8cinTeZgYbCFMzonqC\r\nxYLe9GkRaf+K606QfBji1V6HK2O4VvJYCqphbHmOrPPus0QplfSxbhs4hnkL\r\nIM7Ykqt8nzjsXNwY9FNz5sZYIyF6AK7UjGc=\r\n=yY6R\r\n-----END PGP SIGNATURE-----\r\n","size":16350,"noattachment":false,"key":"/@phenomnomnominal/tsquery/-/@phenomnomnominal/tsquery-5.0.1.tgz"},"_npmUser":{"name":"phenomnomnominal","email":"craigspence0@gmail.com"},"directories":{},"maintainers":[{"name":"phenomnomnominal","email":"craigspence0@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/tsquery_5.0.1_1679108488481_0.11676476092638666"},"_hasShrinkwrap":false,"readme":"# TSQuery\n\n[![npm version](https://img.shields.io/npm/v/@phenomnomnominal/tsquery.svg)](https://img.shields.io/npm/v/@phenomnomnominal/tsquery.svg)\n\nTSQuery is a port of the ESQuery API for TypeScript! TSQuery allows you to query a TypeScript AST for patterns of syntax using a CSS style selector system. \n\n## Demos:\n\n[ESQuery demo](https://estools.github.io/esquery/) - note that the demo requires JavaScript code, not TypeScript\n[TSQuery demo](https://tsquery-playground.firebaseapp.com) by [Uri Shaked](https://github.com/urish)\n\n## Installation\n\n```sh\nnpm install @phenomnomnominal/tsquery --save-dev\n```\n\n## Examples\n\nSay we want to select all instances of an identifier with name \"Animal\", e.g. the identifier in the `class` declaration, and the identifier in the `extends` declaration.\n\nWe would do something like the following:\n\n```ts\nimport { ast, query } from '@phenomnomnominal/tsquery';\n\nconst typescript = `\n\nclass Animal {\n    constructor(public name: string) { }\n    move(distanceInMeters: number = 0) {\n        console.log(\\`\\${this.name} moved \\${distanceInMeters}m.\\`);\n    }\n}\n\nclass Snake extends Animal {\n    constructor(name: string) { super(name); }\n    move(distanceInMeters = 5) {\n        console.log(\"Slithering...\");\n        super.move(distanceInMeters);\n    }\n}\n\n`;\n\nconst ast = ast(typescript);\nconst nodes = query(ast, 'Identifier[name=\"Animal\"]');\nconsole.log(nodes.length); // 2\n```\n\n### Selectors\n\nThe following selectors are supported:\n\n* AST node type: `ForStatement` (see [common node types](#common-ast-node-types))\n* [wildcard](http://dev.w3.org/csswg/selectors4/#universal-selector): `*`\n* [attribute existence](http://dev.w3.org/csswg/selectors4/#attribute-selectors): `[attr]`\n* [attribute value](http://dev.w3.org/csswg/selectors4/#attribute-selectors): `[attr=\"foo\"]` or `[attr=123]`\n* attribute regex: `[attr=/foo.*/]`\n* attribute conditions: `[attr!=\"foo\"]`, `[attr>2]`, `[attr<3]`, `[attr>=2]`, or `[attr<=3]`\n* nested attribute: `[attr.level2=\"foo\"]`\n* field: `FunctionDeclaration > Identifier.id`\n* [First](http://dev.w3.org/csswg/selectors4/#the-first-child-pseudo) or [last](http://dev.w3.org/csswg/selectors4/#the-last-child-pseudo) child: `:first-child` or `:last-child`\n* [nth-child](http://dev.w3.org/csswg/selectors4/#the-nth-child-pseudo) (no ax+b support): `:nth-child(2)`\n* [nth-last-child](http://dev.w3.org/csswg/selectors4/#the-nth-last-child-pseudo) (no ax+b support): `:nth-last-child(1)`\n* [descendant](http://dev.w3.org/csswg/selectors4/#descendant-combinators): `ancestor descendant`\n* [child](http://dev.w3.org/csswg/selectors4/#child-combinators): `parent > child`\n* [following sibling](http://dev.w3.org/csswg/selectors4/#general-sibling-combinators): `node ~ sibling`\n* [adjacent sibling](http://dev.w3.org/csswg/selectors4/#adjacent-sibling-combinators): `node + adjacent`\n* [negation](http://dev.w3.org/csswg/selectors4/#negation-pseudo): `:not(ForStatement)`\n* [matches-any](http://dev.w3.org/csswg/selectors4/#matches): `:matches([attr] > :first-child, :last-child)`\n* [has](https://drafts.csswg.org/selectors-4/#has-pseudo): `IfStatement:has([name=\"foo\"])`\n* class of AST node: `:statement`, `:expression`, `:declaration`, `:function`, or `:pattern`\n\n### Common AST node types\n\n* `Identifier` - any identifier (name of a function, class, variable, etc)\n* `IfStatement`, `ForStatement`, `WhileStatement`, `DoStatement` - control flow\n* `FunctionDeclaration`, `ClassDeclaration`, `ArrowFunction` - declarations\n* `VariableStatement` - var, const, let.\n* `ImportDeclaration` - any `import` statement\n* `StringLiteral` - any string\n* `TrueKeyword`, `FalseKeyword`, `NullKeyword`, `AnyKeyword` - various keywords\n* `CallExpression` - function call\n* `NumericLiteral` - any numeric constant\n* `NoSubstitutionTemplateLiteral`, `TemplateExpression` - template strings and expressions\n\n## API:\n\n### `ast`:\n\nParse a string of code into an Abstract Syntax Tree which can then be queried with TSQuery Selectors.\n\n```typescript\nimport { ast } from '@phenomnomnominal/tsquery';\n\nconst sourceFile = ast('const x = 1;');\n```\n\n### `includes`:\n\nCheck for `Nodes` within a given `string` of code or AST `Node` matching a `Selector`.\n\n```typescript\nimport { includes } from '@phenomnomnominal/tsquery';\n\nconst hasIdentifier = includes('const x = 1;', 'Identifier');\n```\n\n### `map`:\n\nTransform AST `Nodes` within a given `Node` matching a `Selector`. Can be used to do `Node`-based replacement or removal of parts of the input AST.\n\n```typescript\nimport { factory } from 'typescript';\nimport { map } from '@phenomnomnominal/tsquery';\n\nconst tree = ast('const x = 1;')\nconst updatedTree = map(tree, 'Identifier', () => factory.createIdentifier('y'));\n```\n\n### `match`:\n\nFind AST `Nodes` within a given AST `Node` matching a `Selector`.\n\n```typescript\nimport { ast, match } from '@phenomnomnominal/tsquery';\n\nconst tree = ast('const x = 1;')\nconst [xNode] = match(tree, 'Identifier');\n```\n\n### `parse`:\n\nParse a `string` into an [ESQuery](https://github.com/estools/esquery) `Selector`.\n\n```typescript\nimport { parse } from '@phenomnomnominal/tsquery';\n\nconst selector = parse(':matches([attr] > :first-child, :last-child)');\n```\n\n### `print`:\n\nPrint a given `Node` or `SourceFile` to a string, using the default TypeScript printer.\n\n```typescript\nimport { print } from '@phenomnomnominal/tsquery';\nimport { factory } from 'typescript';\n\n  // create synthetic node:\nconst node = factory.createArrowFunction(\n  // ...\n);\nconst code = print(node);\n```\n\n### `project`:\n\nGet all the `SourceFiles` included in a the TypeScript project described by a given config file.\n\n```typescript\nimport { project } from '@phenomnomnominal/tsquery';\n\nconst files = project('./tsconfig.json');\n```\n\n### `files`:\n\nGet all the file paths included ina the TypeScript project described by a given config file.\n\n```typescript\nimport { files } from '@phenomnomnominal/tsquery';\n\nconst filePaths = files('./tsconfig.json');\n```\n\n### `match`:\n\nFind AST `Nodes` within a given `string` of code or AST `Node` matching a `Selector`.\n\n```typescript\nimport {query } from '@phenomnomnominal/tsquery';\n\nconst [xNode] = query('const x = 1;', 'Identifier');\n```\n\n### `replace`:\n\nTransform AST `Nodes` within a given `Node` matching a `Selector`. Can be used to do string-based replacement or removal of parts of the input AST. The updated code will be printed with the TypeScript [`Printer`](https://github.com/microsoft/TypeScript-wiki/blob/main/Using-the-Compiler-API.md#creating-and-printing-a-typescript-ast), so you may need to run your own formatter on any output code.\n\n```typescript\nimport { replace } from '@phenomnomnominal/tsquery';\n\nconst updatedCode = replace('const x = 1;', 'Identifier', () => 'y'));\n```\n","publish_time":1679108488682,"_cnpm_publish_time":1679108488682,"dist-tags":{"latest":"6.2.0"}}