/
renderHelpers.ts
201 lines (187 loc) · 5.92 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import MagicString from 'magic-string';
import { Node, StatementNode } from '../ast/nodes/shared/Node';
import Variable from '../ast/variables/Variable';
import { InternalModuleFormat } from '../rollup/types';
import { PluginDriver } from './PluginDriver';
import { treeshakeNode } from './treeshakeNode';
export interface RenderOptions {
compact: boolean;
dynamicImportFunction: string | undefined;
exportNamesByVariable: Map<Variable, string[]>;
format: InternalModuleFormat;
freeze: boolean;
indent: string;
namespaceToStringTag: boolean;
outputPluginDriver: PluginDriver;
varOrConst: 'var' | 'const';
}
export interface NodeRenderOptions {
end?: number;
isCalleeOfRenderedParent?: boolean;
isNoStatement?: boolean;
isShorthandProperty?: boolean;
preventASI?: boolean;
/* Indicates if the direct parent of an element changed.
Necessary for determining the "this" context of callees. */
renderedParentType?: string;
/* Indicates if the parent or ancestor surrounding an element has changed and what it changed to.
Necessary for adding parentheses. */
renderedSurroundingElement?: string;
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
): number {
let searchPos: number, charCodeAfterSlash: number;
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);
}
}
}
const NON_WHITESPACE = /\S/g;
export function findNonWhiteSpace(code: string, index: number): number {
NON_WHITESPACE.lastIndex = index;
const result = NON_WHITESPACE.exec(code)!;
return result.index;
}
// This assumes "code" only contains white-space and comments
// Returns position of line-comment if applicable
function findFirstLineBreakOutsideComment(code: string): [number, number] {
let lineBreakPos,
charCodeAfterSlash,
start = 0;
lineBreakPos = code.indexOf('\n', start);
while (true) {
start = code.indexOf('/', start);
if (start === -1 || start > lineBreakPos) return [lineBreakPos, lineBreakPos + 1];
// With our assumption, '/' always starts a comment. Determine comment type:
charCodeAfterSlash = code.charCodeAt(start + 1);
if (charCodeAfterSlash === 47 /*"/"*/) return [start, lineBreakPos + 1];
start = code.indexOf('*/', start + 3) + 2;
if (start > lineBreakPos) {
lineBreakPos = code.indexOf('\n', start);
}
}
}
export function renderStatementList(
statements: StatementNode[],
code: MagicString,
start: number,
end: number,
options: RenderOptions
): void {
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!, 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 +
1 +
findFirstLineBreakOutsideComment(code.original.slice(separator + 1, nextNode.start))[1];
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): void {
while (true) {
const [removeStart, removeEnd] = findFirstLineBreakOutsideComment(
code.original.slice(start, end)
);
if (removeStart === -1) {
break;
}
code.remove(start + removeStart, (start += removeEnd));
}
}