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
2 changes: 0 additions & 2 deletions packages/tinyest-for-wgsl/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,5 @@ export {
transpileAcornNode,
transpileBabelFn,
transpileBabelNode,
transpileFn,
transpileNode,
} from './parsers.ts';
export type { Externals, TranspilationResult } from './types.ts';
78 changes: 3 additions & 75 deletions packages/tinyest-for-wgsl/src/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@ import type * as acorn from 'acorn';
import * as tinyest from 'tinyest';
import type { Context, JsNode, TranspilationResult, Transpile, Transpilers } from './types.ts';
import { tryFindExternalChain } from './externals.ts';
import {
acornTranspilers,
babelTranspilers,
transpileAcornProperty,
transpileBabelObjectProperty,
} from './transpilers.ts';
import { acornTranspilers, babelTranspilers } from './transpilers.ts';
import { extractFunctionParts } from './functionParts.ts';

const { NodeTypeCatalog: NODE } = tinyest;
Expand All @@ -30,54 +25,9 @@ function createContext(params: tinyest.FuncParameter[]): Context {
};
}

function createLegacyTraspilers() {
return {
...babelTranspilers,
...acornTranspilers,

ObjectExpression(ctx, node, transpile) {
const objectProperties = node.properties.map((prop) => {
if (prop.type === 'SpreadElement') {
throw new Error('Spread elements are not supported in TGSL.');
}

if (prop.type === 'ObjectMethod' || (prop.type === 'Property' && prop.method)) {
throw new Error('Object method elements are not supported in TGSL.');
}

return prop.type === 'Property'
? transpileAcornProperty(ctx, prop, transpile)
: transpileBabelObjectProperty(ctx, prop, transpile);
});

if (objectProperties.some((prop) => /* computed */ prop[2])) {
return [NODE.objectExpr, objectProperties] as tinyest.ObjectExpression;
}

const obj: Record<string, tinyest.Expression> = {};
const seenKeys = new Set<string>();

for (const prop of objectProperties) {
const key = prop[0] as string;
if (seenKeys.has(key)) {
throw new Error(`Duplicate object property key: '${key}'.`);
}
seenKeys.add(key);
obj[key] = /* value */ prop[1];
}

return [NODE.objectExpr, obj] as tinyest.ObjectExpression;
},
} as Transpilers<JsNode>;
}

