forked from rollup/rollup
/
commentAnnotations.ts
212 lines (199 loc) · 5.87 KB
/
commentAnnotations.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
207
208
209
210
211
212
import type * as acorn from 'acorn';
import { base as basicWalker } from 'acorn-walk';
import {
ArrowFunctionExpression,
BinaryExpression,
CallExpression,
ChainExpression,
ConditionalExpression,
ExportDefaultDeclaration,
ExportNamedDeclaration,
ExpressionStatement,
FunctionDeclaration,
LogicalExpression,
NewExpression,
SequenceExpression,
VariableDeclaration,
VariableDeclarator
} from '../ast/nodes/NodeType';
import { SOURCEMAPPING_URL_RE } from './sourceMappingURL';
export type AnnotationType = 'noSideEffects' | 'pure';
export interface RollupAnnotation extends acorn.Comment {
annotationType: AnnotationType;
}
interface CommentState {
annotationIndex: number;
annotations: RollupAnnotation[];
code: string;
}
export const ANNOTATION_KEY = '_rollupAnnotations';
export const INVALID_COMMENT_KEY = '_rollupRemoved';
interface NodeWithComments extends acorn.Node {
[ANNOTATION_KEY]?: acorn.Comment[];
[INVALID_COMMENT_KEY]?: acorn.Comment[];
}
function handlePureAnnotationsOfNode(
node: acorn.Node,
state: CommentState,
type = node.type
): void {
const { annotations, code } = state;
// eslint-disable-next-line unicorn/consistent-destructuring
let comment = annotations[state.annotationIndex];
while (comment && node.start >= comment.end) {
markPureNode(node, comment, code);
comment = annotations[++state.annotationIndex];
}
if (comment && comment.end <= node.end) {
basicWalker[type](node, state, handlePureAnnotationsOfNode);
// eslint-disable-next-line unicorn/consistent-destructuring
while ((comment = annotations[state.annotationIndex]) && comment.end <= node.end) {
++state.annotationIndex;
annotateNode(node, comment, false);
}
}
}
const neitherWithespaceNorBrackets = /[^\s(]/g;
const noWhitespace = /\S/g;
function markPureNode(node: NodeWithComments, comment: acorn.Comment, code: string): void {
const annotatedNodes: NodeWithComments[] = [];
let invalidAnnotation: boolean | undefined;
const codeInBetween = code.slice(comment.end, node.start);
if (doesNotMatchOutsideComment(codeInBetween, neitherWithespaceNorBrackets)) {
const parentStart = node.start;
while (true) {
annotatedNodes.push(node);
switch (node.type) {
case ExpressionStatement:
case ChainExpression: {
node = (node as any).expression;
continue;
}
case SequenceExpression: {
// if there are parentheses, the annotation would apply to the entire expression
if (doesNotMatchOutsideComment(code.slice(parentStart, node.start), noWhitespace)) {
node = (node as any).expressions[0];
continue;
}
invalidAnnotation = true;
break;
}
case ConditionalExpression: {
// if there are parentheses, the annotation would apply to the entire expression
if (doesNotMatchOutsideComment(code.slice(parentStart, node.start), noWhitespace)) {
node = (node as any).test;
continue;
}
invalidAnnotation = true;
break;
}
case LogicalExpression:
case BinaryExpression: {
// if there are parentheses, the annotation would apply to the entire expression
if (doesNotMatchOutsideComment(code.slice(parentStart, node.start), noWhitespace)) {
node = (node as any).left;
continue;
}
invalidAnnotation = true;
break;
}
case ExportNamedDeclaration:
case ExportDefaultDeclaration: {
node = (node as any).declaration;
continue;
}
case VariableDeclaration: {
// case: /*#__PURE__*/ const foo = () => {}
const declaration = node as any;
if (declaration.kind === 'const') {
// jsdoc only applies to the first declaration
node = declaration.declarations[0].init;
continue;
}
invalidAnnotation = true;
break;
}
case VariableDeclarator: {
node = (node as any).init;
continue;
}
case FunctionDeclaration:
case ArrowFunctionExpression:
case CallExpression:
case NewExpression: {
break;
}
default: {
invalidAnnotation = true;
}
}
break;
}
} else {
invalidAnnotation = true;
}
if (invalidAnnotation) {
annotateNode(node, comment, false);
} else {
for (const node of annotatedNodes) {
annotateNode(node, comment, true);
}
}
}
function doesNotMatchOutsideComment(code: string, forbiddenChars: RegExp): boolean {
let nextMatch: RegExpExecArray | null;
while ((nextMatch = forbiddenChars.exec(code)) !== null) {
if (nextMatch[0] === '/') {
const charCodeAfterSlash = code.charCodeAt(forbiddenChars.lastIndex);
if (charCodeAfterSlash === 42 /*"*"*/) {
forbiddenChars.lastIndex = code.indexOf('*/', forbiddenChars.lastIndex + 1) + 2;
continue;
} else if (charCodeAfterSlash === 47 /*"/"*/) {
forbiddenChars.lastIndex = code.indexOf('\n', forbiddenChars.lastIndex + 1) + 1;
continue;
}
}
forbiddenChars.lastIndex = 0;
return false;
}
return true;
}
const annotationsRegexes: [AnnotationType, RegExp][] = [
['pure', /[#@]__PURE__/],
['noSideEffects', /[#@]__NO_SIDE_EFFECTS__/]
];
export function addAnnotations(
comments: readonly acorn.Comment[],
esTreeAst: acorn.Node,
code: string
): void {
const annotations: RollupAnnotation[] = [];
const sourceMappingComments: acorn.Comment[] = [];
for (const comment of comments) {
for (const [annotationType, regex] of annotationsRegexes) {
if (regex.test(comment.value)) {
annotations.push({ ...comment, annotationType });
}
}
if (SOURCEMAPPING_URL_RE.test(comment.value)) {
sourceMappingComments.push(comment);
}
}
for (const comment of sourceMappingComments) {
annotateNode(esTreeAst, comment, false);
}
handlePureAnnotationsOfNode(esTreeAst, {
annotationIndex: 0,
annotations,
code
});
}
function annotateNode(node: NodeWithComments, comment: acorn.Comment, valid: boolean): void {
const key = valid ? ANNOTATION_KEY : INVALID_COMMENT_KEY;
const property = node[key];
if (property) {
property.push(comment);
} else {
node[key] = [comment];
}
}