Skip to content

Commit

Permalink
feat(unless): add unless tag
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Jul 7, 2018
1 parent 41715b2 commit 60b2c0a
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 0 deletions.
8 changes: 8 additions & 0 deletions fixtures/unless-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 += ' Hello Guest'
out += '\n'
}
return out
})(template, ctx)
3 changes: 3 additions & 0 deletions fixtures/unless-tag/index.edge
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@unless(username)
Hello Guest
@endunless
2 changes: 2 additions & 0 deletions fixtures/unless-tag/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
1 change: 1 addition & 0 deletions fixtures/unless-tag/index.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello Guest
63 changes: 63 additions & 0 deletions src/Tags/Unless.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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 { disAllowExpressions } from '../utils'

export class UnlessTag {
public static block = true
public static seekable = true
public static selfclosed = false
public static tagName = 'unless'

/**
* Expressions which are not allowed by the sequence
* expression
*
* @type {Array}
*/
protected bannedExpressions = ['SequenceExpression']

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

/**
* Start if block
*/
buffer.writeStatement(`if(!${parser.statementToString(parsed)}) {`)

/**
* Indent upcoming code
*/
buffer.indent()

/**
* Process of all kids recursively
*/
token.children.forEach((child, index) => {
parser.processToken(child, buffer)
})

/**
* Remove identation
*/
buffer.dedent()

/**
* Close if block
*/
buffer.writeStatement('}')
}
}
1 change: 1 addition & 0 deletions src/Tags/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ export { ComponentTag as component } from './Component'
export { SlotTag as slot } from './Slot'
export { DebuggerTag as debugger } from './Debugger'
export { SetTag as set } from './Set'
export { UnlessTag as unless } from './Unless'

0 comments on commit 60b2c0a

Please sign in to comment.