-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(expression): add support for conditional expression
- Loading branch information
1 parent
8eb86c9
commit 6b6d1d2
Showing
3 changed files
with
163 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
'use strict' | ||
|
||
/* | ||
* edge | ||
* | ||
* (c) Harminder Virk <virk@adonisjs.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
const BaseExpression = require('./BaseExpression') | ||
|
||
/** | ||
* Conditional expression parses the shorthand if | ||
* statement. | ||
* | ||
* @class ConditionalExpression | ||
* @extends {BaseExpression} | ||
* @constructor | ||
* | ||
* @example | ||
* ```js | ||
* username ? username : 'anonymous' | ||
* ``` | ||
* | ||
*/ | ||
class ConditionalExpression extends BaseExpression { | ||
constructor (lexer) { | ||
super(lexer) | ||
this._tokens = { | ||
members: [], | ||
test: null, | ||
consequent: null, | ||
alternate: null, | ||
wrapAlternate: false, | ||
type: 'conditional' | ||
} | ||
} | ||
|
||
/** | ||
* Parses the expression. It is responsibility of the | ||
* consumer to pass the right expression otherwise | ||
* things will blow. | ||
* | ||
* @method parse | ||
* | ||
* @param {Object} expression | ||
* | ||
* @return {void} | ||
*/ | ||
parse (expression) { | ||
this._tokens.test = this._lexer.parse(expression.test) | ||
this._tokens.consequent = this._lexer.parse(expression.consequent) | ||
this._tokens.alternate = this._lexer.parse(expression.alternate) | ||
this._tokens.wrapAlternate = expression.alternate.type === 'ConditionalExpression' | ||
} | ||
|
||
/** | ||
* Returns the formatted string for the alternate part | ||
* of the token | ||
* | ||
* @method testStatement | ||
* | ||
* @return {String} | ||
*/ | ||
testStatement () { | ||
return this._convertToStatement(this._tokens.test, true) | ||
} | ||
|
||
/** | ||
* Returns the formatted string for the alternate part | ||
* of the token | ||
* | ||
* @method consequentStatement | ||
* | ||
* @return {String} | ||
*/ | ||
consequentStatement () { | ||
return this._convertToStatement(this._tokens.consequent, true) | ||
} | ||
|
||
/** | ||
* Returns the formatted string for the alternate part | ||
* of the token | ||
* | ||
* @method alternateStatement | ||
* | ||
* @return {String} | ||
*/ | ||
alternateStatement () { | ||
const alternateStatement = this._convertToStatement(this._tokens.alternate, true) | ||
return this._tokens.wrapAlternate ? `(${alternateStatement})` : alternateStatement | ||
} | ||
|
||
/** | ||
* Converts tokens into parsed statement | ||
* | ||
* @method toStatement | ||
* | ||
* @return {String} | ||
*/ | ||
toStatement () { | ||
return `${this.testStatement()} ? ${this.consequentStatement()} : ${this.alternateStatement()}` | ||
} | ||
} | ||
|
||
module.exports = ConditionalExpression |
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
'use strict' | ||
|
||
/* | ||
* edge | ||
* | ||
* (c) Harminder Virk <virk@adonisjs.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
const test = require('japa') | ||
const esprima = require('esprima') | ||
const Lexer = require('../../src/Lexer') | ||
const ConditionalExpression = require('../../src/Expressions').ConditionalExpression | ||
const expressionsMixin = require('../../test-helpers/expression-mixin') | ||
|
||
test.group('Conditional Expression', (group) => { | ||
group.beforeEach(() => { | ||
this.exp = new ConditionalExpression(new Lexer()) | ||
}) | ||
|
||
expressionsMixin.bind(this)(test) | ||
|
||
test('parse simple if shorthand', (assert) => { | ||
const statement = `username ? username : 'anonymous'` | ||
this.exp.parse(esprima.parse(statement).body[0].expression) | ||
assert.equal(this.exp.tokens.test.type, 'source') | ||
assert.equal(this.exp.tokens.test.value, 'username') | ||
assert.equal(this.exp.tokens.consequent.type, 'source') | ||
assert.equal(this.exp.tokens.consequent.value, 'username') | ||
assert.equal(this.exp.tokens.alternate.type, 'string') | ||
assert.equal(this.exp.tokens.alternate.value, 'anonymous') | ||
}) | ||
|
||
test('convert simple if shorthand', (assert) => { | ||
const statement = `username ? username : 'anonymous'` | ||
this.exp.parse(esprima.parse(statement).body[0].expression) | ||
assert.equal(this.exp.toStatement(), `this.context.resolve('username') ? this.context.resolve('username') : 'anonymous'`) | ||
}) | ||
|
||
test('convert if shorthand with logical operator', (assert) => { | ||
const statement = `username === 'virk' ? username : 'anonymous'` | ||
this.exp.parse(esprima.parse(statement).body[0].expression) | ||
assert.equal(this.exp.toStatement(), `this.context.resolve('username') === 'virk' ? this.context.resolve('username') : 'anonymous'`) | ||
}) | ||
|
||
test('convert if shorthand with nested logical operator', (assert) => { | ||
const statement = `username ? username : (session ? session.username : 'anonymous')` | ||
this.exp.parse(esprima.parse(statement).body[0].expression) | ||
assert.equal(this.exp.toStatement(), `this.context.resolve('username') ? this.context.resolve('username') : (this.context.resolve('session') ? this.context.accessChild(this.context.resolve('session'), ['username']) : 'anonymous')`) | ||
}) | ||
}) |