hermes-transform 0.5.0 → 0.6.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.
Files changed (74) hide show
  1. package/LICENSE +1 -1
  2. package/dist/detachedNode.js +25 -49
  3. package/dist/detachedNode.js.flow +3 -3
  4. package/dist/generated/TransformCloneSignatures.js.flow +1761 -9
  5. package/dist/generated/TransformReplaceSignatures.js.flow +936 -774
  6. package/dist/generated/node-types.js +1117 -1132
  7. package/dist/generated/node-types.js.flow +26 -1
  8. package/dist/generated/special-case-node-types.js +106 -85
  9. package/dist/generated/special-case-node-types.js.flow +28 -2
  10. package/dist/getVisitorKeys.js +4 -6
  11. package/dist/getVisitorKeys.js.flow +1 -1
  12. package/dist/index.js +13 -18
  13. package/dist/index.js.flow +7 -2
  14. package/dist/transform/Errors.js +8 -116
  15. package/dist/transform/Errors.js.flow +1 -1
  16. package/dist/transform/MutationContext.js +54 -67
  17. package/dist/transform/MutationContext.js.flow +1 -1
  18. package/dist/transform/TransformContext.js +106 -54
  19. package/dist/transform/TransformContext.js.flow +255 -121
  20. package/dist/transform/comments/comments.js +25 -25
  21. package/dist/transform/comments/comments.js.flow +1 -1
  22. package/dist/transform/comments/prettier/common/util.js +46 -47
  23. package/dist/transform/comments/prettier/common/util.js.flow +1 -1
  24. package/dist/transform/comments/prettier/language-js/comments.js +199 -216
  25. package/dist/transform/comments/prettier/language-js/comments.js.flow +1 -1
  26. package/dist/transform/comments/prettier/language-js/loc.js +10 -9
  27. package/dist/transform/comments/prettier/language-js/loc.js.flow +1 -1
  28. package/dist/transform/comments/prettier/language-js/printer-estree.js +7 -6
  29. package/dist/transform/comments/prettier/language-js/printer-estree.js.flow +1 -1
  30. package/dist/transform/comments/prettier/language-js/utils.js +18 -30
  31. package/dist/transform/comments/prettier/language-js/utils.js.flow +1 -1
  32. package/dist/transform/comments/prettier/main/comments.js +225 -298
  33. package/dist/transform/comments/prettier/main/comments.js.flow +1 -1
  34. package/dist/transform/comments/prettier/utils/get-last.js +2 -4
  35. package/dist/transform/comments/prettier/utils/get-last.js.flow +1 -1
  36. package/dist/transform/getTransformedAST.js +85 -101
  37. package/dist/transform/getTransformedAST.js.flow +7 -2
  38. package/dist/transform/mutations/AddLeadingComments.js +15 -22
  39. package/dist/transform/mutations/AddLeadingComments.js.flow +1 -1
  40. package/dist/transform/mutations/AddTrailingComments.js +15 -22
  41. package/dist/transform/mutations/AddTrailingComments.js.flow +1 -1
  42. package/dist/transform/mutations/CloneCommentsTo.js +16 -23
  43. package/dist/transform/mutations/CloneCommentsTo.js.flow +1 -1
  44. package/dist/transform/mutations/InsertStatement.js +27 -30
  45. package/dist/transform/mutations/InsertStatement.js.flow +1 -1
  46. package/dist/transform/mutations/RemoveComment.js +25 -43
  47. package/dist/transform/mutations/RemoveComment.js.flow +1 -1
  48. package/dist/transform/mutations/RemoveNode.js +185 -0
  49. package/dist/transform/mutations/RemoveNode.js.flow +279 -0
  50. package/dist/transform/mutations/RemoveStatement.js +7 -9
  51. package/dist/transform/mutations/RemoveStatement.js.flow +1 -1
  52. package/dist/transform/mutations/ReplaceNode.js +36 -44
  53. package/dist/transform/mutations/ReplaceNode.js.flow +1 -1
  54. package/dist/transform/mutations/ReplaceStatementWithMany.js +11 -13
  55. package/dist/transform/mutations/ReplaceStatementWithMany.js.flow +1 -1
  56. package/dist/transform/mutations/utils/arrayUtils.js +2 -14
  57. package/dist/transform/mutations/utils/arrayUtils.js.flow +1 -1
  58. package/dist/transform/mutations/utils/getStatementParent.js +23 -28
  59. package/dist/transform/mutations/utils/getStatementParent.js.flow +2 -2
  60. package/dist/transform/mutations/utils/isValidModuleDeclarationParent.js +7 -24
  61. package/dist/transform/mutations/utils/isValidModuleDeclarationParent.js.flow +1 -1
  62. package/dist/transform/transform.js +20 -24
  63. package/dist/transform/transform.js.flow +12 -8
  64. package/dist/traverse/NodeEventGenerator.js +91 -165
  65. package/dist/traverse/NodeEventGenerator.js.flow +1 -1
  66. package/dist/traverse/SafeEmitter.js +20 -38
  67. package/dist/traverse/SafeEmitter.js.flow +1 -1
  68. package/dist/traverse/SimpleTraverser.js +67 -98
  69. package/dist/traverse/SimpleTraverser.js.flow +4 -1
  70. package/dist/traverse/esquery.js +6 -6
  71. package/dist/traverse/esquery.js.flow +1 -1
  72. package/dist/traverse/traverse.js +39 -51
  73. package/dist/traverse/traverse.js.flow +1 -1
  74. package/package.json +4 -3
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
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
- function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
11
+ const assert = require('assert');
12
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."); }
13
+ const {
14
+ hasNewline,
15
+ addLeadingComment,
16
+ addDanglingComment,
17
+ addTrailingComment
18
+ } = require('../common/util.js');
14
19
 
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();
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
- var printer = options.printer,
57
- locStart = options.locStart,
58
- locEnd = options.locEnd;
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
- var i;
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
- 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
- });
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
- 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();
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
- 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;
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
- 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.
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
- var quasis = enclosingNode.quasis;
173
- var commentIndex = findExpressionIndexForComment(quasis, comment, options);
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: enclosingNode,
186
- precedingNode: precedingNode,
187
- followingNode: followingNode
142
+ enclosingNode,
143
+ precedingNode,
144
+ followingNode
188
145
  };
