Use tree-sitter-typescript from crates.io

pull/795/head
Wilfred Hughes 2024-12-20 00:09:12 +07:00
parent 88731f932d
commit 776d978f09
67 changed files with 20 additions and 861433 deletions

@ -11,7 +11,7 @@ with YAML.
Improved language detection when one argument is a named pipe. Improved language detection when one argument is a named pipe.
Updated to the latest tree-sitter parser for C, C++, C#, Haskell, Updated to the latest tree-sitter parser for C, C++, C#, Haskell,
JavaScript, Objective-C, OCaml, Python, Ruby and Scala. JavaScript, Objective-C, OCaml, Python, Ruby, Scala and TypeScript.
### Syntax Highlighting ### Syntax Highlighting

11
Cargo.lock generated

@ -260,6 +260,7 @@ dependencies = [
"tree-sitter-python", "tree-sitter-python",
"tree-sitter-ruby", "tree-sitter-ruby",
"tree-sitter-scala", "tree-sitter-scala",
"tree-sitter-typescript",
"tree_magic_mini", "tree_magic_mini",
"typed-arena", "typed-arena",
"unicode-width", "unicode-width",
@ -1117,6 +1118,16 @@ dependencies = [
"tree-sitter-language", "tree-sitter-language",
] ]
[[package]]
name = "tree-sitter-typescript"
version = "0.23.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c5f76ed8d947a75cc446d5fccd8b602ebf0cde64ccf2ffa434d873d7a575eff"
dependencies = [
"cc",
"tree-sitter-language",
]
[[package]] [[package]]
name = "tree_magic_mini" name = "tree_magic_mini"
version = "3.1.5" version = "3.1.5"

@ -88,6 +88,7 @@ tree-sitter-python = "0.23.5"
tree-sitter-cpp = "0.23.4" tree-sitter-cpp = "0.23.4"
tree-sitter-c = "0.23.4" tree-sitter-c = "0.23.4"
tree-sitter-javascript = "0.23.1" tree-sitter-javascript = "0.23.1"
tree-sitter-typescript = "0.23.2"
[dev-dependencies] [dev-dependencies]
# assert_cmd 2.0.10 requires predicates 3. # assert_cmd 2.0.10 requires predicates 3.

