hermes-transform 0.5.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/dist/detachedNode.js +25 -49
- package/dist/detachedNode.js.flow +3 -3
- package/dist/generated/TransformCloneSignatures.js.flow +1728 -9
- package/dist/generated/TransformReplaceSignatures.js.flow +921 -780
- package/dist/generated/node-types.js +1099 -1156
- package/dist/generated/node-types.js.flow +77 -125
- package/dist/generated/special-case-node-types.js +106 -85
- package/dist/generated/special-case-node-types.js.flow +28 -2
- package/dist/getVisitorKeys.js +4 -6
- package/dist/getVisitorKeys.js.flow +1 -1
- package/dist/index.js +13 -18
- package/dist/index.js.flow +8 -2
- package/dist/transform/Errors.js +8 -116
- package/dist/transform/Errors.js.flow +1 -1
- package/dist/transform/MutationContext.js +54 -67
- package/dist/transform/MutationContext.js.flow +4 -3
- package/dist/transform/TransformContext.js +100 -58
- package/dist/transform/TransformContext.js.flow +277 -141
- package/dist/transform/comments/comments.js +127 -40
- package/dist/transform/comments/comments.js.flow +106 -29
- package/dist/transform/comments/prettier/common/util.js +46 -47
- package/dist/transform/comments/prettier/common/util.js.flow +1 -1
- package/dist/transform/comments/prettier/language-js/comments.js +199 -216
- package/dist/transform/comments/prettier/language-js/comments.js.flow +1 -1
- package/dist/transform/comments/prettier/language-js/loc.js +10 -9
- package/dist/transform/comments/prettier/language-js/loc.js.flow +1 -1
- package/dist/transform/comments/prettier/language-js/printer-estree.js +7 -6
- package/dist/transform/comments/prettier/language-js/printer-estree.js.flow +1 -1
- package/dist/transform/comments/prettier/language-js/utils.js +18 -30
- package/dist/transform/comments/prettier/language-js/utils.js.flow +1 -1
- package/dist/transform/comments/prettier/main/comments.js +225 -298
- package/dist/transform/comments/prettier/main/comments.js.flow +1 -1
- package/dist/transform/comments/prettier/utils/get-last.js +2 -4
- package/dist/transform/comments/prettier/utils/get-last.js.flow +1 -1
- package/dist/transform/getTransformedAST.js +81 -104
- package/dist/transform/getTransformedAST.js.flow +18 -13
- package/dist/transform/mutations/AddComments.js +43 -0
- package/dist/transform/mutations/AddComments.js.flow +50 -0
- package/dist/transform/mutations/CloneCommentsTo.js +16 -24
- package/dist/transform/mutations/CloneCommentsTo.js.flow +2 -3
- package/dist/transform/mutations/InsertStatement.js +28 -30
- package/dist/transform/mutations/InsertStatement.js.flow +7 -2
- package/dist/transform/mutations/RemoveComment.js +25 -43
- package/dist/transform/mutations/RemoveComment.js.flow +1 -1
- package/dist/transform/mutations/RemoveNode.js +192 -0
- package/dist/transform/mutations/RemoveNode.js.flow +290 -0
- package/dist/transform/mutations/RemoveStatement.js +7 -9
- package/dist/transform/mutations/RemoveStatement.js.flow +1 -1
- package/dist/transform/mutations/ReplaceNode.js +36 -44
- package/dist/transform/mutations/ReplaceNode.js.flow +1 -1
- package/dist/transform/mutations/ReplaceStatementWithMany.js +11 -13
- package/dist/transform/mutations/ReplaceStatementWithMany.js.flow +1 -1
- package/dist/transform/mutations/utils/arrayUtils.js +16 -14
- package/dist/transform/mutations/utils/arrayUtils.js.flow +16 -1
- package/dist/transform/mutations/utils/getStatementParent.js +23 -28
- package/dist/transform/mutations/utils/getStatementParent.js.flow +2 -2
- package/dist/transform/mutations/utils/isValidModuleDeclarationParent.js +7 -24
- package/dist/transform/mutations/utils/isValidModuleDeclarationParent.js.flow +1 -1
- package/dist/transform/transform.js +58 -30
- package/dist/transform/transform.js.flow +51 -13
- package/dist/traverse/NodeEventGenerator.js +91 -165
- package/dist/traverse/NodeEventGenerator.js.flow +1 -1
- package/dist/traverse/SafeEmitter.js +20 -38
- package/dist/traverse/SafeEmitter.js.flow +1 -1
- package/dist/traverse/SimpleTraverser.js +67 -98
- package/dist/traverse/SimpleTraverser.js.flow +4 -1
- package/dist/traverse/esquery.js +6 -6
- package/dist/traverse/esquery.js.flow +1 -1
- package/dist/traverse/traverse.js +64 -52
- package/dist/traverse/traverse.js.flow +63 -10
- package/package.json +5 -3
- package/dist/transform/mutations/AddLeadingComments.js +0 -47
- package/dist/transform/mutations/AddLeadingComments.js.flow +0 -49
- package/dist/transform/mutations/AddTrailingComments.js +0 -47
- package/dist/transform/mutations/AddTrailingComments.js.flow +0 -49
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Copyright (c)
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
3
|
*
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
@@ -8,61 +8,34 @@
|
|
|
8
8
|
*/
|
|
9
9
|
'use strict';
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
const assert = require('assert');
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
const {
|
|
14
|
+
hasNewline,
|
|
15
|
+
addLeadingComment,
|
|
16
|
+
addDanglingComment,
|
|
17
|
+
addTrailingComment
|
|
18
|
+
} = require('../common/util.js');
|
|
14
19
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
|
18
|
-
|
|
19
|
-
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
|
|
20
|
-
|
|
21
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
|
22
|
-
|
|
23
|
-
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
24
|
-
|
|
25
|
-
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e2) { throw _e2; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e3) { didErr = true; err = _e3; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
|
|
26
|
-
|
|
27
|
-
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
28
|
-
|
|
29
|
-
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
30
|
-
|
|
31
|
-
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
32
|
-
|
|
33
|
-
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
34
|
-
|
|
35
|
-
function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
|
|
36
|
-
|
|
37
|
-
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
38
|
-
|
|
39
|
-
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
|
|
40
|
-
|
|
41
|
-
var assert = require('assert');
|
|
42
|
-
|
|
43
|
-
var _require = require('../common/util.js'),
|
|
44
|
-
hasNewline = _require.hasNewline,
|
|
45
|
-
addLeadingComment = _require.addLeadingComment,
|
|
46
|
-
addDanglingComment = _require.addDanglingComment,
|
|
47
|
-
addTrailingComment = _require.addTrailingComment;
|
|
48
|
-
|
|
49
|
-
var childNodesCache = new WeakMap();
|
|
20
|
+
const childNodesCache = new WeakMap();
|
|
50
21
|
|
|
51
22
|
function getSortedChildNodes(node, options, resultArray) {
|
|
52
23
|
if (!node) {
|
|
53
24
|
return;
|
|
54
25
|
}
|
|
55
26
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
27
|
+
const {
|
|
28
|
+
printer,
|
|
29
|
+
locStart,
|
|
30
|
+
locEnd
|
|
31
|
+
} = options;
|
|
59
32
|
|
|
60
33
|
if (resultArray) {
|
|
61
34
|
if (printer.canAttachComment && printer.canAttachComment(node)) {
|
|
62
35
|
// This reverse insertion sort almost always takes constant
|
|
63
36
|
// time because we almost always (maybe always?) append the
|
|
64
37
|
// nodes in order anyway.
|
|
65
|
-
|
|
38
|
+
let i;
|
|
66
39
|
|
|
67
40
|
for (i = resultArray.length - 1; i >= 0; --i) {
|
|
68
41
|
if (locStart(resultArray[i]) <= locStart(node) && locEnd(resultArray[i]) <= locEnd(node)) {
|
|
@@ -77,17 +50,7 @@ function getSortedChildNodes(node, options, resultArray) {
|
|
|
77
50
|
return childNodesCache.get(node);
|
|
78
51
|
}
|
|
79
52
|
|
|
80
|
-
|
|
81
|
-
var _ref2 = _slicedToArray(_ref, 1),
|
|
82
|
-
key = _ref2[0];
|
|
83
|
-
|
|
84
|
-
return key !== 'enclosingNode' && key !== 'precedingNode' && key !== 'followingNode' && key !== 'tokens' && key !== 'comments';
|
|
85
|
-
}).map(function (_ref3) {
|
|
86
|
-
var _ref4 = _slicedToArray(_ref3, 2),
|
|
87
|
-
value = _ref4[1];
|
|
88
|
-
|
|
89
|
-
return value;
|
|
90
|
-
});
|
|
53
|
+
const childNodes = printer.getCommentChildNodes && printer.getCommentChildNodes(node, options) || typeof node === 'object' && Object.entries(node).filter(([key]) => key !== 'enclosingNode' && key !== 'precedingNode' && key !== 'followingNode' && key !== 'tokens' && key !== 'comments').map(([, value]) => value);
|
|
91
54
|
|
|
92
55
|
if (!childNodes) {
|
|
93
56
|
return;
|
|
@@ -98,18 +61,8 @@ function getSortedChildNodes(node, options, resultArray) {
|
|
|
98
61
|
childNodesCache.set(node, resultArray);
|
|
99
62
|
}
|
|
100
63
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
try {
|
|
105
|
-
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
106
|
-
var childNode = _step.value;
|
|
107
|
-
getSortedChildNodes(childNode, options, resultArray);
|
|
108
|
-
}
|
|
109
|
-
} catch (err) {
|
|
110
|
-
_iterator.e(err);
|
|
111
|
-
} finally {
|
|
112
|
-
_iterator.f();
|
|
64
|
+
for (const childNode of childNodes) {
|
|
65
|
+
getSortedChildNodes(childNode, options, resultArray);
|
|
113
66
|
}
|
|
114
67
|
|
|
115
68
|
return resultArray;
|
|
@@ -119,22 +72,24 @@ function getSortedChildNodes(node, options, resultArray) {
|
|
|
119
72
|
|
|
120
73
|
|
|
121
74
|
function decorateComment(node, comment, options, enclosingNode) {
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
75
|
+
const {
|
|
76
|
+
locStart,
|
|
77
|
+
locEnd
|
|
78
|
+
} = options;
|
|
79
|
+
const commentStart = locStart(comment);
|
|
80
|
+
const commentEnd = locEnd(comment);
|
|
81
|
+
const childNodes = getSortedChildNodes(node, options);
|
|
82
|
+
let precedingNode;
|
|
83
|
+
let followingNode; // Time to dust off the old binary search robes and wizard hat.
|
|
84
|
+
|
|
85
|
+
let left = 0;
|
|
86
|
+
let right = childNodes.length;
|
|
132
87
|
|
|
133
88
|
while (left < right) {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
89
|
+
const middle = left + right >> 1;
|
|
90
|
+
const child = childNodes[middle];
|
|
91
|
+
const start = locStart(child);
|
|
92
|
+
const end = locEnd(child); // The comment is completely contained by this child node.
|
|
138
93
|
|
|
139
94
|
if (start <= commentStart && commentEnd <= end) {
|
|
140
95
|
// Abandon the binary search at this level.
|
|
@@ -169,8 +124,10 @@ function decorateComment(node, comment, options, enclosingNode) {
|
|
|
169
124
|
|
|
170
125
|
|
|
171
126
|
if (enclosingNode && enclosingNode.type === 'TemplateLiteral') {
|
|
172
|
-
|
|
173
|
-
|
|
127
|
+
const {
|
|
128
|
+
quasis
|
|
129
|
+
} = enclosingNode;
|
|
130
|
+
const commentIndex = findExpressionIndexForComment(quasis, comment, options);
|
|
174
131
|
|
|
175
132
|
if (precedingNode && findExpressionIndexForComment(quasis, precedingNode, options) !== commentIndex) {
|
|
176
133
|
precedingNode = null;
|
|
@@ -182,210 +139,189 @@ function decorateComment(node, comment, options, enclosingNode) {
|
|
|
182
139
|
}
|
|
183
140
|
|
|
184
141
|
return {
|
|
185
|
-
enclosingNode
|
|
186
|
-
precedingNode
|
|
187
|
-
followingNode
|
|
142
|
+
enclosingNode,
|
|
143
|
+
precedingNode,
|
|
144
|
+
followingNode
|
|
188
145
|
};
|
|
189
146
|
}
|
|
190
147
|
|
|
191
|
-
|
|
192
|
-
return false;
|
|
193
|
-
};
|
|
148
|
+
const returnFalse = () => false;
|
|
194
149
|
|
|
195
150
|
function attach(comments, ast, text, options) {
|
|
196
151
|
if (!Array.isArray(comments)) {
|
|
197
152
|
return;
|
|
198
153
|
}
|
|
199
154
|
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
});
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
if (_options.parser === 'json' || _options.parser === 'json5' || _options.parser === '__js_expression' || _options.parser === '__vue_expression') {
|
|
242
|
-
if (locStart(_comment) - locStart(_ast) <= 0) {
|
|
243
|
-
addLeadingComment(_ast, _comment);
|
|
244
|
-
continue;
|
|
245
|
-
}
|
|
155
|
+
const tiesToBreak = [];
|
|
156
|
+
const {
|
|
157
|
+
locStart,
|
|
158
|
+
locEnd,
|
|
159
|
+
printer: {
|
|
160
|
+
handleComments = {}
|
|
161
|
+
}
|
|
162
|
+
} = options; // TODO: Make this as default behavior
|
|
163
|
+
|
|
164
|
+
const {
|
|
165
|
+
avoidAstMutation,
|
|
166
|
+
ownLine: handleOwnLineComment = returnFalse,
|
|
167
|
+
endOfLine: handleEndOfLineComment = returnFalse,
|
|
168
|
+
remaining: handleRemainingComment = returnFalse
|
|
169
|
+
} = handleComments;
|
|
170
|
+
const decoratedComments = comments.map((comment, index) => ({ ...decorateComment(ast, comment, options),
|
|
171
|
+
comment,
|
|
172
|
+
text,
|
|
173
|
+
options,
|
|
174
|
+
ast,
|
|
175
|
+
isLastComment: comments.length - 1 === index
|
|
176
|
+
}));
|
|
177
|
+
|
|
178
|
+
for (const [index, context] of decoratedComments.entries()) {
|
|
179
|
+
const {
|
|
180
|
+
comment,
|
|
181
|
+
precedingNode,
|
|
182
|
+
enclosingNode,
|
|
183
|
+
followingNode,
|
|
184
|
+
text,
|
|
185
|
+
options,
|
|
186
|
+
ast,
|
|
187
|
+
isLastComment
|
|
188
|
+
} = context;
|
|
189
|
+
|
|
190
|
+
if (options.parser === 'json' || options.parser === 'json5' || options.parser === '__js_expression' || options.parser === '__vue_expression') {
|
|
191
|
+
if (locStart(comment) - locStart(ast) <= 0) {
|
|
192
|
+
addLeadingComment(ast, comment);
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
246
195
|
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
}
|
|
196
|
+
if (locEnd(comment) - locEnd(ast) >= 0) {
|
|
197
|
+
addTrailingComment(ast, comment);
|
|
198
|
+
continue;
|
|
251
199
|
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
let args;
|
|
252
203
|
|
|
253
|
-
|
|
204
|
+
if (avoidAstMutation) {
|
|
205
|
+
args = [context];
|
|
206
|
+
} else {
|
|
207
|
+
comment.enclosingNode = enclosingNode;
|
|
208
|
+
comment.precedingNode = precedingNode;
|
|
209
|
+
comment.followingNode = followingNode;
|
|
210
|
+
args = [comment, text, options, ast, isLastComment];
|
|
211
|
+
}
|
|
254
212
|
|
|
255
|
-
|
|
256
|
-
|
|
213
|
+
if (isOwnLineComment(text, options, decoratedComments, index)) {
|
|
214
|
+
comment.placement = 'ownLine'; // If a comment exists on its own line, prefer a leading comment.
|
|
215
|
+
// We also need to check if it's the first line of the file.
|
|
216
|
+
|
|
217
|
+
if (handleOwnLineComment(...args)) {// We're good
|
|
218
|
+
} else if (followingNode) {
|
|
219
|
+
// Always a leading comment.
|
|
220
|
+
addLeadingComment(followingNode, comment);
|
|
221
|
+
} else if (precedingNode) {
|
|
222
|
+
addTrailingComment(precedingNode, comment);
|
|
223
|
+
} else if (enclosingNode) {
|
|
224
|
+
addDanglingComment(enclosingNode, comment);
|
|
257
225
|
} else {
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
226
|
+
// There are no nodes, let's attach it to the root of the ast
|
|
227
|
+
|
|
228
|
+
/* istanbul ignore next */
|
|
229
|
+
addDanglingComment(ast, comment);
|
|
262
230
|
}
|
|
231
|
+
} else if (isEndOfLineComment(text, options, decoratedComments, index)) {
|
|
232
|
+
comment.placement = 'endOfLine';
|
|
233
|
+
|
|
234
|
+
if (handleEndOfLineComment(...args)) {// We're good
|
|
235
|
+
} else if (precedingNode) {
|
|
236
|
+
// There is content before this comment on the same line, but
|
|
237
|
+
// none after it, so prefer a trailing comment of the previous node.
|
|
238
|
+
addTrailingComment(precedingNode, comment);
|
|
239
|
+
} else if (followingNode) {
|
|
240
|
+
addLeadingComment(followingNode, comment);
|
|
241
|
+
} else if (enclosingNode) {
|
|
242
|
+
addDanglingComment(enclosingNode, comment);
|
|
243
|
+
} else {
|
|
244
|
+
// There are no nodes, let's attach it to the root of the ast
|
|
263
245
|
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
if (handleEndOfLineComment.apply(void 0, _toConsumableArray(args))) {// We're good
|
|
286
|
-
} else if (precedingNode) {
|
|
287
|
-
// There is content before this comment on the same line, but
|
|
288
|
-
// none after it, so prefer a trailing comment of the previous node.
|
|
289
|
-
addTrailingComment(precedingNode, _comment);
|
|
290
|
-
} else if (followingNode) {
|
|
291
|
-
addLeadingComment(followingNode, _comment);
|
|
292
|
-
} else if (enclosingNode) {
|
|
293
|
-
addDanglingComment(enclosingNode, _comment);
|
|
294
|
-
} else {
|
|
295
|
-
// There are no nodes, let's attach it to the root of the ast
|
|
296
|
-
|
|
297
|
-
/* istanbul ignore next */
|
|
298
|
-
addDanglingComment(_ast, _comment);
|
|
246
|
+
/* istanbul ignore next */
|
|
247
|
+
addDanglingComment(ast, comment);
|
|
248
|
+
}
|
|
249
|
+
} else {
|
|
250
|
+
comment.placement = 'remaining';
|
|
251
|
+
|
|
252
|
+
if (handleRemainingComment(...args)) {// We're good
|
|
253
|
+
} else if (precedingNode && followingNode) {
|
|
254
|
+
// Otherwise, text exists both before and after the comment on
|
|
255
|
+
// the same line. If there is both a preceding and following
|
|
256
|
+
// node, use a tie-breaking algorithm to determine if it should
|
|
257
|
+
// be attached to the next or previous node. In the last case,
|
|
258
|
+
// simply attach the right node;
|
|
259
|
+
const tieCount = tiesToBreak.length;
|
|
260
|
+
|
|
261
|
+
if (tieCount > 0) {
|
|
262
|
+
const lastTie = tiesToBreak[tieCount - 1];
|
|
263
|
+
|
|
264
|
+
if (lastTie.followingNode !== followingNode) {
|
|
265
|
+
breakTies(tiesToBreak, text, options);
|
|
266
|
+
}
|
|
299
267
|
}
|
|
268
|
+
|
|
269
|
+
tiesToBreak.push(context);
|
|
270
|
+
} else if (precedingNode) {
|
|
271
|
+
addTrailingComment(precedingNode, comment);
|
|
272
|
+
} else if (followingNode) {
|
|
273
|
+
addLeadingComment(followingNode, comment);
|
|
274
|
+
} else if (enclosingNode) {
|
|
275
|
+
addDanglingComment(enclosingNode, comment);
|
|
300
276
|
} else {
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
if (handleRemainingComment.apply(void 0, _toConsumableArray(args))) {// We're good
|
|
304
|
-
} else if (precedingNode && followingNode) {
|
|
305
|
-
// Otherwise, text exists both before and after the comment on
|
|
306
|
-
// the same line. If there is both a preceding and following
|
|
307
|
-
// node, use a tie-breaking algorithm to determine if it should
|
|
308
|
-
// be attached to the next or previous node. In the last case,
|
|
309
|
-
// simply attach the right node;
|
|
310
|
-
var tieCount = tiesToBreak.length;
|
|
311
|
-
|
|
312
|
-
if (tieCount > 0) {
|
|
313
|
-
var lastTie = tiesToBreak[tieCount - 1];
|
|
314
|
-
|
|
315
|
-
if (lastTie.followingNode !== followingNode) {
|
|
316
|
-
breakTies(tiesToBreak, _text, _options);
|
|
317
|
-
}
|
|
318
|
-
}
|
|
277
|
+
// There are no nodes, let's attach it to the root of the ast
|
|
319
278
|
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
addTrailingComment(precedingNode, _comment);
|
|
323
|
-
} else if (followingNode) {
|
|
324
|
-
addLeadingComment(followingNode, _comment);
|
|
325
|
-
} else if (enclosingNode) {
|
|
326
|
-
addDanglingComment(enclosingNode, _comment);
|
|
327
|
-
} else {
|
|
328
|
-
// There are no nodes, let's attach it to the root of the ast
|
|
329
|
-
|
|
330
|
-
/* istanbul ignore next */
|
|
331
|
-
addDanglingComment(_ast, _comment);
|
|
332
|
-
}
|
|
279
|
+
/* istanbul ignore next */
|
|
280
|
+
addDanglingComment(ast, comment);
|
|
333
281
|
}
|
|
334
282
|
}
|
|
335
|
-
} catch (err) {
|
|
336
|
-
_iterator2.e(err);
|
|
337
|
-
} finally {
|
|
338
|
-
_iterator2.f();
|
|
339
283
|
}
|
|
340
284
|
|
|
341
285
|
breakTies(tiesToBreak, text, options);
|
|
342
286
|
|
|
343
287
|
if (!avoidAstMutation) {
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
// don't need them anymore, and they create cycles in the AST that
|
|
352
|
-
// may lead to infinite recursion if we don't delete them here.
|
|
353
|
-
delete comment.precedingNode;
|
|
354
|
-
delete comment.enclosingNode;
|
|
355
|
-
delete comment.followingNode;
|
|
356
|
-
}
|
|
357
|
-
} catch (err) {
|
|
358
|
-
_iterator3.e(err);
|
|
359
|
-
} finally {
|
|
360
|
-
_iterator3.f();
|
|
288
|
+
for (const comment of comments) {
|
|
289
|
+
// These node references were useful for breaking ties, but we
|
|
290
|
+
// don't need them anymore, and they create cycles in the AST that
|
|
291
|
+
// may lead to infinite recursion if we don't delete them here.
|
|
292
|
+
delete comment.precedingNode;
|
|
293
|
+
delete comment.enclosingNode;
|
|
294
|
+
delete comment.followingNode;
|
|
361
295
|
}
|
|
362
296
|
}
|
|
363
297
|
}
|
|
364
298
|
|
|
365
|
-
|
|
366
|
-
return !/[\S\n\u2028\u2029]/.test(text);
|
|
367
|
-
};
|
|
299
|
+
const isAllEmptyAndNoLineBreak = text => !/[\S\n\u2028\u2029]/.test(text);
|
|
368
300
|
|
|
369
301
|
function isOwnLineComment(text, options, decoratedComments, commentIndex) {
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
302
|
+
const {
|
|
303
|
+
comment,
|
|
304
|
+
precedingNode
|
|
305
|
+
} = decoratedComments[commentIndex];
|
|
306
|
+
const {
|
|
307
|
+
locStart,
|
|
308
|
+
locEnd
|
|
309
|
+
} = options;
|
|
310
|
+
let start = locStart(comment);
|
|
376
311
|
|
|
377
312
|
if (precedingNode) {
|
|
378
313
|
// Find first comment on the same line
|
|
379
|
-
for (
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
314
|
+
for (let index = commentIndex - 1; index >= 0; index--) {
|
|
315
|
+
const {
|
|
316
|
+
comment,
|
|
317
|
+
precedingNode: currentCommentPrecedingNode
|
|
318
|
+
} = decoratedComments[index];
|
|
383
319
|
|
|
384
|
-
if (currentCommentPrecedingNode !== precedingNode || !isAllEmptyAndNoLineBreak(text.slice(locEnd(
|
|
320
|
+
if (currentCommentPrecedingNode !== precedingNode || !isAllEmptyAndNoLineBreak(text.slice(locEnd(comment), start))) {
|
|
385
321
|
break;
|
|
386
322
|
}
|
|
387
323
|
|
|
388
|
-
start = locStart(
|
|
324
|
+
start = locStart(comment);
|
|
389
325
|
}
|
|
390
326
|
}
|
|
391
327
|
|
|
@@ -395,25 +331,29 @@ function isOwnLineComment(text, options, decoratedComments, commentIndex) {
|
|
|
395
331
|
}
|
|
396
332
|
|
|
397
333
|
function isEndOfLineComment(text, options, decoratedComments, commentIndex) {
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
334
|
+
const {
|
|
335
|
+
comment,
|
|
336
|
+
followingNode
|
|
337
|
+
} = decoratedComments[commentIndex];
|
|
338
|
+
const {
|
|
339
|
+
locStart,
|
|
340
|
+
locEnd
|
|
341
|
+
} = options;
|
|
342
|
+
let end = locEnd(comment);
|
|
404
343
|
|
|
405
344
|
if (followingNode) {
|
|
406
345
|
// Find last comment on the same line
|
|
407
|
-
for (
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
346
|
+
for (let index = commentIndex + 1; index < decoratedComments.length; index++) {
|
|
347
|
+
const {
|
|
348
|
+
comment,
|
|
349
|
+
followingNode: currentCommentFollowingNode
|
|
350
|
+
} = decoratedComments[index];
|
|
411
351
|
|
|
412
|
-
if (currentCommentFollowingNode !== followingNode || !isAllEmptyAndNoLineBreak(text.slice(end, locStart(
|
|
352
|
+
if (currentCommentFollowingNode !== followingNode || !isAllEmptyAndNoLineBreak(text.slice(end, locStart(comment)))) {
|
|
413
353
|
break;
|
|
414
354
|
}
|
|
415
355
|
|
|
416
|
-
end = locEnd(
|
|
356
|
+
end = locEnd(comment);
|
|
417
357
|
}
|
|
418
358
|
}
|
|
419
359
|
|
|
@@ -421,33 +361,35 @@ function isEndOfLineComment(text, options, decoratedComments, commentIndex) {
|
|
|
421
361
|
}
|
|
422
362
|
|
|
423
363
|
function breakTies(tiesToBreak, text, options) {
|
|
424
|
-
|
|
364
|
+
const tieCount = tiesToBreak.length;
|
|
425
365
|
|
|
426
366
|
if (tieCount === 0) {
|
|
427
367
|
return;
|
|
428
368
|
}
|
|
429
369
|
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
370
|
+
const {
|
|
371
|
+
precedingNode,
|
|
372
|
+
followingNode,
|
|
373
|
+
enclosingNode
|
|
374
|
+
} = tiesToBreak[0];
|
|
375
|
+
const gapRegExp = options.printer.getGapRegex && options.printer.getGapRegex(enclosingNode) || /^[\s(]*$/;
|
|
376
|
+
let gapEndPos = options.locStart(followingNode); // Iterate backwards through tiesToBreak, examining the gaps
|
|
436
377
|
// between the tied comments. In order to qualify as leading, a
|
|
437
378
|
// comment must be separated from followingNode by an unbroken series of
|
|
438
379
|
// gaps (or other comments). Gaps should only contain whitespace or open
|
|
439
380
|
// parentheses.
|
|
440
381
|
|
|
441
|
-
|
|
382
|
+
let indexOfFirstLeadingComment;
|
|
442
383
|
|
|
443
384
|
for (indexOfFirstLeadingComment = tieCount; indexOfFirstLeadingComment > 0; --indexOfFirstLeadingComment) {
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
385
|
+
const {
|
|
386
|
+
comment,
|
|
387
|
+
precedingNode: currentCommentPrecedingNode,
|
|
388
|
+
followingNode: currentCommentFollowingNode
|
|
389
|
+
} = tiesToBreak[indexOfFirstLeadingComment - 1];
|
|
448
390
|
assert.strictEqual(currentCommentPrecedingNode, precedingNode);
|
|
449
391
|
assert.strictEqual(currentCommentFollowingNode, followingNode);
|
|
450
|
-
|
|
392
|
+
const gap = text.slice(options.locEnd(comment), gapEndPos);
|
|
451
393
|
|
|
452
394
|
if (gapRegExp.test(gap)) {
|
|
453
395
|
gapEndPos = options.locStart(comment);
|
|
@@ -458,34 +400,19 @@ function breakTies(tiesToBreak, text, options) {
|
|
|
458
400
|
}
|
|
459
401
|
}
|
|
460
402
|
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
_comment4 = _step4$value[1].comment;
|
|
469
|
-
|
|
470
|
-
if (i < indexOfFirstLeadingComment) {
|
|
471
|
-
addTrailingComment(precedingNode, _comment4);
|
|
472
|
-
} else {
|
|
473
|
-
addLeadingComment(followingNode, _comment4);
|
|
474
|
-
}
|
|
403
|
+
for (const [i, {
|
|
404
|
+
comment
|
|
405
|
+
}] of tiesToBreak.entries()) {
|
|
406
|
+
if (i < indexOfFirstLeadingComment) {
|
|
407
|
+
addTrailingComment(precedingNode, comment);
|
|
408
|
+
} else {
|
|
409
|
+
addLeadingComment(followingNode, comment);
|
|
475
410
|
}
|
|
476
|
-
} catch (err) {
|
|
477
|
-
_iterator4.e(err);
|
|
478
|
-
} finally {
|
|
479
|
-
_iterator4.f();
|
|
480
411
|
}
|
|
481
412
|
|
|
482
|
-
for (
|
|
483
|
-
var node = _arr2[_i2];
|
|
484
|
-
|
|
413
|
+
for (const node of [precedingNode, followingNode]) {
|
|
485
414
|
if (node.comments && node.comments.length > 1) {
|
|
486
|
-
node.comments.sort(
|
|
487
|
-
return options.locStart(a) - options.locStart(b);
|
|
488
|
-
});
|
|
415
|
+
node.comments.sort((a, b) => options.locStart(a) - options.locStart(b));
|
|
489
416
|
}
|
|
490
417
|
}
|
|
491
418
|
|
|
@@ -493,9 +420,9 @@ function breakTies(tiesToBreak, text, options) {
|
|
|
493
420
|
}
|
|
494
421
|
|
|
495
422
|
function findExpressionIndexForComment(quasis, comment, options) {
|
|
496
|
-
|
|
423
|
+
const startPos = options.locStart(comment) - 1;
|
|
497
424
|
|
|
498
|
-
for (
|
|
425
|
+
for (let i = 1; i < quasis.length; ++i) {
|
|
499
426
|
if (startPos < options.locStart(quasis[i])) {
|
|
500
427
|
return i - 1;
|
|
501
428
|
}
|
|
@@ -509,5 +436,5 @@ function findExpressionIndexForComment(quasis, comment, options) {
|
|
|
509
436
|
}
|
|
510
437
|
|
|
511
438
|
module.exports = {
|
|
512
|
-
attach
|
|
439
|
+
attach
|
|
513
440
|
};
|