function createParser(kind: 'acorn' | 'babel' | 'legacy') {
function createParser(kind: 'acorn' | 'babel') {
const transpilers = (
kind === 'acorn'
? acornTranspilers
: kind === 'babel'
? babelTranspilers
: createLegacyTraspilers()
kind === 'acorn' ? acornTranspilers : babelTranspilers
) as Transpilers<JsNode>;

const transpile: Transpile<JsNode> = (ctx, node) => {
Expand Down Expand Up @@ -134,8 +84,6 @@ const parsers = {
babel: createParser('babel'),
};

let legacyParser: ReturnType<typeof createParser> | undefined = undefined;

export function transpileAcornFn(rootNode: acorn.AnyNode): TranspilationResult {
return parsers.acorn.transpileFn(rootNode);
}
Expand All @@ -151,23 +99,3 @@ export function transpileBabelFn(rootNode: babel.Node): TranspilationResult {
export function transpileBabelNode(rootNode: babel.Node): tinyest.AnyNode {
return parsers.babel.transpileNode(rootNode);
}

/**
* @deprecated Use {@link transpileAcornFn} or {@link transpileBabelFn} instead.
*/
export function transpileFn(rootNode: JsNode): TranspilationResult {
if (legacyParser === undefined) {
legacyParser = createParser('legacy');
}
return legacyParser.transpileFn(rootNode);
}

/**
* @deprecated Use {@link transpileAcornNode} or {@link transpileBabelNode} instead.
*/
export function transpileNode(rootNode: JsNode): tinyest.AnyNode {
if (legacyParser === undefined) {
legacyParser = createParser('legacy');
}
return legacyParser.transpileNode(rootNode);
}
81 changes: 39 additions & 42 deletions packages/tinyest-for-wgsl/tests/parsers.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { ClassDeclaration, ClassProperty, Expression } from '@babel/types';
import * as acorn from 'acorn';
import { describe, expect, it } from 'vitest';
import { transpileBabelFn, transpileFn } from 'tinyest-for-wgsl';
import { dualTest, parseBabel, parseRollup } from './helpers.ts';
import { transpileBabelFn } from 'tinyest-for-wgsl';
import { dualTest, parseBabel } from './helpers.ts';

describe('transpileBabelFn and transpileAcornFn', () => {
it(
Expand Down Expand Up @@ -472,65 +472,62 @@ describe('transpileBabelFn and transpileAcornFn', () => {
);
});

describe('legacy transpileFn', () => {
it('parsers object expression with identifier and literal keys', () => {
const code = `() => ({
describe('object expressions', () => {
it(
'parses object expression with identifier and literal keys',
dualTest((p, transpileFn) => {
const code = `() => ({
identifier: 1,
'string key': 2,
3: 4,
5n: 6,
});`;

const babelResult = transpileFn(parseBabel(code));
expect(JSON.stringify(babelResult.body)).toMatchInlineSnapshot(
`"[0,[[10,[104,{"3":[5,"4"],"5":[5,"6"],"identifier":[5,"1"],"string key":[5,"2"]}]]]]"`,
);

const acornResult = transpileFn(parseRollup(code));
expect(JSON.stringify(acornResult.body)).toMatchInlineSnapshot(
`"[0,[[10,[104,{"3":[5,"4"],"5":[5,"6"],"identifier":[5,"1"],"string key":[5,"2"]}]]]]"`,
);
});
expect(JSON.stringify(transpileFn(p(code)).body)).toMatchInlineSnapshot(
`"[0,[[10,[104,{"3":[5,"4"],"5":[5,"6"],"identifier":[5,"1"],"string key":[5,"2"]}]]]]"`,
);
}),
);

it('parses computed object properties', () => {
const code = `() => ({
it(
'parses computed object properties',
dualTest((p, transpileFn) => {
const code = `() => ({
[id]: 1,
[getId()]: 2,
});`;

expect(JSON.stringify(transpileFn(parseBabel(code)).body)).toMatchInlineSnapshot(
`"[0,[[10,[104,[["id",[5,"1"],true],[[6,"getId",[]],[5,"2"],true]]]]]]"`,
);
expect(JSON.stringify(transpileFn(parseRollup(code)).body)).toMatchInlineSnapshot(
`"[0,[[10,[104,[["id",[5,"1"],true],[[6,"getId",[]],[5,"2"],true]]]]]]"`,
);
});
expect(JSON.stringify(transpileFn(p(code)).body)).toMatchInlineSnapshot(
`"[0,[[10,[104,[["id",[5,"1"],true],[[6,"getId",[]],[5,"2"],true]]]]]]"`,
);
}),
);

it('rejects spread elements', () => {
const code = `() => ({
it(
'rejects spread elements',
dualTest((p, transpileFn) => {
const code = `() => ({
...obj,
});`;

expect(() => transpileFn(parseBabel(code))).toThrowErrorMatchingInlineSnapshot(
`[Error: Spread elements are not supported in TGSL.]`,
);
expect(() => transpileFn(parseRollup(code))).toThrowErrorMatchingInlineSnapshot(
`[Error: Spread elements are not supported in TGSL.]`,
);
});
expect(() => transpileFn(p(code))).toThrowErrorMatchingInlineSnapshot(
`[Error: Spread elements are not supported in TGSL.]`,
);
}),
);

it('rejects object methods', () => {
const code = `() => ({
it(
'rejects object methods',
dualTest((p, transpileFn) => {
const code = `() => ({
foo() {
return 1;
},
});`;

expect(() => transpileFn(parseBabel(code))).toThrowErrorMatchingInlineSnapshot(
`[Error: Object method elements are not supported in TGSL.]`,
);
expect(() => transpileFn(parseRollup(code))).toThrowErrorMatchingInlineSnapshot(
`[Error: Object method elements are not supported in TGSL.]`,
);
});
expect(() => transpileFn(p(code))).toThrowErrorMatchingInlineSnapshot(
`[Error: Object method elements are not supported in TGSL.]`,
);
}),
);
});