Skip to content

Commit

Permalink
πŸ› Fix each expression not supporting function calls
Browse files Browse the repository at this point in the history
  • Loading branch information
skerit committed Mar 12, 2024
1 parent 480a8e6 commit 5894ba1
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 29 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

* Fix setting `value` property on a `<select>` item not changing the selected option
* Make the `each` expression handle `Map`-like instances correctly
* Fix `each` expression not supporting function calls

## 2.3.18 (2024-02-25)

Expand Down
16 changes: 8 additions & 8 deletions lib/expression/each.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Each.setHasArguments(true);
*
* @author Jelle De Loecker <[email protected]>
* @since 2.0.0
* @version 2.0.0
* @version 2.3.19
*
* @param {Object} options
*
Expand All @@ -30,10 +30,10 @@ Each.setStatic(function parseArguments(options) {
}

let result = {
variable : tokens.getVariable(1),
as_key : null,
as : null,
where : null,
expression : tokens.getExpressionForEach(),
as_key : null,
as : null,
where : null,
};

// Go to the AS keyword
Expand Down Expand Up @@ -63,7 +63,7 @@ Each.setStatic(function parseArguments(options) {
*
* @author Jelle De Loecker <[email protected]>
* @since 1.2.9
* @version 2.2.17
* @version 2.3.19
*
* @param {String} name
* @param {Array} pieces
Expand All @@ -80,9 +80,9 @@ Each.setMethod(function execute() {

let options = this.options[0];

if (options && options.variable) {
if (options && options.expression) {

variable = this.getValueByPath(options.variable);
variable = this.parseExpression(options.expression, this.vars);

if (!variable) {
return this;
Expand Down
24 changes: 6 additions & 18 deletions lib/expression/with.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ With.setHasArguments(true);
*
* @author Jelle De Loecker <[email protected]>
* @since 1.2.9
* @version 2.0.0
* @version 2.3.19
*
* @param {Object} options
*
Expand All @@ -29,9 +29,9 @@ With.setStatic(function parseArguments(options) {
let tokens = options.tokens;

let result = {
variable : tokens.getVariable(),
as : null,
where : null,
expression : tokens.getExpressionForEach(),
as : null,
where : null,
};

// Go to the AS keyword
Expand Down Expand Up @@ -69,24 +69,12 @@ With.setStatic(function parseArguments(options) {
*/
With.setMethod(function execute() {

let variable,
vars,
let vars,
i;

let options = this.options[0];

// Get the possible paths
let paths = options.variable;

// Get the actual variable to work with
for (i = 0; i < paths.length; i++) {
// Get the variable
variable = this.getValueByPath(paths[i]);

if (variable) {
break;
}
}
let variable = this.parseExpression(options.expression, this.vars);

let {keys, values, length} = Hawkejs.splitIntoKeysAndValues(variable);

Expand Down
26 changes: 24 additions & 2 deletions lib/parser/expressions_parser.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const IN_LITERAL = Symbol('literal'),
IN_ARGUMENTS = Symbol('arguments'),
IN_OPTIONS = Symbol('options'),
IN_PATH_EXPRESSION = Symbol('path_expression');
IN_PATH_EXPRESSION = Symbol('path_expression'),
IN_EACH_EXPRESSION = Symbol('each_expression');

/**
* Expressions parser class:
Expand Down Expand Up @@ -520,12 +521,25 @@ Eparser.setMethod(function getObjectLiteral() {
return result;
});

/**
* Get an expression used in an each statement
*
* @author Jelle De Loecker <[email protected]>
* @since 2.3.19
* @version 2.3.19
*
* @return {Array}
*/
Eparser.setMethod(function getExpressionForEach() {
return this.getExpression(0, IN_EACH_EXPRESSION);
});

/**
* Get expression
*
* @author Jelle De Loecker <[email protected]>
* @since 1.2.9
* @version 2.3.18
* @version 2.3.19
*
* @param {Number} level
* @param {Symbol} state Are we in some kind of literal ({}, [])
Expand Down Expand Up @@ -669,6 +683,14 @@ Eparser.setMethod(function getExpression(level, state) {

let lower_value = token.value.toLowerCase();

if (state === IN_EACH_EXPRESSION) {
// If we only need to get the first part of an `each` expression,
// ignore the 'as' or 'where' parts
if (lower_value == 'as' || lower_value == 'where') {
break;
}
}

switch (lower_value) {
case 'where':
case 'neq':
Expand Down
10 changes: 9 additions & 1 deletion test/10-expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,10 @@ describe('Expressions', function() {
describe('With', function() {

var tests = [
[
`{% with str_bla.split('') as c %}{% each %}{{c}}-{% /each %}{% /with %}`,
'b-l-a-',
],
[
`{% with numbers as number %}{% multiple %}{% each %}{%= number %},{% /each %}{% /multiple %}{% /with %}`,
'0,1,2,3,'
Expand Down Expand Up @@ -409,6 +413,10 @@ NO
describe('Each ... as', function() {

var tests = [
[
`{% each str_bla.split('') as c %}{{c}}-{% /each %}`,
'b-l-a-',
],
[
`{% each people as key, person %}{%= person.name %},{% /each %}`,
`Jelle,Roel,Griet,Patrick,Voltorb,`
Expand Down Expand Up @@ -635,7 +643,7 @@ NO
var tests = [
[
`{% block "test" %}TESTING{% /block %}<he-block data-he-name="test"></he-block>`,
`<he-block data-he-name="test" data-hid="hserverside-0" data-he-template="test_170">TESTING</he-block>`
`<he-block data-he-name="test" data-hid="hserverside-0" data-he-template="test_172">TESTING</he-block>`
],
[
`€{% if true %}€<span>€</span>{% /if %}`,
Expand Down

0 comments on commit 5894ba1

Please sign in to comment.