/
renderHelpers.ts
178 lines (166 loc) · 5.12 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
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 };
// This assumes there are only white-space and comments between start and the string we are looking for
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;
// With our assumption, '/' always starts a comment. Determine comment type:
start =
charCodeAfterSlash === 47 /*"/"*/
? code.indexOf('\n', start) + 1
: code.indexOf('*/', start) + 2;
if (start > searchPos) {
searchPos = code.indexOf(searchString, start);
}
}
}
// This assumes "code" only contains white-space and comments
function findFirstLineBreakOutsideComment(code: string) {
let lineBreakPos,
charCodeAfterSlash,
start = 0;
lineBreakPos = code.indexOf('\n', start);
while (true) {
start = code.indexOf('/', start);
if (start === -1 || start > lineBreakPos) return lineBreakPos;
// With our assumption, '/' always starts a comment. Determine comment type:
charCodeAfterSlash = code.charCodeAt(++start);
if (charCodeAfterSlash === 47 /*"/"*/) return lineBreakPos;
start = code.indexOf('*/', start + 2) + 2;
if (start > lineBreakPos) {
lineBreakPos = code.indexOf('\n', start);
}
}
}
export function renderStatementList(
statements: StatementNode[],
code: MagicString,
start: number,
end: number,
options: RenderOptions
) {
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;
}
// This assumes there are only white-space and comments between start and end
export function removeLineBreaks(code: MagicString, start: number, end: number) {
while (true) {
const lineBreakPos = findFirstLineBreakOutsideComment(code.original.slice(start, end));
if (lineBreakPos === -1) {
break;
}
start = start + lineBreakPos + 1;
code.remove(start - 1, start);
}
}