-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
/
Copy pathutils.ts
206 lines (187 loc) · 6.02 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import { SourceMapGenerator } from 'source-map-js'
import {
type CodegenSourceMapGenerator,
NewlineType,
type Position,
type SourceLocation,
advancePositionWithMutation,
locStub,
} from '@vue/compiler-dom'
import { isArray, isString } from '@vue/shared'
import type { CodegenContext } from '../generate'
export { genExpression } from './expression'
export const NEWLINE: unique symbol = Symbol(__DEV__ ? `newline` : ``)
/** increase offset but don't push actual code */
export const LF: unique symbol = Symbol(__DEV__ ? `line feed` : ``)
export const INDENT_START: unique symbol = Symbol(__DEV__ ? `indent start` : ``)
export const INDENT_END: unique symbol = Symbol(__DEV__ ? `indent end` : ``)
type FalsyValue = false | null | undefined
export type CodeFragment =
| typeof NEWLINE
| typeof LF
| typeof INDENT_START
| typeof INDENT_END
| string
| [code: string, newlineIndex?: number, loc?: SourceLocation, name?: string]
| FalsyValue
export type CodeFragments = Exclude<CodeFragment, any[]> | CodeFragment[]
export function buildCodeFragment(
...frag: CodeFragment[]
): [
CodeFragment[],
(...items: CodeFragment[]) => number,
(...items: CodeFragment[]) => number,
] {
const push = frag.push.bind(frag)
const unshift = frag.unshift.bind(frag)
return [frag, push, unshift]
}
export type CodeFragmentDelimiters = [
left: CodeFragments,
right: CodeFragments,
delimiter: CodeFragments,
placeholder?: CodeFragments,
]
export function genMulti(
[left, right, seg, placeholder]: CodeFragmentDelimiters,
...frags: CodeFragments[]
): CodeFragment[] {
if (placeholder) {
while (frags.length > 0 && !frags[frags.length - 1]) {
frags.pop()
}
frags = frags.map(frag => frag || placeholder)
} else {
frags = frags.filter(Boolean)
}
const frag: CodeFragment[] = []
push(left)
for (let [i, fn] of (
frags as Array<Exclude<CodeFragments, FalsyValue>>
).entries()) {
push(fn)
if (i < frags.length - 1) push(seg)
}
push(right)
return frag
function push(fn: CodeFragments) {
if (!isArray(fn)) fn = [fn]
frag.push(...fn)
}
}
export const DELIMITERS_ARRAY: CodeFragmentDelimiters = ['[', ']', ', ']
export const DELIMITERS_ARRAY_NEWLINE: CodeFragmentDelimiters = [
['[', INDENT_START, NEWLINE],
[INDENT_END, NEWLINE, ']'],
[', ', NEWLINE],
]
export const DELIMITERS_OBJECT: CodeFragmentDelimiters = ['{ ', ' }', ', ']
export const DELIMITERS_OBJECT_NEWLINE: CodeFragmentDelimiters = [
['{', INDENT_START, NEWLINE],
[INDENT_END, NEWLINE, '}'],
[', ', NEWLINE],
]
export function genCall(
name: string | [name: string, placeholder?: CodeFragments],
...frags: CodeFragments[]
): CodeFragment[] {
const hasPlaceholder = isArray(name)
const fnName = hasPlaceholder ? name[0] : name
const placeholder = hasPlaceholder ? name[1] : 'null'
return [fnName, ...genMulti(['(', ')', ', ', placeholder], ...frags)]
}
export function codeFragmentToString(
code: CodeFragment[],
context: CodegenContext,
): [code: string, map: CodegenSourceMapGenerator | undefined] {
const {
options: { filename, sourceMap },
} = context
let map: CodegenSourceMapGenerator | undefined
if (sourceMap) {
// lazy require source-map implementation, only in non-browser builds
map = new SourceMapGenerator() as unknown as CodegenSourceMapGenerator
map.setSourceContent(filename, context.ir.source)
map._sources.add(filename)
}
let codegen = ''
const pos = { line: 1, column: 1, offset: 0 }
let indentLevel = 0
for (let frag of code) {
if (!frag) continue
if (frag === NEWLINE) {
frag = [`\n${` `.repeat(indentLevel)}`, NewlineType.Start]
} else if (frag === INDENT_START) {
indentLevel++
continue
} else if (frag === INDENT_END) {
indentLevel--
continue
} else if (frag === LF) {
pos.line++
pos.column = 0
pos.offset++
continue
}
if (isString(frag)) frag = [frag]
let [code, newlineIndex = NewlineType.None, loc, name] = frag
codegen += code
if (map) {
if (loc) addMapping(loc.start, name)
if (newlineIndex === NewlineType.Unknown) {
// multiple newlines, full iteration
advancePositionWithMutation(pos, code)
} else {
// fast paths
pos.offset += code.length
if (newlineIndex === NewlineType.None) {
// no newlines; fast path to avoid newline detection
if (__TEST__ && code.includes('\n')) {
throw new Error(
`CodegenContext.push() called newlineIndex: none, but contains` +
`newlines: ${code.replace(/\n/g, '\\n')}`,
)
}
pos.column += code.length
} else {
// single newline at known index
if (newlineIndex === NewlineType.End) {
newlineIndex = code.length - 1
}
if (
__TEST__ &&
(code.charAt(newlineIndex) !== '\n' ||
code.slice(0, newlineIndex).includes('\n') ||
code.slice(newlineIndex + 1).includes('\n'))
) {
throw new Error(
`CodegenContext.push() called with newlineIndex: ${newlineIndex} ` +
`but does not conform: ${code.replace(/\n/g, '\\n')}`,
)
}
pos.line++
pos.column = code.length - newlineIndex
}
}
if (loc && loc !== locStub) {
addMapping(loc.end)
}
}
}
return [codegen, map]
function addMapping(loc: Position, name: string | null = null) {
// we use the private property to directly add the mapping
// because the addMapping() implementation in source-map-js has a bunch of
// unnecessary arg and validation checks that are pure overhead in our case.
const { _names, _mappings } = map!
if (name !== null && !_names.has(name)) _names.add(name)
_mappings.add({
originalLine: loc.line,
originalColumn: loc.column - 1, // source-map column is 0 based
generatedLine: pos.line,
generatedColumn: pos.column - 1,
source: filename,
name,
})
}
}