Skip to content

Commit

Permalink
refactor: improve edge api and cleanup utils
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Jul 23, 2019
1 parent c2382bc commit c2ffb90
Show file tree
Hide file tree
Showing 13 changed files with 394 additions and 284 deletions.
20 changes: 10 additions & 10 deletions src/Compiler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import { EdgeError } from 'edge-error'
import { Token, TagToken } from 'edge-lexer'

import { CacheManager } from '../CacheManager'
import { isBlock, getLineAndColumn } from '../utils'
import { LoaderContract, TagsContract, LoaderTemplate } from '../Contracts'
import { isBlockToken, getLineAndColumnForToken } from '../utils'
import { LoaderContract, TagsContract, LoaderTemplate, CompilerContract } from '../Contracts'

/**
* Compiler compiles the template to a function, which can be invoked at a later
Expand All @@ -27,7 +27,7 @@ import { LoaderContract, TagsContract, LoaderTemplate } from '../Contracts'
* Also, the `layouts` are handled natively by the compiler. Before starting
* the parsing process, it will merge the layout sections.
*/
export class Compiler {
export class Compiler implements CompilerContract {
private _cacheManager = new CacheManager(this._cache)

constructor (
Expand Down Expand Up @@ -58,7 +58,7 @@ export class Compiler {
* template
*/
if (
isBlock(node, 'layout') ||
isBlockToken(node, 'layout') ||
node.type === 'newline' ||
(node.type === 'raw' && !node.value.trim())
) {
Expand All @@ -68,23 +68,23 @@ export class Compiler {
/**
* Collect parent template sections
*/
if (isBlock(node, 'section')) {
if (isBlockToken(node, 'section')) {
extendedSections[node.properties.jsArg.trim()] = node
return
}

/**
* Collect set calls inside parent templates
*/
if (isBlock(node, 'set')) {
if (isBlockToken(node, 'set')) {
extendedSetCalls.push(node)
return
}

/**
* Everything else if not allowed as top level nodes
*/
const [line, col] = getLineAndColumn(node)
const [line, col] = getLineAndColumnForToken(node)
throw new EdgeError(
`Template extending the layout can only define @sections as top level nodes`,
'E_UNALLOWED_EXPRESSION',
Expand All @@ -97,7 +97,7 @@ export class Compiler {
*/
const finalNodes = base
.map((node) => {
if (!isBlock(node, 'section')) {
if (!isBlockToken(node, 'section')) {
return node
}

Expand All @@ -109,7 +109,7 @@ export class Compiler {
/**
* Concat children when super was called
*/
if (extendedNode.children.length && isBlock(extendedNode.children[0], 'super')) {
if (extendedNode.children.length && isBlockToken(extendedNode.children[0], 'super')) {
extendedNode.children = node.children.concat(extendedNode.children)
}

Expand All @@ -136,7 +136,7 @@ export class Compiler {
* The `layout` is inbuilt feature from core, where we merge the layout
* and parent template sections together
*/
if (isBlock(firstToken, 'layout')) {
if (isBlockToken(firstToken, 'layout')) {
const layoutTokens = this.generateTokens(firstToken.properties.jsArg.replace(/'/g, ''))
templateTokens = this._mergeSections(layoutTokens, templateTokens, parser.options.filename)
}
Expand Down
16 changes: 8 additions & 8 deletions src/Context/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class Context extends Macroable implements ContextContract {
* frame and then resolve the value up until the first
* frame.
*/
private frames: any[] = []
private _frames: any[] = []

constructor (public presenter: any, public sharedState: any) {
super()
Expand All @@ -43,8 +43,8 @@ export class Context extends Macroable implements ContextContract {
* Returns value for a key inside frames. Stops looking for it,
* when value is found inside any frame.
*/
private getFromFrame (key: string): any {
const frameWithVal = this.frames.find((frame) => frame[key] !== undefined)
private _getFromFrame (key: string): any {
const frameWithVal = this._frames.find((frame) => frame[key] !== undefined)
return frameWithVal ? frameWithVal[key] : undefined
}

Expand All @@ -58,7 +58,7 @@ export class Context extends Macroable implements ContextContract {
* ```
*/
public newFrame (): void {
this.frames.unshift({})
this._frames.unshift({})
}

/**
Expand All @@ -75,7 +75,7 @@ export class Context extends Macroable implements ContextContract {
* @throws Error if no frame scopes exists.
*/
public setOnFrame (key: string, value: any): void {
const recentFrame = this.frames[0]
const recentFrame = this._frames[0]

if (!recentFrame) {
throw new Error('Make sure to call {newFrame} before calling {setOnFrame}')
Expand All @@ -89,7 +89,7 @@ export class Context extends Macroable implements ContextContract {
* frame via `setOnFrame` will be removed.
*/
public removeFrame (): void {
this.frames.shift()
this._frames.shift()
}

/**
Expand Down Expand Up @@ -123,7 +123,7 @@ export class Context extends Macroable implements ContextContract {
/**
* Pull from one of the nested frames
*/
value = this.getFromFrame(key)
value = this._getFromFrame(key)
if (value !== undefined) {
return typeof (value) === 'function' ? value.bind(this) : value
}
Expand Down Expand Up @@ -169,7 +169,7 @@ export class Context extends Macroable implements ContextContract {
* If value already exists on the presenter
* state, then mutate it first
*/
if (this.presenter.state[key] !== undefined || !this.frames.length) {
if (this.presenter.state[key] !== undefined || !this._frames.length) {
set(this.presenter.state, key, value)
return
}
Expand Down
36 changes: 36 additions & 0 deletions src/Contracts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* file that was distributed with this source code.
*/

import { Token } from 'edge-lexer'
import { MacroableConstructorContract } from 'macroable'
import { ParseTagDefininationContract } from 'edge-parser'

Expand Down Expand Up @@ -93,3 +94,38 @@ export interface TagContract extends ParseTagDefininationContract {
export type TagsContract = {
[tagName: string]: TagContract,
}

/**
* Shape of the compiler
*/
export interface CompilerContract {
compile (templatePath: string, inline: boolean): LoaderTemplate,
generateTokens (templatePath: string): Token[],
}

/**
* Shape of the renderer that renders the edge templates
*/
export interface EdgeRendererContract {
share (locals: any): this,
render (templatePath: string, state?: any): string,
}

/**
* Shape of the main module
*/
export interface EdgeContract {
loader: LoaderContract,

registerTag (tag: TagContract): this,
registerTemplate (templatePath: string, contents: LoaderTemplate): this,
global (key: string, value: any): this,

mount (diskName: string): this,
mount (diskName: string, dirPath: string): this,
unmount (diskName: string): this,

getRenderer (): EdgeRendererContract,
share (locals: any): EdgeRendererContract,
render (templatePath: string, state?: any): string,
}
Loading

0 comments on commit c2ffb90

Please sign in to comment.