TypeScriptParser 0.4.2.39

dotnet add package TypeScriptParser --version 0.4.2.39
                    
NuGet\Install-Package TypeScriptParser -Version 0.4.2.39
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TypeScriptParser" Version="0.4.2.39" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TypeScriptParser" Version="0.4.2.39" />
                    
Directory.Packages.props
<PackageReference Include="TypeScriptParser" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add TypeScriptParser --version 0.4.2.39
                    
#r "nuget: TypeScriptParser, 0.4.2.39"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package TypeScriptParser@0.4.2.39
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=TypeScriptParser&version=0.4.2.39
                    
Install as a Cake Addin
#tool nuget:?package=TypeScriptParser&version=0.4.2.39
                    
Install as a Cake Tool

TypeScriptParser

基于Tree-sitter的TypeScript解析器.NET绑定库,提供高性能的TypeScript代码解析功能。

安装

dotnet add package TypeScriptParser

快速开始

using System;
using TreeSitter.TypeScript;
using GitHub.TreeSitter;

// 创建解析器实例
using var parser = new TypeScriptParser();

// 解析TypeScript代码
string code = @"
function greet(name: string): string {
    return `Hello, ${name}!`;
}
";

using var tree = parser.ParseString(code);
var rootNode = tree.root_node();

// 遍历语法树
Console.WriteLine($"语法树类型: {rootNode.type()}");
Console.WriteLine($"子节点数量: {rootNode.child_count()}");

// 获取源码文本
Console.WriteLine($"源码内容: {rootNode.text(code)}");

主要功能

  • ✅ 完整的TypeScript语法支持
  • ✅ 高性能的增量解析
  • ✅ 跨平台支持 (Windows, Linux, macOS)
  • ✅ 详细的语法错误信息
  • ✅ 语法树遍历和查询

API 参考

TypeScriptParser 类

位于 TreeSitter.TypeScript 命名空间。

构造函数
var parser = new TypeScriptParser();
主要方法

ParseString(string sourceCode)

  • 解析TypeScript源代码字符串
  • 返回: TSTree - 解析后的语法树
var tree = parser.ParseString("const x = 42;");

CreateCursor(TSTree tree)

  • 创建语法树游标用于高效遍历
  • 返回: TSCursor - 语法树游标对象
using var cursor = parser.CreateCursor(tree);
属性

Language

  • 获取TypeScript语言对象
  • 类型: TSLanguage

IsAvailable

  • 检查解析器是否可用
  • 类型: bool

TSTree 类 (来自 GitHub.TreeSitter)

主要方法:

  • root_node() - 获取根节点
  • copy() - 复制语法树
  • edit(TSInputEdit edit) - 编辑语法树

TSNode 结构 (来自 GitHub.TreeSitter)

主要方法:

  • type() - 获取节点类型名称
  • child_count() - 获取子节点数量
  • child(uint index) - 获取指定索引的子节点
  • start_point() / end_point() - 获取节点位置
  • text(string sourceCode) - 获取节点对应的源码文本

语法树遍历示例

using var parser = new TypeScriptParser();
var tree = parser.ParseString(@"
class Calculator {
    add(a: number, b: number): number {
        return a + b;
    }
}
");

var root = tree.root_node();

// 递归遍历所有节点
void TraverseNode(TSNode node, string source, int depth = 0)
{
    var indent = new string(' ', depth * 2);
    Console.WriteLine($"{indent}{node.type()}: {node.text(source)}");
    
    for (uint i = 0; i < node.child_count(); i++)
    {
        TraverseNode(node.child(i), source, depth + 1);
    }
}

TraverseNode(root, code);

错误处理

using var parser = new TypeScriptParser();
var tree = parser.ParseString("const x = ;"); // 语法错误

var root = tree.root_node();
if (root.has_error())
{
    Console.WriteLine("语法树包含错误");
}

系统要求

  • .NET 9.0 或更高版本
  • 支持的平台:Windows (x64), Linux (x64), macOS (ARM64/x64)

许可证

本项目基于MIT许可证开源。

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.1

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.4.2.39 871 8/27/2025
0.3.3.37 244 8/27/2025
0.3.2.36 239 8/27/2025
0.3.1.35 236 8/27/2025
0.2.9.32 261 8/26/2025
0.2.8.31 262 8/26/2025
0.2.5.30 223 8/25/2025
0.2.4.29 209 8/25/2025
0.2.2.28 207 8/25/2025
0.1.1.26 260 8/24/2025
0.0.9.25 246 8/24/2025
0.0.8.24 252 8/24/2025
0.0.7.21 216 8/24/2025
0.0.6.20 215 8/24/2025
0.0.4.19 146 8/24/2025
0.0.3.18 146 8/24/2025
0.0.2.16 209 8/24/2025

基于Tree-sitter的TypeScript解析器 - 支持跨平台Native库自动管理