-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Add support for ternary conditionals
- Loading branch information
Showing
4 changed files
with
79 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,9 @@ const IN_LITERAL = Symbol('literal'), | |
IN_ARGUMENTS = Symbol('arguments'), | ||
IN_OPTIONS = Symbol('options'), | ||
IN_PATH_EXPRESSION = Symbol('path_expression'), | ||
IN_EACH_EXPRESSION = Symbol('each_expression'); | ||
IN_EACH_EXPRESSION = Symbol('each_expression'), | ||
IN_CONDITIONAL_TRUE = Symbol('conditional_true'), | ||
IN_CONDITIONAL_FALSE = Symbol('conditional_false'); | ||
|
||
/** | ||
* Expressions parser class: | ||
|
@@ -447,7 +449,7 @@ Eparser.setMethod(function getArguments() { | |
* @since 2.0.0 | ||
* @version 2.0.0 | ||
* | ||
* @return {Object} | ||
* @return {Object[]} | ||
*/ | ||
Eparser.setMethod(function getArrayLiteral() { | ||
|
||
|
@@ -507,7 +509,7 @@ Eparser.setMethod(function getArrayLiteral() { | |
* @since 2.0.0 | ||
* @version 2.0.0 | ||
* | ||
* @return {Object} | ||
* @return {Object[]} | ||
*/ | ||
Eparser.setMethod(function getObjectLiteral() { | ||
|
||
|
@@ -573,17 +575,41 @@ Eparser.setMethod(function getExpressionForEach() { | |
return this.getExpression(0, IN_EACH_EXPRESSION); | ||
}); | ||
|
||
/** | ||
* Get the conditional expression | ||
* | ||
* @author Jelle De Loecker <[email protected]> | ||
* @since 2.4.0 | ||
* @version 2.4.0 | ||
* | ||
* @param {Object|Array} condition | ||
* @param {number} level | ||
* | ||
* @return {Object} | ||
*/ | ||
Eparser.setMethod(function getConditionalExpression(condition, level) { | ||
|
||
let truthy = this.getExpression(level, IN_CONDITIONAL_TRUE), | ||
falsy = this.getExpression(level, IN_CONDITIONAL_FALSE); | ||
|
||
return [{ | ||
condition, | ||
truthy, | ||
falsy | ||
}]; | ||
}); | ||
|
||
/** | ||
* Get expression | ||
* | ||
* @author Jelle De Loecker <[email protected]> | ||
* @since 1.2.9 | ||
* @version 2.3.19 | ||
* @version 2.4.0 | ||
* | ||
* @param {Number} level | ||
* @param {Symbol} state Are we in some kind of literal ({}, []) | ||
* | ||
* @return {Object} | ||
* @return {Object[]} | ||
*/ | ||
Eparser.setMethod(function getExpression(level, state) { | ||
|
||
|
@@ -622,6 +648,20 @@ Eparser.setMethod(function getExpression(level, state) { | |
prev = this.previous_token; | ||
entry = result[result.length - 1]; | ||
|
||
// See if this is the start of a conditional ternary expression | ||
if (token.value === '?') { | ||
if (!entry) { | ||
throw new Error('Unexpected `?` token'); | ||
} | ||
|
||
// @TODO: make sure there is a colon somewhere? | ||
|
||
this.goToNext(); | ||
let condition = result.pop(); | ||
push('conditional', this.getConditionalExpression(condition, level + 1)); | ||
continue; | ||
} | ||
|
||
// Get object literals | ||
if (token.value == '{') { | ||
this.goToNext(); | ||
|
@@ -640,6 +680,11 @@ Eparser.setMethod(function getExpression(level, state) { | |
break; | ||
} | ||
|
||
if (state == IN_CONDITIONAL_TRUE && token.value == ':') { | ||
this.goToNext(); | ||
break; | ||
} | ||
|
||
if (token.value == '(') { | ||
|
||
if (prev && !prev.keyword && entry) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters