Skip to content

Commit 561fcfa

Browse files
committed
Remove dollar prefixing when ignoreDollarVariable is set to true
1 parent 842bec3 commit 561fcfa

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/JsPhpize/Compiler/Compiler.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,11 @@ protected function visitVariable(Variable $variable, $indent)
335335
if ($variable->scope) {
336336
$name = '__let_' . spl_object_hash($variable->scope) . $name;
337337
}
338+
if (!$this->engine->getOption('ignoreDollarVariable') || mb_substr($name, 0, 1) !== '$') {
339+
$name = '$' . $name;
340+
}
338341

339-
return $this->handleVariableChildren($variable, $indent, '$' . $name);
342+
return $this->handleVariableChildren($variable, $indent, $name);
340343
}
341344

342345
public function compile(Block $block, $indent = '')

tests/render.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,16 @@ public function __get($name)
122122
$result = $jsPhpize->renderCode('return obj.arrayData.foo', $data);
123123
$this->assertSame('a-bar', (string) $result);
124124
}
125+
126+
public function testDollarVariablePrefix()
127+
{
128+
$jsPhpize = new JsPhpize([
129+
'ignoreDollarVariable' => true,
130+
]);
131+
$code = 'return isset($variable) && $variable !== false';
132+
133+
$this->assertFalse($jsPhpize->renderCode($code));
134+
$this->assertTrue($jsPhpize->renderCode($code, ['variable' => 1]));
135+
$this->assertFalse($jsPhpize->renderCode($code, ['variable' => false]));
136+
}
125137
}

0 commit comments

Comments
 (0)