Skip to content

Commit

Permalink
Fix #63
Browse files Browse the repository at this point in the history
  • Loading branch information
GoogleFeud committed Dec 10, 2024
1 parent 0d60bf6 commit 118d113
Show file tree
Hide file tree
Showing 12 changed files with 130 additions and 213 deletions.
4 changes: 0 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"./unplugin": {
"types": "./dist/unplugin/index.d.ts",
"default": "./dist/unplugin/index.js"
}
},
"scripts": {
Expand Down
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
34 changes: 21 additions & 13 deletions src/gen/expressionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +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;
// normalizes the name to be a valid identifier
name = name.replace(/^[^a-zA-Z_$]/g, "_");
name = name.replace(/[^a-zA-Z0-9_$]/g, "_");
if (normalize) name = _normalizeJsName(name);
return nonUnique ? factory.createIdentifier(name) : factory.createUniqueName(name);
}

Expand Down Expand Up @@ -111,7 +109,7 @@ 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);
}

Expand Down Expand Up @@ -172,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.createElementAccessExpression(exp, ts.factory.createStringLiteral(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 Down Expand Up @@ -229,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] ? ts.factory.createStringLiteral(e[0]) : undefined, e[1] ? e[1] : _ident(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 @@ -268,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");
17 changes: 13 additions & 4 deletions src/gen/nodes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,9 +376,10 @@ export function genNode(validator: Validator, ctx: NodeGenContext): GenResult {
}
case TypeDataKinds.Object: {
const checks: ts.Statement[] = [];
const names: [string, ts.Identifier][] = [];
const names: [string, ts.Identifier, boolean | undefined][] = [];
for (const child of validator.children) {
if (!child.isRedirect() && child.weigh() > 4 && typeof child.name === "string") names.push([child.name, child.setAlias(() => _ident(child.name as string))]);
if (!child.isRedirect() && child.weigh() > 4 && typeof child.name === "string")
names.push([child.name, child.setAlias(() => _ident(child.name as string, false, child.typeData.isStringWrapped)), child.typeData.isStringWrapped]);
checks.push(...validateType(child, ctx));
}

Expand Down Expand Up @@ -430,13 +431,21 @@ export function genNode(validator: Validator, ctx: NodeGenContext): GenResult {
const indexTypeCheck = genNode(indexType, ctx);
checkBody = [
stmt,
_if(indexTypeCheck.condition, error(ctx, [validator, joinElements(["Expected key ", keyName, " of ", ...validator.path(), " ", indexType.translate("to be ", true)])], true)),
_if(
indexTypeCheck.condition,
error(ctx, [validator, joinElements(["Expected key ", keyName, " of ", ...validator.path(), " ", indexType.translate("to be ", true)])], true)
),
...validateType(valueType, ctx)
];
} else checkBody = validateType(valueType, ctx);

if (finalCheck) finalCheck = _if(typeCheck, checkBody, finalCheck);
else finalCheck = _if(typeCheck, checkBody, error(ctx, [validator, joinElements(["Expected key ", keyName, " of ", ...validator.path(), indexType.translate("to be ", true)])], true));
else
finalCheck = _if(
typeCheck,
checkBody,
error(ctx, [validator, joinElements(["Expected key ", keyName, " of ", ...validator.path(), indexType.translate("to be ", true)])], true)
);
}

checks.push(_for_in(validator.expression(), keyName, finalCheck)[0]);
Expand Down
4 changes: 3 additions & 1 deletion src/gen/validators/genValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ export function genValidator(transformer: Transformer, type: ts.Type | undefined
const properties = (parent: Validator) =>
type.getProperties().map(sym => {
const typeOfProp = (transformer.checker.getTypeOfSymbol(sym) || transformer.checker.getNullType()) as ts.Type;
return genValidator(transformer, typeOfProp, sym.name, undefined, parent);
const validator = genValidator(transformer, typeOfProp, sym.name, undefined, parent);
if (validator) validator.typeData.isStringWrapped = sym.declarations?.[0] && ts.isPropertySignature(sym.declarations[0]) && ts.isStringLiteral(sym.declarations[0].name);
return validator;
});
const objValidator = new Validator(type, name, {kind: TypeDataKinds.Object}, exp, parent, properties);
for (const info of ((type as ts.ObjectType).indexInfos as ts.IndexInfo[]) || []) {
Expand Down
12 changes: 9 additions & 3 deletions src/gen/validators/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,11 @@ export interface TransformTypeData {
postChecks?: Validator;
}

export type TypeData =
export interface BaseTypeData {
isStringWrapped?: boolean;
}

export type TypeData = (
| BooleanTypeData
| SymbolTypeData
| FunctionTypeData
Expand All @@ -140,7 +144,9 @@ export type TypeData =
| ResolveTypeData
| RecursiveTypeData
| CheckTypeData
| TransformTypeData;
| TransformTypeData
) &
BaseTypeData;

export type ValidatorTargetName = string | number | ts.Identifier;

Expand Down Expand Up @@ -201,7 +207,7 @@ export class Validator {
if (this._exp) return this._exp;
if (!this.parent) return ts.factory.createNull();
if (this.name === "") return this.parent.expression();
return (this._exp = _access(this.parent.expression(), this.name));
return (this._exp = _access(this.parent.expression(), this.name, this.typeData.isStringWrapped));
}

/**
Expand Down
122 changes: 0 additions & 122 deletions src/plugin/index.ts

This file was deleted.

4 changes: 1 addition & 3 deletions src/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,12 @@ export type CodeReferenceExpand = {kind: CodeReferenceKind; expression: ts.Expre

export type CodeReferenceReplacement = Record<string, ts.Expression | ((...args: ts.Expression[]) => ts.Node)>;

<<<<<<< HEAD
const DEFAULT_MARKER = Markers["Assert"] as MarkerFn;
=======

export interface SymbolImportInfo {
identifierMap: Map<ts.Symbol, ts.Identifier>;
importStatements: ts.ImportDeclaration[];
}
>>>>>>> 6554491137984c00899d86706118722bdb5a05b3

export class Transformer {
checker: ts.TypeChecker;
Expand Down
27 changes: 0 additions & 27 deletions src/unplugin/factory.ts

This file was deleted.

12 changes: 0 additions & 12 deletions src/unplugin/index.ts

This file was deleted.

Loading

0 comments on commit 118d113

Please sign in to comment.