/
renderHelpers.ts
179 lines (169 loc) · 5.04 KB
/
renderHelpers.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
import MagicString from 'magic-string';
import { Node, StatementNode } from '../ast/nodes/shared/Node';
import { treeshakeNode } from './treeshakeNode';
export interface RenderOptions {
compact: boolean;
dynamicImportFunction: string;
format: string;
freeze: boolean;
indent: string;
namespaceToStringTag: boolean;
varOrConst: 'var' | 'const';
}
export interface NodeRenderOptions {
end?: number;
isCalleeOfRenderedParent?: boolean;
isNoStatement?: boolean;
isShorthandProperty?: boolean;
preventASI?: boolean;
renderedParentType?: string; // also serves as a flag if the rendered parent is different from the actual parent
start?: number;
}
export const NO_SEMICOLON: NodeRenderOptions = { isNoStatement: true };
export function findFirstOccurrenceOutsideComment(code: string, searchString: string, start = 0) {
let searchPos, charCodeAfterSlash;
searchPos = code.indexOf(searchString, start);
while (true) {
start = code.indexOf('/', start);
if (start === -1 || start > searchPos) return searchPos;
charCodeAfterSlash = code.charCodeAt(++start);
++start;
if (charCodeAfterSlash === 47 /*"/"*/) {
start = code.indexOf('\n', start) + 1;
if (start === 0) return -1;
if (start > searchPos) {
searchPos = code.indexOf(searchString, start);
}
} else if (charCodeAfterSlash === 42 /*"*"*/) {
start = code.indexOf('*/', start) + 2;
if (start > searchPos) {
searchPos = code.indexOf(searchString, start);
}
}
}
}
export function findFirstLineBreakOutsideComment(code: string, start = 0) {
let lineBreakPos, charCodeAfterSlash;
lineBreakPos = code.indexOf('\n', start);
while (true) {
start = code.indexOf('/', start);
if (start === -1 || start > lineBreakPos) return lineBreakPos;
charCodeAfterSlash = code.charCodeAt(++start);
if (charCodeAfterSlash === 47 /*"/"*/) return lineBreakPos;
++start;
if (charCodeAfterSlash === 42 /*"*"*/) {
start = code.indexOf('*/', start) + 2;
if (start > lineBreakPos) {
lineBreakPos = code.indexOf('\n', start);
}
}
}
}
export function renderStatementList(
statements: StatementNode[],
code: MagicString,
start: number,
end: number,
options: RenderOptions
) {
if (statements.length === 0) return;
let currentNode, currentNodeStart, currentNodeNeedsBoundaries, nextNodeStart;
let nextNode = statements[0];
let nextNodeNeedsBoundaries = !nextNode.included || nextNode.needsBoundaries;
if (nextNodeNeedsBoundaries) {
nextNodeStart =
start + findFirstLineBreakOutsideComment(code.original.slice(start, nextNode.start)) + 1;
}
for (let nextIndex = 1; nextIndex <= statements.length; nextIndex++) {
currentNode = nextNode;
currentNodeStart = nextNodeStart;
currentNodeNeedsBoundaries = nextNodeNeedsBoundaries;
nextNode = statements[nextIndex];
nextNodeNeedsBoundaries =
nextNode === undefined ? false : !nextNode.included || nextNode.needsBoundaries;
if (currentNodeNeedsBoundaries || nextNodeNeedsBoundaries) {
nextNodeStart =
currentNode.end +
findFirstLineBreakOutsideComment(
code.original.slice(currentNode.end, nextNode === undefined ? end : nextNode.start)
) +
1;
if (currentNode.included) {
currentNodeNeedsBoundaries
? currentNode.render(code, options, {
end: nextNodeStart,
start: currentNodeStart
})
: currentNode.render(code, options);
} else {
treeshakeNode(currentNode, code, currentNodeStart as number, nextNodeStart);
}
} else {
currentNode.render(code, options);
}
}
}
// This assumes that the first character is not part of the first node
export function getCommaSeparatedNodesWithBoundaries<N extends Node>(
nodes: N[],
code: MagicString,
start: number,
end: number
): ({
contentEnd: number;
end: number;
node: N;
separator: number | null;
start: number;
})[] {
const splitUpNodes = [];
let node, nextNode, nextNodeStart, contentEnd, char;
let separator = start - 1;
for (let nextIndex = 0; nextIndex < nodes.length; nextIndex++) {
nextNode = nodes[nextIndex];
if (node !== undefined) {
separator =
node.end +
findFirstOccurrenceOutsideComment(code.original.slice(node.end, nextNode.start), ',');
}
nextNodeStart = contentEnd =
separator +
2 +
findFirstLineBreakOutsideComment(code.original.slice(separator + 1, nextNode.start));
while (
((char = code.original.charCodeAt(nextNodeStart)),
char === 32 /*" "*/ || char === 9 /*"\t"*/ || char === 10 /*"\n"*/ || char === 13) /*"\r"*/
)
nextNodeStart++;
if (node !== undefined) {
splitUpNodes.push({
contentEnd,
end: nextNodeStart,
node,
separator,
start
});
}
node = nextNode;
start = nextNodeStart;
}
splitUpNodes.push({
contentEnd: end,
end,
node: node as N,
separator: null,
start
});
return splitUpNodes;
}
export function removeLineBreaks(code: MagicString, start: number, end: number) {
let lineBreakPos = start;
while (true) {
lineBreakPos = findFirstLineBreakOutsideComment(code.original, lineBreakPos);
if (lineBreakPos === -1 || lineBreakPos >= end) {
break;
}
code.remove(lineBreakPos, lineBreakPos + 1);
lineBreakPos++;
}
}