189
146
  }
190
147
 
191
- var returnFalse = function returnFalse() {
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
- 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
- }
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
- if (locEnd(_comment) - locEnd(_ast) >= 0) {
248
- addTrailingComment(_ast, _comment);
249
- continue;
250
- }
196
+ if (locEnd(comment) - locEnd(ast) >= 0) {
197
+ addTrailingComment(ast, comment);
198
+ continue;
251
199
  }
200
+ }
201
+
202
+ let args;
252
203
 
253
- var args = void 0;
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
- if (avoidAstMutation) {
256
- args = [context];
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
- _comment.enclosingNode = enclosingNode;
259
- _comment.precedingNode = precedingNode;
260
- _comment.followingNode = followingNode;
261
- args = [_comment, _text, _options, _ast, isLastComment];
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
- 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);
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
- _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
- }
277
+ // There are no nodes, let's attach it to the root of the ast
319
278
 
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
- }
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
- 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();
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
- var isAllEmptyAndNoLineBreak = function isAllEmptyAndNoLineBreak(text) {
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
- 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);
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 (var index = commentIndex - 1; index >= 0; index--) {
380
- var _decoratedComments$in = decoratedComments[index],
381
- _comment2 = _decoratedComments$in.comment,
382
- currentCommentPrecedingNode = _decoratedComments$in.precedingNode;
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(_comment2), start))) {
320
+ if (currentCommentPrecedingNode !== precedingNode || !isAllEmptyAndNoLineBreak(text.slice(locEnd(comment), start))) {
385
321
  break;
386
322
  }
387
323
 
388
- start = locStart(_comment2);
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
- 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);
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 (var index = commentIndex + 1; index < decoratedComments.length; index++) {
408
- var _decoratedComments$in2 = decoratedComments[index],
409
- _comment3 = _decoratedComments$in2.comment,
410
- currentCommentFollowingNode = _decoratedComments$in2.followingNode;
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(_comment3)))) {
352
+ if (currentCommentFollowingNode !== followingNode || !isAllEmptyAndNoLineBreak(text.slice(end, locStart(comment)))) {
413
353
  break;
414
354
  }
415
355
 
416
- end = locEnd(_comment3);
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
- var tieCount = tiesToBreak.length;
364
+ const tieCount = tiesToBreak.length;
425
365
 
426
366
  if (tieCount === 0) {
427
367
  return;
428
368
  }
429
369
 
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
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
- var indexOfFirstLeadingComment;
382
+ let indexOfFirstLeadingComment;
442
383
 
443
384
  for (indexOfFirstLeadingComment = tieCount; indexOfFirstLeadingComment > 0; --indexOfFirstLeadingComment) {
444
- var _tiesToBreak = tiesToBreak[indexOfFirstLeadingComment - 1],
445
- comment = _tiesToBreak.comment,
446
- currentCommentPrecedingNode = _tiesToBreak.precedingNode,
447
- currentCommentFollowingNode = _tiesToBreak.followingNode;
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
- var gap = text.slice(options.locEnd(comment), gapEndPos);
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
- 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
- }
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 (var _i2 = 0, _arr2 = [precedingNode, followingNode]; _i2 < _arr2.length; _i2++) {
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(function (a, b) {
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
- var startPos = options.locStart(comment) - 1;
423
+ const startPos = options.locStart(comment) - 1;
497
424
 
498
- for (var i = 1; i < quasis.length; ++i) {
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: attach
439
+ attach
513
440
  };