Skip to content

Commit

Permalink
feat(yield): add yield tag
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Jul 9, 2018
1 parent 54a5ff1 commit cc4dc87
Show file tree
Hide file tree
Showing 14 changed files with 100 additions and 0 deletions.
10 changes: 10 additions & 0 deletions fixtures/yield-fallback/compiled.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
(function (template, ctx) {
let out = ''
if(ctx.resolve('username')) {
out += ctx.resolve('username')
} else {
out += ' Hello guest'
out += '\n'
}
return out
})(template, ctx)
3 changes: 3 additions & 0 deletions fixtures/yield-fallback/index.edge
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@yield(username)
Hello guest
@endyield
1 change: 1 addition & 0 deletions fixtures/yield-fallback/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
1 change: 1 addition & 0 deletions fixtures/yield-fallback/index.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello guest
8 changes: 8 additions & 0 deletions fixtures/yield-html/compiled.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(function (template, ctx) {
let out = ''
if(ctx.resolve('greeting')) {
out += ctx.resolve('greeting')
} else {
}
return out
})(template, ctx)
1 change: 1 addition & 0 deletions fixtures/yield-html/index.edge
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@!yield(greeting)
3 changes: 3 additions & 0 deletions fixtures/yield-html/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"greeting": "Hello <strong>virk</strong>"
}
1 change: 1 addition & 0 deletions fixtures/yield-html/index.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello <strong>virk</strong>
8 changes: 8 additions & 0 deletions fixtures/yield-tag/compiled.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(function (template, ctx) {
let out = ''
if(ctx.resolve('username')) {
out += ctx.resolve('username')
} else {
}
return out
})(template, ctx)
1 change: 1 addition & 0 deletions fixtures/yield-tag/index.edge
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@!yield(username)
3 changes: 3 additions & 0 deletions fixtures/yield-tag/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"username": "virk"
}
1 change: 1 addition & 0 deletions fixtures/yield-tag/index.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
virk
58 changes: 58 additions & 0 deletions src/Tags/Yield.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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.
*/

import { Parser } from 'edge-parser'
import { EdgeBuffer } from 'edge-parser/build/src/EdgeBuffer'
import { IBlockNode } from 'edge-lexer/build/src/Contracts'
import { allowExpressions } from '../utils'

export class YieldTag {
public static block = true
public static seekable = true
public static selfclosed = true
public static tagName = 'if'

/**
* Expressions which are not allowed by the sequence
* expression
*
* @type {Array}
*/
private static allowedExpressions = ['Identifier', 'Literal', 'MemberExpression', 'CallExpression']

/**
* Compiles the if block node to a Javascript if statement
*/
public static compile (parser: Parser, buffer: EdgeBuffer, token: IBlockNode) {
const parsed = parser.parseJsArg(token.properties.jsArg, token.lineno)
allowExpressions('yield', parsed, this.allowedExpressions)

/**
* Start if block
*/
const parsedString = parser.statementToString(parsed)

/**
* If main content is truthy
*/
buffer.writeStatement(`if(${parsedString}) {`)
buffer.indent()
buffer.writeLine(parsedString)
buffer.dedent()
buffer.writeStatement('} else {')

/**
* Else write fallback
*/
buffer.indent()
token.children.forEach((child, index) => (parser.processToken(child, buffer)))
buffer.dedent()
buffer.writeStatement('}')
}
}
1 change: 1 addition & 0 deletions src/Tags/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ export { SlotTag as slot } from './Slot'
export { DebuggerTag as debugger } from './Debugger'
export { SetTag as set } from './Set'
export { UnlessTag as unless } from './Unless'
export { YieldTag as yield } from './Yield'

0 comments on commit cc4dc87

Please sign in to comment.