feat(compiler): basic codegen with source map support

This commit is contained in:
Evan You
2019-09-19 23:05:51 -04:00
parent 98571ab496
commit 9b1a548c6b
15 changed files with 235 additions and 41 deletions

View File

@@ -0,0 +1,20 @@
import { parse, generate } from '../src'
import { SourceMapConsumer, RawSourceMap } from 'source-map'
describe('compiler: codegen', () => {
test('basic source map support', async () => {
const ast = parse(`hello {{ world }}`)
const { code, map } = generate(ast, { module: false })
expect(code).toBe(`["hello ", world]`)
const consumer = await new SourceMapConsumer(map as RawSourceMap)
const pos = consumer.originalPositionFor({
line: 1,
column: 11
})
expect(pos).toMatchObject({
line: 1,
column: 6
})
})
})

View File

@@ -1,14 +1,14 @@
import { Position } from '../src/ast'
import { getInnerRange, advancePositionBy } from '../src/utils'
import { getInnerRange, advancePositionWithClone } from '../src/utils'
function p(line: number, column: number, offset: number): Position {
return { column, line, offset }
}
describe('advancePositionBy', () => {
describe('advancePositionWithClone', () => {
test('same line', () => {
const pos = p(1, 1, 0)
const newPos = advancePositionBy(pos, 'foo\nbar', 2)
const newPos = advancePositionWithClone(pos, 'foo\nbar', 2)
expect(newPos.column).toBe(3)
expect(newPos.line).toBe(1)
@@ -17,7 +17,7 @@ describe('advancePositionBy', () => {
test('same line', () => {
const pos = p(1, 1, 0)
const newPos = advancePositionBy(pos, 'foo\nbar', 4)
const newPos = advancePositionWithClone(pos, 'foo\nbar', 4)
expect(newPos.column).toBe(1)
expect(newPos.line).toBe(2)
@@ -26,7 +26,7 @@ describe('advancePositionBy', () => {
test('multiple lines', () => {
const pos = p(1, 1, 0)
const newPos = advancePositionBy(pos, 'foo\nbar\nbaz', 10)
const newPos = advancePositionWithClone(pos, 'foo\nbar\nbaz', 10)
expect(newPos.column).toBe(2)
expect(newPos.line).toBe(3)