Skip to content

zijianhuang/TypeScriptCodeDOM

Repository files navigation

CodeDOM in .NET (Framework) has come with 3 providers:

  1. CSharpCodeProvider
  2. VBCodeProvider
  3. JScriptCodeProvider

While JSCriptCodeProvider is good enough for generating JavaScript codes for Internet Explorer however IE had been discoutinued. TypeScriptCodeProvider provides extra benefits over a "JavaScript Provider" through generating TypeScript codes:

  1. Strongly typed interfaces and function prototypes for validation during design time and compile time.
  2. Re-use some portions of CodeDOM codes if a code generator toolset like WebApiClientGen and OpenApiClientGen targets multiple OO languages and multiple TypeScript libraries/frameworks.
  3. Naturally and inheriently adapt the evolution of the JavaScript standard as long as TypeScript will.
  4. Live well with TypeScript frameworks like Angular 2+ and Aurelia. And with Angular Reactive Forms, runtime validation could become possible, similar to the use cases of using validation attributes to decorate a member property of a .NET class.

Nuget package:

TypeScript CodeDOM logo

What For and Not

TypeScript CodeDOM is for code generators that use CodeDOM over template or Roslyn, for example, Fonlow.Poco2TS that reads an assembly with POCO classes, and WebApiClientGen that reads assemblies and the run time type info of a running Web API, and then create Code DOM that could be used to generate C# and TypeScript codes.

There are scenarios that Roslyn and template may provide more powerful features that you want and help you to write less codes with simpler design when developing the code generators.

What Supported and Not

CodeDOM covers almost every essential C# language features, however, not every features of such coverage is applicable to TypeScript.

bool ICodeGenerator.Supports(GeneratorSupport supports)
{
	return (supports & supported) != 0;
}

const GeneratorSupport supported = GeneratorSupport.ArraysOfArrays
	| GeneratorSupport.MultidimensionalArrays
	| GeneratorSupport.TryCatchStatements
	| GeneratorSupport.DeclareValueTypes
	| GeneratorSupport.DeclareEnums
	| GeneratorSupport.StaticConstructors
	| GeneratorSupport.DeclareInterfaces
	| GeneratorSupport.DeclareDelegates
	| GeneratorSupport.NestedTypes
	| GeneratorSupport.MultipleInterfaceMembers
	| GeneratorSupport.ComplexExpressions
	| GeneratorSupport.GenericTypeReference
	| GeneratorSupport.GenericTypeDeclaration
   ;

Most supported classes are covered by unit testing, while some are covered by integration testing in some code generator projects, marked as "Indirectly" in the table below.

Class Unit Tests
CodeArgumentReferenceExpression TestCodeArgumentReferenceExpression
CodeArrayCreateExpression TestCodeArrayCreateExpression*
CodeArrayIndexerExpression TestCodeArrayIndexerExpression*
CodeAssignStatement TestCodeAssignStatement
CodeAttributeDeclaration / CodeAttributeDeclarationCollection Test*Decorators
CodeAttributeArgument / CodeAttributeArgumentCollection Indirectly
CodeBaseReferenceExpression TestCodeBaseReferenceExpression
CodeBinaryOperatorExpression TestCodeIterationStatement
CodeBinaryOperatorType TestCodeIterationStatement
CodeCastExpression TestCodeCastExpression
CodeCatchClause / CodeCatchClauseCollection TestCodeTryCatchFinallyStatement, TestCodeTryCatchFinallyStatement
CodeCommentStatement / CodeCommentStatementCollection TestCodeCommentStatement*
CodeCompileUnit Indirectly
CodeConditionStatement TestCodeConditionStatement*
CodeConstructor Indirectly
CodeDirective / CodeDirectiveCollection Indirectly with CodeRegionDirective
CodeTypeDelegate TestCodeTypeDelegate
CodeExpressionStatement TestCodeIterationStatement
CodeExpression TestCodeExpression
CodeExpressionCollection Indirectly
CodeFieldReferenceExpression TestCodeFieldReferenceExpression
CodeIndexerExpression TestCodeIterationStatement
CodeIterationStatement TestCodeIterationStatement
CodeMemberField TestCodeTypeDeclarationWithMembers
CodeMemberMethod TestCodeTypeDeclarationWithMethodAndParameterDecorators
CodeMemberProperty TestCodeTypeDeclarationWithPropertyMembers
CodeMethodInvokeExpression TestCodeIterationStatement, TestCodeTryCatchFinallyStatement, TestCodeTryCatchStatement
CodeMethodReferenceExpression TestCodeMethodReferenceExpression, TestCodeMethodReferenceExpressionGeneric, TestCodeIterationStatement
CodeMethodReturnStatement TestCodeMethodReturnStatement
CodeNamespace / CodeNamespaceCollection Indirectly
CodeObject Indirectly in many derived classes
CodeObjectCreateExpression TestCodeObjectCreateExpression*
CodeParameterDeclarationExpression / CodeParameterDeclarationExpressionCollection TestTuple4Callback, TestTupleCallbackSnipet, TestTupleCallbackSnipet, TestCodeParameterDeclarationExpression
CodePrimitiveExpression TestCodeArrayCreateExpressionWithInit, and many
CodePropertyReferenceExpression TestCodePropertyReferenceExpression
CodeRegionDirective TestPersonWithRegions, Test2TypesWithRegions
CodeSnippetExpression TestCodeArrayIndexerExpression, and many
CodeSnippetStatement TestCodeConditionStatement, and many
CodeSnippetTypeMember Indirectly
CodeStatement / CodeStatementCollection TestCodeConditionStatementElse, and many
CodeThisReferenceExpression TestCodePropertyReferenceExpression, and many
CodeThrowExceptionStatement TestCodeThrowExceptionStatement
CodeTryCatchFinallyStatement TestCodeTryCatchFinallyStatement, TestCodeTryFinallyStatement, TestCodeTryCatchStatement
CodeTypeDeclaration TestCodeTypeDeclaration*
CodeTypeDeclarationCollection Indirectly with namespace.Types
CodeTypeMember / CodeTypeMemberCollection Indirectly
CodeTypeOfExpression TestCodeTypeOfExpression
CodeTypeParameter / CodeTypeParameterCollection Indirectly
CodeTypeReference / CodeTypeReferenceCollection TestCodeArrayCreateExpressionWithoutInit, and many
CodeTypeReferenceExpression TestCodeTypeReferenceExpression, TestCodeIterationStatement
CodeVariableDeclarationStatement TestCodeVariableDeclarationStatement*
CodeVariableReferenceExpression TestCodeVariableReferenceExpression, and many

Hints:

  • There are integration tests in PocoToTS, WebApiClientGen and OpenApiClientGen, covering some classes like XxxCollection.

Examples Utilizing TypeScriptProvider

Generated Code Examples

TypeScript Code Generators

CodeDOM Features Applicable to TypeScript but not Supported in TypeScriptProvider

CodeDOM Features Not Applicable to TypeScript

History

TypeScript CodeDOM had been a by-product of Strongly Typed Web API Client Generators since October 2015 until February 2024, as a foundation library of TypeScript code generators of WebApiClientGen and then OpenApiClientGen, as well as internal code generators in some companies in which the development teams have chosen the CodeDOM approach over templates.

Legacy repositories: