js-to-ts-converter icon indicating copy to clipboard operation
js-to-ts-converter copied to clipboard

Added getTypesFromComment() function.

Open IvanFarkas opened this issue 3 years ago • 5 comments

Allow property type metadata to be extracted from property comment.

Javascript input

export class Test {
	constructor() {
		// Types: [`TS Type` #TS Default# @TS IsNullable@ ^OA Type^ ~OA Format~] - https://regex101.com
		// TypeScript Data Types - https://www.typescriptlang.org/docs/handbook/basic-types.html
		// OpenAPI Data Types - https://swagger.io/docs/specification/data-models/data-types
		// TS Type pattern: `` - (?<=`).*(?=`)
		// TS Default pattern: ## - (?<=#).*(?=#)
		// TS IsNullable pattern: @@ - (?<=@).*(?=@)
		// OA Type pattern: ^^ - (?<=\^).*(?=\^)
		// OA Format pattern: ~~ - (?<=~).*(?=~)
		this.strProp = null; // Types: [`string` #'New Value'# @true@ ^string^ ~~] - 
		this.boolProp = null; // Types: [`boolean` #true# @true@ ^boolean^ ~~] - 

		// OpenAPI String - https://swagger.io/docs/specification/data-models/data-types/#string
		this.dateProp = null; // Types: [`Date` ## @@ ^string^ ~date~] - 
		this.dateTimeProp = null; // Types: [`Date` ## @@ ^string^ ~date-time~] - 
		this.byteProp = null; // Types: [`string` ## @@ ^string^ ~byte~] - 
		this.binaryProp = null; // Types: [`string` ## @@ ^string^ ~binary~] - 
		this.emailProp = null; // Types: [`string` ## @@ ^string^ ~email~] - 

		// OpenAPI Numbers - https://swagger.io/docs/specification/data-models/data-types/#numbers
		this.intProp = null; // Types: [`number` ## @@ ^integer^] - 
		this.int32Prop = null; // Types: [`number` ## @@ ^integer^ ~int32~] - 
		this.int64Prop = null; // Types: [`number` ## @@ ^integer^ ~int64~] - 
		this.floatProp = null; // Types: [`number` ## @@ ^number^ ~float~] - 
		this.doubleProp = null; // Types: [`number` ## @@ ^number^ ~double~] - 

		this.classProp = new TestRef(); // Types: [`TestRef` ## @@ ^object^ ~REF:TestRef~] - TestRef class
		this.arrayofClassProp = [new TestRef()]; // Types: [`TestRef[]` ## @@ ^array^ ~REF:TestRef[]~] - Arrray of TestRef class
	}
}

Typescript output:

export class Test {
	public strProp: string | null = 'New Value';
	public boolProp: boolean | null = true;
	public dateProp: Date;
	public dateTimeProp: Date;
	public byteProp: string;
	public binaryProp: string;
	public emailProp: string;
	public intProp: number;
	public int32Prop: number;
	public int64Prop: number;
	public floatProp: number;
	public doubleProp: number;
	public classProp: TestRef;
	public arrayofClassProp: TestRef[];

	constructor() {
		// Types: [`TS Type` #TS Default# @TS IsNullable@ ^OA Type^ ~OA Format~] - https://regex101.com
		// TypeScript Data Types - https://www.typescriptlang.org/docs/handbook/basic-types.html
		// OpenAPI Data Types - https://swagger.io/docs/specification/data-models/data-types
		// TS Type pattern: `` - (?<=`).*(?=`)
		// TS Default pattern: ## - (?<=#).*(?=#)
		// TS IsNullable pattern: @@ - (?<=@).*(?=@)
		// OA Type pattern: ^^ - (?<=\^).*(?=\^)
		// OA Format pattern: ~~ - (?<=~).*(?=~)
		this.strProp = null; // Types: [`string` #'New Value'# @true@ ^string^ ~~] - 
		this.boolProp = null; // Types: [`boolean` #true# @true@ ^boolean^ ~~] - 

		// OpenAPI String - https://swagger.io/docs/specification/data-models/data-types/#string
		this.dateProp = null; // Types: [`Date` ## @@ ^string^ ~date~] - 
		this.dateTimeProp = null; // Types: [`Date` ## @@ ^string^ ~date-time~] - 
		this.byteProp = null; // Types: [`string` ## @@ ^string^ ~byte~] - 
		this.binaryProp = null; // Types: [`string` ## @@ ^string^ ~binary~] - 
		this.emailProp = null; // Types: [`string` ## @@ ^string^ ~email~] - 

		// OpenAPI Numbers - https://swagger.io/docs/specification/data-models/data-types/#numbers
		this.intProp = null; // Types: [`number` ## @@ ^integer^] - 
		this.int32Prop = null; // Types: [`number` ## @@ ^integer^ ~int32~] - 
		this.int64Prop = null; // Types: [`number` ## @@ ^integer^ ~int64~] - 
		this.floatProp = null; // Types: [`number` ## @@ ^number^ ~float~] - 
		this.doubleProp = null; // Types: [`number` ## @@ ^number^ ~double~] - 

		this.classProp = new TestRef(); // Types: [`TestRef` ## @@ ^object^ ~REF:TestRef~] - TestRef class
		this.arrayofClassProp = [new TestRef()]; // Types: [`TestRef[]` ## @@ ^array^ ~REF:TestRef[]~] - Arrray of TestRef class
	}

IvanFarkas avatar Nov 14 '21 23:11 IvanFarkas

Wow well this is interesting! Are those comments left by swagger generating files?

gregjacobs avatar Nov 18 '21 02:11 gregjacobs

Btw, sorry for the delay in seeing this - just saw the email notification now!

gregjacobs avatar Nov 18 '21 02:11 gregjacobs

Wow well this is interesting! Are those comments left by swagger generating files?

No. Hand generated ;-)

IvanFarkas avatar Nov 18 '21 19:11 IvanFarkas

Updated PR with requested items.

IvanFarkas avatar Nov 18 '21 20:11 IvanFarkas

Get constructor, getter, setter, methods, functions parameter and return types from JSDoc.

IvanFarkas avatar Dec 01 '21 10:12 IvanFarkas