Skip to content

Commit

Permalink
feat(tags): implement include tag
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Jul 3, 2018
1 parent 8b2a761 commit 79800b9
Show file tree
Hide file tree
Showing 31 changed files with 135 additions and 5 deletions.
6 changes: 6 additions & 0 deletions fixtures/include-conditionals/compiled.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(function (template, ctx) {
let out = ''
out += template.renderInline(`include-conditionals/${ctx.resolve('username') === 'virk' ? 'virk.edge' : 'guest.edge'}`)(template, ctx)
out += '\n'
return out
})(template, ctx)
1 change: 1 addition & 0 deletions fixtures/include-conditionals/index.edge
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@include(`include-conditionals/${username === 'virk' ? 'virk.edge' : 'guest.edge'}`)
3 changes: 3 additions & 0 deletions fixtures/include-conditionals/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"username": "virk"
}
1 change: 1 addition & 0 deletions fixtures/include-conditionals/index.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello from virk
1 change: 1 addition & 0 deletions fixtures/include-conditionals/virk.edge
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello from virk
6 changes: 6 additions & 0 deletions fixtures/include-identifier/compiled.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(function (template, ctx) {
let out = ''
out += template.renderInline(ctx.resolve('partial'))(template, ctx)
out += '\n'
return out
})(template, ctx)
1 change: 1 addition & 0 deletions fixtures/include-identifier/index.edge
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@include(partial)
3 changes: 3 additions & 0 deletions fixtures/include-identifier/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"partial": "include-identifier/partial"
}
1 change: 1 addition & 0 deletions fixtures/include-identifier/index.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello world
1 change: 1 addition & 0 deletions fixtures/include-identifier/partial.edge
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello world
6 changes: 6 additions & 0 deletions fixtures/include-literal/compiled.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(function (template, ctx) {
let out = ''
out += template.renderInline('include-literal/partial')(template, ctx)
out += '\n'
return out
})(template, ctx)
1 change: 1 addition & 0 deletions fixtures/include-literal/index.edge
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@include('include-literal/partial')
2 changes: 2 additions & 0 deletions fixtures/include-literal/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
1 change: 1 addition & 0 deletions fixtures/include-literal/index.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello world
1 change: 1 addition & 0 deletions fixtures/include-literal/partial.edge
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello world
6 changes: 6 additions & 0 deletions fixtures/include-nested/compiled.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(function (template, ctx) {
let out = ''
out += template.renderInline('include-nested/partial')(template, ctx)
out += '\n'
return out
})(template, ctx)
1 change: 1 addition & 0 deletions fixtures/include-nested/index.edge
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@include('include-nested/partial')
2 changes: 2 additions & 0 deletions fixtures/include-nested/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
1 change: 1 addition & 0 deletions fixtures/include-nested/index.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello world
1 change: 1 addition & 0 deletions fixtures/include-nested/partial-1.edge
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello world
1 change: 1 addition & 0 deletions fixtures/include-nested/partial.edge
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@include('include-nested/partial-1')
6 changes: 6 additions & 0 deletions fixtures/include-shared-ctx/compiled.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(function (template, ctx) {
let out = ''
out += template.renderInline('include-shared-ctx/partial')(template, ctx)
out += '\n'
return out
})(template, ctx)
1 change: 1 addition & 0 deletions fixtures/include-shared-ctx/index.edge
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@include('include-shared-ctx/partial')
3 changes: 3 additions & 0 deletions fixtures/include-shared-ctx/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"username": "virk"
}
1 change: 1 addition & 0 deletions fixtures/include-shared-ctx/index.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello virk
1 change: 1 addition & 0 deletions fixtures/include-shared-ctx/partial.edge
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello {{ username }}
10 changes: 5 additions & 5 deletions src/Compiler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,29 @@ export class Compiler {
/**
* Compiles a given template by loading it using the loader
*/
private _compile (templatePath: string, diskName: string): string {
private _compile (templatePath: string, diskName: string, inline: boolean = false): string {
const template = this.loader.resolve(templatePath, diskName)
const parser = new Parser(this.tags)
return parser.parseTemplate(template)
return parser.parseTemplate(template, !inline)
}

/**
* Compiles a given template by loading it using the loader, also caches
* the template and returns from the cache (if exists).
*/
public compile (templatePath: string, diskName: string = 'default'): string {
public compile (templatePath: string, diskName: string = 'default', inline: boolean = false): string {
templatePath = this.loader.makePath(templatePath, diskName)

/**
* Compile right away when cache is disabled
*/
if (!this.shouldCache) {
return this._compile(templatePath, diskName)
return this._compile(templatePath, diskName, inline)
}

/* istanbul ignore else */
if (!this.cache.get(templatePath)) {
this.cache.set(templatePath, this._compile(templatePath, diskName))
this.cache.set(templatePath, this._compile(templatePath, diskName, inline))
}

return this.cache.get(templatePath)!
Expand Down
36 changes: 36 additions & 0 deletions src/Tags/Include.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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 { BaseTag } from './BaseTag'

export class IncludeTag extends BaseTag {
public static block = false
public static seekable = true
public static selfclosed = false

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

/**
* Compiles else block node to Javascript else statement
*/
public compile (parser: Parser, buffer: EdgeBuffer, token: IBlockNode) {
const parsed = parser.parseJsArg(token.properties.jsArg, token.lineno)
this._validateExpression(parsed, token.lineno)
buffer.writeLine(`template.renderInline(${parser.statementToString(parsed)})(template, ctx)`)
}
}
1 change: 1 addition & 0 deletions src/Tags/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
export { IfTag as if } from './If'
export { ElseTag as else } from './Else'
export { ElseIfTag as elseif } from './ElseIf'
export { IncludeTag as include } from './Include'
4 changes: 4 additions & 0 deletions src/Template/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export class Template {
constructor (private compiler: Compiler, private sharedState: any) {
}

public renderInline (template: string): Function {
return new Function('template', 'ctx', this.compiler.compile(template, 'default', true))
}

public render (template: string, presenter: IPresenter, diskName?: string): string {
const compiledTemplate = this.compiler.compile(template, diskName)
const ctx = new Context(presenter, this.sharedState)
Expand Down
30 changes: 30 additions & 0 deletions test/if-tag.spec.ts → test/tags.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,33 @@ We are writing a bad if condition
}
})
})

test.group('Include', () => {
test('raise errors on correct line with include tag', async (assert) => {
assert.plan(1)

const templateContent = `We are writing a bad include condition
@include(foo bar)`

await fs.outputFile(join(viewsDir, 'foo.edge'), templateContent)
try {
compiler.compile('foo')
} catch (error) {
assert.equal(error.loc.line, 2)
}
})

test('raise errors when using sequence expression', async (assert) => {
assert.plan(2)

const templateContent = `@include('foo', 'bar')`

await fs.outputFile(join(viewsDir, 'foo.edge'), templateContent)
try {
compiler.compile('foo')
} catch (error) {
assert.equal(error.loc.line, 1)
assert.equal(error.message, 'E_UNALLOWED_EXPRESSION: SequenceExpression is not allowed for if tag\n> More details: https://err.sh/poppinss/edge-errors/E_UNALLOWED_EXPRESSION')
}
})
})

0 comments on commit 79800b9

Please sign in to comment.