Skip to content

Commit

Permalink
fix(compiler): pass absolute filepath to parser & lexer for appropria…
Browse files Browse the repository at this point in the history
…te stack trace
  • Loading branch information
thetutlage committed Sep 14, 2019
1 parent 81d5cb8 commit 4c4300c
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/Compiler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,13 @@ export class Compiler implements CompilerContract {
* and parent template sections together
*/
if (isBlockToken(firstToken, 'layout')) {
const layoutTokens = this.generateLexerTokens(firstToken.properties.jsArg.replace(/'/g, ''))
const layoutName = firstToken.properties.jsArg.replace(/'/g, '')
/**
* Making absolute path, so that lexer errors must point to the
* absolute file path
*/
const absPath = this._loader.makePath(layoutName)
const layoutTokens = this.generateLexerTokens(absPath)
templateTokens = this._mergeSections(layoutTokens, templateTokens, parser.options.filename)
}

Expand Down Expand Up @@ -208,7 +214,7 @@ export class Compiler implements CompilerContract {
* instead of the `absPath`, since `templatePath` is relative and readable.
*/
const parser = new Parser(this._tags, {
filename: `${templatePath.replace(/\.edge$/, '')}.edge`,
filename: `${absPath.replace(/\.edge$/, '')}.edge`,
})

/**
Expand Down
145 changes: 145 additions & 0 deletions test/edge.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,149 @@ test.group('Edge', (group) => {

assert.equal(edge.render('hello::foo', { username: 'virk' }).trim(), 'Hello virk')
})

test('pass absolute path of template to lexer errors', async (assert) => {
assert.plan(1)
await fs.add('foo.edge', '@if(1 + 1)')

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

const edge = new Edge(new Loader())
edge.mount(fs.basePath)

try {
edge.render('foo', false)
} catch ({ stack }) {
assert.equal(stack.split('\n')[1].trim(), `at (${join(fs.basePath, 'foo.edge')}:1:4)`)
}
})

test('pass absolute path of template to parser errors', async (assert) => {
assert.plan(1)
await fs.add('foo.edge', 'Hello {{ a,:b }}')

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

const edge = new Edge(new Loader())
edge.mount(fs.basePath)

try {
edge.render('foo', false)
} catch ({ stack }) {
assert.equal(stack.split('\n')[1].trim(), `at (${join(fs.basePath, 'foo.edge')}:1:11)`)
}
})

test('pass absolute path of layout to lexer errors', async (assert) => {
assert.plan(1)
await fs.add('foo.edge', `@layout('bar')`)
await fs.add('bar.edge', `@if(username)`)

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

const edge = new Edge(new Loader())
edge.mount(fs.basePath)

try {
edge.render('foo', false)
} catch ({ stack }) {
assert.equal(stack.split('\n')[1].trim(), `at (${join(fs.basePath, 'bar.edge')}:1:4)`)
}
})

/**
* Will fix it later
*/
test.skip('pass absolute path of layout to parser errors', async (assert) => {
assert.plan(1)
await fs.add('foo.edge', `@layout('bar')`)
await fs.add('bar.edge', `{{ a:b }}`)

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

const edge = new Edge(new Loader())
edge.mount(fs.basePath)

try {
edge.render('foo', false)
} catch ({ stack }) {
assert.equal(stack.split('\n')[1].trim(), `at (${join(fs.basePath, 'bar.edge')}:1:4)`)
}
})

test('pass absolute path of partial to lexer errors', async (assert) => {
assert.plan(1)
await fs.add('foo.edge', `@include('bar')`)
await fs.add('bar.edge', `@if(username)`)

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

const edge = new Edge(new Loader())
edge.mount(fs.basePath)

try {
edge.render('foo', false)
} catch ({ stack }) {
assert.equal(stack.split('\n')[1].trim(), `at (${join(fs.basePath, 'bar.edge')}:1:4)`)
}
})

test('pass absolute path of partial to parser errors', async (assert) => {
assert.plan(1)
await fs.add('foo.edge', `@include('bar')`)
await fs.add('bar.edge', `{{ a:b }}`)

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

const edge = new Edge(new Loader())
edge.mount(fs.basePath)

try {
edge.render('foo', false)
} catch ({ stack }) {
assert.equal(stack.split('\n')[1].trim(), `at (${join(fs.basePath, 'bar.edge')}:1:3)`)
}
})

test('pass absolute path of component to lexer errors', async (assert) => {
assert.plan(1)
await fs.add('foo.edge', `@!component('bar')`)
await fs.add('bar.edge', `@if(username)`)

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

const edge = new Edge(new Loader())
edge.mount(fs.basePath)

try {
edge.render('foo', false)
} catch ({ stack }) {
assert.equal(stack.split('\n')[1].trim(), `at (${join(fs.basePath, 'bar.edge')}:1:4)`)
}
})

test('pass absolute path of component to parser errors', async (assert) => {
assert.plan(1)
await fs.add('foo.edge', `@!component('bar')`)
await fs.add('bar.edge', `{{ a:b }}`)

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

const edge = new Edge(new Loader())
edge.mount(fs.basePath)

try {
edge.render('foo', false)
} catch ({ stack }) {
assert.equal(stack.split('\n')[1].trim(), `at (${join(fs.basePath, 'bar.edge')}:1:3)`)
}
})
})

0 comments on commit 4c4300c

Please sign in to comment.