├── .editorconfig ├── .firebaserc ├── .github └── workflows │ └── nodejs.yml ├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── angular.json ├── e2e ├── protractor.conf.js ├── src │ ├── app.e2e-spec.ts │ └── app.po.ts └── tsconfig.e2e.json ├── firebase.json ├── jest.config.js ├── package.json ├── prettier.config.js ├── src ├── app │ ├── app.component.html │ ├── app.component.scss │ ├── app.component.ts │ ├── app.module.ts │ ├── ast-utils.ts │ ├── ast-viewer │ │ ├── ast-viewer.component.css │ │ ├── ast-viewer.component.html │ │ └── ast-viewer.component.ts │ ├── node-equals-to.pipe.ts │ ├── node-item │ │ ├── node-item.component.css │ │ ├── node-item.component.html │ │ ├── node-item.component.spec.ts │ │ └── node-item.component.ts │ └── scroll-into-view.directive.ts ├── assets │ ├── .gitkeep │ └── github-logo.svg ├── browserslist ├── dummy.ts ├── environments │ ├── environment.prod.ts │ └── environment.ts ├── favicon.ico ├── index.html ├── karma.conf.js ├── main.ts ├── polyfills.ts ├── styles.css ├── tsconfig.app.json └── tslint.json ├── tsconfig.json ├── tsconfig.spec.json ├── tslint.json ├── wallaby.js └── yarn.lock /.editorconfig: -------------------------------------------------------------------------------- 1 | # Editor configuration, see http://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | charset = utf-8 6 | indent_style = space 7 | indent_size = 2 8 | insert_final_newline = true 9 | trim_trailing_whitespace = true 10 | 11 | [*.md] 12 | max_line_length = off 13 | trim_trailing_whitespace = false 14 | -------------------------------------------------------------------------------- /.firebaserc: -------------------------------------------------------------------------------- 1 | { 2 | "projects": { 3 | "default": "tsquery-playground" 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /.github/workflows/nodejs.yml: -------------------------------------------------------------------------------- 1 | name: Node CI 2 | 3 | on: [push] 4 | 5 | jobs: 6 | build: 7 | 8 | runs-on: ubuntu-latest 9 | 10 | strategy: 11 | matrix: 12 | node-version: [10.x, 12.x] 13 | 14 | steps: 15 | - uses: actions/checkout@v1 16 | - name: Use Node.js ${{ matrix.node-version }} 17 | uses: actions/setup-node@v1 18 | with: 19 | node-version: ${{ matrix.node-version }} 20 | - name: npm install, build, and test 21 | run: | 22 | yarn 23 | yarn test 24 | yarn build --prod 25 | env: 26 | CI: true 27 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See http://help.github.com/ignore-files/ for more about ignoring files. 2 | 3 | # compiled output 4 | /dist 5 | /tmp 6 | /out-tsc 7 | 8 | # dependencies 9 | /node_modules 10 | 11 | # IDEs and editors 12 | /.idea 13 | .project 14 | .classpath 15 | .c9/ 16 | *.launch 17 | .settings/ 18 | *.sublime-workspace 19 | 20 | # IDE - VSCode 21 | .vscode/* 22 | !.vscode/settings.json 23 | !.vscode/tasks.json 24 | !.vscode/launch.json 25 | !.vscode/extensions.json 26 | 27 | # misc 28 | /.sass-cache 29 | /connect.lock 30 | /coverage 31 | /libpeerconnection.log 32 | npm-debug.log 33 | yarn-error.log 34 | testem.log 35 | /typings 36 | 37 | # System Files 38 | .DS_Store 39 | Thumbs.db 40 | 41 | # Firebase 42 | firebase-debug.log 43 | .firebase 44 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - 10 5 | cache: 6 | directories: 7 | - node_modules 8 | install: 9 | - yarn 10 | script: 11 | - yarn test 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2018 Uri Shaked and contributors 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # TSQuery Playground 2 | 3 | [![Build Status](https://travis-ci.org/urish/tsquery-playground.svg?branch=master)](https://travis-ci.org/urish/tsquery-playground) 4 | 5 | 🔍 [Live Demo](https://tsquery-playground.web.app/) 6 | 7 | ## Development server 8 | 9 | Run `yarn start` for a dev server, then navigate to `http://localhost:4200/`. 10 | 11 | ## License 12 | 13 | Copyright (C) 2018-2020, Uri Shaked. The code in this repo is published under the MIT license. 14 | -------------------------------------------------------------------------------- /angular.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "./node_modules/@angular/cli/lib/config/schema.json", 3 | "version": 1, 4 | "newProjectRoot": "projects", 5 | "projects": { 6 | "tsquery-playground": { 7 | "root": "", 8 | "sourceRoot": "src", 9 | "projectType": "application", 10 | "prefix": "app", 11 | "schematics": {}, 12 | "architect": { 13 | "build": { 14 | "builder": "@angular-devkit/build-angular:browser", 15 | "options": { 16 | "outputPath": "dist/tsquery-playground", 17 | "index": "src/index.html", 18 | "main": "src/main.ts", 19 | "polyfills": "src/polyfills.ts", 20 | "tsConfig": "src/tsconfig.app.json", 21 | "assets": ["src/favicon.ico", "src/assets"], 22 | "styles": [ 23 | { 24 | "input": "node_modules/@angular/material/prebuilt-themes/indigo-pink.css" 25 | }, 26 | "src/styles.css" 27 | ], 28 | "scripts": [] 29 | }, 30 | "configurations": { 31 | "production": { 32 | "fileReplacements": [ 33 | { 34 | "replace": "src/environments/environment.ts", 35 | "with": "src/environments/environment.prod.ts" 36 | } 37 | ], 38 | "optimization": true, 39 | "outputHashing": "all", 40 | "sourceMap": false, 41 | "extractCss": true, 42 | "namedChunks": false, 43 | "aot": true, 44 | "extractLicenses": true, 45 | "vendorChunk": false, 46 | "buildOptimizer": true 47 | } 48 | } 49 | }, 50 | "serve": { 51 | "builder": "@angular-devkit/build-angular:dev-server", 52 | "options": { 53 | "browserTarget": "tsquery-playground:build" 54 | }, 55 | "configurations": { 56 | "production": { 57 | "browserTarget": "tsquery-playground:build:production" 58 | } 59 | } 60 | }, 61 | "extract-i18n": { 62 | "builder": "@angular-devkit/build-angular:extract-i18n", 63 | "options": { 64 | "browserTarget": "tsquery-playground:build" 65 | } 66 | }, 67 | "test": { 68 | "builder": "@angular-builders/jest:run", 69 | "options": {} 70 | }, 71 | "lint": { 72 | "builder": "@angular-devkit/build-angular:tslint", 73 | "options": { 74 | "tsConfig": ["src/tsconfig.app.json", "src/tsconfig.spec.json"], 75 | "exclude": ["**/node_modules/**"] 76 | } 77 | } 78 | } 79 | }, 80 | "tsquery-playground-e2e": { 81 | "root": "e2e/", 82 | "projectType": "application", 83 | "architect": { 84 | "e2e": { 85 | "builder": "@angular-devkit/build-angular:protractor", 86 | "options": { 87 | "protractorConfig": "e2e/protractor.conf.js", 88 | "devServerTarget": "tsquery-playground:serve" 89 | } 90 | }, 91 | "lint": { 92 | "builder": "@angular-devkit/build-angular:tslint", 93 | "options": { 94 | "tsConfig": "e2e/tsconfig.e2e.json", 95 | "exclude": ["**/node_modules/**"] 96 | } 97 | } 98 | } 99 | } 100 | }, 101 | "defaultProject": "tsquery-playground", 102 | "cli": { 103 | "analytics": "d1eda589-77cc-407e-8be3-299d3e9591ac" 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /e2e/protractor.conf.js: -------------------------------------------------------------------------------- 1 | // Protractor configuration file, see link for more information 2 | // https://github.com/angular/protractor/blob/master/lib/config.ts 3 | 4 | const { SpecReporter } = require('jasmine-spec-reporter'); 5 | 6 | exports.config = { 7 | allScriptsTimeout: 11000, 8 | specs: [ 9 | './src/**/*.e2e-spec.ts' 10 | ], 11 | capabilities: { 12 | 'browserName': 'chrome' 13 | }, 14 | directConnect: true, 15 | baseUrl: 'http://localhost:4200/', 16 | framework: 'jasmine', 17 | jasmineNodeOpts: { 18 | showColors: true, 19 | defaultTimeoutInterval: 30000, 20 | print: function() {} 21 | }, 22 | onPrepare() { 23 | require('ts-node').register({ 24 | project: require('path').join(__dirname, './tsconfig.e2e.json') 25 | }); 26 | jasmine.getEnv().addReporter(new SpecReporter({ spec: { displayStacktrace: true } })); 27 | } 28 | }; -------------------------------------------------------------------------------- /e2e/src/app.e2e-spec.ts: -------------------------------------------------------------------------------- 1 | import { AppPage } from './app.po'; 2 | 3 | describe('workspace-project App', () => { 4 | let page: AppPage; 5 | 6 | beforeEach(() => { 7 | page = new AppPage(); 8 | }); 9 | 10 | it('should display welcome message', () => { 11 | page.navigateTo(); 12 | expect(page.getParagraphText()).toEqual('Welcome to app!'); 13 | }); 14 | }); 15 | -------------------------------------------------------------------------------- /e2e/src/app.po.ts: -------------------------------------------------------------------------------- 1 | import { browser, by, element } from 'protractor'; 2 | 3 | export class AppPage { 4 | navigateTo() { 5 | return browser.get('/'); 6 | } 7 | 8 | getParagraphText() { 9 | return element(by.css('app-root h1')).getText(); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /e2e/tsconfig.e2e.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "../out-tsc/app", 5 | "module": "commonjs", 6 | "target": "es5", 7 | "types": [ 8 | "jasmine", 9 | "jasminewd2", 10 | "node" 11 | ] 12 | } 13 | } -------------------------------------------------------------------------------- /firebase.json: -------------------------------------------------------------------------------- 1 | { 2 | "hosting": { 3 | "public": "dist/tsquery-playground", 4 | "ignore": ["firebase.json", "**/.*", "**/node_modules/**"] 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /jest.config.js: -------------------------------------------------------------------------------- 1 | module.exports = {}; 2 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "tsquery-playground", 3 | "version": "0.0.0", 4 | "scripts": { 5 | "ng": "ng", 6 | "start": "ng serve", 7 | "build": "ng build", 8 | "test": "ng test", 9 | "lint": "ng lint", 10 | "e2e": "ng e2e", 11 | "deploy": "yarn build --prod && firebase deploy", 12 | "precommit": "lint-staged" 13 | }, 14 | "private": true, 15 | "dependencies": { 16 | "@angular/animations": "^9.0.2", 17 | "@angular/cdk": "^9.0.1", 18 | "@angular/common": "^9.0.2", 19 | "@angular/compiler": "^9.0.2", 20 | "@angular/core": "^9.0.2", 21 | "@angular/forms": "^9.0.2", 22 | "@angular/material": "^9.0.1", 23 | "@angular/platform-browser": "^9.0.2", 24 | "@angular/platform-browser-dynamic": "^9.0.2", 25 | "@angular/router": "^9.0.2", 26 | "@ctrl/ngx-codemirror": "^2.1.1", 27 | "@phenomnomnominal/tsquery": "^3.0.0", 28 | "codemirror": "^5.58.2", 29 | "core-js": "^2.5.4", 30 | "rxjs": "^6.5.4", 31 | "zone.js": "^0.10.2" 32 | }, 33 | "devDependencies": { 34 | "@angular-builders/jest": "^8.2.0", 35 | "@angular-devkit/build-angular": "^0.900.3", 36 | "@angular/cli": "~9.0.3", 37 | "@angular/compiler-cli": "^9.0.2", 38 | "@angular/language-service": "^9.0.2", 39 | "@types/jasmine": "~2.8.6", 40 | "@types/jasminewd2": "~2.0.3", 41 | "@types/jest": "^24.0.18", 42 | "@types/node": "~8.9.4", 43 | "codelyzer": "^5.2.1", 44 | "husky": "^3.0.4", 45 | "jasmine-core": "~2.99.1", 46 | "jasmine-spec-reporter": "~4.2.1", 47 | "jest": "^24.9.0", 48 | "lint-staged": "^9.2.4", 49 | "prettier": "^1.18.2", 50 | "protractor": "~5.3.0", 51 | "ts-node": "^8.3.0", 52 | "tslib": "^1.11.0", 53 | "tslint": "~5.9.1", 54 | "typescript": "~3.7.5" 55 | }, 56 | "lint-staged": { 57 | "*.{js,json}": [ 58 | "prettier --write", 59 | "git add" 60 | ], 61 | "*.ts": [ 62 | "prettier --write", 63 | "tslint --fix", 64 | "git add" 65 | ] 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /prettier.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | arrowParens: 'always', 3 | printWidth: 100, 4 | singleQuote: true, 5 | tabWidth: 2, 6 | trailingComma: 'all', 7 | }; 8 | -------------------------------------------------------------------------------- /src/app/app.component.html: -------------------------------------------------------------------------------- 1 |
2 |
3 |

