Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dev #3

Merged
merged 4 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .github/workflows/publish_to_npm.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
on:
push:
branches: master

jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: "18"
- run: npm ci
- run: npm run build
- run: npm test
- uses: JS-DevTools/npm-publish@v2
with:
token: ${{ secrets.NPM_TOKEN }}
7 changes: 7 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
test
src
public

webpack.config.js
tsconfig.json
jest.config.js
13 changes: 0 additions & 13 deletions index.html

This file was deleted.

7 changes: 0 additions & 7 deletions src/ExpressionGenerator.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/HeritageClauseHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,12 @@ export class HeritageClauseGenerator {

addIdentifier(name: string): this {
if (!this._token) throw new Error("HeritageClauseGenerator.addIdentifier | At first you should detiemine the token for HeritageClause");
console.log("333333333333333333", name)
const identifier = ts.factory.createIdentifier(name)
console.log("333333334444444444", name)

if (this._token === ts.SyntaxKind.ExtendsKeyword)
this._identifiers = [identifier];
else
this._identifiers.push(identifier)
console.log("4444444444444", name)

return this;
}
Expand Down
31 changes: 0 additions & 31 deletions src/TypeParameterDelarationGenerator.ts

This file was deleted.

22 changes: 0 additions & 22 deletions src/api.ts

This file was deleted.

11 changes: 0 additions & 11 deletions src/assignmentGenerator.ts

This file was deleted.

23 changes: 20 additions & 3 deletions src/bodyGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
import * as ts from "typescript";
import { statementFromExpression } from "./statements";
import { getPropertyOfClassExpression } from "./expressions";

export class BodyGenerator {

generate() {
// ts.factory.createBlock(
private _statements: ts.Statement[] = []

// )

addStatement(statement: ts.Statement): this {
this._statements.push(statement);
return this;
}
addExpression(expression: ts.Expression): this {
this._statements.push(statementFromExpression(expression));
return this;
}

returnsProperty(name: string | ts.Identifier): this {
this._statements.push(ts.factory.createReturnStatement(getPropertyOfClassExpression(name)))
return this;
}

generate(): ts.Block {
return ts.factory.createBlock(this._statements, true);
}
}
47 changes: 39 additions & 8 deletions src/classGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import * as ts from "typescript";
import { ModifierLikeHandler } from "./generator/modifiers";
import { TypeParameterDelarationGenerator } from "./generator/type";
import { ModifierLikeHandler } from "./modifiers";
import { TypeParameterDelarationGenerator } from "./types";

import { HeritageClauseGenerator } from "./HeritageClauseHandler";
import { ConstructorGenerator } from "./constructorGenerator";
import { PropertyGenerator } from "./properties";
import { GetterGenerator } from "./functions";


export class ClassGenerator {
Expand All @@ -13,14 +17,14 @@ export class ClassGenerator {
private _extended_heritageClause: HeritageClauseGenerator;
private _implemented_heritageClause: HeritageClauseGenerator;

constructor(className: string) {
this._name = className;
}
private _constructor: ConstructorGenerator;
private _properties: PropertyGenerator[] = [];

private _getters: GetterGenerator[] = [];

addMember(element: ts.ClassElement): this {
this._members.push(element);
return this;

constructor(className: string) {
this._name = className;
}

extends(baseClass: string): this {
Expand Down Expand Up @@ -51,6 +55,14 @@ export class ClassGenerator {
extimp.push(this._extended_heritageClause.generate());
if (this._implemented_heritageClause)
extimp.push(this._implemented_heritageClause.generate());

// add all properties to members
this._properties.forEach(p => this._members.push(p.generate()));
this._getters.forEach(p => this._members.push(p.generate()));

// add constructor to the members
if (this._constructor) this._members.push(this._constructor.generate());

return ts.factory.createClassDeclaration(
this._modifiers.toArray(),
this._name,
Expand All @@ -59,4 +71,23 @@ export class ClassGenerator {
this._members
)
}

setConstructor(): ConstructorGenerator {
if (this._constructor) return this._constructor;
this._constructor = new ConstructorGenerator();
return this._constructor;
}

addProperty(name: string): PropertyGenerator {
const result = new PropertyGenerator(name);
this._properties.push(result);
return result;
}

addGetter(name: string | ts.Identifier) {
const result = new GetterGenerator(name);
this._getters.push(result);
return result;
}

}
15 changes: 11 additions & 4 deletions src/constructorGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import * as ts from "typescript";
import { ModifierLikeHandler } from "../modifiers";
import { ParameterGenerator } from "./ParameterGenerator";
import { ModifierLikeHandler } from "./modifiers";
import { ParameterGenerator } from "./parameters";
import { BodyGenerator } from "./bodyGenerator";

export class ConstructorGenerator {

private _modifiers: ModifierLikeHandler = new ModifierLikeHandler();
private _parameters: ParameterGenerator[] = [];

private _block: BodyGenerator;

addParameter(name: string): ParameterGenerator {
const result = new ParameterGenerator(name);
Expand All @@ -18,8 +19,14 @@ export class ConstructorGenerator {
return ts.factory.createConstructorDeclaration(
this._modifiers.toArray(),
this._parameters.map(p => p.generate()),
undefined
this._block.generate(),
)
}

setBody() {
if (this._block) return this._block;
this._block = new BodyGenerator();
return this._block;
}

}
File renamed without changes.
29 changes: 29 additions & 0 deletions src/expressions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as ts from "typescript";
import { statementFromExpression } from "./statements";

export function or(expr1: ts.Expression, expr2: ts.Expression): ts.BinaryExpression {
return ts.factory.createBinaryExpression(expr1, ts.SyntaxKind.BarBarToken, expr2);
}

export function expOrNull(expr: ts.Expression): ts.BinaryExpression {
return ts.factory.createBinaryExpression(expr, ts.SyntaxKind.BarBarToken, ts.factory.createNull())
}

export function assignToClassProperty(name: string | ts.Identifier, expresion: ts.Expression): ts.AssignmentExpression<ts.AssignmentOperatorToken> {
const property = getPropertyOfClassExpression(name);
return ts.factory.createAssignment(property, expresion);
}

export function getPropertyOfClassExpression(name: string | ts.Identifier): ts.Expression {
return ts.factory.createPropertyAccessExpression(
ts.factory.createIdentifier("this"),
typeof name === "string"
? ts.factory.createIdentifier(name)
: name
);
}


export function expressionToStatement(expr: ts.Expression): ts.Statement {
return statementFromExpression(expr);
}
54 changes: 54 additions & 0 deletions src/functions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import * as ts from "typescript";
import { ModifierLikeHandler } from "./modifiers";
import { BodyGenerator } from "./bodyGenerator";
import { ParameterGenerator } from "./parameters";

export class GetterGenerator {
private _name: string;
private _modifiers: ModifierLikeHandler = new ModifierLikeHandler();
private _parameters: ParameterGenerator[] = [];
private _identifier: ts.Identifier;
private _block: BodyGenerator;
private _type: ts.TypeNode;


constructor(name: string | ts.Identifier) {
if (typeof name === "string") {
this._name = name;
this._identifier = ts.factory.createIdentifier(name);
} else {
this._name = name.text;
this._identifier = name;
}
}

readonly(): this {
this._modifiers.readonly();
return this;
}

private(): this {
this._modifiers.private();
return this;
}

setType(typeNode: ts.TypeNode): this { this._type = typeNode; return this; }


generate(): ts.GetAccessorDeclaration {
return ts.factory.createGetAccessorDeclaration(
this._modifiers.toArray(),
this._name,
this._parameters.map(p => p.generate()),
this._type,
this._block.generate()
);
}

setBody() {
if (this._block) return this._block;
this._block = new BodyGenerator();
return this._block;
}

}
Loading