Skip to content

Commit

Permalink
Fix #1
Browse files Browse the repository at this point in the history
  • Loading branch information
luttje committed Mar 11, 2024
1 parent 226b645 commit aff2f00
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/strategies/base-transformation-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ addDefaultTransform('meta', regexForMeta, (transformer, metaString) => {
// }
// NOTE: For simplicty sake the JSON cannot contain %}
export const regexForRender = /{%\s*render\s*((?:'[^']+?)'|"(?:[^']+?)"){1}(?:\s*,\s*((?:[^%]+?|%(?!}))*))*?\s*%}/g;
export const regexForVariableString = /(\w+):\s*((?:"(?:[^"\\]|\\.)*?"|'(?:[^'\\]|\\.)*?'))/g;
export const regexForVariableString = /(\w+):\s*((?:"(?:[^"\\]|\\.)*?"|'(?:[^'\\]|\\.)*?'|\d+\.\d+|\d+|true|false|null))/g;
addDefaultTransform('render', regexForRender, (transformer, component, variablesString) => {
const variables = {};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
<span>Welcome</span>
{/tmpl_if}

{tmpl_if name="___" op="!=" value="show-it"}
Override
{/tmpl_if}

{tmpl_if name="___" op="==" value="dont-show-it"}
<span>Online</span>
{/tmpl_if}
Expand Down
7 changes: 6 additions & 1 deletion tests/fixtures/render-attributes-defaults.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"age": 25,
"isOnline": false
}
}
},
"overrideDefault": false
}
} %}
<button class="px-4 py-2 self-start text-sm whitespace-nowrap font-medium text-white bg-[#c70f19] border-bottom-[#980b13] hover:bg-[#df111c] hover:border-bottom-[#800a10] rounded-md shadow uppercase"
Expand All @@ -20,6 +21,10 @@
<span>Welcome</span>
{% endif %}

{% if overrideDefault %}
Override
{%endif%}

{% if complex.human.isOnline %}
<span>Online</span>
{% endif %}
Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/render-attributes-defaults.php.expected.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
<span>Welcome</span>
<?php endif; ?>

<?php if (true) : ?>
Override
<?php endif; ?>

<?php if (false) : ?>
<span>Online</span>
<?php endif; ?>
Expand Down
2 changes: 1 addition & 1 deletion tests/ispconfig-transformation-strategy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ describe('ISPConfig Transformation Strategy', () => {
});

it('should transform render statements using default parameters in metadata', () => {
const transformed = getISPConfigTransform(`{% render './render-attributes-defaults.liquid' %}`, resolve(fixturesPath, 'render-attributes.liquid'));
const transformed = getISPConfigTransform(`{% render './render-attributes-defaults.liquid', overrideDefault: true %}`, resolve(fixturesPath, 'render-attributes.liquid'));
const expected = readFixtureFile('render-attributes-defaults.ispconfig.expected.htm');

expect(transformed).toBe(expected);
Expand Down
2 changes: 1 addition & 1 deletion tests/php-transformation-strategy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ describe('PHP Transformation Strategy', () => {
});

it('should transform render statements using default parameters in metadata', () => {
const transformed = getPHPConfigTransform(`{% render './render-attributes-defaults.liquid' %}`, resolve(fixturesPath, 'render-attributes.liquid'));
const transformed = getPHPConfigTransform(`{% render './render-attributes-defaults.liquid', overrideDefault: true %}`, resolve(fixturesPath, 'render-attributes.liquid'));
const expected = readFixtureFile('render-attributes-defaults.php.expected.php');

expect(transformed).toBe(expected);
Expand Down
14 changes: 14 additions & 0 deletions tests/regexp.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,3 +459,17 @@ describe('RegExp Tests for loop-for', () => {
}
});
});

describe('RegExp Tests for variable strings', () => {
it('variable string regex should match variable strings', () => {
assertMatch(regexForVariableString, 'key: \'string\'');
assertMatch(regexForVariableString, 'anotherKey: "string"');
assertMatch(regexForVariableString, 'ints: 42');
assertMatch(regexForVariableString, 'floats: 3.14');
assertMatch(regexForVariableString, 'bools: true');
assertMatch(regexForVariableString, 'bools: false');
assertMatch(regexForVariableString, 'nulls: null');
assertMatch(regexForVariableString, 'nulls: null, key: \'string\'');
assertMatch(regexForVariableString, 'key: \'string\', anotherKey: "string", ints: 42, floats: 3.14, bools: true, false, nulls: null');
});
});

0 comments on commit aff2f00

Please sign in to comment.