CSharpToJavaScript 0.0.7

dotnet add package CSharpToJavaScript --version 0.0.7                
NuGet\Install-Package CSharpToJavaScript -Version 0.0.7                
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="CSharpToJavaScript" Version="0.0.7" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add CSharpToJavaScript --version 0.0.7                
#r "nuget: CSharpToJavaScript, 0.0.7"                
#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.
// Install CSharpToJavaScript as a Cake Addin
#addin nuget:?package=CSharpToJavaScript&version=0.0.7

// Install CSharpToJavaScript as a Cake Tool
#tool nuget:?package=CSharpToJavaScript&version=0.0.7                

CSharpToJavaScript

Brute forcing conversion(generating) from C# to Javascript. This is a personal project with the goal of learning and understanding c# and js at the same time. Many stuff is not supported and some won't. Updates will be happening when I'm using this library.(irregular)

Nuget package | Website | Try it online! | CLI | VS Code Extension | VS Extension

C# input

namespace ConsoleAppTest.CSharp;

public class Test							
{
	public Test()
	{
		Console.WriteLine("HelloWorld!");
	}
}

Javascript output

class Test
{
	constructor()
	{
		console.log("HelloWorld!");
	}
}

How to use

  • 1 Create c# project or use existed one.
  • 2 Install nuget package or Download a specific version(visit releases) or Download a master(Code-Local-Download ZIP).
  • 3 Skip this if using Nuget package. Follow this to add reference to the project.
  • 4 In the Main method add:
CSTOJS cstojs = new();
await cstojs.GenerateOneAsync("FULL PATH TO CSHARP FILE/FOLDER YOU WHAT TO CONVERT");
  • 5 Run program and file will be generated in output path(default is "Directory.GetCurrentDirectory()") with name "|CS FILE NAME|.js"(default)
  • 6 See below for simple example "HelloWorld"

Example "HelloWorld"

Program.cs

using CSharpToJavaScript;
namespace ConsoleAppTest;

public class Program
{
	public static async Task Main()
	{
		CSTOJS cstojs = new();
		await cstojs.GenerateOneAsync("C:\\GitReps\\ConsoleAppTest\\CSharp\\Test.cs");

		Console.ReadKey();
	}
}

CSharp/Test.cs

using static CSharpToJavaScript.APIs.JS.GlobalObject;
namespace ConsoleAppTest.CSharp;

public class Test
{
	public Test()
	{
		GlobalThis.Console.Log("HelloWorld!");
	}
}

Above code will generate "Test.js" file that contains:

class Test
{
	constructor()
 	{
   		globalThis.console.log("HelloWorld!");
 	}
}

More examples here. WIP!

Some Todos

CLI for library: https://github.com/TiLied/CSTOJS_CLI

VS Code Extension using CLI: https://github.com/TiLied/CSTOJS_VSCode_Ext

VS Extension using CLI: https://github.com/TiLied/CSTOJS_VS_Ext

Website/documentation: https://github.com/TiLied/CSTOJS_Pages

Thanks for packages and content ❤️

Microsoft CodeAnalysis CSharp nuget package

MDN-content for js docs

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
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
0.0.7 94 5/3/2024
0.0.6 237 12/6/2023

- [Better output to the console.](https://github.com/TiLied/CSharpToJavaScript/commit/76d6195f4ff7a3ee5121ebaf556d868a14bf0772)
- [Better formatting for logging.](https://github.com/TiLied/CSharpToJavaScript/commit/d638bd8d42c2471455a228bb60232e1666048510)
- [Back to trace.](https://github.com/TiLied/CSharpToJavaScript/commit/13cf989165895d9e3b0b89fea66b6519729171dd)
- - Ignore readonly/required keywords.
- [C# enums -> JS const objects.](https://github.com/TiLied/CSharpToJavaScript/commit/d2dc159dce613742a33f34da986fd0ddf1518eb5)
- - Added modulus/percent token.
- - C# .? -> JS .?
- - C# ?? -> JS ??
- - Lambda "()=>..." in arguments.
- - Variable declaration with comma.
- - C# Console.Write -> JS console.log
- - C# List.FindLast -> JS Array.findLast
- - C# string.Length -> JS string.length
- - C# Math.Sqrt -> JS Math.sqrt
- [Added option "KeepBraceOnTheSameLine" default false.](https://github.com/TiLied/CSharpToJavaScript/commit/73855deb06275ad1bce2f67d55c5153f5c7f0134)
- - Added option "NormalizeWhitespace" default false.
- ["NormalizeWhitespace" applies before "KeepBraceOnTheSameLine".](https://github.com/TiLied/CSharpToJavaScript/commit/6715097349f3cf81c54f671ebc9155f416c13048)
- - Updated "Microsoft.CodeAnalysis.CSharp" to 4.9.2.
- - c# bitwise and shift operators -> js bitwise and shift operators.
- - c# try-catch-finally -> js try-catch-finally.
- - c# do...while -> js do...while.
- - Updated generated xml docs.
- - Updated generated c#.
- [Updated generated xml docs with a fixed bug.](https://github.com/TiLied/CSharpToJavaScript/commit/242962efdebca83c9721d546b8e1f6ef546ff6e0)
- - See: https://github.com/TiLied/GenDocsLib/commit/258677e41337b5dd97372186cb943af33fd89beb
- [Added ValueAttribute.](https://github.com/TiLied/CSharpToJavaScript/commit/f23574964911e634aa971967e788674517bf4b28)
- - Updated generated c# with ValueAttribute.
- [Updated generated c# with missing data.](https://github.com/TiLied/CSharpToJavaScript/commit/fad7b03a0e0048ab4a960fb398350ddb399c0367)
- - See: https://github.com/TiLied/GenCSharpLib/commit/ea766a3f9e556305ee4057ff6ed5c1f9b6bcf49e
- - Added static to fields.
- [Fixed Paranthesis.](https://github.com/TiLied/CSharpToJavaScript/commit/a2c6bcc59ceb3dfd95fe1dad6c35ba34408d249f)
- - Added Array conversion (new Type[]->new Array()).
- - Fixed some formating.
- - Small change to "this.".
- [Added numeric length support for array.](https://github.com/TiLied/CSharpToJavaScript/commit/222c69030d4fcabf1b8f3e542f91682574c4eb26)
- [Ignore partial keyword for classes.](https://github.com/TiLied/CSharpToJavaScript/commit/b956ab2a8560286cac8adaa801b346e7fb1157b6)
- - Small clean up.
- [Updated generated xml docs.](https://github.com/TiLied/CSharpToJavaScript/commit/cdb82d5dc26c44895950714509e71edee2e86dad)
- - See: https://github.com/TiLied/GenDocsLib/commit/27e38fd26cb11ce1cbfc320a201a94690744d10d
- - Updated generated csharp.
- - See: https://github.com/TiLied/GenCSharpLib/commit/2f9d033dcdc7d34c351a1b602b3f19bac507bde6
- [Ignore attributes while generating js.](https://github.com/TiLied/CSharpToJavaScript/commit/af59d727a0c67a90b56b564df0003a5c52412b74)
- - ValueAttribute is now public.

**Full Changelog**: https://github.com/TiLied/CSharpToJavaScript/compare/0.0.06...0.0.07