Skip to content

Commit

Permalink
fix(ast): Better offset range
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Alpert committed Jun 23, 2018
1 parent b1689a7 commit f735716
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
32 changes: 13 additions & 19 deletions src/BaseVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,19 @@ export class BaseVisitor extends Visitor {
}

protected byLine(elements: ASTNode[] = []): NumericDictionary<ASTNode> {
return mapKeys<ASTNode>(this.asArray(elements), (el: ASTNode): number => {
return el.loc && el.loc.start.line
})
return mapKeys<ASTNode>(
this.asArray(elements),
(el: ASTNode): number => {
return el.loc && el.loc.start.line
}
)
}

protected asArray(value: ASTNode | ASTNode[] = []): ASTNode[] {
if (!value) {
return []
}
return isArray(value) ? filter(value) : [ value ]
return isArray(value) ? filter(value) : [value]
}

protected mergeTrailing(elements: ASTNode[] = [], trailing: ASTNode[] = []): ASTNode[] {
Expand All @@ -55,7 +58,7 @@ export class BaseVisitor extends Visitor {
return { loc: null, range: null }
}

const range: [number, number] = [ null, null ]
const range: [number, number] = [null, null]
const loc = { start: { line: null, column: null }, end: { line: null, column: null } }

if (head.loc !== null) {
Expand All @@ -78,10 +81,10 @@ export class BaseVisitor extends Visitor {
}

protected RenderNode(node): ASTNode {
const visitor = (subNode) => {
const visitor = subNode => {
return this.visit(subNode)
}
const mapped = filter(map(node, (subNode) => visitor(subNode)))
const mapped = filter(map(node, subNode => visitor(subNode)))

return mapped.length === 1 ? mapped[0] : mapped
}
Expand Down Expand Up @@ -134,30 +137,21 @@ export class BaseVisitor extends Visitor {
return false
}

return this.mapArguments(ctx, (nodes) => first(values(nodes)))
return this.mapArguments(ctx, nodes => first(values(nodes)))
}

protected Position(line: number, column: number): Position {
return { line, column }
}

protected RenderToken(node): ASTNode | ASTNode[] {
const mapper = ({
startLine,
startColumn,
image,
endLine,
endColumn,
startOffset,
endOffset,
tokenType
}: TokenContext): ASTNode => {
const mapper = ({ startLine, startColumn, image, endLine, endColumn, startOffset, endOffset, tokenType }: TokenContext): ASTNode => {
const start = this.Position(startLine, startColumn)
const end = this.Position(endLine, endColumn)

return {
loc: { source: image, start, end },
range: [ startOffset, endOffset ],
range: [startOffset, endOffset + image.length],
type: tokenType.tokenName
}
}
Expand Down
13 changes: 13 additions & 0 deletions test/location.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { sourceAST } from './helpers'

describe('Location tests', () => {
test.only('Should identify location of print statement', () => {
expect(() => {
const ast = sourceAST(`print a, b, c`, 'BlockStatement')
const ast2 = sourceAST(`? a, b, c`, 'BlockStatement')

expect(ast.range).toEqual([0, 13])
expect(ast2.range).toEqual([0, 9])
}).not.toThrow()
})
})

0 comments on commit f735716

Please sign in to comment.