@ -289,16 +289,6 @@ fn main() {
src_dir: "vendored_parsers/tree-sitter-toml-src", src_dir: "vendored_parsers/tree-sitter-toml-src",
extra_files: vec!["scanner.c"], extra_files: vec!["scanner.c"],
}, },
TreeSitterParser {
name: "tree-sitter-tsx",
src_dir: "vendored_parsers/tree-sitter-typescript-src/tsx/src",
extra_files: vec!["scanner.c"],
},
TreeSitterParser {
name: "tree-sitter-typescript",
src_dir: "vendored_parsers/tree-sitter-typescript-src/typescript/src",
extra_files: vec!["scanner.c"],
},
TreeSitterParser { TreeSitterParser {
name: "tree-sitter-vhdl", name: "tree-sitter-vhdl",
src_dir: "vendored_parsers/tree-sitter-vhdl-src", src_dir: "vendored_parsers/tree-sitter-vhdl-src",

@ -106,8 +106,6 @@ extern "C" {
fn tree_sitter_sql() -> ts::Language; fn tree_sitter_sql() -> ts::Language;
fn tree_sitter_swift() -> ts::Language; fn tree_sitter_swift() -> ts::Language;
fn tree_sitter_toml() -> ts::Language; fn tree_sitter_toml() -> ts::Language;
fn tree_sitter_tsx() -> ts::Language;
fn tree_sitter_typescript() -> ts::Language;
fn tree_sitter_vhdl() -> ts::Language; fn tree_sitter_vhdl() -> ts::Language;
fn tree_sitter_xml() -> ts::Language; fn tree_sitter_xml() -> ts::Language;
fn tree_sitter_yaml() -> ts::Language; fn tree_sitter_yaml() -> ts::Language;
@ -874,9 +872,7 @@ pub(crate) fn from_language(language: guess::Language) -> TreeSitterConfig {
let language = unsafe { tree_sitter_qmljs() }; let language = unsafe { tree_sitter_qmljs() };
let mut highlight_query = tree_sitter_javascript::HIGHLIGHT_QUERY.to_owned(); let mut highlight_query = tree_sitter_javascript::HIGHLIGHT_QUERY.to_owned();
highlight_query.push_str(include_str!( highlight_query.push_str(tree_sitter_typescript::HIGHLIGHTS_QUERY);
"../../vendored_parsers/highlights/typescript.scm"
));
highlight_query.push_str(include_str!("../../vendored_parsers/highlights/qmljs.scm")); highlight_query.push_str(include_str!("../../vendored_parsers/highlights/qmljs.scm"));
TreeSitterConfig { TreeSitterConfig {
@ -1070,12 +1066,11 @@ pub(crate) fn from_language(language: guess::Language) -> TreeSitterConfig {
} }
} }
TypeScriptTsx => { TypeScriptTsx => {
let language = unsafe { tree_sitter_tsx() }; let language_fn = tree_sitter_typescript::LANGUAGE_TSX;
let language = tree_sitter::Language::new(language_fn);
let mut highlight_query = tree_sitter_javascript::HIGHLIGHT_QUERY.to_owned(); let mut highlight_query = tree_sitter_javascript::HIGHLIGHT_QUERY.to_owned();
highlight_query.push_str(include_str!( highlight_query.push_str(tree_sitter_typescript::HIGHLIGHTS_QUERY);
"../../vendored_parsers/highlights/typescript.scm"
));
TreeSitterConfig { TreeSitterConfig {
language: language.clone(), language: language.clone(),
@ -1086,12 +1081,11 @@ pub(crate) fn from_language(language: guess::Language) -> TreeSitterConfig {
} }
} }
TypeScript => { TypeScript => {
let language = unsafe { tree_sitter_typescript() }; let language_fn = tree_sitter_typescript::LANGUAGE_TYPESCRIPT;
let language = tree_sitter::Language::new(language_fn);
let mut highlight_query = tree_sitter_javascript::HIGHLIGHT_QUERY.to_owned(); let mut highlight_query = tree_sitter_javascript::HIGHLIGHT_QUERY.to_owned();
highlight_query.push_str(include_str!( highlight_query.push_str(tree_sitter_typescript::HIGHLIGHTS_QUERY);
"../../vendored_parsers/highlights/typescript.scm"
));
TreeSitterConfig { TreeSitterConfig {
language: language.clone(), language: language.clone(),

@ -1 +0,0 @@
../tree-sitter-typescript/queries/highlights.scm

@ -1 +0,0 @@
../tree-sitter-typescript/common

@ -1 +0,0 @@
../tree-sitter-typescript/tsx

@ -1 +0,0 @@
../tree-sitter-typescript/typescript

@ -1,20 +0,0 @@
module.exports = {
'env': {
'commonjs': true,
'es2021': true,
},
'extends': 'google',
'overrides': [
],
'parserOptions': {
'ecmaVersion': 'latest',
'sourceType': 'module',
},
'rules': {
'indent': ['error', 2, {'SwitchCase': 1}],
'max-len': [
'error',
{'code': 120, 'ignoreComments': true, 'ignoreUrls': true, 'ignoreStrings': true},
],
},
};

@ -1,20 +0,0 @@
typescript/src/** linguist-vendored
tsx/src/** linguist-vendored
/examples/* linguist-vendored
typescript/src/grammar.json linguist-generated
typescript/src/node-types.json linguist-generated
typescript/src/parser.c linguist-generated
tsx/src/grammar.json linguist-generated
tsx/src/node-types.json linguist-generated
tsx/src/parser.c linguist-generated
typescript/src/grammar.json -diff
typescript/src/node-types.json -diff
typescript/src/parser.c -diff
tsx/src/grammar.json -diff
tsx/src/node-types.json -diff
tsx/src/parser.c -diff

@ -1,59 +0,0 @@
name: Bug Report
description: File a bug or issue
title: "bug: "
labels: [bug]
body:
- type: markdown
attributes:
value: |
**Before** reporting an issue, make sure to search [existing issues](https://github.com/tree-sitter/tree-sitter-typescript/issues). Usage questions such as ***"How do I...?"*** either belong in [Discussions](https://github.com/tree-sitter/tree-sitter/discussions) upstream or in our [Discord server](https://discord.gg/w7nTvsVJhm) and will be closed.
If your issue is related to a bug in your editor-experience because your editor *leverages* tree-sitter and this parser, then it is likely your issue does *NOT* belong here and belongs in the relevant editor's repository.
- type: checkboxes
attributes:
label: Did you check existing issues?
description: Make sure you've checked all of the below before submitting an issue
options:
- label: I have read all the [tree-sitter docs](https://tree-sitter.github.io/tree-sitter/using-parsers) if it relates to using the parser
required: false
- label: I have searched the existing issues of tree-sitter-typescript
required: true
- type: input
attributes:
label: "Tree-Sitter CLI Version, if relevant (output of `tree-sitter --version`)"
placeholder: "tree-sitter 0.20.8 (6bbb50bef8249e6460e7d69e42cc8146622fa4fd)"
validations:
required: false
- type: textarea
attributes:
label: Describe the bug
description: A clear and concise description of what the bug is. Please include any related errors you see such as parsing errors or tree-sitter cli errors.
validations:
required: true
- type: textarea
attributes:
label: Steps To Reproduce/Bad Parse Tree
description: Steps to reproduce the behavior. If you have a bad parse tree, please include it here. You can get this by running `tree-sitter parse <path-to-file>` and copying the output.
placeholder: |
1.
2.
3.
validations:
required: true
- type: textarea
attributes:
label: Expected Behavior/Parse Tree
description: A concise description of what you expected to happen, or in the case of a bad parse tree, the expected parse tree.
validations:
required: true
- type: textarea
attributes:
label: Repro
description: Minimal code to reproduce this issue. Ideally this should be reproducible with the C library or the tree-sitter cli, do not suggest an editor or external tool. Also, a link to the TypeScript Playground showing that the snippet below is valid JavaScript or TypeScript would be great (https://www.typescriptlang.org/play)
value: |
// Example code that causes the issue
function foo() {
// Code that fails to parse, or causes an error
}
render: TypeScript
validations:
required: false

@ -1,36 +0,0 @@
name: Feature Request
description: Suggest a new feature
title: "feature: "
labels: [enhancement]
body:
- type: checkboxes
attributes:
label: Did you check the tree-sitter docs?
description: Make sure you read all the docs before submitting a feature request
options:
- label: I have read all the [tree-sitter docs](https://tree-sitter.github.io/tree-sitter/using-parsers) if it relates to using the parser
required: false
- type: textarea
validations:
required: true
attributes:
label: Is your feature request related to a problem? Please describe.
description: A clear and concise description of what the problem is. Ex. I think the grammar models this rule incorrectly and can be improved, or the TypeScript spec has officially added a new feature that should be added to the grammar.
- type: textarea
validations:
required: true
attributes:
label: Describe the solution you'd like
description: A clear and concise description of what you want to happen.
- type: textarea
validations:
required: true
attributes:
label: Describe alternatives you've considered
description: A clear and concise description of any alternative solutions or features you've considered.
- type: textarea
validations:
required: false
attributes:
label: Additional context
description: Add any other context or screenshots about the feature request here. If your feature request is related to a new TypeScript feature, please include a link to the relevant **official** TypeScript documentation.

@ -1,7 +0,0 @@
# Checklist
- [ ] All tests pass in CI.
- [ ] There are sufficient tests for the new fix/feature.
- [ ] Grammar rules have not been renamed unless absolutely necessary.
- [ ] The conflicts section hasn't grown too much.
- [ ] The parser size hasn't grown too much (check the value of STATE_COUNT in src/parser.c).

@ -1,30 +0,0 @@
name: CI
on:
workflow_dispatch:
pull_request:
push:
jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: true
matrix:
os: [macos-latest, ubuntu-latest]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 18
- run: npm install
- run: npm test
test_windows:
runs-on: windows-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 18
- run: npm install
- run: npm run test-windows

@ -1,19 +0,0 @@
name: Lint
on:
push:
branches:
- master
pull_request:
branches:
- "**"
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install modules
run: npm install
- name: Run ESLint
run: npm run lint

@ -1,103 +0,0 @@
name: Release
on:
workflow_run:
workflows: ["CI"]
branches:
- master
types:
- completed
jobs:
release:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Get previous commit SHA
id: get_previous_commit
run: |
LATEST_TAG=$(git describe --tags --abbrev=0)
if [[ -z "$LATEST_TAG" ]]; then
echo "No tag found. Failing..."
exit 1
fi
echo "latest_tag=${LATEST_TAG#v}" >> "$GITHUB_ENV" # Remove 'v' prefix from the tag
- name: Check if version changed and is greater than the previous
id: version_check
run: |
# Compare the current version with the version from the previous commit
PREVIOUS_NPM_VERSION=${{ env.latest_tag }}
CURRENT_NPM_VERSION=$(jq -r '.version' package.json)
CURRENT_CARGO_VERSION=$(awk -F '"' '/^version/ {print $2}' Cargo.toml)
if [[ "$CURRENT_NPM_VERSION" != "$CURRENT_CARGO_VERSION" ]]; then # Cargo.toml and package.json versions must match
echo "Mismatch: NPM version ($CURRENT_NPM_VERSION) and Cargo.toml version ($CURRENT_CARGO_VERSION)"
echo "version_changed=false" >> "$GITHUB_ENV"
else
if [[ "$PREVIOUS_NPM_VERSION" == "$CURRENT_NPM_VERSION" ]]; then
echo "version_changed=" >> "$GITHUB_ENV"
else
IFS='.' read -ra PREVIOUS_VERSION_PARTS <<< "$PREVIOUS_NPM_VERSION"
IFS='.' read -ra CURRENT_VERSION_PARTS <<< "$CURRENT_NPM_VERSION"
VERSION_CHANGED=false
for i in "${!PREVIOUS_VERSION_PARTS[@]}"; do
if [[ ${CURRENT_VERSION_PARTS[i]} -gt ${PREVIOUS_VERSION_PARTS[i]} ]]; then
VERSION_CHANGED=true
break
elif [[ ${CURRENT_VERSION_PARTS[i]} -lt ${PREVIOUS_VERSION_PARTS[i]} ]]; then
break
fi
done
echo "version_changed=$VERSION_CHANGED" >> "$GITHUB_ENV"
echo "current_version=${CURRENT_NPM_VERSION}" >> "$GITHUB_ENV"
fi
fi
- name: Display result
run: |
echo "Version bump detected: ${{ env.version_changed }}"
- name: Fail if version is lower
if: env.version_changed == 'false'
run: exit 1
- name: Setup Node
if: env.version_changed == 'true'
uses: actions/setup-node@v4
with:
node-version: 18
registry-url: "https://registry.npmjs.org"
- name: Publish to NPM
if: env.version_changed == 'true'
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
run: npm publish
- name: Setup Rust
if: env.version_changed == 'true'
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- name: Publish to Crates.io
if: env.version_changed == 'true'
uses: katyo/publish-crates@v2
with:
registry-token: ${{ secrets.CARGO_REGISTRY_TOKEN }}
- name: Tag versions
if: env.version_changed == 'true'
run: |
git checkout master
git config user.name github-actions[bot]
git config user.email github-actions[bot]@users.noreply.github.com
git tag -d "v${{ env.current_version }}" || true
git push origin --delete "v${{ env.current_version }}" || true
git tag -a "v${{ env.current_version }}" -m "Version ${{ env.current_version }}"
git push origin "v${{ env.current_version }}"

@ -1,24 +0,0 @@
Cargo.lock
node_modules
.node-version
target
build
/.build
*.log
Cargo.lock
package-lock.json
/test.ts
examples/desktop
examples/redux
examples/vscode
log.html
yarn.lock
# These files would be generated by 'tree-sitter generate' with the default
# settings. We don't want them because there's already a copy at the root.
/tsx/Cargo.toml
/tsx/binding.gyp
/tsx/bindings
/typescript/Cargo.toml
/typescript/binding.gyp
/typescript/bindings

@ -1,6 +0,0 @@
/test
/examples
/build
/script
/target
appveyor.yml

@ -1,32 +0,0 @@
[package]
name = "tree-sitter-typescript"
description = "Typescript grammar for tree-sitter"
version = "0.20.5"
authors = ["Max Brunsfeld <maxbrunsfeld@gmail.com>"]
license = "MIT"
readme = "bindings/rust/README.md"
keywords = ["incremental", "parsing", "typescript", "tsx"]
categories = ["parsing", "text-editors"]
repository = "https://github.com/tree-sitter/tree-sitter-typescript"
edition = "2021"
autoexamples = false
build = "bindings/rust/build.rs"
include = [
"common",
"bindings/rust",
"typescript/grammar.js",
"typescript/src",
"tsx/grammar.js",
"tsx/src",
"queries",
]
[lib]
path = "bindings/rust/lib.rs"
[dependencies]
tree-sitter = "~0.20.10"
[build-dependencies]
cc = "~1.0.83"

@ -1,21 +0,0 @@
The MIT License (MIT)
Copyright (c) 2017 GitHub
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

@ -1,27 +0,0 @@
// swift-tools-version: 5.6
import PackageDescription
let package = Package(
name: "TreeSitterTypeScript",
platforms: [.macOS(.v10_13), .iOS(.v11)],
products: [.library(name: "TreeSitterTypeScript", targets: ["TreeSitterTypeScript"])],
targets: [
.target(
name: "TreeSitterTypeScript",
path: ".",
exclude: [
],
sources: [
"typescript/src/parser.c",
"typescript/src/scanner.c",
"tsx/src/parser.c",
"tsx/src/scanner.c",
],
resources: [
.copy("queries"),
],
publicHeadersPath: "bindings/swift",
cSettings: [.headerSearchPath("typescript/src")]
),
]
)

@ -1,24 +0,0 @@
# tree-sitter-typescript
[![CI](https://github.com/tree-sitter/tree-sitter-typescript/actions/workflows/ci.yml/badge.svg)](https://github.com/tree-sitter/tree-sitter-typescript/actions/workflows/ci.yml)
[![Discord](https://img.shields.io/discord/1063097320771698699?logo=discord)](https://discord.gg/w7nTvsVJhm)
[![Rust Crate](https://img.shields.io/crates/v/tree-sitter-typescript.svg)](https://crates.io/crates/tree-sitter-typescript)
[![Node Package](https://img.shields.io/npm/v/tree-sitter-typescript.svg)](https://www.npmjs.com/package/tree-sitter-typescript)
TypeScript and TSX grammars for [tree-sitter][].
Because TSX and TypeScript are actually two different dialects, this module defines two grammars. Require them as follows:
```js
require("tree-sitter-typescript").typescript; // TypeScript grammar
require("tree-sitter-typescript").tsx; // TSX grammar
```
For Javascript files with [flow] type annotations you can use the the `tsx` parser.
[tree-sitter]: https://github.com/tree-sitter/tree-sitter
[flow]: https://flow.org/en/
References
- [TypeScript Language Spec](https://github.com/microsoft/TypeScript/blob/main/doc/spec-ARCHIVED.md)

@ -1,22 +0,0 @@
image: Visual Studio 2015
environment:
nodejs_version: "8"
platform:
- x64
install:
- ps: Install-Product node $env:nodejs_version
- node --version
- npm --version
- npm install
test_script:
- npm run test-windows
build: off
branches:
only:
- master

@ -1,21 +0,0 @@
{
"targets": [
{
"target_name": "tree_sitter_typescript_binding",
"include_dirs": [
"<!(node -e \"require('nan')\")",
"typescript/src"
],
"sources": [
"typescript/src/parser.c",
"typescript/src/scanner.c",
"tsx/src/parser.c",
"tsx/src/scanner.c",
"bindings/node/binding.cc"
],
"cflags_c": [
"-std=c99",
]
},
]
}

@ -1,37 +0,0 @@
#include "tree_sitter/parser.h"
#include <node.h>
#include "nan.h"
using namespace v8;
extern "C" TSLanguage * tree_sitter_typescript();
extern "C" TSLanguage * tree_sitter_tsx();
namespace {
NAN_METHOD(New) {}
void Init(Local<Object> exports, Local<Object> module) {
Local<FunctionTemplate> ts_tpl = Nan::New<FunctionTemplate>(New);
ts_tpl->SetClassName(Nan::New("Language").ToLocalChecked());
ts_tpl->InstanceTemplate()->SetInternalFieldCount(1);
Local<Function> ts_constructor = Nan::GetFunction(ts_tpl).ToLocalChecked();
Local<Object> ts_instance = ts_constructor->NewInstance(Nan::GetCurrentContext()).ToLocalChecked();
Nan::SetInternalFieldPointer(ts_instance, 0, tree_sitter_typescript());
Nan::Set(ts_instance, Nan::New("name").ToLocalChecked(), Nan::New("typescript").ToLocalChecked());
Local<FunctionTemplate> tsx_tpl = Nan::New<FunctionTemplate>(New);
tsx_tpl->SetClassName(Nan::New("Language").ToLocalChecked());
tsx_tpl->InstanceTemplate()->SetInternalFieldCount(1);
Local<Function> tsx_constructor = Nan::GetFunction(tsx_tpl).ToLocalChecked();
Local<Object> tsx_instance = tsx_constructor->NewInstance(Nan::GetCurrentContext()).ToLocalChecked();
Nan::SetInternalFieldPointer(tsx_instance, 0, tree_sitter_tsx());
Nan::Set(tsx_instance, Nan::New("name").ToLocalChecked(), Nan::New("tsx").ToLocalChecked());
Nan::Set(exports, Nan::New("typescript").ToLocalChecked(), ts_instance);
Nan::Set(exports, Nan::New("tsx").ToLocalChecked(), tsx_instance);
}
NODE_MODULE(tree_sitter_typescript_binding, Init)
} // namespace

@ -1,20 +0,0 @@
try {
module.exports = require("../../build/Release/tree_sitter_typescript_binding");
} catch (error1) {
if (error1.code !== 'MODULE_NOT_FOUND') {
throw error1;
}
try {
module.exports = require("../../build/Debug/tree_sitter_typescript_binding");
} catch (error2) {
if (error2.code !== 'MODULE_NOT_FOUND') {
throw error2;
}
throw error1
}
}
try {
module.exports.typescript.nodeTypeInfo = require("../../typescript/src/node-types.json");
module.exports.tsx.nodeTypeInfo = require("../../tsx/src/node-types.json");
} catch (_) {}

@ -1 +0,0 @@
module.exports = require('./index').tsx;

@ -1 +0,0 @@
module.exports = require('./index').typescript;

@ -1,46 +0,0 @@
# tree-sitter-typescript
This crate provides a TypeScript grammar for the [tree-sitter][] parsing
library. To use this crate, add it to the `[dependencies]` section of your
`Cargo.toml` file. (Note that you will probably also need to depend on the
[`tree-sitter`][tree-sitter crate] crate to use the parsed result in any useful
way.)
```toml
[dependencies]
tree-sitter = "~0.20.10"
tree-sitter-typescript = "~0.20.5"
```
Typically, you will use the [language][language func] function to add this
grammar to a tree-sitter [Parser][], and then use the parser to parse some code.
The below example demonstrates a simple program that parses a TypeScript
function and prints the result to your terminal.
```rust
use tree_sitter::Parser;
fn main() {
let code = r#"
function double(x) {
return x * 2;
}
"#;
let mut parser = Parser::new();
parser
.set_language(tree_sitter_typescript::language())
.expect("Error loading TypeScript grammar");
let parsed = parser.parse(code, None);
println!("{:#?}", parsed);
}
```
If you have any questions, please reach out to us in the [tree-sitter
discussions] page.
[language func]: https://docs.rs/tree-sitter-typescript/*/tree_sitter_typescript/fn.language.html
[Parser]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Parser.html
[tree-sitter]: https://tree-sitter.github.io/
[tree-sitter crate]: https://crates.io/crates/tree-sitter
[tree-sitter discussions]: https://github.com/tree-sitter/tree-sitter/discussions

@ -1,28 +0,0 @@
fn main() {
let root_dir = std::path::Path::new(".");
let typescript_dir = root_dir.join("typescript").join("src");
let tsx_dir = root_dir.join("tsx").join("src");
let mut config = cc::Build::new();
config.include(&typescript_dir);
config
.flag_if_supported("-Wno-unused-parameter")
.flag_if_supported("-Wno-unused-but-set-variable");
for path in &[
typescript_dir.join("parser.c"),
typescript_dir.join("scanner.c"),
tsx_dir.join("parser.c"),
tsx_dir.join("scanner.c"),
] {
config.file(&path);
println!("cargo:rerun-if-changed={}", path.to_str().unwrap());
}
println!(
"cargo:rerun-if-changed={}",
root_dir.join("common").join("scanner.h").to_str().unwrap()
);
config.compile("parser-scanner");
}

@ -1,62 +0,0 @@
//! This crate provides Typescript and TSX grammars for the [tree-sitter][] parsing library.
//!
//! Typically, you will use the [language_typescript][language func] function to add this grammar to a
//! tree-sitter [Parser][], and then use the parser to parse some code:
//!
//! ```
//! use tree_sitter::Parser;
//!
//! let code = r#"
//! function double(x: number): number {
//! return x * 2;
//! }
//! "#;
//! let mut parser = Parser::new();
//! parser
//! .set_language(tree_sitter_typescript::language_typescript())
//! .expect("Error loading typescript grammar");
//! let parsed = parser.parse(code, None).unwrap();
//! let root = parsed.root_node();
//! assert!(!root.has_error());
//! ```
//!
//! [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html
//! [language func]: fn.language_typescript.html
//! [Parser]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Parser.html
//! [tree-sitter]: https://tree-sitter.github.io/
use tree_sitter::Language;
extern "C" {
fn tree_sitter_typescript() -> Language;
fn tree_sitter_tsx() -> Language;
}
/// Returns the tree-sitter [Language][] for this Typescript.
///
/// [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html
pub fn language_typescript() -> Language {
unsafe { tree_sitter_typescript() }
}
/// Returns the tree-sitter [Language][] for TSX.
///
/// [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html
pub fn language_tsx() -> Language {
unsafe { tree_sitter_tsx() }
}
/// The syntax highlighting query for this language.
pub const HIGHLIGHT_QUERY: &str = include_str!("../../queries/highlights.scm");
/// The local-variable syntax highlighting query for this language.
pub const LOCALS_QUERY: &str = include_str!("../../queries/locals.scm");
/// The symbol tagging query for this language.
pub const TAGGING_QUERY: &str = include_str!("../../queries/tags.scm");
/// The content of the [`node-types.json`][] file for this grammar.
///
/// [`node-types.json`]: https://tree-sitter.github.io/tree-sitter/using-parsers#static-node-types
pub const TYPESCRIPT_NODE_TYPES: &str = include_str!("../../typescript/src/node-types.json");
pub const TSX_NODE_TYPES: &str = include_str!("../../tsx/src/node-types.json");

@ -1,17 +0,0 @@
#ifndef TREE_SITTER_TYPESCRIPT_H_
#define TREE_SITTER_TYPESCRIPT_H_
typedef struct TSLanguage TSLanguage;
#ifdef __cplusplus
extern "C" {
#endif
extern TSLanguage *tree_sitter_typescript();
extern TSLanguage *tree_sitter_tsx();
#ifdef __cplusplus
}
#endif
#endif // TREE_SITTER_TYPESCRIPT_H_

File diff suppressed because it is too large Load Diff

@ -1,357 +0,0 @@
==================================
As expressions
==================================
h as `hello`
T as {} & { [t: T]: T }
T as {} & { [t: T]: T } & { [g: G]: G }
---
(program
(expression_statement
(as_expression
(identifier)
(template_literal_type)))
(expression_statement
(as_expression
(identifier)
(intersection_type
(object_type)
(object_type
(index_signature
(identifier)
(type_identifier)
(type_annotation
(type_identifier)))))))
(expression_statement
(as_expression
(identifier)
(intersection_type
(intersection_type
(object_type)
(object_type
(index_signature
(identifier)
(type_identifier)
(type_annotation
(type_identifier)))))
(object_type
(index_signature
(identifier)
(type_identifier)
(type_annotation
(type_identifier))))))))
==================================
Satisfies expressions
==================================
h satisfies `hello`
T satisfies {} & { [t: T]: T }
T satisfies {} & { [t: T]: T } & { [g: G]: G }
---
(program
(expression_statement
(satisfies_expression
(identifier)
(template_literal_type)))
(expression_statement
(satisfies_expression
(identifier)
(intersection_type
(object_type)
(object_type
(index_signature
(identifier)
(type_identifier)
(type_annotation
(type_identifier)))))))
(expression_statement
(satisfies_expression
(identifier)
(intersection_type
(intersection_type
(object_type)
(object_type
(index_signature
(identifier)
(type_identifier)
(type_annotation
(type_identifier)))))
(object_type
(index_signature
(identifier)
(type_identifier)
(type_annotation
(type_identifier))))))))
==================================
Typeof expressions
==================================
typeof class {} === "function";
typeof module === "object" && typeof module.exports === "object"
---
(program
(expression_statement
(binary_expression
(unary_expression
(class
(class_body)))
(string
(string_fragment))))
(expression_statement
(binary_expression
(binary_expression
(unary_expression
(identifier))
(string
(string_fragment)))
(binary_expression
(unary_expression
(member_expression
(identifier)
(property_identifier)))
(string
(string_fragment))))))
==================================
Array with empty elements
==================================
[, a, , b, , , , s, , , ]
---
(program
(expression_statement
(array
(identifier)
(identifier)
(identifier))))
==================================
Variable named 'module'
==================================
var module;
module;
---
(program
(variable_declaration
(variable_declarator
(identifier)))
(expression_statement
(identifier)))
==================================
Multi-line variable declarations
==================================
var a = b
, c = d
, e = f
---
(program
(variable_declaration
(variable_declarator
(identifier)
(identifier))
(variable_declarator
(identifier)
(identifier))
(variable_declarator
(identifier)
(identifier))))
=====================================
The 'less than' operator
=====================================
i < foo.length;
i < type.length;
i < string.length;
---
(program
(expression_statement
(binary_expression
(identifier)
(member_expression
(identifier)
(property_identifier))))
(expression_statement
(binary_expression
(identifier)
(member_expression
(identifier)
(property_identifier))))
(expression_statement
(binary_expression
(identifier)
(member_expression
(identifier)
(property_identifier)))))
=====================================
Subscript expressions in if statements
=====================================
if ( foo ) {
set[ 1 ].apply()
}
---
(program
(if_statement
(parenthesized_expression
(identifier))
(statement_block
(expression_statement
(call_expression
(member_expression
(subscript_expression
(identifier)
(number))
(property_identifier))
(arguments))))))
====================================
Objects with reserved words as keys
====================================
{
public: true,
private: true,
readonly: true
};
{
readonly: 1,
abstract: 1,
static: 1
};
---
(program
(expression_statement
(object
(pair
key: (property_identifier)
value: (true))
(pair
key: (property_identifier)
value: (true))
(pair
key: (property_identifier)
value: (true))))
(expression_statement
(object
(pair
key: (property_identifier)
value: (number))
(pair
key: (property_identifier)
value: (number))
(pair
key: (property_identifier)
value: (number)))))
====================================
Assignment to non-null LHS
====================================
foo! = bar;
foo! += bar;
(foo)! = bar;
(foo)! += bar;
---
(program
(expression_statement
(assignment_expression
(non_null_expression
(identifier))
(identifier)))
(expression_statement
(augmented_assignment_expression
(non_null_expression
(identifier))
(identifier)))
(expression_statement
(assignment_expression
(non_null_expression
(parenthesized_expression
(identifier)))
(identifier)))
(expression_statement
(augmented_assignment_expression
(non_null_expression
(parenthesized_expression
(identifier)))
(identifier))))
==================================
Generic calls
==================================
f<T>(x)
---
(program
(expression_statement
(call_expression
(identifier)
(type_arguments
(type_identifier))
(arguments
(identifier)))))
==================================
Instantiation expressions
==================================
const makeHammerBox = makeBox<Hammer>;
const makeStringBox = makeBox<string>;
const ErrorMap = Map<string, Error>;
---
(program
(lexical_declaration
(variable_declarator
(identifier)
(instantiation_expression
(identifier)
(type_arguments
(type_identifier)))))
(lexical_declaration
(variable_declarator
(identifier)
(instantiation_expression
(identifier)
(type_arguments
(predefined_type)))))
(lexical_declaration
(variable_declarator
(identifier)
(instantiation_expression
(identifier)
(type_arguments
(predefined_type)
(type_identifier))))))

@ -1,340 +0,0 @@
==================================
Functions with typed parameters
==================================
function greeter(person: string) {
return "Hello, " + person;
}
function foo<T>(x: T): T {
}
function foo<T, U>(a: T[], f: (x: T) => U): U[] {
}
function foo<T, U>(this: T[]): U[] {
return []
}
function foo<const T, const U extends string>(x: T, y: U) {
}
---
(program
(function_declaration
name: (identifier)
parameters: (formal_parameters (required_parameter pattern: (identifier) type: (type_annotation (predefined_type))))
body: (statement_block
(return_statement (binary_expression
left: (string (string_fragment))
right: (identifier)))))
(function_declaration
name: (identifier)
type_parameters: (type_parameters (type_parameter name: (type_identifier)))
parameters: (formal_parameters (required_parameter pattern: (identifier) type: (type_annotation (type_identifier))))
return_type: (type_annotation (type_identifier))
body: (statement_block))
(function_declaration
name: (identifier)
type_parameters: (type_parameters (type_parameter name: (type_identifier)) (type_parameter name: (type_identifier)))
parameters: (formal_parameters
(required_parameter
pattern: (identifier)
type: (type_annotation (array_type (type_identifier))))
(required_parameter
pattern: (identifier)
type: (type_annotation
(function_type
parameters: (formal_parameters (required_parameter pattern: (identifier) type: (type_annotation (type_identifier))))
return_type: (type_identifier)))))
return_type: (type_annotation (array_type (type_identifier)))
body: (statement_block))
(function_declaration
name: (identifier)
type_parameters: (type_parameters (type_parameter name: (type_identifier)) (type_parameter name: (type_identifier)))
parameters: (formal_parameters
(required_parameter
pattern: (this)
type: (type_annotation (array_type (type_identifier)))))
return_type: (type_annotation (array_type (type_identifier)))
body: (statement_block (return_statement (array))))
(function_declaration
name: (identifier)
type_parameters: (type_parameters
(type_parameter name: (type_identifier))
(type_parameter name: (type_identifier) constraint: (constraint (predefined_type))))
parameters: (formal_parameters
(required_parameter
pattern: (identifier)
type: (type_annotation (type_identifier)))
(required_parameter
pattern: (identifier)
type: (type_annotation (type_identifier))))
body: (statement_block)))
==================================
New object with type arguments
==================================
const lines = new Array<DiffLine>()
---
(program
(lexical_declaration
(variable_declarator
(identifier)
(new_expression
(identifier)
(type_arguments (type_identifier))
(arguments)))))
===================================================
Function calls with array and tuple type arguments
===================================================
a.b<[C]>();
a<C.D[]>();
---
(program
(expression_statement
(call_expression
function: (member_expression
object: (identifier)
property: (property_identifier))
type_arguments: (type_arguments (tuple_type (type_identifier)))
arguments: (arguments)))
(expression_statement
(call_expression
function: (identifier)
type_arguments: (type_arguments
(array_type (nested_type_identifier
module: (identifier)
name: (type_identifier))))
arguments: (arguments))))
=========================================================
Function calls with optional chaining and type arguments
=========================================================
A?.<B>();
---
(program
(expression_statement
(call_expression
(identifier)
(type_arguments (type_identifier))
(arguments))))
==================================
Arrow functions and generators with call signatures
==================================
<A>(amount, interestRate, duration): number => 2
function* foo<A>(amount, interestRate, duration): number {
yield amount * interestRate * duration / 12
}
(module: any): number => 2
---
(program
(expression_statement
(arrow_function
type_parameters: (type_parameters (type_parameter name: (type_identifier)))
parameters: (formal_parameters
(required_parameter pattern: (identifier))
(required_parameter pattern: (identifier))
(required_parameter pattern: (identifier)))
return_type: (type_annotation (predefined_type))
body: (number)))
(generator_function_declaration
name: (identifier)
type_parameters: (type_parameters (type_parameter name: (type_identifier)))
parameters: (formal_parameters
(required_parameter pattern: (identifier))
(required_parameter pattern: (identifier))
(required_parameter pattern: (identifier)))
return_type: (type_annotation (predefined_type))
body: (statement_block
(expression_statement (yield_expression (binary_expression
left: (binary_expression left: (binary_expression left: (identifier) right: (identifier)) right: (identifier))
right: (number))))))
(expression_statement
(arrow_function
parameters: (formal_parameters (required_parameter pattern: (identifier) type: (type_annotation (predefined_type))))
return_type: (type_annotation (predefined_type))
body: (number))))
==================================
Arrow function with parameter named async
==================================
const x = async => async;
---
(program
(lexical_declaration (variable_declarator (identifier) (arrow_function (identifier) (identifier)))))
==================================
Super
==================================
class A extends B {
constructor(x: number, y: number) {
super(x);
}
public toString() {
return super.toString() + " y=" + this.y;
}
}
---
(program
(class_declaration
name: (type_identifier)
(class_heritage (extends_clause value: (identifier)))
body: (class_body
(method_definition
name: (property_identifier)
parameters: (formal_parameters
(required_parameter pattern: (identifier) type: (type_annotation (predefined_type)))
(required_parameter pattern: (identifier) type: (type_annotation (predefined_type))))
body: (statement_block
(expression_statement (call_expression
function: (super)
arguments: (arguments (identifier))))))
(method_definition
(accessibility_modifier)
name: (property_identifier)
parameters: (formal_parameters)
body: (statement_block
(return_statement
(binary_expression
left: (binary_expression
left: (call_expression
function: (member_expression
object: (super)
property: (property_identifier))
arguments: (arguments))
right: (string (string_fragment)))
right: (member_expression
object: (this)
property: (property_identifier)))))))))
==================
Function signature
==================
export default function foo(): bar
---
(program
(export_statement
(function_signature
(identifier)
(formal_parameters)
(type_annotation (type_identifier)))))
=============================================================
Ambiguity between function signature and function declaration
=============================================================
function foo()
{}
function foo(bar)
function foo(bar): baz;
function foo(bar)
function foo(): () => { [key: foo]: bar }
function foo(): () => { [key: foo]: bar } {}
---
(program
(function_declaration
name: (identifier)
parameters: (formal_parameters)
body: (statement_block))
(function_signature
name: (identifier)
parameters: (formal_parameters (required_parameter pattern: (identifier))))
(function_signature
name: (identifier)
parameters: (formal_parameters (required_parameter pattern: (identifier)))
return_type: (type_annotation (type_identifier)))
(function_signature
name: (identifier)
parameters: (formal_parameters
(required_parameter pattern: (identifier))))
(function_signature
name: (identifier)
parameters: (formal_parameters)
return_type: (type_annotation
(function_type
parameters: (formal_parameters)
return_type: (object_type
(index_signature
name: (identifier)
index_type: (type_identifier)
type: (type_annotation (type_identifier)))))))
(function_declaration
name: (identifier)
parameters: (formal_parameters)
return_type: (type_annotation
(function_type
parameters: (formal_parameters)
return_type: (object_type
(index_signature
name: (identifier)
index_type: (type_identifier)
type: (type_annotation (type_identifier))))))
body: (statement_block)))
====================================================================================
Ambiguity between function signature and function declaration: comments and newlines
====================================================================================
function foo()
// above is a signature
foo();
function bar()
// above is a function declaration
{}
function foo()
: number;
---
(program
(function_signature (identifier) (formal_parameters))
(comment)
(expression_statement (call_expression (identifier) (arguments)))
(function_declaration
(identifier)
(formal_parameters)
(comment)
(statement_block))
(function_signature
(identifier)
(formal_parameters)
(type_annotation (predefined_type))))

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

@ -1,297 +0,0 @@
#include "tree_sitter/parser.h"
#include <wctype.h>
enum TokenType {
AUTOMATIC_SEMICOLON,
TEMPLATE_CHARS,
TERNARY_QMARK,
HTML_COMMENT,
LOGICAL_OR,
ESCAPE_SEQUENCE,
FUNCTION_SIGNATURE_AUTOMATIC_SEMICOLON,
ERROR_RECOVERY,
};
static void advance(TSLexer *lexer) { lexer->advance(lexer, false); }
static void skip(TSLexer *lexer) { lexer->advance(lexer, true); }
static bool scan_template_chars(TSLexer *lexer) {
lexer->result_symbol = TEMPLATE_CHARS;
for (bool has_content = false;; has_content = true) {
lexer->mark_end(lexer);
switch (lexer->lookahead) {
case '`':
return has_content;
case '\0':
return false;
case '$':
advance(lexer);
if (lexer->lookahead == '{') {
return has_content;
}
break;
case '\\':
return has_content;
default:
advance(lexer);
}
}
}
static bool scan_whitespace_and_comments(TSLexer *lexer, bool *scanned_comment) {
for (;;) {
while (iswspace(lexer->lookahead)) {
skip(lexer);
}
if (lexer->lookahead == '/') {
skip(lexer);
if (lexer->lookahead == '/') {
skip(lexer);
while (lexer->lookahead != 0 && lexer->lookahead != '\n') {
skip(lexer);
}
*scanned_comment = true;
} else if (lexer->lookahead == '*') {
skip(lexer);
while (lexer->lookahead != 0) {
if (lexer->lookahead == '*') {
skip(lexer);
if (lexer->lookahead == '/') {
skip(lexer);
break;
}
} else {
skip(lexer);
}
}
} else {
return false;
}
} else {
return true;
}
}
}
static bool scan_automatic_semicolon(TSLexer *lexer, const bool *valid_symbols, bool *scanned_comment) {
lexer->result_symbol = AUTOMATIC_SEMICOLON;
lexer->mark_end(lexer);
for (;;) {
if (lexer->lookahead == 0) {
return true;
}
if (lexer->lookahead == '}') {
// Automatic semicolon insertion breaks detection of object patterns
// in a typed context:
// type F = ({a}: {a: number}) => number;
// Therefore, disable automatic semicolons when followed by typing
do {
skip(lexer);
} while (iswspace(lexer->lookahead));
if (lexer->lookahead == ':') {
return false;
}
return true;
}
if (!iswspace(lexer->lookahead)) {
return false;
}
if (lexer->lookahead == '\n') {
break;
}
skip(lexer);
}
skip(lexer);
if (!scan_whitespace_and_comments(lexer, scanned_comment)) {
return false;
}
switch (lexer->lookahead) {
case ',':
case '.':
case ';':
case '*':
case '%':
case '>':
case '<':
case '=':
case '?':
case '^':
case '|':
case '&':
case '/':
case ':':
return false;
case '{':
if (valid_symbols[FUNCTION_SIGNATURE_AUTOMATIC_SEMICOLON]) {
return false;
}
break;
// Don't insert a semicolon before a '[' or '(', unless we're parsing
// a type. Detect whether we're parsing a type or an expression using
// the validity of a binary operator token.
case '(':
case '[':
if (valid_symbols[LOGICAL_OR]) {
return false;
}
break;
// Insert a semicolon before `--` and `++`, but not before binary `+` or `-`.
case '+':
skip(lexer);
return lexer->lookahead == '+';
case '-':
skip(lexer);
return lexer->lookahead == '-';
// Don't insert a semicolon before `!=`, but do insert one before a unary `!`.
case '!':
skip(lexer);
return lexer->lookahead != '=';
// Don't insert a semicolon before `in` or `instanceof`, but do insert one
// before an identifier.
case 'i':
skip(lexer);
if (lexer->lookahead != 'n') {
return true;
}
skip(lexer);
if (!iswalpha(lexer->lookahead)) {
return false;
}
for (unsigned i = 0; i < 8; i++) {
if (lexer->lookahead != "stanceof"[i]) {
return true;
}
skip(lexer);
}
if (!iswalpha(lexer->lookahead)) {
return false;
}
break;
}
return true;
}
static bool scan_ternary_qmark(TSLexer *lexer) {
for (;;) {
if (!iswspace(lexer->lookahead)) {
break;
}
skip(lexer);
}
if (lexer->lookahead == '?') {
advance(lexer);
/* Optional chaining. */
if (lexer->lookahead == '?' || lexer->lookahead == '.') {
return false;
}
lexer->mark_end(lexer);
lexer->result_symbol = TERNARY_QMARK;
/* TypeScript optional arguments contain the ?: sequence, possibly
with whitespace. */
for (;;) {
if (!iswspace(lexer->lookahead)) {
break;
}
advance(lexer);
}
if (lexer->lookahead == ':' || lexer->lookahead == ')' || lexer->lookahead == ',') {
return false;
}
if (lexer->lookahead == '.') {
advance(lexer);
if (iswdigit(lexer->lookahead)) {
return true;
}
return false;
}
return true;
}
return false;
}
static bool scan_closing_comment(TSLexer *lexer) {
while (iswspace(lexer->lookahead) || lexer->lookahead == 0x2028 || lexer->lookahead == 0x2029) {
skip(lexer);
}
const char *comment_start = "<!--";
const char *comment_end = "-->";
if (lexer->lookahead == '<') {
for (unsigned i = 0; i < 4; i++) {
if (lexer->lookahead != comment_start[i]) {
return false;
}
advance(lexer);
}
} else if (lexer->lookahead == '-') {
for (unsigned i = 0; i < 3; i++) {
if (lexer->lookahead != comment_end[i]) {
return false;
}
advance(lexer);
}
} else {
return false;
}
while (lexer->lookahead != 0 && lexer->lookahead != '\n' && lexer->lookahead != 0x2028 &&
lexer->lookahead != 0x2029) {
advance(lexer);
}
lexer->result_symbol = HTML_COMMENT;
lexer->mark_end(lexer);
return true;
}
static inline bool external_scanner_scan(void *payload, TSLexer *lexer, const bool *valid_symbols) {
if (valid_symbols[TEMPLATE_CHARS]) {
if (valid_symbols[AUTOMATIC_SEMICOLON]) {
return false;
}
return scan_template_chars(lexer);
}
if (valid_symbols[AUTOMATIC_SEMICOLON] || valid_symbols[FUNCTION_SIGNATURE_AUTOMATIC_SEMICOLON]) {
bool scanned_comment = false;
bool ret = scan_automatic_semicolon(lexer, valid_symbols, &scanned_comment);
if (!ret && !scanned_comment && valid_symbols[TERNARY_QMARK] && lexer->lookahead == '?') {
return scan_ternary_qmark(lexer);
}
return ret;
}
if (valid_symbols[TERNARY_QMARK]) {
return scan_ternary_qmark(lexer);
}
if (valid_symbols[HTML_COMMENT] && !valid_symbols[LOGICAL_OR] && !valid_symbols[ESCAPE_SEQUENCE]) {
return scan_closing_comment(lexer);
}
return false;
}

File diff suppressed because it is too large Load Diff

@ -1,100 +0,0 @@
{
"name": "tree-sitter-typescript",
"version": "0.20.5",
"description": "TypeScript grammar for tree-sitter",
"main": "./bindings/node",
"keywords": [
"parser",
"tree-sitter",
"typescript",
"tsx"
],
"repository": {
"type": "git",
"url": "https://github.com/tree-sitter/tree-sitter-typescript.git"
},
"author": "Max Brunsfeld",
"license": "MIT",
"dependencies": {
"nan": "^2.18.0",
"tree-sitter": "^0.20.6"
},
"devDependencies": {
"eslint": ">=8.56.0",
"eslint-config-google": "^0.14.0",
"tree-sitter-cli": "^0.20.8",
"tree-sitter-javascript": "^0.20.3"
},
"scripts": {
"build": "npm run build-typescript && npm run build-tsx",
"build-typescript": "cd typescript && npx tree-sitter generate --no-bindings",
"build-tsx": "cd tsx && npx tree-sitter generate --no-bindings",
"lint": "eslint common/define-grammar.js",
"test-load": "node -e \"console.log(require('./typescript').name, require('./tsx').name)\"",
"test": "npm run test-typescript && npm run test-tsx && npm run test-load && script/parse-examples",
"test-typescript": "cd typescript && npx tree-sitter test",
"test-tsx": "cd tsx && npx tree-sitter test",
"test-windows": "pushd typescript && npx tree-sitter test && popd && pushd tsx && npx tree-sitter test"
},
"tree-sitter": [
{
"scope": "source.ts",
"file-types": [
"ts"
],
"path": "typescript",
"highlights": [
"queries/highlights.scm",
"node_modules/tree-sitter-javascript/queries/highlights.scm"
],
"locals": [
"queries/locals.scm",
"node_modules/tree-sitter-javascript/queries/locals.scm"
],
"tags": [
"queries/tags.scm",
"node_modules/tree-sitter-javascript/queries/tags.scm"
],
"injections": "node_modules/tree-sitter-javascript/queries/injections.scm",
"injection-regex": "^(ts|typescript)$"
},
{
"scope": "source.tsx",
"file-types": [
"tsx"
],
"path": "tsx",
"highlights": [
"queries/highlights.scm",
"node_modules/tree-sitter-javascript/queries/highlights-jsx.scm",
"node_modules/tree-sitter-javascript/queries/highlights.scm"
],
"locals": "node_modules/tree-sitter-javascript/queries/locals.scm",
"tags": [
"queries/tags.scm",
"node_modules/tree-sitter-javascript/queries/tags.scm"
],
"injections": "node_modules/tree-sitter-javascript/queries/injections.scm",
"injection-regex": "^(ts|typescript)$"
},
{
"scope": "source.js.flow",
"file-types": [
"js"
],
"path": "tsx",
"highlights": [
"queries/highlights.scm",
"node_modules/tree-sitter-javascript/queries/highlights-jsx.scm",
"node_modules/tree-sitter-javascript/queries/highlights.scm"
],
"locals": "node_modules/tree-sitter-javascript/queries/locals.scm",
"tags": [
"queries/tags.scm",
"node_modules/tree-sitter-javascript/queries/tags.scm"
],
"injections": "node_modules/tree-sitter-javascript/queries/injections.scm",
"content-regex": "@flow"
}
]
}

@ -1,35 +0,0 @@
; Types
(type_identifier) @type
(predefined_type) @type.builtin
((identifier) @type
(#match? @type "^[A-Z]"))
(type_arguments
"<" @punctuation.bracket
">" @punctuation.bracket)
; Variables
(required_parameter (identifier) @variable.parameter)
(optional_parameter (identifier) @variable.parameter)
; Keywords
[ "abstract"
"declare"
"enum"
"export"
"implements"
"interface"
"keyof"
"namespace"
"private"
"protected"
"public"
"type"
"readonly"
"override"
"satisfies"
] @keyword

@ -1,2 +0,0 @@
(required_parameter (identifier) @local.definition)
(optional_parameter (identifier) @local.definition)

@ -1,23 +0,0 @@
(function_signature
name: (identifier) @name) @definition.function
(method_signature
name: (property_identifier) @name) @definition.method
(abstract_method_signature
name: (property_identifier) @name) @definition.method
(abstract_class_declaration
name: (type_identifier) @name) @definition.class
(module
name: (identifier) @name) @definition.module
(interface_declaration
name: (type_identifier) @name) @definition.interface
(type_annotation
(type_identifier) @name) @reference.type
(new_expression
constructor: (identifier) @name) @reference.class

@ -1,19 +0,0 @@
#!/usr/bin/env bash
set -eu
## Update index to make sure the subsequent diff-index command only reports
## a change if the file content actually changed. Not updating the index can
## give incorrect results when this script is run right after a build.
git update-index -q --really-refresh
if ! git diff-index --exit-code --name-status HEAD -- {tsx,typescript}/src/; then
echo "The following files are not up to date in the repository:" 1>&2
git diff-index --name-status HEAD 1>&2
echo "Run a build and commit the generated files to resolve this issue." 1>&2
git diff-index -p HEAD
exit 1
fi
exit 0

@ -1 +0,0 @@
examples/redux/src/types/store.ts

@ -1,55 +0,0 @@
#!/bin/bash
set -e
cd "$(dirname "$0")/.."
root="$PWD"
clone_repo() {
local owner=$1
local name=$2
local sha=$3
local path="$root/examples/$name"
if [ -d "$path" ]; then
pushd "$path" >/dev/null
if [ "$(git rev-parse HEAD 2>/dev/null)" == "$sha" ]; then
popd >/dev/null
return
else
popd >/dev/null
rm -rf "$path"
echo "Updating $owner/$name to $sha"
fi
else
echo "Cloning $owner/$name"
fi
mkdir -p "$path"
pushd "$path" >/dev/null
git init
git remote add origin "https://github.com/$owner/$name"
git pull --ff-only --depth 1 origin "$sha"
popd >/dev/null
}
clone_repo desktop desktop 7c5111425cab9de911f82dd7e42f92d8f7927bb1
clone_repo reduxjs redux 936e134b827a2f8022d51f6a42942e935ee2a935
clone_repo microsoft vscode 4acf2d9fd883d247b903cc9c33221e18e39bffd8
known_failures="$(cat script/known-failures.txt)"
# shellcheck disable=SC2046
tree-sitter parse -q \
'examples/**/*.ts' \
'examples/**/*.tsx' \
$(for failure in $known_failures; do echo "!${failure}"; done)
example_count=$(find examples -name '*.ts*' -or -name '*.tx' | wc -l)
failure_count=$(wc -w <<<"$known_failures")
success_count=$((example_count - failure_count))
success_percent=$(bc -l <<<"100*${success_count}/${example_count}")
printf \
"Successfully parsed %d of %d example files (%.1f%%)\n" \
"$success_count" "$example_count" "$success_percent"

@ -1,25 +0,0 @@
==========================================================
Type arguments in JSX
==========================================================
<Element<T>>hi</Element>;
<Element<T> />;
<>fragment</>;
---
(program
(expression_statement
(jsx_element
(jsx_opening_element (identifier) (type_arguments (type_identifier)))
(jsx_text)
(jsx_closing_element (identifier))))
(expression_statement
(jsx_self_closing_element
(identifier) (type_arguments (type_identifier))))
(expression_statement
(jsx_element
(jsx_opening_element)
(jsx_text)
(jsx_closing_element)))
)

@ -1,3 +0,0 @@
const defineGrammar = require('../common/define-grammar');
module.exports = defineGrammar('tsx');

@ -1,3 +0,0 @@
{
"main": "../bindings/node/tsx"
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

@ -1,15 +0,0 @@
#include "../../common/scanner.h"
void *tree_sitter_tsx_external_scanner_create() { return NULL; }
void tree_sitter_tsx_external_scanner_destroy(void *payload) {}
void tree_sitter_tsx_external_scanner_reset(void *payload) {}
unsigned tree_sitter_tsx_external_scanner_serialize(void *payload, char *buffer) { return 0; }
void tree_sitter_tsx_external_scanner_deserialize(void *payload, const char *buffer, unsigned length) {}
bool tree_sitter_tsx_external_scanner_scan(void *payload, TSLexer *lexer, const bool *valid_symbols) {
return external_scanner_scan(payload, lexer, valid_symbols);
}

@ -1,230 +0,0 @@
#ifndef TREE_SITTER_PARSER_H_
#define TREE_SITTER_PARSER_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#define ts_builtin_sym_error ((TSSymbol)-1)
#define ts_builtin_sym_end 0
#define TREE_SITTER_SERIALIZATION_BUFFER_SIZE 1024
#ifndef TREE_SITTER_API_H_
typedef uint16_t TSStateId;
typedef uint16_t TSSymbol;
typedef uint16_t TSFieldId;
typedef struct TSLanguage TSLanguage;
#endif
typedef struct {
TSFieldId field_id;
uint8_t child_index;
bool inherited;
} TSFieldMapEntry;
typedef struct {
uint16_t index;
uint16_t length;
} TSFieldMapSlice;
typedef struct {
bool visible;
bool named;
bool supertype;
} TSSymbolMetadata;
typedef struct TSLexer TSLexer;
struct TSLexer {
int32_t lookahead;
TSSymbol result_symbol;
void (*advance)(TSLexer *, bool);
void (*mark_end)(TSLexer *);
uint32_t (*get_column)(TSLexer *);
bool (*is_at_included_range_start)(const TSLexer *);
bool (*eof)(const TSLexer *);
};
typedef enum {
TSParseActionTypeShift,
TSParseActionTypeReduce,
TSParseActionTypeAccept,
TSParseActionTypeRecover,
} TSParseActionType;
typedef union {
struct {
uint8_t type;
TSStateId state;
bool extra;
bool repetition;
} shift;
struct {
uint8_t type;
uint8_t child_count;
TSSymbol symbol;
int16_t dynamic_precedence;
uint16_t production_id;
} reduce;
uint8_t type;
} TSParseAction;
typedef struct {
uint16_t lex_state;
uint16_t external_lex_state;
} TSLexMode;
typedef union {
TSParseAction action;
struct {
uint8_t count;
bool reusable;
} entry;
} TSParseActionEntry;
struct TSLanguage {
uint32_t version;
uint32_t symbol_count;
uint32_t alias_count;
uint32_t token_count;
uint32_t external_token_count;
uint32_t state_count;
uint32_t large_state_count;
uint32_t production_id_count;
uint32_t field_count;
uint16_t max_alias_sequence_length;
const uint16_t *parse_table;
const uint16_t *small_parse_table;
const uint32_t *small_parse_table_map;
const TSParseActionEntry *parse_actions;
const char * const *symbol_names;
const char * const *field_names;
const TSFieldMapSlice *field_map_slices;
const TSFieldMapEntry *field_map_entries;
const TSSymbolMetadata *symbol_metadata;
const TSSymbol *public_symbol_map;
const uint16_t *alias_map;
const TSSymbol *alias_sequences;
const TSLexMode *lex_modes;
bool (*lex_fn)(TSLexer *, TSStateId);
bool (*keyword_lex_fn)(TSLexer *, TSStateId);
TSSymbol keyword_capture_token;
struct {
const bool *states;
const TSSymbol *symbol_map;
void *(*create)(void);
void (*destroy)(void *);
bool (*scan)(void *, TSLexer *, const bool *symbol_whitelist);
unsigned (*serialize)(void *, char *);
void (*deserialize)(void *, const char *, unsigned);
} external_scanner;
const TSStateId *primary_state_ids;
};
/*
* Lexer Macros
*/
#ifdef _MSC_VER
#define UNUSED __pragma(warning(suppress : 4101))
#else
#define UNUSED __attribute__((unused))
#endif
#define START_LEXER() \
bool result = false; \
bool skip = false; \
UNUSED \
bool eof = false; \
int32_t lookahead; \
goto start; \
next_state: \
lexer->advance(lexer, skip); \
start: \
skip = false; \
lookahead = lexer->lookahead;
#define ADVANCE(state_value) \
{ \
state = state_value; \
goto next_state; \
}
#define SKIP(state_value) \
{ \
skip = true; \
state = state_value; \
goto next_state; \
}
#define ACCEPT_TOKEN(symbol_value) \
result = true; \
lexer->result_symbol = symbol_value; \
lexer->mark_end(lexer);
#define END_STATE() return result;
/*
* Parse Table Macros
*/
#define SMALL_STATE(id) ((id) - LARGE_STATE_COUNT)
#define STATE(id) id
#define ACTIONS(id) id
#define SHIFT(state_value) \
{{ \
.shift = { \
.type = TSParseActionTypeShift, \
.state = (state_value) \
} \
}}
#define SHIFT_REPEAT(state_value) \
{{ \
.shift = { \
.type = TSParseActionTypeShift, \
.state = (state_value), \
.repetition = true \
} \
}}
#define SHIFT_EXTRA() \
{{ \
.shift = { \
.type = TSParseActionTypeShift, \
.extra = true \
} \
}}
#define REDUCE(symbol_val, child_count_val, ...) \
{{ \
.reduce = { \
.type = TSParseActionTypeReduce, \
.symbol = symbol_val, \
.child_count = child_count_val, \
__VA_ARGS__ \
}, \
}}
#define RECOVER() \
{{ \
.type = TSParseActionTypeRecover \
}}
#define ACCEPT_INPUT() \
{{ \
.type = TSParseActionTypeAccept \
}}
#ifdef __cplusplus
}
#endif
#endif // TREE_SITTER_PARSER_H_

@ -1,20 +0,0 @@
==========================
Type assertions
==========================
<A>b;
<C<D>>e.f;
---
(program
(expression_statement (type_assertion
(type_arguments (type_identifier))
(identifier)))
(expression_statement (type_assertion
(type_arguments (generic_type
(type_identifier)
(type_arguments (type_identifier))))
(member_expression
(identifier)
(property_identifier)))))

@ -1,3 +0,0 @@
const defineGrammar = require('../common/define-grammar');
module.exports = defineGrammar('typescript');

@ -1,3 +0,0 @@
{
"main": "../bindings/node/typescript"
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

@ -1,15 +0,0 @@
#include "../../common/scanner.h"
void *tree_sitter_typescript_external_scanner_create() { return NULL; }
void tree_sitter_typescript_external_scanner_destroy(void *payload) {}
void tree_sitter_typescript_external_scanner_reset(void *payload) {}
unsigned tree_sitter_typescript_external_scanner_serialize(void *payload, char *buffer) { return 0; }
void tree_sitter_typescript_external_scanner_deserialize(void *payload, const char *buffer, unsigned length) {}
bool tree_sitter_typescript_external_scanner_scan(void *payload, TSLexer *lexer, const bool *valid_symbols) {
return external_scanner_scan(payload, lexer, valid_symbols);
}

@ -1,230 +0,0 @@
#ifndef TREE_SITTER_PARSER_H_
#define TREE_SITTER_PARSER_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#define ts_builtin_sym_error ((TSSymbol)-1)
#define ts_builtin_sym_end 0
#define TREE_SITTER_SERIALIZATION_BUFFER_SIZE 1024
#ifndef TREE_SITTER_API_H_
typedef uint16_t TSStateId;
typedef uint16_t TSSymbol;
typedef uint16_t TSFieldId;
typedef struct TSLanguage TSLanguage;
#endif
typedef struct {
TSFieldId field_id;
uint8_t child_index;
bool inherited;
} TSFieldMapEntry;
typedef struct {
uint16_t index;
uint16_t length;
} TSFieldMapSlice;
typedef struct {
bool visible;
bool named;
bool supertype;
} TSSymbolMetadata;
typedef struct TSLexer TSLexer;
struct TSLexer {
int32_t lookahead;
TSSymbol result_symbol;
void (*advance)(TSLexer *, bool);
void (*mark_end)(TSLexer *);
uint32_t (*get_column)(TSLexer *);
bool (*is_at_included_range_start)(const TSLexer *);
bool (*eof)(const TSLexer *);
};
typedef enum {
TSParseActionTypeShift,
TSParseActionTypeReduce,
TSParseActionTypeAccept,
TSParseActionTypeRecover,
} TSParseActionType;
typedef union {
struct {
uint8_t type;
TSStateId state;
bool extra;
bool repetition;
} shift;
struct {
uint8_t type;
uint8_t child_count;
TSSymbol symbol;
int16_t dynamic_precedence;
uint16_t production_id;
} reduce;
uint8_t type;
} TSParseAction;
typedef struct {
uint16_t lex_state;
uint16_t external_lex_state;
} TSLexMode;
typedef union {
TSParseAction action;
struct {
uint8_t count;
bool reusable;
} entry;
} TSParseActionEntry;
struct TSLanguage {
uint32_t version;
uint32_t symbol_count;
uint32_t alias_count;
uint32_t token_count;
uint32_t external_token_count;
uint32_t state_count;
uint32_t large_state_count;
uint32_t production_id_count;
uint32_t field_count;
uint16_t max_alias_sequence_length;
const uint16_t *parse_table;
const uint16_t *small_parse_table;
const uint32_t *small_parse_table_map;
const TSParseActionEntry *parse_actions;
const char * const *symbol_names;
const char * const *field_names;
const TSFieldMapSlice *field_map_slices;
const TSFieldMapEntry *field_map_entries;
const TSSymbolMetadata *symbol_metadata;
const TSSymbol *public_symbol_map;
const uint16_t *alias_map;
const TSSymbol *alias_sequences;
const TSLexMode *lex_modes;
bool (*lex_fn)(TSLexer *, TSStateId);
bool (*keyword_lex_fn)(TSLexer *, TSStateId);
TSSymbol keyword_capture_token;
struct {
const bool *states;
const TSSymbol *symbol_map;
void *(*create)(void);
void (*destroy)(void *);
bool (*scan)(void *, TSLexer *, const bool *symbol_whitelist);
unsigned (*serialize)(void *, char *);
void (*deserialize)(void *, const char *, unsigned);
} external_scanner;
const TSStateId *primary_state_ids;
};
/*
* Lexer Macros
*/
#ifdef _MSC_VER
#define UNUSED __pragma(warning(suppress : 4101))
#else
#define UNUSED __attribute__((unused))
#endif
#define START_LEXER() \
bool result = false; \
bool skip = false; \
UNUSED \
bool eof = false; \
int32_t lookahead; \
goto start; \
next_state: \
lexer->advance(lexer, skip); \
start: \
skip = false; \
lookahead = lexer->lookahead;
#define ADVANCE(state_value) \
{ \
state = state_value; \
goto next_state; \
}
#define SKIP(state_value) \
{ \
skip = true; \
state = state_value; \
goto next_state; \
}
#define ACCEPT_TOKEN(symbol_value) \
result = true; \
lexer->result_symbol = symbol_value; \
lexer->mark_end(lexer);
#define END_STATE() return result;
/*
* Parse Table Macros
*/
#define SMALL_STATE(id) ((id) - LARGE_STATE_COUNT)
#define STATE(id) id
#define ACTIONS(id) id
#define SHIFT(state_value) \
{{ \
.shift = { \
.type = TSParseActionTypeShift, \
.state = (state_value) \
} \
}}
#define SHIFT_REPEAT(state_value) \
{{ \
.shift = { \
.type = TSParseActionTypeShift, \
.state = (state_value), \
.repetition = true \
} \
}}
#define SHIFT_EXTRA() \
{{ \
.shift = { \
.type = TSParseActionTypeShift, \
.extra = true \
} \
}}
#define REDUCE(symbol_val, child_count_val, ...) \
{{ \
.reduce = { \
.type = TSParseActionTypeReduce, \
.symbol = symbol_val, \
.child_count = child_count_val, \
__VA_ARGS__ \
}, \
}}
#define RECOVER() \
{{ \
.type = TSParseActionTypeRecover \
}}
#define ACCEPT_INPUT() \
{{ \
.type = TSParseActionTypeAccept \
}}
#ifdef __cplusplus
}
#endif
#endif // TREE_SITTER_PARSER_H_