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.
Files changed (74) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +8 -0
  3. package/dist/detachedNode.js +128 -0
  4. package/dist/detachedNode.js.flow +113 -0
  5. package/dist/generated/TransformCloneSignatures.js.flow +19 -0
  6. package/dist/generated/TransformReplaceSignatures.js.flow +943 -0
  7. package/dist/generated/node-types.js +2071 -0
  8. package/dist/generated/node-types.js.flow +3149 -0
  9. package/dist/generated/special-case-node-types.js +178 -0
  10. package/dist/generated/special-case-node-types.js.flow +248 -0
  11. package/dist/getVisitorKeys.js +35 -0
  12. package/dist/getVisitorKeys.js.flow +31 -0
  13. package/dist/index.js +41 -0
  14. package/dist/index.js.flow +15 -0
  15. package/dist/transform/Errors.js +151 -0
  16. package/dist/transform/Errors.js.flow +17 -0
  17. package/dist/transform/MutationContext.js +94 -0
  18. package/dist/transform/MutationContext.js.flow +80 -0
  19. package/dist/transform/TransformContext.js +136 -0
  20. package/dist/transform/TransformContext.js.flow +378 -0
  21. package/dist/transform/comments/comments.js +140 -0
  22. package/dist/transform/comments/comments.js.flow +145 -0
  23. package/dist/transform/comments/prettier/README.md +6 -0
  24. package/dist/transform/comments/prettier/common/util.js +365 -0
  25. package/dist/transform/comments/prettier/common/util.js.flow +349 -0
  26. package/dist/transform/comments/prettier/language-js/comments.js +777 -0
  27. package/dist/transform/comments/prettier/language-js/comments.js.flow +950 -0
  28. package/dist/transform/comments/prettier/language-js/loc.js +41 -0
  29. package/dist/transform/comments/prettier/language-js/loc.js.flow +41 -0
  30. package/dist/transform/comments/prettier/language-js/printer-estree.js +31 -0
  31. package/dist/transform/comments/prettier/language-js/printer-estree.js.flow +37 -0
  32. package/dist/transform/comments/prettier/language-js/utils.js +131 -0
  33. package/dist/transform/comments/prettier/language-js/utils.js.flow +135 -0
  34. package/dist/transform/comments/prettier/main/comments.js +513 -0
  35. package/dist/transform/comments/prettier/main/comments.js.flow +436 -0
  36. package/dist/transform/comments/prettier/utils/get-last.js +15 -0
  37. package/dist/transform/comments/prettier/utils/get-last.js.flow +14 -0
  38. package/dist/transform/getTransformedAST.js +159 -0
  39. package/dist/transform/getTransformedAST.js.flow +128 -0
  40. package/dist/transform/mutations/AddLeadingComments.js +47 -0
  41. package/dist/transform/mutations/AddLeadingComments.js.flow +49 -0
  42. package/dist/transform/mutations/AddTrailingComments.js +47 -0
  43. package/dist/transform/mutations/AddTrailingComments.js.flow +49 -0
  44. package/dist/transform/mutations/CloneCommentsTo.js +46 -0
  45. package/dist/transform/mutations/CloneCommentsTo.js.flow +51 -0
  46. package/dist/transform/mutations/InsertStatement.js +92 -0
  47. package/dist/transform/mutations/InsertStatement.js.flow +113 -0
  48. package/dist/transform/mutations/RemoveComment.js +96 -0
  49. package/dist/transform/mutations/RemoveComment.js.flow +80 -0
  50. package/dist/transform/mutations/RemoveStatement.js +61 -0
  51. package/dist/transform/mutations/RemoveStatement.js.flow +68 -0
  52. package/dist/transform/mutations/ReplaceNode.js +96 -0
  53. package/dist/transform/mutations/ReplaceNode.js.flow +113 -0
  54. package/dist/transform/mutations/ReplaceStatementWithMany.js +81 -0
  55. package/dist/transform/mutations/ReplaceStatementWithMany.js.flow +102 -0
  56. package/dist/transform/mutations/utils/arrayUtils.js +41 -0
  57. package/dist/transform/mutations/utils/arrayUtils.js.flow +35 -0
  58. package/dist/transform/mutations/utils/getStatementParent.js +147 -0
  59. package/dist/transform/mutations/utils/getStatementParent.js.flow +143 -0
  60. package/dist/transform/mutations/utils/isValidModuleDeclarationParent.js +53 -0
  61. package/dist/transform/mutations/utils/isValidModuleDeclarationParent.js.flow +50 -0
  62. package/dist/transform/transform.js +69 -0
  63. package/dist/transform/transform.js.flow +60 -0
  64. package/dist/traverse/NodeEventGenerator.js +427 -0
  65. package/dist/traverse/NodeEventGenerator.js.flow +406 -0
  66. package/dist/traverse/SafeEmitter.js +70 -0
  67. package/dist/traverse/SafeEmitter.js.flow +46 -0
  68. package/dist/traverse/SimpleTraverser.js +149 -0
  69. package/dist/traverse/SimpleTraverser.js.flow +109 -0
  70. package/dist/traverse/esquery.js +37 -0
  71. package/dist/traverse/esquery.js.flow +173 -0
  72. package/dist/traverse/traverse.js +139 -0
  73. package/dist/traverse/traverse.js.flow +149 -0
  74. package/package.json +22 -0
