1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = void 0;
|
7 |
|
8 | var _base = _interopRequireDefault(require("./base"));
|
9 |
|
10 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
11 |
|
12 | function last(stack) {
|
13 | return stack[stack.length - 1];
|
14 | }
|
15 |
|
16 | class CommentsParser extends _base.default {
|
17 | addComment(comment) {
|
18 | if (this.filename) comment.loc.filename = this.filename;
|
19 | this.state.trailingComments.push(comment);
|
20 | this.state.leadingComments.push(comment);
|
21 | }
|
22 |
|
23 | adjustCommentsAfterTrailingComma(node, elements, takeAllComments) {
|
24 | if (this.state.leadingComments.length === 0) {
|
25 | return;
|
26 | }
|
27 |
|
28 | let lastElement = null;
|
29 | let i = elements.length;
|
30 |
|
31 | while (lastElement === null && i > 0) {
|
32 | lastElement = elements[--i];
|
33 | }
|
34 |
|
35 | if (lastElement === null) {
|
36 | return;
|
37 | }
|
38 |
|
39 | for (let j = 0; j < this.state.leadingComments.length; j++) {
|
40 | if (this.state.leadingComments[j].end < this.state.commentPreviousNode.end) {
|
41 | this.state.leadingComments.splice(j, 1);
|
42 | j--;
|
43 | }
|
44 | }
|
45 |
|
46 | const newTrailingComments = [];
|
47 |
|
48 | for (let i = 0; i < this.state.leadingComments.length; i++) {
|
49 | const leadingComment = this.state.leadingComments[i];
|
50 |
|
51 | if (leadingComment.end < node.end) {
|
52 | newTrailingComments.push(leadingComment);
|
53 |
|
54 | if (!takeAllComments) {
|
55 | this.state.leadingComments.splice(i, 1);
|
56 | i--;
|
57 | }
|
58 | } else {
|
59 | if (node.trailingComments === undefined) {
|
60 | node.trailingComments = [];
|
61 | }
|
62 |
|
63 | node.trailingComments.push(leadingComment);
|
64 | }
|
65 | }
|
66 |
|
67 | if (takeAllComments) this.state.leadingComments = [];
|
68 |
|
69 | if (newTrailingComments.length > 0) {
|
70 | lastElement.trailingComments = newTrailingComments;
|
71 | } else if (lastElement.trailingComments !== undefined) {
|
72 | lastElement.trailingComments = [];
|
73 | }
|
74 | }
|
75 |
|
76 | processComment(node) {
|
77 | if (node.type === "Program" && node.body.length > 0) return;
|
78 | const stack = this.state.commentStack;
|
79 | let firstChild, lastChild, trailingComments, i, j;
|
80 |
|
81 | if (this.state.trailingComments.length > 0) {
|
82 | if (this.state.trailingComments[0].start >= node.end) {
|
83 | trailingComments = this.state.trailingComments;
|
84 | this.state.trailingComments = [];
|
85 | } else {
|
86 | this.state.trailingComments.length = 0;
|
87 | }
|
88 | } else if (stack.length > 0) {
|
89 | const lastInStack = last(stack);
|
90 |
|
91 | if (lastInStack.trailingComments && lastInStack.trailingComments[0].start >= node.end) {
|
92 | trailingComments = lastInStack.trailingComments;
|
93 | delete lastInStack.trailingComments;
|
94 | }
|
95 | }
|
96 |
|
97 | if (stack.length > 0 && last(stack).start >= node.start) {
|
98 | firstChild = stack.pop();
|
99 | }
|
100 |
|
101 | while (stack.length > 0 && last(stack).start >= node.start) {
|
102 | lastChild = stack.pop();
|
103 | }
|
104 |
|
105 | if (!lastChild && firstChild) lastChild = firstChild;
|
106 |
|
107 | if (firstChild) {
|
108 | switch (node.type) {
|
109 | case "ObjectExpression":
|
110 | this.adjustCommentsAfterTrailingComma(node, node.properties);
|
111 | break;
|
112 |
|
113 | case "ObjectPattern":
|
114 | this.adjustCommentsAfterTrailingComma(node, node.properties, true);
|
115 | break;
|
116 |
|
117 | case "CallExpression":
|
118 | this.adjustCommentsAfterTrailingComma(node, node.arguments);
|
119 | break;
|
120 |
|
121 | case "ArrayExpression":
|
122 | this.adjustCommentsAfterTrailingComma(node, node.elements);
|
123 | break;
|
124 |
|
125 | case "ArrayPattern":
|
126 | this.adjustCommentsAfterTrailingComma(node, node.elements, true);
|
127 | break;
|
128 | }
|
129 | } else if (this.state.commentPreviousNode && (this.state.commentPreviousNode.type === "ImportSpecifier" && node.type !== "ImportSpecifier" || this.state.commentPreviousNode.type === "ExportSpecifier" && node.type !== "ExportSpecifier")) {
|
130 | this.adjustCommentsAfterTrailingComma(node, [this.state.commentPreviousNode]);
|
131 | }
|
132 |
|
133 | if (lastChild) {
|
134 | if (lastChild.leadingComments) {
|
135 | if (lastChild !== node && lastChild.leadingComments.length > 0 && last(lastChild.leadingComments).end <= node.start) {
|
136 | node.leadingComments = lastChild.leadingComments;
|
137 | delete lastChild.leadingComments;
|
138 | } else {
|
139 | for (i = lastChild.leadingComments.length - 2; i >= 0; --i) {
|
140 | if (lastChild.leadingComments[i].end <= node.start) {
|
141 | node.leadingComments = lastChild.leadingComments.splice(0, i + 1);
|
142 | break;
|
143 | }
|
144 | }
|
145 | }
|
146 | }
|
147 | } else if (this.state.leadingComments.length > 0) {
|
148 | if (last(this.state.leadingComments).end <= node.start) {
|
149 | if (this.state.commentPreviousNode) {
|
150 | for (j = 0; j < this.state.leadingComments.length; j++) {
|
151 | if (this.state.leadingComments[j].end < this.state.commentPreviousNode.end) {
|
152 | this.state.leadingComments.splice(j, 1);
|
153 | j--;
|
154 | }
|
155 | }
|
156 | }
|
157 |
|
158 | if (this.state.leadingComments.length > 0) {
|
159 | node.leadingComments = this.state.leadingComments;
|
160 | this.state.leadingComments = [];
|
161 | }
|
162 | } else {
|
163 | for (i = 0; i < this.state.leadingComments.length; i++) {
|
164 | if (this.state.leadingComments[i].end > node.start) {
|
165 | break;
|
166 | }
|
167 | }
|
168 |
|
169 | const leadingComments = this.state.leadingComments.slice(0, i);
|
170 |
|
171 | if (leadingComments.length) {
|
172 | node.leadingComments = leadingComments;
|
173 | }
|
174 |
|
175 | trailingComments = this.state.leadingComments.slice(i);
|
176 |
|
177 | if (trailingComments.length === 0) {
|
178 | trailingComments = null;
|
179 | }
|
180 | }
|
181 | }
|
182 |
|
183 | this.state.commentPreviousNode = node;
|
184 |
|
185 | if (trailingComments) {
|
186 | if (trailingComments.length && trailingComments[0].start >= node.start && last(trailingComments).end <= node.end) {
|
187 | node.innerComments = trailingComments;
|
188 | } else {
|
189 | const firstTrailingCommentIndex = trailingComments.findIndex(comment => comment.end >= node.end);
|
190 |
|
191 | if (firstTrailingCommentIndex > 0) {
|
192 | node.innerComments = trailingComments.slice(0, firstTrailingCommentIndex);
|
193 | node.trailingComments = trailingComments.slice(firstTrailingCommentIndex);
|
194 | } else {
|
195 | node.trailingComments = trailingComments;
|
196 | }
|
197 | }
|
198 | }
|
199 |
|
200 | stack.push(node);
|
201 | }
|
202 |
|
203 | }
|
204 |
|
205 | exports.default = CommentsParser; |
\ | No newline at end of file |