TSQuery Playground

4 |
5 | 6 | GitHub 7 | 8 |
9 | 10 | 11 | {{selectorError}} 12 | 13 |
14 |
15 |

Code

16 | 17 | 18 |
19 |
20 |
21 |

AST

22 |
23 | 24 |
25 |
26 |
27 |

28 | Total Matches: {{results.length}} 29 | 30 | © 2018-2020 Uri Shaked 31 |

32 |
33 | -------------------------------------------------------------------------------- /src/app/app.component.scss: -------------------------------------------------------------------------------- 1 | .container { 2 | display: flex; 3 | flex-direction: column; 4 | position: fixed; 5 | top: 0; 6 | bottom: 0; 7 | right: 0; 8 | left: 0; 9 | padding: 16px; 10 | } 11 | 12 | header { 13 | display: flex; 14 | align-items: center; 15 | } 16 | 17 | .github-logo { 18 | height: 26px; 19 | margin: 0 4px 3px 0; 20 | } 21 | 22 | .flex-spacer { 23 | flex: 1; 24 | } 25 | 26 | .split-view { 27 | display: flex; 28 | flex-direction: column; 29 | flex: 1; 30 | height: 100%; 31 | @media only screen and (min-width: 600px) { 32 | flex-direction: row; 33 | } 34 | } 35 | 36 | .query-input { 37 | width: 100%; 38 | margin-bottom: 8px; 39 | } 40 | 41 | .query-input .error-message { 42 | color: #f44336; 43 | } 44 | 45 | .split-view h3 { 46 | text-align: center; 47 | margin-bottom: 0; 48 | } 49 | 50 | .split-view > div { 51 | flex: 1; 52 | display: flex; 53 | flex-direction: column; 54 | height: 50%; 55 | overflow: scroll; 56 | 57 | @media only screen and (min-width: 600px) { 58 | height: 100%; 59 | } 60 | } 61 | 62 | .scrollable { 63 | overflow: scroll; 64 | } 65 | 66 | .split-view ngx-codemirror { 67 | flex: 1; 68 | display: flex; 69 | } 70 | 71 | .split-view ::ng-deep .CodeMirror { 72 | flex: 1; 73 | height: 100%; 74 | } 75 | 76 | .split-view .spacer { 77 | flex: 0; 78 | min-width: 12px; 79 | } 80 | 81 | .status-bar { 82 | display: flex; 83 | } 84 | -------------------------------------------------------------------------------- /src/app/app.component.ts: -------------------------------------------------------------------------------- 1 | import { AfterViewInit, Component, ViewChild } from '@angular/core'; 2 | import { CodemirrorComponent } from '@ctrl/ngx-codemirror'; 3 | import { tsquery } from '@phenomnomnominal/tsquery'; 4 | import { Doc, TextMarker } from 'codemirror'; 5 | import 'codemirror/mode/javascript/javascript'; 6 | import * as ts from 'typescript'; 7 | import { nodeToMarker, positionToNode } from './ast-utils'; 8 | 9 | const matchHighlightClass = 'ast-match-highlight'; 10 | 11 | @Component({ 12 | selector: 'app-root', 13 | templateUrl: './app.component.html', 14 | styleUrls: ['./app.component.scss'], 15 | }) 16 | export class AppComponent implements AfterViewInit { 17 | @ViewChild('codeEditor', { static: false }) codeEditor: CodemirrorComponent; 18 | 19 | private _sourceCode = 20 | 'const magic = 5;\n\nfunction f(n:any){\n return n+n;\n}\n\n\nfunction g() {\n return f(magic);\n}\n\nconsole.log(g());'; 21 | query = 'FunctionDeclaration'; 22 | ast: ts.SourceFile | null = null; 23 | activeNode: ts.Node | null = null; 24 | selectorError: string | null = null; 25 | results: ts.Node[] = []; 26 | 27 | readonly codemirrorOptions = { 28 | lineNumbers: true, 29 | theme: 'material', 30 | mode: { name: 'javascript', typescript: true }, 31 | }; 32 | 33 | private markers: TextMarker[] = []; 34 | 35 | ngAfterViewInit() { 36 | setTimeout(() => this.runQuery()); 37 | } 38 | 39 | get sourceCode() { 40 | return this._sourceCode; 41 | } 42 | 43 | set sourceCode(value: string) { 44 | if (value !== this._sourceCode) { 45 | this._sourceCode = value; 46 | this.runQuery(); 47 | } 48 | } 49 | 50 | updateQuery(query: string) { 51 | this.query = query; 52 | this.runQuery(); 53 | } 54 | 55 | get doc() { 56 | return (this.codeEditor.codeMirror as any) as Doc; 57 | } 58 | 59 | runQuery() { 60 | this.ast = tsquery.ast(this.sourceCode, 'playground.ts', ts.ScriptKind.TSX); 61 | this.selectorError = null; 62 | try { 63 | this.results = tsquery(this.ast, this.query, { visitAllChildren: true }); 64 | } catch (err) { 65 | this.selectorError = err.toString(); 66 | return; 67 | } 68 | const { doc } = this; 69 | if (doc) { 70 | this.clearMarkers(); 71 | const markerPositions = this.results.map(nodeToMarker); 72 | this.markers = markerPositions.map(({ start, end }) => 73 | doc.markText(start, end, { 74 | className: matchHighlightClass, 75 | title: this.query, 76 | }), 77 | ); 78 | } 79 | } 80 | 81 | cursorMoved() { 82 | if (this.ast) { 83 | const cursorPos = this.doc.getCursor(); 84 | this.activeNode = positionToNode(this.ast, cursorPos); 85 | } 86 | } 87 | 88 | private clearMarkers() { 89 | for (const marker of this.markers) { 90 | marker.clear(); 91 | } 92 | this.markers = []; 93 | } 94 | 95 | activateNode(node: ts.Node) { 96 | const { start, end } = nodeToMarker(node); 97 | this.doc.setSelection(end, start, { scroll: true }); 98 | this.activeNode = node; 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /src/app/app.module.ts: -------------------------------------------------------------------------------- 1 | import { NgModule } from '@angular/core'; 2 | import { FormsModule } from '@angular/forms'; 3 | import { MatButtonModule } from '@angular/material/button'; 4 | import { MatIconModule } from '@angular/material/icon'; 5 | import { MatInputModule } from '@angular/material/input'; 6 | import { MatToolbarModule } from '@angular/material/toolbar'; 7 | import { MatTreeModule } from '@angular/material/tree'; 8 | import { BrowserModule } from '@angular/platform-browser'; 9 | import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; 10 | import { CodemirrorModule } from '@ctrl/ngx-codemirror'; 11 | import { AppComponent } from './app.component'; 12 | import { AstViewerComponent } from './ast-viewer/ast-viewer.component'; 13 | import { NodeEqualsToPipe } from './node-equals-to.pipe'; 14 | import { NodeItemComponent } from './node-item/node-item.component'; 15 | import { ScrollIntoViewDirective } from './scroll-into-view.directive'; 16 | 17 | @NgModule({ 18 | imports: [ 19 | BrowserModule, 20 | FormsModule, 21 | CodemirrorModule, 22 | BrowserAnimationsModule, 23 | MatToolbarModule, 24 | MatInputModule, 25 | MatButtonModule, 26 | MatTreeModule, 27 | MatIconModule, 28 | ], 29 | declarations: [ 30 | AppComponent, 31 | AstViewerComponent, 32 | ScrollIntoViewDirective, 33 | NodeEqualsToPipe, 34 | NodeItemComponent, 35 | ], 36 | bootstrap: [AppComponent], 37 | }) 38 | export class AppModule {} 39 | -------------------------------------------------------------------------------- /src/app/ast-utils.ts: -------------------------------------------------------------------------------- 1 | import { Position } from 'codemirror'; 2 | import * as ts from 'typescript'; 3 | 4 | export function astChildren(node: ts.Node) { 5 | const result = []; 6 | ts.forEachChild(node, (child) => (result.push(child), false)); 7 | return result; 8 | } 9 | 10 | function lineCh({ line, character }: { line: number; character: number }): Position { 11 | return { line, ch: character }; 12 | } 13 | 14 | export function nodeToMarker(node: ts.Node) { 15 | const sourceFile = node.getSourceFile(); 16 | return { 17 | start: lineCh(ts.getLineAndCharacterOfPosition(sourceFile, node.getStart())), 18 | end: lineCh(ts.getLineAndCharacterOfPosition(sourceFile, node.getEnd())), 19 | }; 20 | } 21 | 22 | export function getNodeAtFileOffset(node: ts.Node, offset: number) { 23 | let result = null as ts.Node | null; 24 | const visit = (childNode: ts.Node) => { 25 | ts.forEachChild(childNode, visit); 26 | if (!result && (childNode.getStart() <= offset && childNode.getEnd() > offset)) { 27 | result = childNode; 28 | } 29 | }; 30 | visit(node); 31 | return result; 32 | } 33 | 34 | export function positionToNode(ast: ts.SourceFile, position: Position) { 35 | return getNodeAtFileOffset( 36 | ast, 37 | ts.getPositionOfLineAndCharacter(ast, position.line, position.ch), 38 | ); 39 | } 40 | -------------------------------------------------------------------------------- /src/app/ast-viewer/ast-viewer.component.css: -------------------------------------------------------------------------------- 1 | :host { 2 | display: flex; 3 | } 4 | 5 | .ast-tree { 6 | flex: 1; 7 | } 8 | 9 | .ast-tree-invisible { 10 | display: none; 11 | } 12 | 13 | .ast-tree ul, 14 | .ast-tree li { 15 | margin-top: 0; 16 | margin-bottom: 0; 17 | list-style-type: none; 18 | } 19 | 20 | .highlight { 21 | background: yellow; 22 | } 23 | 24 | .ast-node-name { 25 | font-weight: inherit; 26 | } 27 | -------------------------------------------------------------------------------- /src/app/ast-viewer/ast-viewer.component.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
  • 4 | 5 | 8 |
  • 9 |
    10 | 11 | 12 |
  • 13 |
    14 | 19 | 22 |
    23 |
      24 | 25 |
    26 |
  • 27 |
    28 |
    29 | -------------------------------------------------------------------------------- /src/app/ast-viewer/ast-viewer.component.ts: -------------------------------------------------------------------------------- 1 | import { NestedTreeControl } from '@angular/cdk/tree'; 2 | import { Component, EventEmitter, Input, OnChanges, OnInit, Output } from '@angular/core'; 3 | import { MatTreeNestedDataSource } from '@angular/material/tree'; 4 | import { of as observableOf } from 'rxjs'; 5 | import * as ts from 'typescript'; 6 | import { astChildren } from '../ast-utils'; 7 | 8 | @Component({ 9 | selector: 'app-ast-viewer', 10 | templateUrl: './ast-viewer.component.html', 11 | styleUrls: ['./ast-viewer.component.css'], 12 | }) 13 | export class AstViewerComponent implements OnInit, OnChanges { 14 | @Input() ast: ts.Node; 15 | @Input() highlightNode: ts.Node; 16 | @Output() nodeSelected = new EventEmitter(); 17 | 18 | treeControl = new NestedTreeControl((node: ts.Node) => observableOf(astChildren(node))); 19 | dataSource = new MatTreeNestedDataSource(); 20 | 21 | constructor() {} 22 | 23 | ngOnInit() { 24 | this.dataSource.data = this.ast ? [this.ast] : []; 25 | } 26 | 27 | ngOnChanges() { 28 | this.dataSource.data = this.ast ? [this.ast] : []; 29 | this.treeControl.expand(this.ast); 30 | if (this.highlightNode) { 31 | this.revealHighlightedNode(this.ast); 32 | } 33 | } 34 | 35 | hasChild(_: number, node: ts.Node) { 36 | return node.getChildCount() > 0; 37 | } 38 | 39 | private revealHighlightedNode(node: ts.Node) { 40 | if ( 41 | node.getStart() <= this.highlightNode.getStart() && 42 | node.getEnd() >= this.highlightNode.getEnd() 43 | ) { 44 | this.treeControl.expand(node); 45 | ts.forEachChild(node, (child) => { 46 | this.revealHighlightedNode(child); 47 | }); 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/app/node-equals-to.pipe.ts: -------------------------------------------------------------------------------- 1 | import { Pipe, PipeTransform } from '@angular/core'; 2 | import * as ts from 'typescript'; 3 | 4 | @Pipe({ 5 | name: 'nodeEqualsTo', 6 | pure: true, 7 | }) 8 | export class NodeEqualsToPipe implements PipeTransform { 9 | transform(node: ts.Node, other: ts.Node): any { 10 | if (!node || !other) { 11 | return node === other; 12 | } 13 | return ( 14 | node.getStart() === other.getStart() && 15 | node.getEnd() === other.getEnd() && 16 | node.kind === other.kind 17 | ); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/app/node-item/node-item.component.css: -------------------------------------------------------------------------------- 1 | .node-attribute { 2 | color: gray; 3 | } 4 | 5 | .node-attribute:before { 6 | display: inline-block; 7 | content: " "; 8 | } 9 | -------------------------------------------------------------------------------- /src/app/node-item/node-item.component.html: -------------------------------------------------------------------------------- 1 | {{nodeInfo.kindName}} 2 | 3 | [value={{nodeInfo.value}}] 4 | 5 | 6 | [name={{nodeInfo.text}}] 7 | 8 | -------------------------------------------------------------------------------- /src/app/node-item/node-item.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { async, ComponentFixture, TestBed, tick, fakeAsync } from '@angular/core/testing'; 2 | import * as ts from 'typescript'; 3 | import { NodeItemComponent } from './node-item.component'; 4 | 5 | function normalize(value: string) { 6 | return value.replace(/\s+/g, ' ').trim(); 7 | } 8 | 9 | describe('NodeItemComponent', () => { 10 | let component: NodeItemComponent; 11 | let fixture: ComponentFixture; 12 | 13 | beforeEach(async(() => { 14 | TestBed.configureTestingModule({ 15 | declarations: [NodeItemComponent], 16 | }).compileComponents(); 17 | })); 18 | 19 | beforeEach(() => { 20 | fixture = TestBed.createComponent(NodeItemComponent); 21 | component = fixture.componentInstance; 22 | }); 23 | 24 | it('should display the name for Identifier nodes', () => { 25 | component.node = ts.createIdentifier('foo'); 26 | fixture.detectChanges(); 27 | const { textContent } = fixture.nativeElement; 28 | expect(normalize(textContent)).toEqual('Identifier [name=foo]'); 29 | }); 30 | 31 | it('should display the value for NumericLiteral nodes', () => { 32 | component.node = ts.createNumericLiteral('5'); 33 | fixture.detectChanges(); 34 | const { textContent } = fixture.nativeElement; 35 | expect(normalize(textContent)).toEqual('NumericLiteral [value=5]'); 36 | }); 37 | 38 | it('should display the value for StringLiteral nodes', () => { 39 | component.node = ts.createStringLiteral('hello'); 40 | fixture.detectChanges(); 41 | const { textContent } = fixture.nativeElement; 42 | expect(normalize(textContent)).toEqual('StringLiteral [value=hello]'); 43 | }); 44 | }); 45 | -------------------------------------------------------------------------------- /src/app/node-item/node-item.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, OnInit, Input } from '@angular/core'; 2 | import * as ts from 'typescript'; 3 | import { getProperties } from '@phenomnomnominal/tsquery/dist/src/traverse'; 4 | 5 | @Component({ 6 | selector: 'app-node-item', 7 | templateUrl: './node-item.component.html', 8 | styleUrls: ['./node-item.component.css'], 9 | }) 10 | export class NodeItemComponent implements OnInit { 11 | @Input() node: ts.Node; 12 | 13 | constructor() {} 14 | 15 | ngOnInit() {} 16 | 17 | get nodeInfo() { 18 | return getProperties(this.node); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/app/scroll-into-view.directive.ts: -------------------------------------------------------------------------------- 1 | import { Directive, ElementRef, OnChanges, Input } from '@angular/core'; 2 | 3 | @Directive({ 4 | selector: '[appScrollIntoView]', 5 | }) 6 | export class ScrollIntoViewDirective implements OnChanges { 7 | @Input() appScrollIntoView: boolean; 8 | 9 | constructor(private elRef: ElementRef) {} 10 | 11 | ngOnChanges() { 12 | if (this.appScrollIntoView) { 13 | // give the tree time to animate and expand 14 | setTimeout( 15 | () => 16 | this.elRef.nativeElement.scrollIntoView({ 17 | behavior: 'smooth', 18 | inline: 'start', 19 | }), 20 | 150, 21 | ); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/assets/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/urish/tsquery-playground/bfbceea8a2b1fa6512b8df65f516647c6e452fda/src/assets/.gitkeep -------------------------------------------------------------------------------- /src/assets/github-logo.svg: -------------------------------------------------------------------------------- 1 | github-circle-white-transparent 2 | -------------------------------------------------------------------------------- /src/browserslist: -------------------------------------------------------------------------------- 1 | # This file is currently used by autoprefixer to adjust CSS to support the below specified browsers 2 | # For additional information regarding the format and rule options, please see: 3 | # https://github.com/browserslist/browserslist#queries 4 | # For IE 9-11 support, please uncomment the last line of the file and adjust as needed 5 | > 0.5% 6 | last 2 versions 7 | Firefox ESR 8 | not dead 9 | # IE 9-11 -------------------------------------------------------------------------------- /src/dummy.ts: -------------------------------------------------------------------------------- 1 | // just an empty file referenced from `tsconfig.json` to fix broken TypeScript imports 2 | -------------------------------------------------------------------------------- /src/environments/environment.prod.ts: -------------------------------------------------------------------------------- 1 | export const environment = { 2 | production: true 3 | }; 4 | -------------------------------------------------------------------------------- /src/environments/environment.ts: -------------------------------------------------------------------------------- 1 | // This file can be replaced during build by using the `fileReplacements` array. 2 | // `ng build ---prod` replaces `environment.ts` with `environment.prod.ts`. 3 | // The list of file replacements can be found in `angular.json`. 4 | 5 | export const environment = { 6 | production: false 7 | }; 8 | 9 | /* 10 | * In development mode, to ignore zone related error stack frames such as 11 | * `zone.run`, `zoneDelegate.invokeTask` for easier debugging, you can 12 | * import the following file, but please comment it out in production mode 13 | * because it will have performance impact when throw error 14 | */ 15 | // import 'zone.js/dist/zone-error'; // Included with Angular CLI. 16 | -------------------------------------------------------------------------------- /src/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/urish/tsquery-playground/bfbceea8a2b1fa6512b8df65f516647c6e452fda/src/favicon.ico -------------------------------------------------------------------------------- /src/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | TSQuery Playground 8 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /src/karma.conf.js: -------------------------------------------------------------------------------- 1 | // Karma configuration file, see link for more information 2 | // https://karma-runner.github.io/1.0/config/configuration-file.html 3 | 4 | module.exports = function(config) { 5 | config.set({ 6 | basePath: '', 7 | frameworks: ['jasmine', '@angular-devkit/build-angular'], 8 | plugins: [ 9 | require('karma-jasmine'), 10 | require('karma-chrome-launcher'), 11 | require('karma-jasmine-html-reporter'), 12 | require('karma-coverage-istanbul-reporter'), 13 | require('@angular-devkit/build-angular/plugins/karma'), 14 | ], 15 | client: { 16 | clearContext: false, // leave Jasmine Spec Runner output visible in browser 17 | }, 18 | coverageIstanbulReporter: { 19 | dir: require('path').join(__dirname, '../coverage'), 20 | reports: ['html', 'lcovonly'], 21 | fixWebpackSourcePaths: true, 22 | }, 23 | reporters: ['progress', 'kjhtml'], 24 | port: 9876, 25 | colors: true, 26 | logLevel: config.LOG_INFO, 27 | autoWatch: true, 28 | browsers: ['Chrome'], 29 | customLaunchers: { 30 | ChromeNoSandbox: { 31 | base: 'Chrome', 32 | flags: ['--no-sandbox'], 33 | }, 34 | }, 35 | singleRun: false, 36 | }); 37 | }; 38 | -------------------------------------------------------------------------------- /src/main.ts: -------------------------------------------------------------------------------- 1 | import { enableProdMode } from '@angular/core'; 2 | import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; 3 | 4 | import { AppModule } from './app/app.module'; 5 | import { environment } from './environments/environment'; 6 | 7 | if (environment.production) { 8 | enableProdMode(); 9 | } 10 | 11 | platformBrowserDynamic().bootstrapModule(AppModule) 12 | .catch(err => console.log(err)); 13 | -------------------------------------------------------------------------------- /src/polyfills.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * This file includes polyfills needed by Angular and is loaded before the app. 3 | * You can add your own extra polyfills to this file. 4 | * 5 | * This file is divided into 2 sections: 6 | * 1. Browser polyfills. These are applied before loading ZoneJS and are sorted by browsers. 7 | * 2. Application imports. Files imported after ZoneJS that should be loaded before your main 8 | * file. 9 | * 10 | * The current setup is for so-called "evergreen" browsers; the last versions of browsers that 11 | * automatically update themselves. This includes Safari >= 10, Chrome >= 55 (including Opera), 12 | * Edge >= 13 on the desktop, and iOS 10 and Chrome on mobile. 13 | * 14 | * Learn more in https://angular.io/docs/ts/latest/guide/browser-support.html 15 | */ 16 | 17 | /*************************************************************************************************** 18 | * BROWSER POLYFILLS 19 | */ 20 | 21 | /** IE9, IE10 and IE11 requires all of the following polyfills. **/ 22 | // import 'core-js/es6/symbol'; 23 | // import 'core-js/es6/object'; 24 | // import 'core-js/es6/function'; 25 | // import 'core-js/es6/parse-int'; 26 | // import 'core-js/es6/parse-float'; 27 | // import 'core-js/es6/number'; 28 | // import 'core-js/es6/math'; 29 | // import 'core-js/es6/string'; 30 | // import 'core-js/es6/date'; 31 | // import 'core-js/es6/array'; 32 | // import 'core-js/es6/regexp'; 33 | // import 'core-js/es6/map'; 34 | // import 'core-js/es6/weak-map'; 35 | // import 'core-js/es6/set'; 36 | 37 | /** IE10 and IE11 requires the following for NgClass support on SVG elements */ 38 | // import 'classlist.js'; // Run `npm install --save classlist.js`. 39 | 40 | /** IE10 and IE11 requires the following for the Reflect API. */ 41 | // import 'core-js/es6/reflect'; 42 | 43 | 44 | /** Evergreen browsers require these. **/ 45 | // Used for reflect-metadata in JIT. If you use AOT (and only Angular decorators), you can remove. 46 | import 'core-js/es7/reflect'; 47 | 48 | 49 | /** 50 | * Web Animations `@angular/platform-browser/animations` 51 | * Only required if AnimationBuilder is used within the application and using IE/Edge or Safari. 52 | * Standard animation support in Angular DOES NOT require any polyfills (as of Angular 6.0). 53 | **/ 54 | // import 'web-animations-js'; // Run `npm install --save web-animations-js`. 55 | 56 | /** 57 | * By default, zone.js will patch all possible macroTask and DomEvents 58 | * user can disable parts of macroTask/DomEvents patch by setting following flags 59 | */ 60 | 61 | // (window as any).__Zone_disable_requestAnimationFrame = true; // disable patch requestAnimationFrame 62 | // (window as any).__Zone_disable_on_property = true; // disable patch onProperty such as onclick 63 | // (window as any).__zone_symbol__BLACK_LISTED_EVENTS = ['scroll', 'mousemove']; // disable patch specified eventNames 64 | 65 | /* 66 | * in IE/Edge developer tools, the addEventListener will also be wrapped by zone.js 67 | * with the following flag, it will bypass `zone.js` patch for IE/Edge 68 | */ 69 | // (window as any).__Zone_enable_cross_context_check = true; 70 | 71 | /*************************************************************************************************** 72 | * Zone JS is required by default for Angular itself. 73 | */ 74 | import 'zone.js/dist/zone'; // Included with Angular CLI. 75 | 76 | 77 | 78 | /*************************************************************************************************** 79 | * APPLICATION IMPORTS 80 | */ 81 | -------------------------------------------------------------------------------- /src/styles.css: -------------------------------------------------------------------------------- 1 | @import '~codemirror/lib/codemirror.css'; 2 | @import '~codemirror/theme/material.css'; 3 | 4 | .ast-match-highlight { 5 | background-color: rgba(255, 255, 0, 0.3); 6 | } 7 | 8 | .CodeMirror { 9 | font-size: 20px; 10 | } 11 | -------------------------------------------------------------------------------- /src/tsconfig.app.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "../out-tsc/app", 5 | "module": "es2015", 6 | "types": [] 7 | }, 8 | "exclude": [ 9 | "src/test.ts", 10 | "**/*.spec.ts" 11 | ] 12 | } 13 | -------------------------------------------------------------------------------- /src/tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../tslint.json", 3 | "rules": { 4 | "directive-selector": [ 5 | true, 6 | "attribute", 7 | "app", 8 | "camelCase" 9 | ], 10 | "component-selector": [ 11 | true, 12 | "element", 13 | "app", 14 | "kebab-case" 15 | ] 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compileOnSave": false, 3 | "compilerOptions": { 4 | "baseUrl": "./", 5 | "outDir": "./dist/out-tsc", 6 | "sourceMap": true, 7 | "declaration": false, 8 | "moduleResolution": "node", 9 | "emitDecoratorMetadata": true, 10 | "experimentalDecorators": true, 11 | "target": "es5", 12 | "typeRoots": ["node_modules/@types"], 13 | "lib": ["es2017", "dom"], 14 | "types": ["jest"], 15 | "paths": { 16 | "crypto": ["src/dummy.ts"], 17 | "fs": ["src/dummy.ts"], 18 | "path": ["src/dummy.ts"], 19 | "source-map-support": ["src/dummy.ts"] 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /tsconfig.spec.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "./out-tsc/spec", 5 | "module": "commonjs", 6 | "types": ["jest"] 7 | }, 8 | "files": ["src/polyfills.ts", "src/dummy.ts"], 9 | "include": ["**/*.spec.ts", "**/*.d.ts"] 10 | } 11 | -------------------------------------------------------------------------------- /tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "rulesDirectory": [ 3 | "node_modules/codelyzer" 4 | ], 5 | "rules": { 6 | "arrow-return-shorthand": true, 7 | "callable-types": true, 8 | "class-name": true, 9 | "comment-format": [ 10 | true, 11 | "check-space" 12 | ], 13 | "curly": true, 14 | "deprecation": { 15 | "severity": "warn" 16 | }, 17 | "eofline": true, 18 | "forin": true, 19 | "import-blacklist": [ 20 | true, 21 | "rxjs/Rx" 22 | ], 23 | "import-spacing": true, 24 | "indent": [ 25 | true, 26 | "spaces" 27 | ], 28 | "interface-over-type-literal": true, 29 | "label-position": true, 30 | "max-line-length": [ 31 | true, 32 | 140 33 | ], 34 | "member-access": false, 35 | "member-ordering": [ 36 | true, 37 | { 38 | "order": [ 39 | "static-field", 40 | "instance-field", 41 | "static-method", 42 | "instance-method" 43 | ] 44 | } 45 | ], 46 | "no-arg": true, 47 | "no-bitwise": true, 48 | "no-console": [ 49 | true, 50 | "debug", 51 | "info", 52 | "time", 53 | "timeEnd", 54 | "trace" 55 | ], 56 | "no-construct": true, 57 | "no-debugger": true, 58 | "no-duplicate-super": true, 59 | "no-empty": false, 60 | "no-empty-interface": true, 61 | "no-eval": true, 62 | "no-inferrable-types": [ 63 | true, 64 | "ignore-params" 65 | ], 66 | "no-misused-new": true, 67 | "no-non-null-assertion": true, 68 | "no-shadowed-variable": true, 69 | "no-string-literal": false, 70 | "no-string-throw": true, 71 | "no-switch-case-fall-through": true, 72 | "no-trailing-whitespace": true, 73 | "no-unnecessary-initializer": true, 74 | "no-unused-expression": true, 75 | "no-use-before-declare": true, 76 | "no-var-keyword": true, 77 | "object-literal-sort-keys": false, 78 | "one-line": [ 79 | true, 80 | "check-open-brace", 81 | "check-catch", 82 | "check-else", 83 | "check-whitespace" 84 | ], 85 | "prefer-const": true, 86 | "quotemark": [ 87 | true, 88 | "single" 89 | ], 90 | "radix": true, 91 | "semicolon": [ 92 | true, 93 | "always" 94 | ], 95 | "triple-equals": [ 96 | true, 97 | "allow-null-check" 98 | ], 99 | "typedef-whitespace": [ 100 | true, 101 | { 102 | "call-signature": "nospace", 103 | "index-signature": "nospace", 104 | "parameter": "nospace", 105 | "property-declaration": "nospace", 106 | "variable-declaration": "nospace" 107 | } 108 | ], 109 | "unified-signatures": true, 110 | "variable-name": false, 111 | "whitespace": [ 112 | true, 113 | "check-branch", 114 | "check-decl", 115 | "check-operator", 116 | "check-separator", 117 | "check-type" 118 | ], 119 | "no-output-on-prefix": true, 120 | "use-input-property-decorator": true, 121 | "use-output-property-decorator": true, 122 | "use-host-property-decorator": true, 123 | "no-input-rename": true, 124 | "no-output-rename": true, 125 | "use-life-cycle-interface": true, 126 | "use-pipe-transform-interface": true, 127 | "component-class-suffix": true, 128 | "directive-class-suffix": true 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /wallaby.js: -------------------------------------------------------------------------------- 1 | module.exports = function() { 2 | const jestTransform = (file) => 3 | require('jest-preset-angular/preprocessor').process(file.content, file.path, { 4 | globals: { __TRANSFORM_HTML__: true }, 5 | rootDir: __dirname, 6 | }); 7 | 8 | return { 9 | files: [ 10 | 'src/**/*.+(ts|html|json|snap|css|less|sass|scss|jpg|jpeg|gif|png|svg)', 11 | 'jest.setup.ts', 12 | '!src/**/*.spec.ts', 13 | ], 14 | 15 | tests: ['src/**/*.spec.ts'], 16 | 17 | env: { 18 | type: 'node', 19 | runner: 'node', 20 | }, 21 | 22 | compilers: { 23 | '**/*.html': (file) => ({ 24 | code: jestTransform(file), 25 | map: { version: 3, sources: [], names: [], mappings: [] }, 26 | ranges: [], 27 | }), 28 | }, 29 | 30 | preprocessors: { 31 | 'src/**/*.js': jestTransform, 32 | }, 33 | 34 | testFramework: 'jest', 35 | }; 36 | }; 37 | --------------------------------------------------------------------------------