Skip to content

Commit

Permalink
Merge branch 'release/1.0.15'
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Jul 10, 2018
2 parents 8948f70 + b451d0d commit 24418ab
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
<a name="1.0.15"></a>
## [1.0.15](https://github.com/poppinss/edge-parser/compare/v1.0.14...v1.0.15) (2018-07-10)


### Bug Fixes

* **parser:** patch loc for all the tokens of acorn ([1c4c24e](https://github.com/poppinss/edge-parser/commit/1c4c24e))



<a name="1.0.14"></a>
## [1.0.14](https://github.com/poppinss/edge-parser/compare/v1.0.13...v1.0.14) (2018-07-10)

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "edge-parser",
"version": "1.0.14",
"version": "1.0.15",
"description": "Parser for edge template engine",
"scripts": {
"pretest": "npm run lint",
Expand Down
7 changes: 5 additions & 2 deletions src/Parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,11 @@ export class Parser {
*/
public generateAst (arg: string, lineno: number): any {
try {
const ast = acorn.parse(arg, this.acornArgs)
this.patchLoc(ast.body[0].loc, lineno)
const ast = acorn.parse(arg, Object.assign(this.acornArgs, {
onToken: (token) => {
this.patchLoc(token.loc, lineno)
},
}))

return ast
} catch (error) {
Expand Down
27 changes: 26 additions & 1 deletion test/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import * as test from 'japa'
import * as dedent from 'dedent'
import { Parser } from '../src/Parser'
import { EdgeBuffer } from '../src/EdgeBuffer'
import { IBlockNode } from 'edge-lexer/build/src/Contracts'
import { IBlockNode, IMustacheNode } from 'edge-lexer/build/src/Contracts'

const tags = {
if: class If {
Expand Down Expand Up @@ -65,4 +65,29 @@ test.group('Parser', () => {
assert.equal(stack.split('\n')[1], ' at (foo.edge:5:16)')
}
})

test('patch line number of all the tokens inside the top level expressions', (assert) => {
const parser = new Parser(tags, { filename: 'foo.edge' })
const template = dedent`
Hello world!
The list of friends are {{
users.map((user) => {
return user.username
})
}}
`

const tokens = parser.generateTokens(template)
const mustacheToken = tokens.find((token) => token.type === 'mustache')
const mustacheExpression = parser.parseJsArg((mustacheToken as IMustacheNode).properties.jsArg, (mustacheToken as IMustacheNode).lineno)

assert.equal(mustacheExpression.loc.start.line, 4)
assert.equal(mustacheExpression.callee.loc.start.line, 4)
assert.equal(mustacheExpression.arguments[0].loc.start.line, 4)
assert.equal(mustacheExpression.arguments[0].body.body[0].loc.start.line, 5)
assert.equal(mustacheExpression.arguments[0].body.body[0].argument.loc.start.line, 5)
assert.equal(mustacheExpression.arguments[0].body.body[0].argument.object.loc.start.line, 5)
assert.equal(mustacheExpression.arguments[0].body.body[0].argument.property.loc.start.line, 5)
})
})

0 comments on commit 24418ab

Please sign in to comment.