-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
pureComments.ts
173 lines (162 loc) · 4.89 KB
/
pureComments.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
import * as acorn from 'acorn';
import { BaseWalker, base as basicWalker } from 'acorn-walk';
import {
BinaryExpression,
CallExpression,
ChainExpression,
ConditionalExpression,
ExpressionStatement,
LogicalExpression,
NewExpression,
SequenceExpression
} from '../ast/nodes/NodeType';
import { SOURCEMAPPING_URL_RE } from './sourceMappingURL';
// patch up acorn-walk until class-fields are officially supported
basicWalker.PropertyDefinition = function (node: any, st: any, c: any) {
if (node.computed) {
c(node.key, st, 'Expression');
}
if (node.value) {
c(node.value, st, 'Expression');
}
};
interface CommentState {
annotationIndex: number;
annotations: acorn.Comment[];
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: string = node.type
) {
const { annotations } = state;
let comment = annotations[state.annotationIndex];
while (comment && node.start >= comment.end) {
markPureNode(node, comment, state.code);
comment = annotations[++state.annotationIndex];
}
if (comment && comment.end <= node.end) {
(basicWalker as BaseWalker<CommentState>)[type](node, state, handlePureAnnotationsOfNode);
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) {
const annotatedNodes = [];
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 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 pureCommentRegex = /[@#]__PURE__/;
export function addAnnotations(
comments: acorn.Comment[],
esTreeAst: acorn.Node,
code: string
): void {
const annotations: acorn.Comment[] = [];
const sourceMappingComments: acorn.Comment[] = [];
for (const comment of comments) {
if (pureCommentRegex.test(comment.value)) {
annotations.push(comment);
} else 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) {
const key = valid ? ANNOTATION_KEY : INVALID_COMMENT_KEY;
const property = node[key];
if (property) {
property.push(comment);
} else {
node[key] = [comment];
}
}