Skip to content
2,377 changes: 1,013 additions & 1,364 deletions docs/package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
"react": "^18.3.1",
"react-dom": "^18.3.1",
"tailwindcss": "^3.4.3",
"typescript": "^5.5.3"
"typescript": "^5.7.2"
},
"devDependencies": {
"@types/js-beautify": "^1.14.3"
}
}
}
136 changes: 130 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 12 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
"version": "0.6.2",
"description": "A typescript transformer which automatically generates validation code from your types.",
"main": "dist/index.js",
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
},
"scripts": {
"test": "tsc && cd ./tests && tspc && mocha dist/integrated/**/*.js && node ./dist/snapshots/index",
"ci": "cd ./tests && tspc && mocha dist/integrated/**/*.js && node ./dist/snapshots/index force",
Expand All @@ -20,7 +26,8 @@
"typescript",
"typecheck",
"runtime",
"check"
"check",
"unplugin"
],
"author": "GoogleFeud",
"license": "MIT",
Expand All @@ -33,7 +40,7 @@
"@types/diff": "^5.0.2",
"@types/mocha": "^9.1.0",
"@types/node": "^22.5.5",
"@types/ts-expose-internals": "npm:ts-expose-internals@^5.6.2",
"@types/ts-expose-internals": "npm:ts-expose-internals@^5.7.2",
"@typescript-eslint/eslint-plugin": "^5.62.0",
"@typescript-eslint/parser": "^5.62.0",
"chai": "^4.3.6",
Expand All @@ -43,7 +50,7 @@
"mitata": "^0.1.6",
"mocha": "^9.2.2",
"prettier": "^3.2.5",
"ts-patch": "^3.2.1",
"typescript": "^5.6.2"
"ts-patch": "^3.3.0",
"typescript": "^5.7.2"
}
}
}
24 changes: 0 additions & 24 deletions src/block.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,5 @@
import ts from "typescript";

// export class Block {
// nodes: Array<ts.Node>;
// cache: Set<ts.Symbol>;
// events: Array<() => void>;
// parent: Block<unknown> | undefined;
// constructor(parent?: Block) {
// this.nodes = [];
// this.cache = new Set();
// this.parent = parent;
// this.events = [];
// }

// isInCache(sym: ts.Symbol): boolean {
// // eslint-disable-next-line @typescript-eslint/no-this-alias
// let parent: Block<unknown> | undefined = this;
// while (parent) {
// if (parent.cache.has(sym)) return true;
// parent = parent.parent;
// }
// return false;
// }

// }

export interface Block<T> {
nodes: Array<T>;
cache: Set<ts.Symbol>;
Expand Down
55 changes: 33 additions & 22 deletions src/gen/expressionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export function _if(condition: ts.Expression, ifTrue: BlockLike, ifFalse?: Block
}

export function _if_chain(ind: number, check: [ts.Expression, BlockLike][], last?: BlockLike): ts.Statement | undefined {
if (ind >= check.length) return last ? _stmt(last) : undefined;;
if (ind >= check.length) return last ? _stmt(last) : undefined;
return factory.createIfStatement(check[ind]![0], _stmt(check[ind]![1]), _if_chain(ind + 1, check, last));
}

Expand All @@ -74,8 +74,9 @@ export function _var(name: ts.BindingName | string, initializer?: ts.Expression,
];
}

