All Projects → ToCSharp → TypeScriptAST

ToCSharp / TypeScriptAST

Licence: Apache-2.0 license
.NET port of Microsoft's TypeScript parser for simple AST manipulation

Programming Languages

C#
18002 projects

Labels

Projects that are alternatives of or similar to TypeScriptAST

Cppast.net
CppAst is a .NET library providing a C/C++ parser for header files powered by Clang/libclang with access to the full AST, comments and macros
Stars: ✭ 228 (+516.22%)
Mutual labels:  ast
html5parser
A super tiny and fast html5 AST parser.
Stars: ✭ 153 (+313.51%)
Mutual labels:  ast
gox
JSX for Go
Stars: ✭ 165 (+345.95%)
Mutual labels:  ast
Tsutils
utility functions for working with typescript's AST
Stars: ✭ 240 (+548.65%)
Mutual labels:  ast
php2python
Convert PHP code to Python under CGI (beta)
Stars: ✭ 44 (+18.92%)
Mutual labels:  ast
sast
Parse CSS, Sass, SCSS, and Less into a unist syntax tree
Stars: ✭ 51 (+37.84%)
Mutual labels:  ast
Ast Query
Tentative to a simple JavaScript AST modification library
Stars: ✭ 221 (+497.3%)
Mutual labels:  ast
BBob
⚡️Blazing-fast js-bbcode-parser, bbcode js, that transforms and parses to AST with plugin support in pure javascript, no dependencies
Stars: ✭ 133 (+259.46%)
Mutual labels:  ast
stutter
Implement a Lisp, in C, from scratch, no libs
Stars: ✭ 65 (+75.68%)
Mutual labels:  ast
venusscript
A dynamic, interpreted, scripting language written in Java.
Stars: ✭ 17 (-54.05%)
Mutual labels:  ast
Gulp Strip Debug
Strip console, alert, and debugger statements from JavaScript code
Stars: ✭ 242 (+554.05%)
Mutual labels:  ast
Gengen
A Go source transformation tool for generics
Stars: ✭ 253 (+583.78%)
Mutual labels:  ast
java-ast
Java Parser for JavaScript/TypeScript (based on antlr4ts)
Stars: ✭ 58 (+56.76%)
Mutual labels:  ast
Unified
☔️ interface for parsing, inspecting, transforming, and serializing content through syntax trees
Stars: ✭ 3,036 (+8105.41%)
Mutual labels:  ast
rehype-dom
HTML processor to parse and compile with browser APIs, powered by plugins
Stars: ✭ 20 (-45.95%)
Mutual labels:  ast
Php Parser
A PHP parser written in PHP
Stars: ✭ 15,101 (+40713.51%)
Mutual labels:  ast
astexplorer-go
No description or website provided.
Stars: ✭ 17 (-54.05%)
Mutual labels:  ast
deco
Minimalist Function Decorators for Elixir
Stars: ✭ 21 (-43.24%)
Mutual labels:  ast
parser-reflection
Parser Reflection API - Provides source code analysis without loading classes into the PHP memory
Stars: ✭ 97 (+162.16%)
Mutual labels:  ast
awesome-ruby-ast
A list of awesome tools and libraries which deals with ASTs in Ruby
Stars: ✭ 24 (-35.14%)
Mutual labels:  ast

This is old project for TypeScript, which is developing so fast. I think now is not the best option for parsing TypeScript. Now it's not easy to upgrade to the current TypeScript. For JavaScript I think it still good. It's time to rewrite TypeScriptAST. Microsoft showed us how to do it in System.Text.Json for .NET Core 3.0: "Provide high-performance JSON APIs. We needed a new set of JSON APIs that are highly tuned for performance by using Span" https://devblogs.microsoft.com/dotnet/try-the-new-system-text-json-apis/ https://github.com/dotnet/runtime/blob/master/src/libraries/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.cs

TypeScriptAST

.NET port of Microsoft's TypeScript parser for simple AST manipulation.

It works with TypeScript, JavaScript and DefinitelyTyped(".d.ts") files and gives the same tree as typescriptServices.js.

Join the chat at https://gitter.im/TypeScriptAST/Lobby

Install TypeScriptAST via NuGet

If you want to include TypeScriptAST in your project, you can install it directly from NuGet

PM> Install-Package TypeScriptAST

Create AST

var ast = new TypeScriptAST(File.ReadAllText(file), file);

Find Node

By SyntaxKind

  var functions = ast.OfKind(SyntaxKind.FunctionExpression);
  var vars = functions.FirstOrDefault()?.OfKind(SyntaxKind.VariableDeclaration);

By Node type

  var functions = ast.GetDescendants().OfType<FunctionExpression>();
  var vars = functions.FirstOrDefault()?.GetDescendants().OfType<VariableDeclaration>();

GetText

  var firstFunc = ast.OfKind(SyntaxKind.FunctionExpression).FirstOrDefault();
  var text = firstFunc?.GetText();
  var withComments = firstFunc?.GetTextWithComments();

Change Node

  var funcNewCode = "function() {}";
  var change = new ChangeAST();
  change.ChangeNode(firstFunc, funcNewCode);
  var newSource = change.GetChangedSource(ast.SourceStr);
  File.WriteAllText(file, newSource);

File modification Example

This example included in TypeScriptAstExample. It finds modules in file, collects some info, adds new function to module.

  using Zu.TypeScript;
  using Zu.TypeScript.Change;
  using Zu.TypeScript.TsTypes;
  
            var fileName = "parser.ts";
            var source = File.ReadAllText(fileName);

            var ast = new TypeScriptAST(source, fileName);

            var change = new ChangeAST();

            foreach (var module in ast.GetDescendants().OfType<ModuleDeclaration>())
            {
                var funcs = module.Body.Children.OfType<FunctionDeclaration>().ToList();
                var enums = module.Body.Children.OfType<EnumDeclaration>();
                var moduleInfoFunc = $@"
    export function getModuleInfo() {{
        return ""Module {module.IdentifierStr} contains {funcs.Count()} functions ({funcs.Count(v => v.IdentifierStr.StartsWith("parse"))} starts with parse), {enums.Count()} enums ..."";
    }}
";
                change.InsertBefore(module.Body.Children.First(), moduleInfoFunc);
            }
            var newSource = change.GetChangedSource(ast.SourceStr);

            File.WriteAllText("parser2.ts", newSource);
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].