refactor(compiler): provide context.resetMapping()
This commit is contained in:
parent
a407b16b2b
commit
d491a022a7
@ -12,7 +12,9 @@ import {
|
|||||||
CallExpression,
|
CallExpression,
|
||||||
ArrayExpression,
|
ArrayExpression,
|
||||||
ObjectExpression,
|
ObjectExpression,
|
||||||
IfBranchNode
|
IfBranchNode,
|
||||||
|
SourceLocation,
|
||||||
|
Position
|
||||||
} from './ast'
|
} from './ast'
|
||||||
import { SourceMapGenerator, RawSourceMap } from 'source-map'
|
import { SourceMapGenerator, RawSourceMap } from 'source-map'
|
||||||
import {
|
import {
|
||||||
@ -67,6 +69,7 @@ export interface CodegenContext extends Required<CodegenOptions> {
|
|||||||
map?: SourceMapGenerator
|
map?: SourceMapGenerator
|
||||||
helper(name: string): string
|
helper(name: string): string
|
||||||
push(code: string, node?: CodegenNode, openOnly?: boolean): void
|
push(code: string, node?: CodegenNode, openOnly?: boolean): void
|
||||||
|
resetMapping(loc: SourceLocation): void
|
||||||
indent(): void
|
indent(): void
|
||||||
deindent(withoutNewLine?: boolean): void
|
deindent(withoutNewLine?: boolean): void
|
||||||
newline(): void
|
newline(): void
|
||||||
@ -104,7 +107,7 @@ function createCodegenContext(
|
|||||||
},
|
},
|
||||||
push(code, node, openOnly) {
|
push(code, node, openOnly) {
|
||||||
context.code += code
|
context.code += code
|
||||||
if (context.map) {
|
if (!__BROWSER__ && context.map) {
|
||||||
if (node) {
|
if (node) {
|
||||||
let name
|
let name
|
||||||
if (
|
if (
|
||||||
@ -117,33 +120,17 @@ function createCodegenContext(
|
|||||||
name = content
|
name = content
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
context.map.addMapping({
|
addMapping(node.loc.start, name)
|
||||||
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
|
|
||||||
}
|
}
|
||||||
})
|
advancePositionWithMutation(context, code)
|
||||||
}
|
|
||||||
if (code) advancePositionWithMutation(context, code)
|
|
||||||
if (node && !openOnly) {
|
if (node && !openOnly) {
|
||||||
context.map.addMapping({
|
addMapping(node.loc.end)
|
||||||
source: context.filename,
|
}
|
||||||
original: {
|
}
|
||||||
line: node.loc.end.line,
|
|
||||||
column: node.loc.end.column - 1
|
|
||||||
},
|
},
|
||||||
generated: {
|
resetMapping(loc: SourceLocation) {
|
||||||
line: context.line,
|
if (!__BROWSER__ && context.map) {
|
||||||
column: context.column - 1
|
addMapping(loc.start)
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
indent() {
|
indent() {
|
||||||
@ -160,7 +147,26 @@ function createCodegenContext(
|
|||||||
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) {
|
if (!__BROWSER__ && context.map) {
|
||||||
context.map.setSourceContent(filename, context.source)
|
context.map.setSourceContent(filename, context.source)
|
||||||
}
|
}
|
||||||
@ -512,14 +518,14 @@ function genCallExpression(
|
|||||||
}
|
}
|
||||||
|
|
||||||
function genObjectExpression(node: ObjectExpression, context: CodegenContext) {
|
function genObjectExpression(node: ObjectExpression, context: CodegenContext) {
|
||||||
const { push, indent, deindent, newline } = context
|
const { push, indent, deindent, newline, resetMapping } = context
|
||||||
const { properties } = node
|
const { properties } = node
|
||||||
const multilines = properties.length > 1
|
const multilines = properties.length > 1
|
||||||
push(multilines ? `{` : `{ `)
|
push(multilines ? `{` : `{ `)
|
||||||
multilines && indent()
|
multilines && indent()
|
||||||
for (let i = 0; i < properties.length; i++) {
|
for (let i = 0; i < properties.length; i++) {
|
||||||
const { key, value, loc } = properties[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
|
// key
|
||||||
genExpressionAsPropertyKey(key, context)
|
genExpressionAsPropertyKey(key, context)
|
||||||
push(`: `)
|
push(`: `)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user