Skip to content

Commit

Permalink
refactor: finalize compiler API
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Jul 22, 2019
1 parent 5cc6452 commit a0d5502
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 33 deletions.
10 changes: 5 additions & 5 deletions src/CacheManager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import { LoaderTemplate } from '../Contracts'

/**
* In memory cache manager to parsed pre-compiled templates
* In memory cache manager to cache pre-compiled templates
*/
export class CacheManager {
private _cacheStore: Map<string, LoaderTemplate> = new Map()
Expand All @@ -27,23 +27,23 @@ export class CacheManager {
* cache. If caching is disabled, then it will
* return undefined.
*/
public get (templatePath: string): undefined | LoaderTemplate {
public get (absPath: string): undefined | LoaderTemplate {
if (!this._enabled) {
return
}

return this._cacheStore.get(templatePath)
return this._cacheStore.get(absPath)
}

/**
* Set's the template path and the payload to the cache. If
* cache is disabled, then this function returns in noop.
*/
public set (templatePath: string, payload: LoaderTemplate) {
public set (absPath: string, payload: LoaderTemplate) {
if (!this._enabled) {
return
}

this._cacheStore.set(templatePath, payload)
this._cacheStore.set(absPath, payload)
}
}
14 changes: 7 additions & 7 deletions src/Compiler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@ import { CacheManager } from '../CacheManager'

/**
* Compiler compiles the template to a function, which can be invoked at a later
* stage.
* stage using the [[Context]]. [edge-parser](https://npm.im/edge-parser) is
* used under the hood to parse the templates.
*
* Compiler uses [edge-parser](https://npm.im/edge-parser) under the hood and also
* handles the layouts.
*
* When caching is set to `true`, the compiled templates will be cached in-memory
* to improve performance.
* Also, the `layouts` are handled natively by the compiler. Before starting
* the parsing process, it will merge the layout sections.
*/
export class Compiler {
private _cacheManager = new CacheManager(this._cache)
Expand Down Expand Up @@ -122,7 +120,9 @@ export class Compiler {
* Get a new instance of the parser. We use the `templatePath` as the filename
* instead of the `absPath`, since `templatePath` are relative and readable.
*/
const parser = new Parser(this._tags, { filename: templatePath })
const parser = new Parser(this._tags, {
filename: `${templatePath.replace(/\.edge$/, '')}.edge`,
})

/**
* Resolve the template and Presenter using the given loader
Expand Down
22 changes: 7 additions & 15 deletions test/compiler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,14 @@
* file that was distributed with this source code.
*/

import { join } from 'path'
import * as test from 'japa'
import { Filesystem } from '@poppinss/dev-utils'

import { join } from 'path'
import { Loader } from '../src/Loader'
import { Compiler } from '../src/Compiler'

const tags = {
if: class If {
public static block = true
public static seekable = true
public static tagName = 'if'
public static compile (): void {
}
},
}
const tags = {}

const fs = new Filesystem(join(__dirname, 'views'))

Expand All @@ -46,35 +38,35 @@ test.group('Compiler', (group) => {
})(template, ctx)`)
})

test('save template to cache', async (assert) => {
test('save template to cache when is turned on', async (assert) => {
await fs.add('foo.edge', 'Hello {{ username }}')

const loader = new Loader()
loader.mount('default', fs.basePath)

const compiler = new Compiler(loader, tags)
const compiler = new Compiler(loader, tags, true)
assert.equal(
compiler.compile('foo', false),
compiler['_cacheManager'].get(join(fs.basePath, 'foo.edge')),
)
})

test('save template and presenter both to the cache', async (assert) => {
test('save template and presenter both to the cache when caching is turned on', async (assert) => {
await fs.add('foo.edge', 'Hello {{ username }}')
await fs.add('foo.presenter.js', 'module.exports = class Foo {}')

const loader = new Loader()
loader.mount('default', fs.basePath)

const compiler = new Compiler(loader, tags)
const compiler = new Compiler(loader, tags, true)
compiler.compile('foo', false)
assert.equal(
compiler['_cacheManager'].get(join(fs.basePath, 'foo.edge'))!.Presenter!['name'],
'Foo',
)
})

test('do not cache template when caching is disabled', async (assert) => {
test('do not cache template when caching is turned off', async (assert) => {
await fs.add('foo.edge', 'Hello {{ username }}')

const loader = new Loader()
Expand Down
12 changes: 6 additions & 6 deletions test/tags.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ test.group('Include', (group) => {
try {
compiler.compile('foo', true)
} catch (error) {
assert.equal(error.stack.split('\n')[1], ` at (foo:2:13)`)
assert.equal(error.stack.split('\n')[1], ` at (foo.edge:2:13)`)
}
})

Expand All @@ -105,7 +105,7 @@ test.group('Include', (group) => {
try {
compiler.compile('foo', true)
} catch (error) {
assert.equal(error.stack.split('\n')[1], ` at (foo:1:9)`)
assert.equal(error.stack.split('\n')[1], ` at (foo.edge:1:9)`)
assert.equal(error.message, 'SequenceExpression is not allowed for include tag.')
}
})
Expand All @@ -130,7 +130,7 @@ test.group('Component', (group) => {
try {
compiler.compile('foo', true)
} catch (error) {
assert.equal(error.stack.split('\n')[1], ` at (foo:3:0)`)
assert.equal(error.stack.split('\n')[1], ` at (foo.edge:3:0)`)
assert.equal(error.message, 'Identifier is not allowed for slot tag.')
}
})
Expand All @@ -149,7 +149,7 @@ test.group('Component', (group) => {
try {
compiler.compile('foo', true)
} catch (error) {
assert.equal(error.stack.split('\n')[1], ` at (foo:3:0)`)
assert.equal(error.stack.split('\n')[1], ` at (foo.edge:3:0)`)
assert.equal(error.message, 'Maximum of 2 arguments are allowed for slot tag')
}
})
Expand All @@ -168,7 +168,7 @@ test.group('Component', (group) => {
try {
compiler.compile('foo', true)
} catch (error) {
assert.equal(error.stack.split('\n')[1], ` at (foo:3:0)`)
assert.equal(error.stack.split('\n')[1], ` at (foo.edge:3:0)`)
assert.equal(error.message, 'Identifier is not allowed for slot tag.')
}
})
Expand All @@ -190,7 +190,7 @@ test.group('Component', (group) => {
try {
compiler.compile('foo', true)
} catch (error) {
assert.equal(error.stack.split('\n')[1], ` at (foo:5:6)`)
assert.equal(error.stack.split('\n')[1], ` at (foo.edge:5:6)`)
assert.equal(error.message, 'Literal is not allowed for slot tag.')
}
})
Expand Down

0 comments on commit a0d5502

Please sign in to comment.