CallExpression,
ArrayExpression,
ObjectExpression,
- IfBranchNode
+ IfBranchNode,
+ SourceLocation,
+ Position
} from './ast'
import { SourceMapGenerator, RawSourceMap } from 'source-map'
import {
map?: SourceMapGenerator
helper(name: string): string
push(code: string, node?: CodegenNode, openOnly?: boolean): void
+ resetMapping(loc: SourceLocation): void
indent(): void
deindent(withoutNewLine?: boolean): void
newline(): void
},
push(code, node, openOnly) {
context.code += code
- if (context.map) {
+ if (!__BROWSER__ && context.map) {
if (node) {
let name
if (
name = content
}
}
- context.map.addMapping({
- name,
- source: context.filename,
- original: {
- line: node.loc.start.line,
- column: node.loc.start.column - 1 // source-map column is 0 based
- },
- generated: {
- line: context.line,
- column: context.column - 1
- }
- })
+ addMapping(node.loc.start, name)
}
- if (code) advancePositionWithMutation(context, code)
+ advancePositionWithMutation(context, code)
if (node && !openOnly) {
- context.map.addMapping({
- source: context.filename,
- original: {
- line: node.loc.end.line,
- column: node.loc.end.column - 1
- },
- generated: {
- line: context.line,
- column: context.column - 1
- }
- })
+ addMapping(node.loc.end)
}
}
},
+ resetMapping(loc: SourceLocation) {
+ if (!__BROWSER__ && context.map) {
+ addMapping(loc.start)
+ }
+ },
indent() {
newline(++context.indentLevel)
},
newline(context.indentLevel)
}
}
- const newline = (n: number) => context.push('\n' + ` `.repeat(n))
+
+ function newline(n: number) {
+ context.push('\n' + ` `.repeat(n))
+ }
+
+ function addMapping(loc: Position, name?: string) {
+ context.map!.addMapping({
+ name,
+ source: context.filename,
+ original: {
+ line: loc.line,
+ column: loc.column - 1 // source-map column is 0 based
+ },
+ generated: {
+ line: context.line,
+ column: context.column - 1
+ }
+ })
+ }
+
if (!__BROWSER__ && context.map) {
context.map.setSourceContent(filename, context.source)
}
}
function genObjectExpression(node: ObjectExpression, context: CodegenContext) {
- const { push, indent, deindent, newline } = context
+ const { push, indent, deindent, newline, resetMapping } = context
const { properties } = node
const multilines = properties.length > 1
push(multilines ? `{` : `{ `)
multilines && indent()
for (let i = 0; i < properties.length; i++) {
const { key, value, loc } = properties[i]
- push('', { loc } as any, true) // resets source mapping for every property.
+ resetMapping(loc) // reset source mapping for every property.
// key
genExpressionAsPropertyKey(key, context)
push(`: `)