export function _ident(name: string | ts.Identifier, nonUnique?: boolean): ts.Identifier {
export function _ident(name: string | ts.Identifier, nonUnique?: boolean, normalize?: boolean): ts.Identifier {
if (typeof name !== "string") return name;
if (normalize) name = _normalizeJsName(name);
return nonUnique ? factory.createIdentifier(name) : factory.createUniqueName(name);
}

Expand Down Expand Up @@ -108,11 +109,12 @@ export function _throw(exp: ts.Expression): ts.Statement {
return factory.createThrowStatement(exp);
}

export function _str(string: string): ts.Expression {
export function _str(string: string): ts.StringLiteral {
return factory.createStringLiteral(string);
}

export function _num(number: number): ts.Expression {
if (number < 0) return factory.createPrefixUnaryExpression(ts.SyntaxKind.MinusToken, ts.factory.createNumericLiteral(Math.abs(number)));
return factory.createNumericLiteral(number);
}

Expand Down Expand Up @@ -168,10 +170,11 @@ export function _not(exp: ts.Expression): ts.Expression {
return factory.createPrefixUnaryExpression(ts.SyntaxKind.ExclamationToken, exp);
}

export function _access(exp: ts.Expression, key: string | number | ts.Expression): ts.Expression {
export function _access(exp: ts.Expression, key: string | number | ts.Expression, isStringWrapped?: boolean): ts.Expression {
if (typeof key === "string") {
if (isInt(key)) return factory.createElementAccessExpression(exp, ts.factory.createNumericLiteral(key));
return ts.factory.createPropertyAccessExpression(exp, key);
else if (isStringWrapped) return ts.factory.createElementAccessExpression(exp, ts.factory.createStringLiteral(key));
else return ts.factory.createPropertyAccessExpression(exp, key);
} else return factory.createElementAccessExpression(exp, key);
}

Expand All @@ -197,21 +200,20 @@ export function _for_in(arr: ts.Expression, elName: ts.Identifier | string, body
return [factory.createForInStatement(initializerCreate.declarationList, arr, _stmt(body)), initializer];
}

export function _val(val: unknown) : ts.Expression {
if (typeof val === "string") return _str(val);
else if (typeof val === "number") return _num(val);
else if (val === true) return _bool(true);
else if (val === false) return _bool(false);
else if (Array.isArray(val)) return _arr(val);
else if (val === null) return ts.factory.createNull();
else if (typeof val === "object") {
export function _val(val: unknown): ts.Expression {
if (typeof val === "string") return _str(val);
else if (typeof val === "number") return _num(val);
else if (val === true) return _bool(true);
else if (val === false) return _bool(false);
else if (Array.isArray(val)) return _arr(val);
else if (val === null) return ts.factory.createNull();
else if (typeof val === "object") {
if ("kind" in val && "pos" in val) return val as ts.Expression;
else return _obj(val as Record<string, unknown>);
}
else return UNDEFINED;
} else return UNDEFINED;
}

export function _arr(array: Array<unknown>) : ts.Expression {
export function _arr(array: Array<unknown>): ts.Expression {
return ts.factory.createArrayLiteralExpression(array.map(val => _val(val)));
}

Expand All @@ -226,13 +228,17 @@ export function _obj(props: Record<string | number | symbol, unknown>): ts.Expre
return factory.createObjectLiteralExpression(propNodes);
}

export function _obj_binding_decl(elements: [string, ts.Identifier?][], value: ts.Expression): ts.VariableDeclaration {
return factory.createVariableDeclaration(
factory.createObjectBindingPattern(elements.map(e => factory.createBindingElement(undefined, e[1] ? e[0] : undefined, e[1] ? e[1] : e[0], undefined))),
undefined,
undefined,
value
export function _obj_binding_decl(elements: [string, ts.Identifier | undefined, boolean | undefined][], value: ts.Expression): ts.VariableDeclaration {
const mappedBindingElements = elements.map(e =>
factory.createBindingElement(
undefined,
// Property name
e[1] ? (e[2] ? _str(e[0]) : e[0]) : undefined,
// Alias
e[1] ? e[1] : e[2] ? _normalizeJsName(e[0]) : e[0]
)
);
return factory.createVariableDeclaration(factory.createObjectBindingPattern(mappedBindingElements), undefined, undefined, value);
}

export function _arr_binding_decl(elements: [number, ts.Identifier][], value: ts.Expression): ts.VariableDeclaration {
Expand Down Expand Up @@ -265,4 +271,9 @@ export function _assign(left: ts.Expression, right: ts.Expression): ts.Expressio
return factory.createAssignment(left, right);
}

export function _normalizeJsName(name: string): string {
name = name.replace(/^[^a-zA-Z_$]/g, "_");
return name.replace(/[^a-zA-Z0-9_$]/g, "_");
}

export const UNDEFINED = factory.createIdentifier("undefined");
Loading
Loading