TypeScriptAST.Corrected 1.0.0

.NET Framework 4.5.2
NuGet\Install-Package TypeScriptAST.Corrected -Version 1.0.0
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.
dotnet add package TypeScriptAST.Corrected --version 1.0.0
<PackageReference Include="TypeScriptAST.Corrected" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add TypeScriptAST.Corrected --version 1.0.0
#r "nuget: TypeScriptAST.Corrected, 1.0.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install TypeScriptAST.Corrected as a Cake Addin
#addin nuget:?package=TypeScriptAST.Corrected&version=1.0.0

// Install TypeScriptAST.Corrected as a Cake Tool
#tool nuget:?package=TypeScriptAST.Corrected&version=1.0.0

This is based on package https://www.nuget.org/packages/TypeScriptAST/ and enhanced with PR: https://github.com/ToCSharp/TypeScriptAST/pull/9 If you face the issue described in the PR you need this package otherwise just use the original please ๐Ÿ˜ƒ.

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.Corrected in your project, you can install it directly from NuGet

PM> Install-Package TypeScriptAST.Corrected

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);

Product Versions
.NET Framework net452 net46 net461 net462 net463 net47 net471 net472 net48
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

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
1.0.0 297 8/11/2021

Initial release.