Skip to content

Commit

Permalink
Merge f016262 into 7da730a
Browse files Browse the repository at this point in the history
  • Loading branch information
ericmorand committed May 11, 2020
2 parents 7da730a + f016262 commit 129e23a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/lib/Transpiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
TwingNodeExpressionName,
TwingNodeFor,
TwingNodeIf,
TwingNodePrint,
TwingNodePrint, TwingNodeSet,
TwingNodeText,
TwingNodeType,
TwingSource,
Expand Down Expand Up @@ -196,6 +196,19 @@ export class Transpiler {
return `${node.getAttribute('name')}(${parameters.join(',')})`;
}

private transpileSetNode(node: TwingNodeSet): string {
let results: Array<string> = [];

const names = node.getNode('names');
const values = node.getNode('values');

for (let [k, v] of names.getNodes()) {
results.push(`<?php $${v.getAttribute('name')} = ${this.transpileNode(values.getNode(k))} ?>`);
}

return results.join('\n');
}

private transpileNode(node: TwingNode, raw: boolean = false): string {
if (node.getType() === TwingNodeType.PRINT) {
return this.transpilePrintNode(node as TwingNodePrint);
Expand Down Expand Up @@ -245,6 +258,10 @@ export class Transpiler {
return this.transpileExpressionFunctionNode(node as TwingNodeExpressionFunction);
}

if (node.getType() === TwingNodeType.SET) {
return this.transpileSetNode(node as TwingNodeSet);
}

let results = [];

for (let [name, child] of node.getNodes()) {
Expand Down
10 changes: 10 additions & 0 deletions test/lib/Transpiler/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ bar*/ ?>`);
test.end();
});

test.test('supports set', (test) => {
test.same(transpiler.transpile('{% set foo = "bar" %}'), '<?php $foo = "bar" ?>');
test.same(transpiler.transpile('{% set foo, bar = "foo", "bar" %}'), `<?php $foo = "foo" ?>
<?php $bar = "bar" ?>`);
test.same(transpiler.transpile('{% set foo = lorem.ipsum %}'), '<?php $foo = $lorem->ipsum ?>');
test.same(transpiler.transpile('{% set foo = lorem.ipsum() %}'), '<?php $foo = $lorem->ipsum() ?>');

test.end();
});

test.end();
});

Expand Down

0 comments on commit 129e23a

Please sign in to comment.