@@ -0,0 +1,777 @@
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 _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); }
16
+
17
+ function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
18
+
19
+ function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
20
+
21
+ 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; }
22
+
23
+ var _require = require('../common/util.js'),
24
+ getLast = _require.getLast,
25
+ hasNewline = _require.hasNewline,
26
+ addLeadingComment = _require.addLeadingComment,
27
+ getNextNonSpaceNonCommentCharacterIndexWithStartIndex = _require.getNextNonSpaceNonCommentCharacterIndexWithStartIndex,
28
+ getNextNonSpaceNonCommentCharacterIndex = _require.getNextNonSpaceNonCommentCharacterIndex,
29
+ hasNewlineInRange = _require.hasNewlineInRange,
30
+ addTrailingComment = _require.addTrailingComment,
31
+ addDanglingComment = _require.addDanglingComment,
32
+ getNextNonSpaceNonCommentCharacter = _require.getNextNonSpaceNonCommentCharacter,
33
+ isNonEmptyArray = _require.isNonEmptyArray;
34
+
35
+ var _require2 = require('./utils.js'),
36
+ isBlockComment = _require2.isBlockComment,
37
+ getFunctionParameters = _require2.getFunctionParameters,
38
+ isPrettierIgnoreComment = _require2.isPrettierIgnoreComment,
39
+ isCallLikeExpression = _require2.isCallLikeExpression,
40
+ getCallArguments = _require2.getCallArguments,
41
+ isCallExpression = _require2.isCallExpression,
42
+ isMemberExpression = _require2.isMemberExpression,
43
+ isObjectProperty = _require2.isObjectProperty;
44
+
45
+ var _require3 = require('./loc.js'),
46
+ locStart = _require3.locStart,
47
+ locEnd = _require3.locEnd;
48
+ /**
49
+ * @typedef {import("./types/estree").Node} Node
50
+ * @typedef {import("./types/estree").Comment} Comment
51
+ * @typedef {import("../common/ast-path")} AstPath
52
+ *
53
+ * @typedef {Object} CommentContext
54
+ * @property {Comment} comment
55
+ * @property {Node} precedingNode
56
+ * @property {Node} enclosingNode
57
+ * @property {Node} followingNode
58
+ * @property {string} text
59
+ * @property {any} options
60
+ * @property {Node} ast
61
+ * @property {boolean} isLastComment
62
+ */
63
+
64
+ /**
65
+ * @param {CommentContext} context
66
+ * @returns {boolean}
67
+ */
68
+
69
+
70
+ function handleOwnLineComment(context) {
71
+ return [handleIgnoreComments, handleLastFunctionArgComments, handleMemberExpressionComments, handleIfStatementComments, handleWhileComments, handleTryStatementComments, handleClassComments, handleImportSpecifierComments, handleForComments, handleUnionTypeComments, handleOnlyComments, handleImportDeclarationComments, handleAssignmentPatternComments, handleMethodNameComments, handleLabeledStatementComments].some(function (fn) {
72
+ return fn(context);
73
+ });
74
+ }
75
+ /**
76
+ * @param {CommentContext} context
77
+ * @returns {boolean}
78
+ */
79
+
80
+
81
+ function handleEndOfLineComment(context) {
82
+ return [handleClosureTypeCastComments, handleLastFunctionArgComments, handleConditionalExpressionComments, handleImportSpecifierComments, handleIfStatementComments, handleWhileComments, handleTryStatementComments, handleClassComments, handleLabeledStatementComments, handleCallExpressionComments, handlePropertyComments, handleOnlyComments, handleTypeAliasComments, handleVariableDeclaratorComments].some(function (fn) {
83
+ return fn(context);
84
+ });
85
+ }
86
+ /**
87
+ * @param {CommentContext} context
88
+ * @returns {boolean}
89
+ */
90
+
91
+
92
+ function handleRemainingComment(context) {
93
+ return [handleIgnoreComments, handleIfStatementComments, handleWhileComments, handleObjectPropertyAssignment, handleCommentInEmptyParens, handleMethodNameComments, handleOnlyComments, handleCommentAfterArrowParams, handleFunctionNameComments, handleTSMappedTypeComments, handleBreakAndContinueStatementComments, handleTSFunctionTrailingComments].some(function (fn) {
94
+ return fn(context);
95
+ });
96
+ }
97
+ /**
98
+ * @param {Node} node
99
+ * @returns {void}
100
+ */
101
+
102
+
103
+ function addBlockStatementFirstComment(node, comment) {
104
+ // @ts-expect-error
105
+ var firstNonEmptyNode = (node.body || node.properties).find(function (_ref) {
106
+ var type = _ref.type;
107
+ return type !== 'EmptyStatement';
108
+ });
109
+
110
+ if (firstNonEmptyNode) {
111
+ addLeadingComment(firstNonEmptyNode, comment);
112
+ } else {
113
+ addDanglingComment(node, comment);
114
+ }
115
+ }
116
+ /**
117
+ * @param {Node} node
118
+ * @returns {void}
119
+ */
120
+
121
+
122
+ function addBlockOrNotComment(node, comment) {
123
+ if (node.type === 'BlockStatement') {
124
+ addBlockStatementFirstComment(node, comment);
125
+ } else {
126
+ addLeadingComment(node, comment);
127
+ }
128
+ }
129
+
130
+ function handleClosureTypeCastComments(_ref2) {
131
+ var comment = _ref2.comment,
132
+ followingNode = _ref2.followingNode;
133
+
134
+ if (followingNode && isTypeCastComment(comment)) {
135
+ addLeadingComment(followingNode, comment);
136
+ return true;
137
+ }
138
+
139
+ return false;
140
+ } // There are often comments before the else clause of if statements like
141
+ //
142
+ // if (1) { ... }
143
+ // // comment
144
+ // else { ... }
145
+ //
146
+ // They are being attached as leading comments of the BlockExpression which
147
+ // is not well printed. What we want is to instead move the comment inside
148
+ // of the block and make it leadingComment of the first element of the block
149
+ // or dangling comment of the block if there is nothing inside
150
+ //
151
+ // if (1) { ... }
152
+ // else {
153
+ // // comment
154
+ // ...
155
+ // }
156
+
157
+
158
+ function handleIfStatementComments(_ref3) {
159
+ var comment = _ref3.comment,
160
+ precedingNode = _ref3.precedingNode,
161
+ enclosingNode = _ref3.enclosingNode,
162
+ followingNode = _ref3.followingNode,
163
+ text = _ref3.text;
164
+
165
+ if (!enclosingNode || enclosingNode.type !== 'IfStatement' || !followingNode) {
166
+ return false;
167
+ } // We unfortunately have no way using the AST or location of nodes to know
168
+ // if the comment is positioned before the condition parenthesis:
169
+ // if (a /* comment */) {}
170
+ // The only workaround I found is to look at the next character to see if
171
+ // it is a ).
172
+
173
+
174
+ var nextCharacter = getNextNonSpaceNonCommentCharacter(text, comment, locEnd);
175
+
176
+ if (nextCharacter === ')') {
177
+ addTrailingComment(precedingNode, comment);
178
+ return true;
179
+ } // Comments before `else`:
180
+ // - treat as trailing comments of the consequent, if it's a BlockStatement
181
+ // - treat as a dangling comment otherwise
182
+
183
+
184
+ if (precedingNode === enclosingNode.consequent && followingNode === enclosingNode.alternate) {
185
+ if (precedingNode.type === 'BlockStatement') {
186
+ addTrailingComment(precedingNode, comment);
187
+ } else {
188
+ addDanglingComment(enclosingNode, comment);
189
+ }
190
+
191
+ return true;
192
+ }
193
+
194
+ if (followingNode.type === 'BlockStatement') {
195
+ addBlockStatementFirstComment(followingNode, comment);
196
+ return true;
197
+ }
198
+
199
+ if (followingNode.type === 'IfStatement') {
200
+ addBlockOrNotComment(followingNode.consequent, comment);
201
+ return true;
202
+ } // For comments positioned after the condition parenthesis in an if statement
203
+ // before the consequent without brackets on, such as
204
+ // if (a) /* comment */ true,
205
+ // we look at the next character to see if the following node
206
+ // is the consequent for the if statement
207
+
208
+
209
+ if (enclosingNode.consequent === followingNode) {
210
+ addLeadingComment(followingNode, comment);
211
+ return true;
212
+ }
213
+
214
+ return false;
215
+ }
216
+
217
+ function handleWhileComments(_ref4) {
218
+ var comment = _ref4.comment,
219
+ precedingNode = _ref4.precedingNode,
220
+ enclosingNode = _ref4.enclosingNode,
221
+ followingNode = _ref4.followingNode,
222
+ text = _ref4.text;
223
+
224
+ if (!enclosingNode || enclosingNode.type !== 'WhileStatement' || !followingNode) {
225
+ return false;
226
+ } // We unfortunately have no way using the AST or location of nodes to know
227
+ // if the comment is positioned before the condition parenthesis:
228
+ // while (a /* comment */) {}
229
+ // The only workaround I found is to look at the next character to see if
230
+ // it is a ).
231
+
232
+
233
+ var nextCharacter = getNextNonSpaceNonCommentCharacter(text, comment, locEnd);
234
+
235
+ if (nextCharacter === ')') {
236
+ addTrailingComment(precedingNode, comment);
237
+ return true;
238
+ }
239
+
240
+ if (followingNode.type === 'BlockStatement') {
241
+ addBlockStatementFirstComment(followingNode, comment);
242
+ return true;
243
+ }
244
+
245
+ if (enclosingNode.body === followingNode) {
246
+ addLeadingComment(followingNode, comment);
247
+ return true;
248
+ }
249
+
250
+ return false;
251
+ } // Same as IfStatement but for TryStatement
252
+
253
+
254
+ function handleTryStatementComments(_ref5) {
255
+ var comment = _ref5.comment,
256
+ precedingNode = _ref5.precedingNode,
257
+ enclosingNode = _ref5.enclosingNode,
258
+ followingNode = _ref5.followingNode;
259
+
260
+ if (!enclosingNode || enclosingNode.type !== 'TryStatement' && enclosingNode.type !== 'CatchClause' || !followingNode) {
261
+ return false;
262
+ }
263
+
264
+ if (enclosingNode.type === 'CatchClause' && precedingNode) {
265
+ addTrailingComment(precedingNode, comment);
266
+ return true;
267
+ }
268
+
269
+ if (followingNode.type === 'BlockStatement') {
270
+ addBlockStatementFirstComment(followingNode, comment);
271
+ return true;
272
+ }
273
+
274
+ if (followingNode.type === 'TryStatement') {
275
+ addBlockOrNotComment(followingNode.finalizer, comment);
276
+ return true;
277
+ }
278
+
279
+ if (followingNode.type === 'CatchClause') {
280
+ addBlockOrNotComment(followingNode.body, comment);
281
+ return true;
282
+ }
283
+
284
+ return false;
285
+ }
286
+
287
+ function handleMemberExpressionComments(_ref6) {
288
+ var comment = _ref6.comment,
289
+ enclosingNode = _ref6.enclosingNode,
290
+ followingNode = _ref6.followingNode;
291
+
292
+ if (isMemberExpression(enclosingNode) && followingNode && followingNode.type === 'Identifier') {
293
+ addLeadingComment(enclosingNode, comment);
294
+ return true;
295
+ }
296
+
297
+ return false;
298
+ }
299
+
300
+ function handleConditionalExpressionComments(_ref7) {
301
+ var comment = _ref7.comment,
302
+ precedingNode = _ref7.precedingNode,
303
+ enclosingNode = _ref7.enclosingNode,
304
+ followingNode = _ref7.followingNode,
305
+ text = _ref7.text;
306
+ var isSameLineAsPrecedingNode = precedingNode && !hasNewlineInRange(text, locEnd(precedingNode), locStart(comment));
307
+
308
+ if ((!precedingNode || !isSameLineAsPrecedingNode) && enclosingNode && (enclosingNode.type === 'ConditionalExpression' || enclosingNode.type === 'TSConditionalType') && followingNode) {
309
+ addLeadingComment(followingNode, comment);
310
+ return true;
311
+ }
312
+
313
+ return false;
314
+ }
315
+
316
+ function handleObjectPropertyAssignment(_ref8) {
317
+ var comment = _ref8.comment,
318
+ precedingNode = _ref8.precedingNode,
319
+ enclosingNode = _ref8.enclosingNode;
320
+
321
+ if (isObjectProperty(enclosingNode) && enclosingNode.shorthand && enclosingNode.key === precedingNode && enclosingNode.value.type === 'AssignmentPattern') {
322
+ addTrailingComment(enclosingNode.value.left, comment);
323
+ return true;
324
+ }
325
+
326
+ return false;
327
+ }
328
+
329
+ function handleClassComments(_ref9) {
330
+ var comment = _ref9.comment,
331
+ precedingNode = _ref9.precedingNode,
332
+ enclosingNode = _ref9.enclosingNode,
333
+ followingNode = _ref9.followingNode;
334
+
335
+ if (enclosingNode && (enclosingNode.type === 'ClassDeclaration' || enclosingNode.type === 'ClassExpression' || enclosingNode.type === 'DeclareClass' || enclosingNode.type === 'DeclareInterface' || enclosingNode.type === 'InterfaceDeclaration' || enclosingNode.type === 'TSInterfaceDeclaration')) {
336
+ if (isNonEmptyArray(enclosingNode.decorators) && !(followingNode && followingNode.type === 'Decorator')) {
337
+ addTrailingComment(getLast(enclosingNode.decorators), comment);
338
+ return true;
339
+ }
340
+
341
+ if (enclosingNode.body && followingNode === enclosingNode.body) {
342
+ addBlockStatementFirstComment(enclosingNode.body, comment);
343
+ return true;
344
+ } // Don't add leading comments to `implements`, `extends`, `mixins` to
345
+ // avoid printing the comment after the keyword.
346
+
347
+
348
+ if (followingNode) {
349
+ for (var _i = 0, _arr = ['implements', 'extends', 'mixins']; _i < _arr.length; _i++) {
350
+ var prop = _arr[_i];
351
+
352
+ if (enclosingNode[prop] && followingNode === enclosingNode[prop][0]) {
353
+ if (precedingNode && (precedingNode === enclosingNode.id || precedingNode === enclosingNode.typeParameters || precedingNode === enclosingNode.superClass)) {
354
+ addTrailingComment(precedingNode, comment);
355
+ } else {
356
+ addDanglingComment(enclosingNode, comment, prop);
357
+ }
358
+
359
+ return true;
360
+ }
361
+ }
362
+ }
363
+ }
364
+
365
+ return false;
366
+ }
367
+
368
+ function handleMethodNameComments(_ref10) {
369
+ var comment = _ref10.comment,
370
+ precedingNode = _ref10.precedingNode,
371
+ enclosingNode = _ref10.enclosingNode,
372
+ text = _ref10.text;
373
+
374
+ // This is only needed for estree parsers (flow, typescript) to attach
375
+ // after a method name:
376
+ // obj = { fn /*comment*/() {} };
377
+ if (enclosingNode && precedingNode && ( // "MethodDefinition" is handled in getCommentChildNodes
378
+ enclosingNode.type === 'Property' || enclosingNode.type === 'TSDeclareMethod' || enclosingNode.type === 'TSAbstractMethodDefinition') && precedingNode.type === 'Identifier' && enclosingNode.key === precedingNode && // special Property case: { key: /*comment*/(value) };
379
+ // comment should be attached to value instead of key
380
+ getNextNonSpaceNonCommentCharacter(text, precedingNode, locEnd) !== ':') {
381
+ addTrailingComment(precedingNode, comment);
382
+ return true;
383
+ } // Print comments between decorators and class methods as a trailing comment
384
+ // on the decorator node instead of the method node
385
+
386
+
387
+ if (precedingNode && enclosingNode && precedingNode.type === 'Decorator' && (enclosingNode.type === 'ClassMethod' || enclosingNode.type === 'ClassProperty' || enclosingNode.type === 'PropertyDefinition' || enclosingNode.type === 'TSAbstractPropertyDefinition' || enclosingNode.type === 'TSAbstractMethodDefinition' || enclosingNode.type === 'TSDeclareMethod' || enclosingNode.type === 'MethodDefinition')) {
388
+ addTrailingComment(precedingNode, comment);
389
+ return true;
390
+ }
391
+
392
+ return false;
393
+ }
394
+
395
+ function handleFunctionNameComments(_ref11) {
396
+ var comment = _ref11.comment,
397
+ precedingNode = _ref11.precedingNode,
398
+ enclosingNode = _ref11.enclosingNode,
399
+ text = _ref11.text;
400
+
401
+ if (getNextNonSpaceNonCommentCharacter(text, comment, locEnd) !== '(') {
402
+ return false;
403
+ }
404
+
405
+ if (precedingNode && enclosingNode && (enclosingNode.type === 'FunctionDeclaration' || enclosingNode.type === 'FunctionExpression' || enclosingNode.type === 'ClassMethod' || enclosingNode.type === 'MethodDefinition' || enclosingNode.type === 'ObjectMethod')) {
406
+ addTrailingComment(precedingNode, comment);
407
+ return true;
408
+ }
409
+
410
+ return false;
411
+ }
412
+
413
+ function handleCommentAfterArrowParams(_ref12) {
414
+ var comment = _ref12.comment,
415
+ enclosingNode = _ref12.enclosingNode,
416
+ text = _ref12.text;
417
+
418
+ if (!(enclosingNode && enclosingNode.type === 'ArrowFunctionExpression')) {
419
+ return false;
420
+ }
421
+
422
+ var index = getNextNonSpaceNonCommentCharacterIndex(text, comment, locEnd);
423
+
424
+ if (index !== false && text.slice(index, index + 2) === '=>') {
425
+ addDanglingComment(enclosingNode, comment);
426
+ return true;
427
+ }
428
+
429
+ return false;
430
+ }
431
+
432
+ function handleCommentInEmptyParens(_ref13) {
433
+ var comment = _ref13.comment,
434
+ enclosingNode = _ref13.enclosingNode,
435
+ text = _ref13.text;
436
+
437
+ if (getNextNonSpaceNonCommentCharacter(text, comment, locEnd) !== ')') {
438
+ return false;
439
+ } // Only add dangling comments to fix the case when no params are present,
440
+ // i.e. a function without any argument.
441
+
442
+
443
+ if (enclosingNode && (isRealFunctionLikeNode(enclosingNode) && getFunctionParameters(enclosingNode).length === 0 || isCallLikeExpression(enclosingNode) && getCallArguments(enclosingNode).length === 0)) {
444
+ addDanglingComment(enclosingNode, comment);
445
+ return true;
446
+ }
447
+
448
+ if (enclosingNode && (enclosingNode.type === 'MethodDefinition' || enclosingNode.type === 'TSAbstractMethodDefinition') && getFunctionParameters(enclosingNode.value).length === 0) {
449
+ addDanglingComment(enclosingNode.value, comment);
450
+ return true;
451
+ }
452
+
453
+ return false;
454
+ }
455
+
456
+ function handleLastFunctionArgComments(_ref14) {
457
+ var comment = _ref14.comment,
458
+ precedingNode = _ref14.precedingNode,
459
+ enclosingNode = _ref14.enclosingNode,
460
+ followingNode = _ref14.followingNode,
461
+ text = _ref14.text;
462
+
463
+ // Flow function type definitions
464
+ if (precedingNode && precedingNode.type === 'FunctionTypeParam' && enclosingNode && enclosingNode.type === 'FunctionTypeAnnotation' && followingNode && followingNode.type !== 'FunctionTypeParam') {
465
+ addTrailingComment(precedingNode, comment);
466
+ return true;
467
+ } // Real functions and TypeScript function type definitions
468
+
469
+
470
+ if (precedingNode && (precedingNode.type === 'Identifier' || precedingNode.type === 'AssignmentPattern') && enclosingNode && isRealFunctionLikeNode(enclosingNode) && getNextNonSpaceNonCommentCharacter(text, comment, locEnd) === ')') {
471
+ addTrailingComment(precedingNode, comment);
472
+ return true;
473
+ }
474
+
475
+ if (enclosingNode && enclosingNode.type === 'FunctionDeclaration' && followingNode && followingNode.type === 'BlockStatement') {
476
+ var functionParamRightParenIndex = function () {
477
+ var parameters = getFunctionParameters(enclosingNode);
478
+
479
+ if (parameters.length > 0) {
480
+ return getNextNonSpaceNonCommentCharacterIndexWithStartIndex(text, locEnd(getLast(parameters)));
481
+ }
482
+
483
+ var functionParamLeftParenIndex = getNextNonSpaceNonCommentCharacterIndexWithStartIndex(text, locEnd(enclosingNode.id));
484
+ return functionParamLeftParenIndex !== false && getNextNonSpaceNonCommentCharacterIndexWithStartIndex(text, functionParamLeftParenIndex + 1);
485
+ }();
486
+
487
+ if (locStart(comment) > functionParamRightParenIndex) {
488
+ addBlockStatementFirstComment(followingNode, comment);
489
+ return true;
490
+ }
491
+ }
492
+
493
+ return false;
494
+ }
495
+
496
+ function handleImportSpecifierComments(_ref15) {
497
+ var comment = _ref15.comment,
498
+ enclosingNode = _ref15.enclosingNode;
499
+
500
+ if (enclosingNode && enclosingNode.type === 'ImportSpecifier') {
501
+ addLeadingComment(enclosingNode, comment);
502
+ return true;
503
+ }
504
+
505
+ return false;
506
+ }
507
+
508
+ function handleLabeledStatementComments(_ref16) {
509
+ var comment = _ref16.comment,
510
+ enclosingNode = _ref16.enclosingNode;
511
+
512
+ if (enclosingNode && enclosingNode.type === 'LabeledStatement') {
513
+ addLeadingComment(enclosingNode, comment);
514
+ return true;
515
+ }
516
+
517
+ return false;
518
+ }
519
+
520
+ function handleBreakAndContinueStatementComments(_ref17) {
521
+ var comment = _ref17.comment,
522
+ enclosingNode = _ref17.enclosingNode;
523
+
524
+ if (enclosingNode && (enclosingNode.type === 'ContinueStatement' || enclosingNode.type === 'BreakStatement') && !enclosingNode.label) {
525
+ addTrailingComment(enclosingNode, comment);
526
+ return true;
527
+ }
528
+
529
+ return false;
530
+ }
531
+
532
+ function handleCallExpressionComments(_ref18) {
533
+ var comment = _ref18.comment,
534
+ precedingNode = _ref18.precedingNode,
535
+ enclosingNode = _ref18.enclosingNode;
536
+
537
+ if (isCallExpression(enclosingNode) && precedingNode && enclosingNode.callee === precedingNode && enclosingNode.arguments.length > 0) {
538
+ addLeadingComment(enclosingNode.arguments[0], comment);
539
+ return true;
540
+ }
541
+
542
+ return false;
543
+ }
544
+
545
+ function handleUnionTypeComments(_ref19) {
546
+ var comment = _ref19.comment,
547
+ precedingNode = _ref19.precedingNode,
548
+ enclosingNode = _ref19.enclosingNode,
549
+ followingNode = _ref19.followingNode;
550
+
551
+ if (enclosingNode && (enclosingNode.type === 'UnionTypeAnnotation' || enclosingNode.type === 'TSUnionType')) {
552
+ if (isPrettierIgnoreComment(comment)) {
553
+ followingNode.prettierIgnore = true;
554
+ comment.unignore = true;
555
+ }
556
+
557
+ if (precedingNode) {
558
+ addTrailingComment(precedingNode, comment);
559
+ return true;
560
+ }
561
+
562
+ return false;
563
+ }
564
+
565
+ if (followingNode && (followingNode.type === 'UnionTypeAnnotation' || followingNode.type === 'TSUnionType') && isPrettierIgnoreComment(comment)) {
566
+ followingNode.types[0].prettierIgnore = true;
567
+ comment.unignore = true;
568
+ }
569
+
570
+ return false;
571
+ }
572
+
573
+ function handlePropertyComments(_ref20) {
574
+ var comment = _ref20.comment,
575
+ enclosingNode = _ref20.enclosingNode;
576
+
577
+ if (isObjectProperty(enclosingNode)) {
578
+ addLeadingComment(enclosingNode, comment);
579
+ return true;
580
+ }
581
+
582
+ return false;
583
+ }
584
+
585
+ function handleOnlyComments(_ref21) {
586
+ var comment = _ref21.comment,
587
+ enclosingNode = _ref21.enclosingNode,
588
+ followingNode = _ref21.followingNode,
589
+ ast = _ref21.ast,
590
+ isLastComment = _ref21.isLastComment;
591
+
592
+ // With Flow the enclosingNode is undefined so use the AST instead.
593
+ if (ast && ast.body && ast.body.length === 0) {
594
+ if (isLastComment) {
595
+ addDanglingComment(ast, comment);
596
+ } else {
597
+ addLeadingComment(ast, comment);
598
+ }
599
+
600
+ return true;
601
+ }
602
+
603
+ if (enclosingNode && enclosingNode.type === 'Program' && enclosingNode.body.length === 0 && !isNonEmptyArray(enclosingNode.directives)) {
604
+ if (isLastComment) {
605
+ addDanglingComment(enclosingNode, comment);
606
+ } else {
607
+ addLeadingComment(enclosingNode, comment);
608
+ }
609
+
610
+ return true;
611
+ }
612
+
613
+ if (followingNode && followingNode.type === 'Program' && followingNode.body.length === 0 && enclosingNode && enclosingNode.type === 'ModuleExpression') {
614
+ addDanglingComment(followingNode, comment);
615
+ return true;
616
+ }
617
+
618
+ return false;
619
+ }
620
+
621
+ function handleForComments(_ref22) {
622
+ var comment = _ref22.comment,
623
+ enclosingNode = _ref22.enclosingNode;
624
+
625
+ if (enclosingNode && (enclosingNode.type === 'ForInStatement' || enclosingNode.type === 'ForOfStatement')) {
626
+ addLeadingComment(enclosingNode, comment);
627
+ return true;
628
+ }
629
+
630
+ return false;
631
+ }
632
+
633
+ function handleImportDeclarationComments(_ref23) {
634
+ var comment = _ref23.comment,
635
+ precedingNode = _ref23.precedingNode,
636
+ enclosingNode = _ref23.enclosingNode,
637
+ text = _ref23.text;
638
+
639
+ if (precedingNode && precedingNode.type === 'ImportSpecifier' && enclosingNode && enclosingNode.type === 'ImportDeclaration' && hasNewline(text, locEnd(comment))) {
640
+ addTrailingComment(precedingNode, comment);
641
+ return true;
642
+ }
643
+
644
+ return false;
645
+ }
646
+
647
+ function handleAssignmentPatternComments(_ref24) {
648
+ var comment = _ref24.comment,
649
+ enclosingNode = _ref24.enclosingNode;
650
+
651
+ if (enclosingNode && enclosingNode.type === 'AssignmentPattern') {
652
+ addLeadingComment(enclosingNode, comment);
653
+ return true;
654
+ }
655
+
656
+ return false;
657
+ }
658
+
659
+ function handleTypeAliasComments(_ref25) {
660
+ var comment = _ref25.comment,
661
+ enclosingNode = _ref25.enclosingNode;
662
+
663
+ if (enclosingNode && enclosingNode.type === 'TypeAlias') {
664
+ addLeadingComment(enclosingNode, comment);
665
+ return true;
666
+ }
667
+
668
+ return false;
669
+ }
670
+
671
+ function handleVariableDeclaratorComments(_ref26) {
672
+ var comment = _ref26.comment,
673
+ enclosingNode = _ref26.enclosingNode,
674
+ followingNode = _ref26.followingNode;
675
+
676
+ if (enclosingNode && (enclosingNode.type === 'VariableDeclarator' || enclosingNode.type === 'AssignmentExpression') && followingNode && (followingNode.type === 'ObjectExpression' || followingNode.type === 'ArrayExpression' || followingNode.type === 'TemplateLiteral' || followingNode.type === 'TaggedTemplateExpression' || isBlockComment(comment))) {
677
+ addLeadingComment(followingNode, comment);
678
+ return true;
679
+ }
680
+
681
+ return false;
682
+ }
683
+
684
+ function handleTSFunctionTrailingComments(_ref27) {
685
+ var comment = _ref27.comment,
686
+ enclosingNode = _ref27.enclosingNode,
687
+ followingNode = _ref27.followingNode,
688
+ text = _ref27.text;
689
+
690
+ if (!followingNode && enclosingNode && (enclosingNode.type === 'TSMethodSignature' || enclosingNode.type === 'TSDeclareFunction' || enclosingNode.type === 'TSAbstractMethodDefinition') && getNextNonSpaceNonCommentCharacter(text, comment, locEnd) === ';') {
691
+ addTrailingComment(enclosingNode, comment);
692
+ return true;
693
+ }
694
+
695
+ return false;
696
+ }
697
+
698
+ function handleIgnoreComments(_ref28) {
699
+ var comment = _ref28.comment,
700
+ enclosingNode = _ref28.enclosingNode,
701
+ followingNode = _ref28.followingNode;
702
+
703
+ if (isPrettierIgnoreComment(comment) && enclosingNode && enclosingNode.type === 'TSMappedType' && followingNode && followingNode.type === 'TSTypeParameter' && followingNode.constraint) {
704
+ enclosingNode.prettierIgnore = true;
705
+ comment.unignore = true;
706
+ return true;
707
+ }
708
+ }
709
+
710
+ function handleTSMappedTypeComments(_ref29) {
711
+ var comment = _ref29.comment,
712
+ precedingNode = _ref29.precedingNode,
713
+ enclosingNode = _ref29.enclosingNode,
714
+ followingNode = _ref29.followingNode;
715
+
716
+ if (!enclosingNode || enclosingNode.type !== 'TSMappedType') {
717
+ return false;
718
+ }
719
+
720
+ if (followingNode && followingNode.type === 'TSTypeParameter' && followingNode.name) {
721
+ addLeadingComment(followingNode.name, comment);
722
+ return true;
723
+ }
724
+
725
+ if (precedingNode && precedingNode.type === 'TSTypeParameter' && precedingNode.constraint) {
726
+ addTrailingComment(precedingNode.constraint, comment);
727
+ return true;
728
+ }
729
+
730
+ return false;
731
+ }
732
+ /**
733
+ * @param {Node} node
734
+ * @returns {boolean}
735
+ */
736
+
737
+
738
+ function isRealFunctionLikeNode(node) {
739
+ return node.type === 'ArrowFunctionExpression' || node.type === 'FunctionExpression' || node.type === 'FunctionDeclaration' || node.type === 'ObjectMethod' || node.type === 'ClassMethod' || node.type === 'TSDeclareFunction' || node.type === 'TSCallSignatureDeclaration' || node.type === 'TSConstructSignatureDeclaration' || node.type === 'TSMethodSignature' || node.type === 'TSConstructorType' || node.type === 'TSFunctionType' || node.type === 'TSDeclareMethod';
740
+ }
741
+ /**
742
+ * @param {any} node
743
+ * @returns {Node[] | void}
744
+ */
745
+
746
+
747
+ function getCommentChildNodes(node, options) {
748
+ // Prevent attaching comments to FunctionExpression in this case:
749
+ // class Foo {
750
+ // bar() // comment
751
+ // {
752
+ // baz();
753
+ // }
754
+ // }
755
+ if ((options.parser === 'typescript' || options.parser === 'flow' || options.parser === 'espree' || options.parser === 'meriyah' || options.parser === '__babel_estree') && node.type === 'MethodDefinition' && node.value && node.value.type === 'FunctionExpression' && getFunctionParameters(node.value).length === 0 && !node.value.returnType && !isNonEmptyArray(node.value.typeParameters) && node.value.body) {
756
+ return [].concat(_toConsumableArray(node.decorators || []), [node.key, node.value.body]);
757
+ }
758
+ }
759
+ /**
760
+ * @param {Comment} comment
761
+ * @returns {boolean}
762
+ */
763
+
764
+
765
+ function isTypeCastComment(comment) {
766
+ return isBlockComment(comment) && comment.value[0] === '*' && // TypeScript expects the type to be enclosed in curly brackets, however
767
+ // Closure Compiler accepts types in parens and even without any delimiters at all.
768
+ // That's why we just search for "@type".
769
+ /@type\b/.test(comment.value);
770
+ }
771
+
772
+ module.exports = {
773
+ handleOwnLineComment: handleOwnLineComment,
774
+ handleEndOfLineComment: handleEndOfLineComment,
775
+ handleRemainingComment: handleRemainingComment,
776
+ getCommentChildNodes: getCommentChildNodes
777
+ };