Skip to content

Commit

Permalink
refactor(tags): make tags all static
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Jul 9, 2018
1 parent 60b2c0a commit f074718
Show file tree
Hide file tree
Showing 17 changed files with 73 additions and 28 deletions.
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@
]
},
"dependencies": {
"deep-extend": "^0.6.0",
"edge-parser": "^1.0.11",
"edge-parser": "^1.0.12",
"he": "^1.1.1",
"lodash": "^4.17.10",
"macroable": "^1.0.0",
"node-exceptions": "^3.0.0",
"require-uncached": "^1.0.3"
Expand Down
9 changes: 8 additions & 1 deletion src/Edge/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,17 @@ import { Compiler } from '../Compiler'
import { Loader } from '../Loader'
import { ILoaderConstructor, ILoader, ITag } from '../Contracts'
import { Template } from '../Template'
import { Context } from '../Context'

let loader: null | ILoader = null
let compiler: null | Compiler = null

Object.keys(Tags).forEach((tag) => {
if (typeof (Tags[tag].run) === 'function') {
Tags[tag].run(Context)
}
})

type configOptions = {
Loader?: ILoaderConstructor,
cache?: boolean,
Expand Down Expand Up @@ -46,7 +53,7 @@ export class Edge {
*/
public static configure (options: configOptions) {
loader = new (options.Loader || Loader)()
compiler = new Compiler(loader!, Tags, options.cache || true)
compiler = new Compiler(loader!, Tags, !!options.cache)
}

public static mount (diskName: string, dirPath: string): void
Expand Down
9 changes: 8 additions & 1 deletion src/Tags/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@ export class ComponentTag {
/**
* Compiles else block node to Javascript else statement
*/
public compile (parser: Parser, buffer: EdgeBuffer, token: IBlockNode) {
public static compile (parser: Parser, buffer: EdgeBuffer, token: IBlockNode) {
const parsed = parser.generateAst(token.properties.jsArg, token.lineno)
const expression = parser.parseStatement(parsed.body[0])
let [name, props] = parseSequenceExpression(expression, parser)

const slots = {}

/**
* Loop over all the children and set them as part of slots. If no slot
* is defined, then it will be main slot.
*/
token.children.forEach((child, index) => {
let slotName: string = `'main'`
let slotProps: string | null = null
Expand All @@ -39,6 +43,9 @@ export class ComponentTag {
slotProps = parsed[1]
}

/**
* Create a new slot with buffer to process the childs
*/
if (!slots[slotName]) {
slots[slotName] = { buffer: new EdgeBuffer(`slot_${index}`), props: slotProps }

Expand Down
2 changes: 1 addition & 1 deletion src/Tags/Debugger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class DebuggerTag {
/**
* Compiles else block node to Javascript else statement
*/
public compile (parser: Parser, buffer: EdgeBuffer, token: IBlockNode) {
public static compile (parser: Parser, buffer: EdgeBuffer, token: IBlockNode) {
buffer.writeStatement('debugger;')
}
}
16 changes: 11 additions & 5 deletions src/Tags/Each.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@
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'
import { each, size as lodashSize } from 'lodash'
import { allowExpressions } from '../utils'

export class EachTag {
public static block = true
public static seekable = true
public static selfclosed = true
public static tagName = 'each'

private allowedExpressions = ['BinaryExpression']
private static allowedExpressions = ['BinaryExpression']

/**
* Returns the value and key names for the foreach loop
*/
private _getLoopKeyValue (expression: any): [string, string] {
private static _getLoopKeyValue (expression: any): [string, string] {
allowExpressions('each', expression, ['SequenceExpression', 'Identifier'])

if (expression.type === 'SequenceExpression') {
Expand All @@ -34,14 +34,17 @@ export class EachTag {
return [expression.name, 'key']
}

private _getLoopSource (expression: any, parser: Parser): string {
/**
* Returns the source on which we should execute the loop
*/
private static _getLoopSource (expression: any, parser: Parser): string {
return parser.statementToString(parser.parseStatement(expression))
}

/**
* Compiles else block node to Javascript else statement
*/
public compile (parser: Parser, buffer: EdgeBuffer, token: IBlockNode) {
public static compile (parser: Parser, buffer: EdgeBuffer, token: IBlockNode) {
const ast = parser.generateAst(token.properties.jsArg, token.lineno)
const expression = ast.body[0].expression

Expand Down Expand Up @@ -123,6 +126,9 @@ export class EachTag {
}
}

/**
* Add methods to the runtime context for running the loop
*/
public static run (Context) {
Context.macro('loop', function loop (source, callback) {
let index = 0
Expand Down
2 changes: 1 addition & 1 deletion src/Tags/Else.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class ElseTag {
/**
* Compiles else block node to Javascript else statement
*/
public compile (parser: Parser, buffer: EdgeBuffer, token: IBlockNode) {
public static compile (parser: Parser, buffer: EdgeBuffer, token: IBlockNode) {
buffer.dedent()
buffer.writeStatement(`} else {`)
buffer.indent()
Expand Down
4 changes: 2 additions & 2 deletions src/Tags/ElseIf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ export class ElseIfTag {
public static selfclosed = false
public static tagName = 'elseif'

protected bannedExpressions = ['SequenceExpression']
private static bannedExpressions = ['SequenceExpression']

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

Expand Down
4 changes: 2 additions & 2 deletions src/Tags/If.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ export class IfTag {
*
* @type {Array}
*/
protected bannedExpressions = ['SequenceExpression']
private static bannedExpressions = ['SequenceExpression']

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

Expand Down
4 changes: 2 additions & 2 deletions src/Tags/Include.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ export class IncludeTag {
*
* @type {Array}
*/
protected bannedExpressions = ['SequenceExpression']
private static bannedExpressions = ['SequenceExpression']

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

Expand Down
2 changes: 1 addition & 1 deletion src/Tags/Set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class SetTag {
/**
* Compiles else block node to Javascript else statement
*/
public compile (parser: Parser, buffer: EdgeBuffer, token: IBlockNode) {
public static compile (parser: Parser, buffer: EdgeBuffer, token: IBlockNode) {
const ast = parser.parseJsArg(token.properties.jsArg, token.lineno)
const [key, value] = parseAsKeyValuePair(ast, parser, [])
buffer.writeStatement(`ctx.set(${key}, ${value})`)
Expand Down
2 changes: 1 addition & 1 deletion src/Tags/Slot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class SlotTag {
/**
* Compiles else block node to Javascript else statement
*/
public compile (parser: Parser, buffer: EdgeBuffer, token: IBlockNode) {
public static compile (parser: Parser, buffer: EdgeBuffer, token: IBlockNode) {
token.children.forEach((child, index) => {
parser.processToken(child, buffer)
})
Expand Down
4 changes: 2 additions & 2 deletions src/Tags/Unless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ export class UnlessTag {
*
* @type {Array}
*/
protected bannedExpressions = ['SequenceExpression']
private static bannedExpressions = ['SequenceExpression']

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

Expand Down
6 changes: 6 additions & 0 deletions test/compiler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,21 @@ import { EOL } from 'os'
import { join } from 'path'
import { Loader } from '../src/Loader'
import { Compiler } from '../src/Compiler'
import { Parser } from 'edge-parser'
import { EdgeBuffer } from 'edge-parser/build/src/EdgeBuffer'
import { IBlockNode } from 'edge-lexer/build/src/Contracts'

const tags = {
if: class If {
public static block = true
public static seekable = true
public static selfclosed = false
public static tagName = 'if'
public static compile (parser: Parser, buffer: EdgeBuffer, token: IBlockNode): void {
}
},
}

const viewsDir = join(__dirname, 'views')

test.group('Compiler', (group) => {
Expand Down
7 changes: 7 additions & 0 deletions test/edge.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import { Edge } from '../src/Edge'
import { Loader } from '../src/Loader'
import { Compiler } from '../src/Compiler'

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

const viewsDir = join(__dirname, 'views')

test.group('Template', (group) => {
Expand Down Expand Up @@ -57,7 +61,10 @@ test.group('Template', (group) => {
public static block = true
public static seekable = true
public static selfclosed = true
public static compile (parser: Parser, buffer: EdgeBuffer, token: IBlockNode): void {
}
}

Edge.tag(MyTag)

Edge.configure({})
Expand Down
5 changes: 5 additions & 0 deletions test/sequence-expression.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@
import * as test from 'japa'
import { parseSequenceExpression } from '../src/utils'
import { Parser } from 'edge-parser'
import { EdgeBuffer } from 'edge-parser/build/src/EdgeBuffer'
import { IBlockNode } from 'edge-lexer/build/src/Contracts'

const tags = {
if: class If {
public static block = true
public static seekable = true
public static selfclosed = false
public static tagName = 'if'
public static compile (parser: Parser, buffer: EdgeBuffer, token: IBlockNode): void {
}
},
}

Expand Down
9 changes: 8 additions & 1 deletion test/template.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,23 @@ import { Template } from '../src/Template'
import { Compiler } from '../src/Compiler'
import { Loader } from '../src/Loader'

const viewsDir = join(__dirname, 'views')
import { Parser } from 'edge-parser'
import { EdgeBuffer } from 'edge-parser/build/src/EdgeBuffer'
import { IBlockNode } from 'edge-lexer/build/src/Contracts'

const tags = {
if: class If {
public static block = true
public static seekable = true
public static selfclosed = false
public static tagName = 'if'
public static compile (parser: Parser, buffer: EdgeBuffer, token: IBlockNode): void {
}
},
}

const viewsDir = join(__dirname, 'views')

const loader = new Loader()
loader.mount('default', viewsDir)
const compiler = new Compiler(loader, tags, false)
Expand Down

0 comments on commit f074718

Please sign in to comment.