Skip to content

Commit

Permalink
refactor: finalize if and else tags
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Mar 26, 2020
1 parent ad4ee7e commit 6879174
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 26 deletions.
24 changes: 9 additions & 15 deletions src/Tags/ElseIf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,12 @@

import { expressions } from 'edge-parser'
import { TagContract } from '../Contracts'
import { isNotSubsetOf, unallowedExpression } from '../utils'
import { isNotSubsetOf, unallowedExpression, parseJsArg } from '../utils'

/**
* Else if tag is used to define conditional blocks.
*
* ```edge
* @if(username)
* // If
* @elseif(user.username)
* // Else if
* @endif
* ```
* Else if tag is used to define conditional blocks. We keep `@elseif` tag
* is a inline tag, so that everything between the `if` and the `elseif`
* comes `if` children.
*/
export const elseIfTag: TagContract = {
block: false,
Expand All @@ -31,11 +25,11 @@ export const elseIfTag: TagContract = {
* Compiles the else if block node to a Javascript if statement
*/
compile (parser, buffer, token) {
const parsed = parser.utils.transformAst(
parser.utils.generateAST(token.properties.jsArg, token.loc, token.filename),
token.filename,
)
const parsed = parseJsArg(parser, token)

/**
* Disallow sequence expressions
*/
isNotSubsetOf(
parsed,
[expressions.SequenceExpression],
Expand All @@ -49,7 +43,7 @@ export const elseIfTag: TagContract = {
)

/**
* Start else block
* Start else if block
*/
buffer.writeStatement(`} else if (${parser.utils.stringify(parsed)}) {`, token.filename, token.loc.start.line)
},
Expand Down
17 changes: 6 additions & 11 deletions src/Tags/If.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,10 @@
import { expressions } from 'edge-parser'

import { TagContract } from '../Contracts'
import { unallowedExpression, isNotSubsetOf } from '../utils'
import { unallowedExpression, isNotSubsetOf, parseJsArg } from '../utils'

/**
* If tag is used to define conditional blocks.
*
* ```edge
* @if(username)
* @endif
* ```
*/
export const ifTag: TagContract = {
block: true,
Expand All @@ -29,11 +24,11 @@ export const ifTag: TagContract = {
* Compiles the if block node to a Javascript if statement
*/
compile (parser, buffer, token) {
const parsed = parser.utils.transformAst(
parser.utils.generateAST(token.properties.jsArg, token.loc, token.filename),
token.filename,
)
const parsed = parseJsArg(parser, token)

/**
* Disallow sequence expressions
*/
isNotSubsetOf(
parsed,
[expressions.SequenceExpression],
Expand All @@ -52,7 +47,7 @@ export const ifTag: TagContract = {
buffer.writeStatement(`if (${parser.utils.stringify(parsed)}) {`, token.filename, token.loc.start.line)

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

Expand Down

0 comments on commit 6879174

Please sign in to comment.