hermes-transform 0.5.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 +21 -0
- package/README.md +8 -0
- package/dist/detachedNode.js +128 -0
- package/dist/detachedNode.js.flow +113 -0
- package/dist/generated/TransformCloneSignatures.js.flow +19 -0
- package/dist/generated/TransformReplaceSignatures.js.flow +943 -0
- package/dist/generated/node-types.js +2071 -0
- package/dist/generated/node-types.js.flow +3149 -0
- package/dist/generated/special-case-node-types.js +178 -0
- package/dist/generated/special-case-node-types.js.flow +248 -0
- package/dist/getVisitorKeys.js +35 -0
- package/dist/getVisitorKeys.js.flow +31 -0
- package/dist/index.js +41 -0
- package/dist/index.js.flow +15 -0
- package/dist/transform/Errors.js +151 -0
- package/dist/transform/Errors.js.flow +17 -0
- package/dist/transform/MutationContext.js +94 -0
- package/dist/transform/MutationContext.js.flow +80 -0
- package/dist/transform/TransformContext.js +136 -0
- package/dist/transform/TransformContext.js.flow +378 -0
- package/dist/transform/comments/comments.js +140 -0
- package/dist/transform/comments/comments.js.flow +145 -0
- package/dist/transform/comments/prettier/README.md +6 -0
- package/dist/transform/comments/prettier/common/util.js +365 -0
- package/dist/transform/comments/prettier/common/util.js.flow +349 -0
- package/dist/transform/comments/prettier/language-js/comments.js +777 -0
- package/dist/transform/comments/prettier/language-js/comments.js.flow +950 -0
- package/dist/transform/comments/prettier/language-js/loc.js +41 -0
- package/dist/transform/comments/prettier/language-js/loc.js.flow +41 -0
- package/dist/transform/comments/prettier/language-js/printer-estree.js +31 -0
- package/dist/transform/comments/prettier/language-js/printer-estree.js.flow +37 -0
- package/dist/transform/comments/prettier/language-js/utils.js +131 -0
- package/dist/transform/comments/prettier/language-js/utils.js.flow +135 -0
- package/dist/transform/comments/prettier/main/comments.js +513 -0
- package/dist/transform/comments/prettier/main/comments.js.flow +436 -0
- package/dist/transform/comments/prettier/utils/get-last.js +15 -0
- package/dist/transform/comments/prettier/utils/get-last.js.flow +14 -0
- package/dist/transform/getTransformedAST.js +159 -0
- package/dist/transform/getTransformedAST.js.flow +128 -0
- package/dist/transform/mutations/AddLeadingComments.js +47 -0
- package/dist/transform/mutations/AddLeadingComments.js.flow +49 -0
- package/dist/transform/mutations/AddTrailingComments.js +47 -0
- package/dist/transform/mutations/AddTrailingComments.js.flow +49 -0
- package/dist/transform/mutations/CloneCommentsTo.js +46 -0
- package/dist/transform/mutations/CloneCommentsTo.js.flow +51 -0
- package/dist/transform/mutations/InsertStatement.js +92 -0
- package/dist/transform/mutations/InsertStatement.js.flow +113 -0
- package/dist/transform/mutations/RemoveComment.js +96 -0
- package/dist/transform/mutations/RemoveComment.js.flow +80 -0
- package/dist/transform/mutations/RemoveStatement.js +61 -0
- package/dist/transform/mutations/RemoveStatement.js.flow +68 -0
- package/dist/transform/mutations/ReplaceNode.js +96 -0
- package/dist/transform/mutations/ReplaceNode.js.flow +113 -0
- package/dist/transform/mutations/ReplaceStatementWithMany.js +81 -0
- package/dist/transform/mutations/ReplaceStatementWithMany.js.flow +102 -0
- package/dist/transform/mutations/utils/arrayUtils.js +41 -0
- package/dist/transform/mutations/utils/arrayUtils.js.flow +35 -0
- package/dist/transform/mutations/utils/getStatementParent.js +147 -0
- package/dist/transform/mutations/utils/getStatementParent.js.flow +143 -0
- package/dist/transform/mutations/utils/isValidModuleDeclarationParent.js +53 -0
- package/dist/transform/mutations/utils/isValidModuleDeclarationParent.js.flow +50 -0
- package/dist/transform/transform.js +69 -0
- package/dist/transform/transform.js.flow +60 -0
- package/dist/traverse/NodeEventGenerator.js +427 -0
- package/dist/traverse/NodeEventGenerator.js.flow +406 -0
- package/dist/traverse/SafeEmitter.js +70 -0
- package/dist/traverse/SafeEmitter.js.flow +46 -0
- package/dist/traverse/SimpleTraverser.js +149 -0
- package/dist/traverse/SimpleTraverser.js.flow +109 -0
- package/dist/traverse/esquery.js +37 -0
- package/dist/traverse/esquery.js.flow +173 -0
- package/dist/traverse/traverse.js +139 -0
- package/dist/traverse/traverse.js.flow +149 -0
- package/package.json +22 -0
|
@@ -0,0 +1,513 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*
|
|
7
|
+
* @format
|
|
8
|
+
*/
|
|
9
|
+
'use strict';
|
|
10
|
+
|
|
11
|
+
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
|
12
|
+
|
|
13
|
+
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
14
|
+
|
|
15
|
+
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
|
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();
|
|
50
|
+
|
|
51
|
+
function getSortedChildNodes(node, options, resultArray) {
|
|
52
|
+
if (!node) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
var printer = options.printer,
|
|
57
|
+
locStart = options.locStart,
|
|
58
|
+
locEnd = options.locEnd;
|
|
59
|
+
|
|
60
|
+
if (resultArray) {
|
|
61
|
+
if (printer.canAttachComment && printer.canAttachComment(node)) {
|
|
62
|
+
// This reverse insertion sort almost always takes constant
|
|
63
|
+
// time because we almost always (maybe always?) append the
|
|
64
|
+
// nodes in order anyway.
|
|
65
|
+
var i;
|
|
66
|
+
|
|
67
|
+
for (i = resultArray.length - 1; i >= 0; --i) {
|
|
68
|
+
if (locStart(resultArray[i]) <= locStart(node) && locEnd(resultArray[i]) <= locEnd(node)) {
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
resultArray.splice(i + 1, 0, node);
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
} else if (childNodesCache.has(node)) {
|
|
77
|
+
return childNodesCache.get(node);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
var childNodes = printer.getCommentChildNodes && printer.getCommentChildNodes(node, options) || _typeof(node) === 'object' && Object.entries(node).filter(function (_ref) {
|
|
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
|
+
});
|
|
91
|
+
|
|
92
|
+
if (!childNodes) {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (!resultArray) {
|
|
97
|
+
resultArray = [];
|
|
98
|
+
childNodesCache.set(node, resultArray);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
var _iterator = _createForOfIteratorHelper(childNodes),
|
|
102
|
+
_step;
|
|
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();
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return resultArray;
|
|
116
|
+
} // As efficiently as possible, decorate the comment object with
|
|
117
|
+
// .precedingNode, .enclosingNode, and/or .followingNode properties, at
|
|
118
|
+
// least one of which is guaranteed to be defined.
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
function decorateComment(node, comment, options, enclosingNode) {
|
|
122
|
+
var locStart = options.locStart,
|
|
123
|
+
locEnd = options.locEnd;
|
|
124
|
+
var commentStart = locStart(comment);
|
|
125
|
+
var commentEnd = locEnd(comment);
|
|
126
|
+
var childNodes = getSortedChildNodes(node, options);
|
|
127
|
+
var precedingNode;
|
|
128
|
+
var followingNode; // Time to dust off the old binary search robes and wizard hat.
|
|
129
|
+
|
|
130
|
+
var left = 0;
|
|
131
|
+
var right = childNodes.length;
|
|
132
|
+
|
|
133
|
+
while (left < right) {
|
|
134
|
+
var middle = left + right >> 1;
|
|
135
|
+
var child = childNodes[middle];
|
|
136
|
+
var start = locStart(child);
|
|
137
|
+
var end = locEnd(child); // The comment is completely contained by this child node.
|
|
138
|
+
|
|
139
|
+
if (start <= commentStart && commentEnd <= end) {
|
|
140
|
+
// Abandon the binary search at this level.
|
|
141
|
+
return decorateComment(child, comment, options, child);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (end <= commentStart) {
|
|
145
|
+
// This child node falls completely before the comment.
|
|
146
|
+
// Because we will never consider this node or any nodes
|
|
147
|
+
// before it again, this node must be the closest preceding
|
|
148
|
+
// node we have encountered so far.
|
|
149
|
+
precedingNode = child;
|
|
150
|
+
left = middle + 1;
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (commentEnd <= start) {
|
|
155
|
+
// This child node falls completely after the comment.
|
|
156
|
+
// Because we will never consider this node or any nodes after
|
|
157
|
+
// it again, this node must be the closest following node we
|
|
158
|
+
// have encountered so far.
|
|
159
|
+
followingNode = child;
|
|
160
|
+
right = middle;
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
/* istanbul ignore next */
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
throw new Error('Comment location overlaps with node location');
|
|
167
|
+
} // We don't want comments inside of different expressions inside of the same
|
|
168
|
+
// template literal to move to another expression.
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
if (enclosingNode && enclosingNode.type === 'TemplateLiteral') {
|
|
172
|
+
var quasis = enclosingNode.quasis;
|
|
173
|
+
var commentIndex = findExpressionIndexForComment(quasis, comment, options);
|
|
174
|
+
|
|
175
|
+
if (precedingNode && findExpressionIndexForComment(quasis, precedingNode, options) !== commentIndex) {
|
|
176
|
+
precedingNode = null;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
if (followingNode && findExpressionIndexForComment(quasis, followingNode, options) !== commentIndex) {
|
|
180
|
+
followingNode = null;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
return {
|
|
185
|
+
enclosingNode: enclosingNode,
|
|
186
|
+
precedingNode: precedingNode,
|
|
187
|
+
followingNode: followingNode
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
var returnFalse = function returnFalse() {
|
|
192
|
+
return false;
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
function attach(comments, ast, text, options) {
|
|
196
|
+
if (!Array.isArray(comments)) {
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
var tiesToBreak = [];
|
|
201
|
+
var locStart = options.locStart,
|
|
202
|
+
locEnd = options.locEnd,
|
|
203
|
+
_options$printer$hand = options.printer.handleComments,
|
|
204
|
+
handleComments = _options$printer$hand === void 0 ? {} : _options$printer$hand; // TODO: Make this as default behavior
|
|
205
|
+
|
|
206
|
+
var avoidAstMutation = handleComments.avoidAstMutation,
|
|
207
|
+
_handleComments$ownLi = handleComments.ownLine,
|
|
208
|
+
handleOwnLineComment = _handleComments$ownLi === void 0 ? returnFalse : _handleComments$ownLi,
|
|
209
|
+
_handleComments$endOf = handleComments.endOfLine,
|
|
210
|
+
handleEndOfLineComment = _handleComments$endOf === void 0 ? returnFalse : _handleComments$endOf,
|
|
211
|
+
_handleComments$remai = handleComments.remaining,
|
|
212
|
+
handleRemainingComment = _handleComments$remai === void 0 ? returnFalse : _handleComments$remai;
|
|
213
|
+
var decoratedComments = comments.map(function (comment, index) {
|
|
214
|
+
return _objectSpread(_objectSpread({}, decorateComment(ast, comment, options)), {}, {
|
|
215
|
+
comment: comment,
|
|
216
|
+
text: text,
|
|
217
|
+
options: options,
|
|
218
|
+
ast: ast,
|
|
219
|
+
isLastComment: comments.length - 1 === index
|
|
220
|
+
});
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
var _iterator2 = _createForOfIteratorHelper(decoratedComments.entries()),
|
|
224
|
+
_step2;
|
|
225
|
+
|
|
226
|
+
try {
|
|
227
|
+
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
|
228
|
+
var _step2$value = _slicedToArray(_step2.value, 2),
|
|
229
|
+
index = _step2$value[0],
|
|
230
|
+
context = _step2$value[1];
|
|
231
|
+
|
|
232
|
+
var _comment = context.comment,
|
|
233
|
+
precedingNode = context.precedingNode,
|
|
234
|
+
enclosingNode = context.enclosingNode,
|
|
235
|
+
followingNode = context.followingNode,
|
|
236
|
+
_text = context.text,
|
|
237
|
+
_options = context.options,
|
|
238
|
+
_ast = context.ast,
|
|
239
|
+
isLastComment = context.isLastComment;
|
|
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
|
+
}
|
|
246
|
+
|
|
247
|
+
if (locEnd(_comment) - locEnd(_ast) >= 0) {
|
|
248
|
+
addTrailingComment(_ast, _comment);
|
|
249
|
+
continue;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
var args = void 0;
|
|
254
|
+
|
|
255
|
+
if (avoidAstMutation) {
|
|
256
|
+
args = [context];
|
|
257
|
+
} else {
|
|
258
|
+
_comment.enclosingNode = enclosingNode;
|
|
259
|
+
_comment.precedingNode = precedingNode;
|
|
260
|
+
_comment.followingNode = followingNode;
|
|
261
|
+
args = [_comment, _text, _options, _ast, isLastComment];
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
if (isOwnLineComment(_text, _options, decoratedComments, index)) {
|
|
265
|
+
_comment.placement = 'ownLine'; // If a comment exists on its own line, prefer a leading comment.
|
|
266
|
+
// We also need to check if it's the first line of the file.
|
|
267
|
+
|
|
268
|
+
if (handleOwnLineComment.apply(void 0, _toConsumableArray(args))) {// We're good
|
|
269
|
+
} else if (followingNode) {
|
|
270
|
+
// Always a leading comment.
|
|
271
|
+
addLeadingComment(followingNode, _comment);
|
|
272
|
+
} else if (precedingNode) {
|
|
273
|
+
addTrailingComment(precedingNode, _comment);
|
|
274
|
+
} else if (enclosingNode) {
|
|
275
|
+
addDanglingComment(enclosingNode, _comment);
|
|
276
|
+
} else {
|
|
277
|
+
// There are no nodes, let's attach it to the root of the ast
|
|
278
|
+
|
|
279
|
+
/* istanbul ignore next */
|
|
280
|
+
addDanglingComment(_ast, _comment);
|
|
281
|
+
}
|
|
282
|
+
} else if (isEndOfLineComment(_text, _options, decoratedComments, index)) {
|
|
283
|
+
_comment.placement = 'endOfLine';
|
|
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);
|
|
299
|
+
}
|
|
300
|
+
} else {
|
|
301
|
+
_comment.placement = 'remaining';
|
|
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
|
+
}
|
|
319
|
+
|
|
320
|
+
tiesToBreak.push(context);
|
|
321
|
+
} else if (precedingNode) {
|
|
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
|
+
}
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
} catch (err) {
|
|
336
|
+
_iterator2.e(err);
|
|
337
|
+
} finally {
|
|
338
|
+
_iterator2.f();
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
breakTies(tiesToBreak, text, options);
|
|
342
|
+
|
|
343
|
+
if (!avoidAstMutation) {
|
|
344
|
+
var _iterator3 = _createForOfIteratorHelper(comments),
|
|
345
|
+
_step3;
|
|
346
|
+
|
|
347
|
+
try {
|
|
348
|
+
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
|
|
349
|
+
var comment = _step3.value;
|
|
350
|
+
// These node references were useful for breaking ties, but we
|
|
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();
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
var isAllEmptyAndNoLineBreak = function isAllEmptyAndNoLineBreak(text) {
|
|
366
|
+
return !/[\S\n\u2028\u2029]/.test(text);
|
|
367
|
+
};
|
|
368
|
+
|
|
369
|
+
function isOwnLineComment(text, options, decoratedComments, commentIndex) {
|
|
370
|
+
var _decoratedComments$co = decoratedComments[commentIndex],
|
|
371
|
+
comment = _decoratedComments$co.comment,
|
|
372
|
+
precedingNode = _decoratedComments$co.precedingNode;
|
|
373
|
+
var locStart = options.locStart,
|
|
374
|
+
locEnd = options.locEnd;
|
|
375
|
+
var start = locStart(comment);
|
|
376
|
+
|
|
377
|
+
if (precedingNode) {
|
|
378
|
+
// Find first comment on the same line
|
|
379
|
+
for (var index = commentIndex - 1; index >= 0; index--) {
|
|
380
|
+
var _decoratedComments$in = decoratedComments[index],
|
|
381
|
+
_comment2 = _decoratedComments$in.comment,
|
|
382
|
+
currentCommentPrecedingNode = _decoratedComments$in.precedingNode;
|
|
383
|
+
|
|
384
|
+
if (currentCommentPrecedingNode !== precedingNode || !isAllEmptyAndNoLineBreak(text.slice(locEnd(_comment2), start))) {
|
|
385
|
+
break;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
start = locStart(_comment2);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
return hasNewline(text, start, {
|
|
393
|
+
backwards: true
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
function isEndOfLineComment(text, options, decoratedComments, commentIndex) {
|
|
398
|
+
var _decoratedComments$co2 = decoratedComments[commentIndex],
|
|
399
|
+
comment = _decoratedComments$co2.comment,
|
|
400
|
+
followingNode = _decoratedComments$co2.followingNode;
|
|
401
|
+
var locStart = options.locStart,
|
|
402
|
+
locEnd = options.locEnd;
|
|
403
|
+
var end = locEnd(comment);
|
|
404
|
+
|
|
405
|
+
if (followingNode) {
|
|
406
|
+
// Find last comment on the same line
|
|
407
|
+
for (var index = commentIndex + 1; index < decoratedComments.length; index++) {
|
|
408
|
+
var _decoratedComments$in2 = decoratedComments[index],
|
|
409
|
+
_comment3 = _decoratedComments$in2.comment,
|
|
410
|
+
currentCommentFollowingNode = _decoratedComments$in2.followingNode;
|
|
411
|
+
|
|
412
|
+
if (currentCommentFollowingNode !== followingNode || !isAllEmptyAndNoLineBreak(text.slice(end, locStart(_comment3)))) {
|
|
413
|
+
break;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
end = locEnd(_comment3);
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
return hasNewline(text, end);
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
function breakTies(tiesToBreak, text, options) {
|
|
424
|
+
var tieCount = tiesToBreak.length;
|
|
425
|
+
|
|
426
|
+
if (tieCount === 0) {
|
|
427
|
+
return;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
var _tiesToBreak$ = tiesToBreak[0],
|
|
431
|
+
precedingNode = _tiesToBreak$.precedingNode,
|
|
432
|
+
followingNode = _tiesToBreak$.followingNode,
|
|
433
|
+
enclosingNode = _tiesToBreak$.enclosingNode;
|
|
434
|
+
var gapRegExp = options.printer.getGapRegex && options.printer.getGapRegex(enclosingNode) || /^[\s(]*$/;
|
|
435
|
+
var gapEndPos = options.locStart(followingNode); // Iterate backwards through tiesToBreak, examining the gaps
|
|
436
|
+
// between the tied comments. In order to qualify as leading, a
|
|
437
|
+
// comment must be separated from followingNode by an unbroken series of
|
|
438
|
+
// gaps (or other comments). Gaps should only contain whitespace or open
|
|
439
|
+
// parentheses.
|
|
440
|
+
|
|
441
|
+
var indexOfFirstLeadingComment;
|
|
442
|
+
|
|
443
|
+
for (indexOfFirstLeadingComment = tieCount; indexOfFirstLeadingComment > 0; --indexOfFirstLeadingComment) {
|
|
444
|
+
var _tiesToBreak = tiesToBreak[indexOfFirstLeadingComment - 1],
|
|
445
|
+
comment = _tiesToBreak.comment,
|
|
446
|
+
currentCommentPrecedingNode = _tiesToBreak.precedingNode,
|
|
447
|
+
currentCommentFollowingNode = _tiesToBreak.followingNode;
|
|
448
|
+
assert.strictEqual(currentCommentPrecedingNode, precedingNode);
|
|
449
|
+
assert.strictEqual(currentCommentFollowingNode, followingNode);
|
|
450
|
+
var gap = text.slice(options.locEnd(comment), gapEndPos);
|
|
451
|
+
|
|
452
|
+
if (gapRegExp.test(gap)) {
|
|
453
|
+
gapEndPos = options.locStart(comment);
|
|
454
|
+
} else {
|
|
455
|
+
// The gap string contained something other than whitespace or open
|
|
456
|
+
// parentheses.
|
|
457
|
+
break;
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
var _iterator4 = _createForOfIteratorHelper(tiesToBreak.entries()),
|
|
462
|
+
_step4;
|
|
463
|
+
|
|
464
|
+
try {
|
|
465
|
+
for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
|
|
466
|
+
var _step4$value = _slicedToArray(_step4.value, 2),
|
|
467
|
+
i = _step4$value[0],
|
|
468
|
+
_comment4 = _step4$value[1].comment;
|
|
469
|
+
|
|
470
|
+
if (i < indexOfFirstLeadingComment) {
|
|
471
|
+
addTrailingComment(precedingNode, _comment4);
|
|
472
|
+
} else {
|
|
473
|
+
addLeadingComment(followingNode, _comment4);
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
} catch (err) {
|
|
477
|
+
_iterator4.e(err);
|
|
478
|
+
} finally {
|
|
479
|
+
_iterator4.f();
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
for (var _i2 = 0, _arr2 = [precedingNode, followingNode]; _i2 < _arr2.length; _i2++) {
|
|
483
|
+
var node = _arr2[_i2];
|
|
484
|
+
|
|
485
|
+
if (node.comments && node.comments.length > 1) {
|
|
486
|
+
node.comments.sort(function (a, b) {
|
|
487
|
+
return options.locStart(a) - options.locStart(b);
|
|
488
|
+
});
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
tiesToBreak.length = 0;
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
function findExpressionIndexForComment(quasis, comment, options) {
|
|
496
|
+
var startPos = options.locStart(comment) - 1;
|
|
497
|
+
|
|
498
|
+
for (var i = 1; i < quasis.length; ++i) {
|
|
499
|
+
if (startPos < options.locStart(quasis[i])) {
|
|
500
|
+
return i - 1;
|
|
501
|
+
}
|
|
502
|
+
} // We haven't found it, it probably means that some of the locations are off.
|
|
503
|
+
// Let's just return the first one.
|
|
504
|
+
|
|
505
|
+
/* istanbul ignore next */
|
|
506
|
+
|
|
507
|
+
|
|
508
|
+
return 0;
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
module.exports = {
|
|
512
|
+
attach: attach
|
|
513
|
+
};
|