Skip to content
Open
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
44 changes: 25 additions & 19 deletions packages/typegpu/src/shared/tseynit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,73 +9,79 @@ export function stringifyNode(node: tinyest.AnyNode): string {
return stringifyStatement(node, '');
}

function stringifyStatement(node: tinyest.Statement, ident: string): string {
function stringifyStatement(
node: tinyest.Statement,
ident: string,
omitInitialIndent = false,
): string {
const initialIndent = omitInitialIndent ? '' : ident;

if (isExpression(node)) {
return `${ident}${stringifyExpression(node, ident)};`;
return `${initialIndent}${stringifyExpression(node, ident)};`;
}

if (node[0] === NODE.block) {
const statements = node[1].map((n) => stringifyStatement(n, ident + ' '));
return `{\n${statements.join('\n')}\n${ident}}`;
return `${initialIndent}{\n${statements.join('\n')}\n${ident}}`;
}

if (node[0] === NODE.return) {
const expr = node[1] === undefined ? '' : ` ${stringifyExpression(node[1], '')}`;
return `${ident}return${expr};`;
return `${initialIndent}return${expr};`;
}

if (node[0] === NODE.if) {
const cond = stringifyExpression(node[1], ident);
const then = stringifyStatement(node[2], ident);
const base = `${ident}if (${cond}) ${then}`;
const then = stringifyStatement(node[2], ident, true);
const base = `${initialIndent}if (${cond}) ${then}`;
if (node[3] !== undefined) {
return `${base} else ${stringifyStatement(node[3], ident)}`;
return `${base} else ${stringifyStatement(node[3], ident, true)}`;
}
return base;
}

if (node[0] === NODE.let) {
if (node[2] !== undefined) {
return `${ident}let ${node[1]} = ${stringifyExpression(node[2], ident)};`;
return `${initialIndent}let ${node[1]} = ${stringifyExpression(node[2], ident)};`;
}
return `${ident}let ${node[1]};`;
return `${initialIndent}let ${node[1]};`;
}

if (node[0] === NODE.const) {
if (node[2] !== undefined) {
return `${ident}const ${node[1]} = ${stringifyExpression(node[2], ident)};`;
return `${initialIndent}const ${node[1]} = ${stringifyExpression(node[2], ident)};`;
}
return `${ident}const ${node[1]};`;
return `${initialIndent}const ${node[1]};`;
}

if (node[0] === NODE.for) {
const init = node[1] ? stringifyStatement(node[1], '') : ';';
const cond = node[2] ? stringifyExpression(node[2], ident) : '';
const update = node[3] ? stringifyStatement(node[3], '') : '';
const body = stringifyStatement(node[4], ident);
return `${ident}for (${init} ${cond}; ${update.slice(0, -1) /* trim the ';' */}) ${body}`;
const body = stringifyStatement(node[4], ident, true);
return `${initialIndent}for (${init} ${cond}; ${update.slice(0, -1) /* trim the ';' */}) ${body}`;
}

if (node[0] === NODE.while) {
const cond = stringifyExpression(node[1], ident);
const body = stringifyStatement(node[2], ident);
return `${ident}while (${cond}) ${body}`;
const body = stringifyStatement(node[2], ident, true);
return `${initialIndent}while (${cond}) ${body}`;
}

if (node[0] === NODE.continue) {
return `${ident}continue;`;
return `${initialIndent}continue;`;
}

if (node[0] === NODE.break) {
return `${ident}break;`;
return `${initialIndent}break;`;
}

if (node[0] === NODE.forOf) {
const leftKind = node[1][0] === NODE.const ? 'const' : 'let';
const leftName = node[1][1];
const right = stringifyExpression(node[2], ident);
const body = stringifyStatement(node[3], ident);
return `${ident}for (${leftKind} ${leftName} of ${right}) ${body}`;
const body = stringifyStatement(node[3], ident, true);
return `${initialIndent}for (${leftKind} ${leftName} of ${right}) ${body}`;
}

assertExhaustive(node);
Expand Down
22 changes: 22 additions & 0 deletions packages/typegpu/tests/internal/tseynit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,28 @@ describe('ast to JS transformation', () => {
`);
});

it('indents standalone nested blocks without adding spaces to control-flow blocks', () => {
const fn = () => {
'use gpu';
{
[1, 2];
}
if (true) {
[3, 4];
}
};
expect(stringifyNode(getBodyAst(fn))).toMatchInlineSnapshot(`
"{
{
[1, 2];
}
if (true) {
[3, 4];
}
}"
`);
});

it('handles declarations', () => {
const fn = () => {
'use gpu';
Expand Down
6 changes: 3 additions & 3 deletions packages/unplugin-typegpu/test/obfuscation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ describe('obfuscate', () => {
expect(stringifyNode(body)).toMatchInlineSnapshot(`
"{
const a = 1;
{
{
const a = 2;
if (false) {
return a;
Expand Down Expand Up @@ -540,7 +540,7 @@ describe('obfuscate', () => {
`);
expect(stringifyNode(body)).toMatchInlineSnapshot(`
"{
{
{
const a = 2;
if (false) {
return a;
Expand Down Expand Up @@ -569,7 +569,7 @@ describe('obfuscate', () => {
expect(stringifyNode(body)).toMatchInlineSnapshot(`
"{
const a = b;
{
{
const b = 1;
return b;
}
Expand Down