mirror of
https://github.com/hi-language/transpiler.git
synced 2026-01-14 00:28:05 +00:00
Feat: Support ObjectLiteral; keep IIFE for blocks
This commit is contained in:
@@ -11,7 +11,8 @@ export function generate(ast) {
|
||||
ExpressionStatement: (node) => `${generate(node.expression)};`,
|
||||
|
||||
VariableDeclaration: (node) => {
|
||||
const keyword = node.value.type.includes('Function') ? 'const' : 'let';
|
||||
const t = node.value.type;
|
||||
const keyword = (t === 'FunctionExpression' || t === 'ArrowFunctionExpression') ? 'const' : 'let';
|
||||
return `${keyword} ${node.identifier} = ${generate(node.value)};`;
|
||||
},
|
||||
AssignmentExpression: (node) => `${generate(node.left)} = ${generate(node.right)}`,
|
||||
@@ -57,13 +58,17 @@ export function generate(ast) {
|
||||
ArrowFunctionExpression: (node) => {
|
||||
const params = node.params.map(generate).join(', ');
|
||||
const body = generate(node.body);
|
||||
// If body is not a block, it's an implicit return
|
||||
const bodyStr = node.body.type === 'Block' || node.body.type === 'BlockStatement' ? body : `(${body})`;
|
||||
return `(${params}) => ${bodyStr}`;
|
||||
},
|
||||
|
||||
ArrayLiteral: (node) => `[${node.elements.map(generate).join(', ')}]`,
|
||||
|
||||
ObjectLiteral: (node) => {
|
||||
const props = node.properties.map(p => `${p.key}: ${generate(p.value)}`).join(', ');
|
||||
return `{ ${props} }`;
|
||||
},
|
||||
|
||||
Identifier: (node) => node.name,
|
||||
ThisExpression: () => 'this',
|
||||
NumericLiteral: (node) => node.value,
|
||||
@@ -77,3 +82,4 @@ export function generate(ast) {
|
||||
}
|
||||
return generators[ast.type](ast);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user