diff --git a/fixtures/unless-tag/compiled.js b/fixtures/unless-tag/compiled.js new file mode 100644 index 0000000..88a0a2e --- /dev/null +++ b/fixtures/unless-tag/compiled.js @@ -0,0 +1,8 @@ +(function (template, ctx) { + let out = '' + if(!ctx.resolve('username')) { + out += ' Hello Guest' + out += '\n' + } + return out +})(template, ctx) \ No newline at end of file diff --git a/fixtures/unless-tag/index.edge b/fixtures/unless-tag/index.edge new file mode 100644 index 0000000..eee7087 --- /dev/null +++ b/fixtures/unless-tag/index.edge @@ -0,0 +1,3 @@ +@unless(username) + Hello Guest +@endunless \ No newline at end of file diff --git a/fixtures/unless-tag/index.json b/fixtures/unless-tag/index.json new file mode 100644 index 0000000..7a73a41 --- /dev/null +++ b/fixtures/unless-tag/index.json @@ -0,0 +1,2 @@ +{ +} \ No newline at end of file diff --git a/fixtures/unless-tag/index.txt b/fixtures/unless-tag/index.txt new file mode 100644 index 0000000..b4ca873 --- /dev/null +++ b/fixtures/unless-tag/index.txt @@ -0,0 +1 @@ +Hello Guest \ No newline at end of file diff --git a/src/Tags/Unless.ts b/src/Tags/Unless.ts new file mode 100644 index 0000000..3ee5fbf --- /dev/null +++ b/src/Tags/Unless.ts @@ -0,0 +1,63 @@ +/* +* edge +* +* (c) Harminder Virk +* +* 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('}') + } +} diff --git a/src/Tags/index.ts b/src/Tags/index.ts index 6830810..24391d3 100644 --- a/src/Tags/index.ts +++ b/src/Tags/index.ts @@ -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'