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

Fix build error for special characters #64

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
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
30 changes: 16 additions & 14 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 @@ -76,6 +76,9 @@ export function _var(name: ts.BindingName | string, initializer?: ts.Expression,

export function _ident(name: string | ts.Identifier, nonUnique?: 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, "_");
return nonUnique ? factory.createIdentifier(name) : factory.createUniqueName(name);
}

Expand Down Expand Up @@ -171,7 +174,7 @@ export function _not(exp: ts.Expression): ts.Expression {
export function _access(exp: ts.Expression, key: string | number | ts.Expression): ts.Expression {
if (typeof key === "string") {
if (isInt(key)) return factory.createElementAccessExpression(exp, ts.factory.createNumericLiteral(key));
return ts.factory.createPropertyAccessExpression(exp, key);
return ts.factory.createElementAccessExpression(exp, ts.factory.createStringLiteral(key));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this always create element access expressions? It would be nice to create element access expressions only when necessary because otherwise code turns to this:

// We don't want this
errs["push"]("Expected value to be an object");

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It sure looks good to use createElementAccessExpression only when there are special characters, but since it is only used to access data and is a built file, it doesn't seem to matter much.

} 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 @@ -228,7 +230,7 @@ export function _obj(props: Record<string | number | symbol, unknown>): ts.Expre

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))),
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
Expand Down