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.
@@ -9,9 +9,11 @@
9
9
  */
10
10
 
11
11
  import type {
12
+ ClassMember,
12
13
  Comment,
13
14
  ESNode,
14
15
  Expression,
16
+ FunctionParameter,
15
17
  ModuleDeclaration,
16
18
  Statement,
17
19
  TypeAnnotationType,
@@ -24,10 +26,12 @@ import type {AddTrailingCommentsMutation} from './mutations/AddTrailingComments'
24
26
  import type {CloneCommentsToMutation} from './mutations/CloneCommentsTo';
25
27
  import type {InsertStatementMutation} from './mutations/InsertStatement';
26
28
  import type {RemoveCommentMutation} from './mutations/RemoveComment';
29
+ import type {RemoveNodeMutation} from './mutations/RemoveNode';
27
30
  import type {RemoveStatementMutation} from './mutations/RemoveStatement';
28
31
  import type {ReplaceNodeMutation} from './mutations/ReplaceNode';
29
32
  import type {ReplaceStatementWithManyMutation} from './mutations/ReplaceStatementWithMany';
30
33
 
34
+ import {codeFrameColumns} from '@babel/code-frame';
31
35
  import {deepCloneNode, shallowCloneNode} from '../detachedNode';
32
36
  import {
33
37
  getCommentsForNode,
@@ -39,6 +43,7 @@ import {createAddTrailingCommentsMutation} from './mutations/AddTrailingComments
39
43
  import {createCloneCommentsToMutation} from './mutations/CloneCommentsTo';
40
44
  import {createInsertStatementMutation} from './mutations/InsertStatement';
41
45
  import {createRemoveCommentMutation} from './mutations/RemoveComment';
46
+ import {createRemoveNodeMutation} from './mutations/RemoveNode';
42
47
  import {createRemoveStatementMutation} from './mutations/RemoveStatement';
43
48
  import {createReplaceNodeMutation} from './mutations/ReplaceNode';
44
49
  import {createReplaceStatementWithManyMutation} from './mutations/ReplaceStatementWithMany';
@@ -49,6 +54,7 @@ type Mutation = $ReadOnly<
49
54
  | CloneCommentsToMutation
50
55
  | InsertStatementMutation
51
56
  | RemoveCommentMutation
57
+ | RemoveNodeMutation
52
58
  | RemoveStatementMutation
53
59
  | ReplaceNodeMutation
54
60
  | ReplaceStatementWithManyMutation,
@@ -65,12 +71,10 @@ type ReplaceNodeOptions = $ReadOnly<{
65
71
  keepComments?: boolean,
66
72
  }>;
67
73
 
68
- export type TransformContext = $ReadOnly<{
69
- mutations: $ReadOnlyArray<Mutation>,
70
- astWasMutated: boolean,
71
-
74
+ type TransformCloneAPIs = $ReadOnly<{
72
75
  /**
73
- * Shallowly clones the given node and applies the given overrides.
76
+ * Shallowly clones the given node.
77
+ *
74
78
  * !!! Be careful about using this !!!
75
79
  * This does not clone children nodes. This means that if you keep
76
80
  * the original node in the AST then you will have two trees in the
@@ -84,12 +88,17 @@ export type TransformContext = $ReadOnly<{
84
88
  * If you want to literally duplicate a node to place somewhere else
85
89
  * in the AST, then use `deepCloneNode` instead.
86
90
  */
87
- shallowCloneNode: TransformCloneSignatures & {
88
- // generic passthrough cases with no options
91
+ shallowCloneNode: {
89
92
  <T: ESNode>(node: T): DetachedNode<T>,
90
93
  <T: ESNode>(node: ?T): DetachedNode<T> | null,
91
94
  },
92
95
 
96
+ /**
97
+ * Shallowly clones the given node and applies the given overrides.
98
+ * {@see shallowCloneNode}
99
+ */
100
+ shallowCloneNodeWithOverrides: TransformCloneSignatures,
101
+
93
102
  /**
94
103
  * {@see shallowCloneNode}
95
104
  */
@@ -99,25 +108,83 @@ export type TransformContext = $ReadOnly<{
99
108
  },
100
109
 
101
110
  /**
102
- * Deeply clones the node and all its children, then applies the
103
- * given overrides.
111
+ * Deeply clones the node and all its children.
112
+ *
104
113
  * !!! Be careful about using this !!!
105
114
  * Because this is a deep clone, using it high up in the AST can
106
115
  * result in a lot of work being done.
107
116
  */
108
- deepCloneNode: TransformCloneSignatures & {
109
- // generic passthrough cases with no options
117
+ deepCloneNode: {
110
118
  <T: ESNode>(node: T): DetachedNode<T>,
111
119
  <T: ESNode>(node: ?T): DetachedNode<T> | null,
112
120
  },
113
121
 
122
+ /**
123
+ * Deeply clones the node and all its children, then applies the
124
+ * given overrides.
125
+ * {@see deepCloneNode}
126
+ */
127
+ deepCloneNodeWithOverrides: TransformCloneSignatures,
128
+ }>;
129
+
130
+ type TransformCommentAPIs = $ReadOnly<{
131
+ /**
132
+ * Gets all of the comments attached to the given node.
133
+ */
134
+ getComments: (node: ESNode) => Array<Comment>,
135
+
136
+ /**
137
+ * Gets the leading comments attached to the given node.
138
+ */
139
+ getLeadingComments: (node: ESNode) => Array<Comment>,
140
+
141
+ /**
142
+ * Gets the trailing comments attached to the given node.
143
+ */
144
+ getTrailingComments: (node: ESNode) => Array<Comment>,
145
+
146
+ /**
147
+ * Clones all of the comments from the target node to the destination node.
148
+ * As its name suggest - this will clone the comments, duplicating them
149
+ * entirely. It will not remove the comments from the target node afterward.
150
+ */
151
+ cloneCommentsTo: (
152
+ target: ESNode,
153
+ destination: ESNode | DetachedNode<ESNode>,
154
+ ) => void,
155
+
156
+ /**
157
+ * Add leading comments to the specified node.
158
+ */
159
+ addLeadingComments: (
160
+ node: ESNode | DetachedNode<ESNode>,
161
+ comments: SingleOrArray<Comment>,
162
+ ) => void,
163
+
164
+ /**
165
+ * Add trailing comments to the specified node.
166
+ */
167
+ addTrailingComments: (
168
+ node: ESNode | DetachedNode<ESNode>,
169
+ comments: SingleOrArray<Comment>,
170
+ ) => void,
171
+
172
+ /**
173
+ * Removes the specified comments
174
+ */
175
+ removeComments: (comments: SingleOrArray<Comment>) => void,
176
+ }>;
177
+
178
+ type TransformInsertAPIs = $ReadOnly<{
114
179
  /**
115
180
  * Insert `nodeToInsert` after the `target` statement.
116
181
  * The inserted nodes will be kept in the order given.
117
182
  */
118
183
  insertAfterStatement: (
119
- target: ModuleDeclaration | Statement,
120
- nodeToInsert: SingleOrArray<DetachedNode<ModuleDeclaration | Statement>>,
184
+ target: InsertStatementMutation['target'],
185
+ nodeToInsert: SingleOrArray<
186
+ DetachedNode<InsertStatementMutation['target']>,
187
+ >,
121
188
  ) => void,
122
189
 
123
190
  /**
@@ -125,39 +192,69 @@ export type TransformContext = $ReadOnly<{
125
192
  * The inserted nodes will be kept in the order given.
126
193
  */
127
194
  insertBeforeStatement: (
128
- target: ModuleDeclaration | Statement,
129
- nodeToInsert: SingleOrArray<DetachedNode<ModuleDeclaration | Statement>>,
195
+ target: InsertStatementMutation['target'],
196
+ nodeToInsert: SingleOrArray<
197
+ DetachedNode<InsertStatementMutation['target']>,
198
+ >,
130
199
  ) => void,
200
+ }>;
131
201
 
202
+ type TransformRemoveAPIs = $ReadOnly<{
203
+ /**
204
+ * Removes a given node from the AST.
205
+ * The set of thigns that can be removed is intentionally restricted by types.
206
+ * This represents the set of "misc nodes" that are known to be safe to remove without outright breaking the AST.
207
+ */
208
+ removeNode: (node: RemoveNodeMutation['node']) => void,
209
+
210
+ /**
211
+ * Removes a given statement from the AST.
212
+ */
213
+ removeStatement: (node: RemoveStatementMutation['node']) => void,
214
+ }>;
215
+
216
+ type TransformReplaceAPIs = $ReadOnly<{
132
217
  /**
133
218
  * Replace the `target` node with the `nodeToReplaceWith` node.
134
219
  * This simply does an in-place replacement in the AST.
135
220
  */
136
221
  replaceNode: {
137
- // expressions must be replaced with other expressions
222
+ // Expressions may be replaced with other expressions
138
223
  (
139
224
  target: Expression,
140
225
  nodeToReplaceWith: DetachedNode<Expression>,
141
226
  options?: ReplaceNodeOptions,
142
227
  ): void,
143
- // module declarations must be replaced with statements or other module declarations
228
+ // Module declarations may be replaced with statements or other module declarations
144
229
  (
145
230
  target: ModuleDeclaration,
146
231
  nodeToReplaceWith: DetachedNode<ModuleDeclaration | Statement>,
147
232
  options?: ReplaceNodeOptions,
148
233
  ): void,
149
- // Statement must be replaced with statements or module declarations
234
+ // Statement maybe be replaced with statements or module declarations
150
235
  (
151
236
  target: Statement,
152
237
  nodeToReplaceWith: DetachedNode<ModuleDeclaration | Statement>,
153
238
  options?: ReplaceNodeOptions,
154
239
  ): void,
155
- // Types must be replaced with types
240
+ // Types maybe be replaced with other types
156
241
  (
157
242
  target: TypeAnnotationType,
158
243
  nodeToReplaceWith: DetachedNode<TypeAnnotationType>,
159
244
  options?: ReplaceNodeOptions,
160
245
  ): void,
246
+ // Class members may be replaced with other class members
247
+ (
248
+ target: ClassMember,
249
+ nodeToReplaceWith: DetachedNode<ClassMember>,
250
+ options?: ReplaceNodeOptions,
251
+ ): void,
252
+ // Function params amy be replace with other function params
253
+ (
254
+ target: FunctionParameter,
255
+ nodeToReplaceWith: DetachedNode<FunctionParameter>,
256
+ options?: ReplaceNodeOptions,
257
+ ): void,
161
258
  } & TransformReplaceSignatures, // allow like-for-like replacements as well
162
259
 
163
260
  /**
@@ -165,10 +262,8 @@ export type TransformContext = $ReadOnly<{
165
262
  * The nodes will be kept in the order given.
166
263
  */
167
264
  replaceStatementWithMany: (
168
- target: ModuleDeclaration | Statement,
169
- nodesToReplaceWith: $ReadOnlyArray<
170
- DetachedNode<ModuleDeclaration | Statement>,
171
- >,
265
+ target: ReplaceStatementWithManyMutation['target'],
266
+ nodesToReplaceWith: ReplaceStatementWithManyMutation['nodesToReplaceWith'],
172
267
  options?: {
173
268
  /**
174
269
  * Moves the comments from the target node to the first node in the array.
@@ -178,64 +273,45 @@ export type TransformContext = $ReadOnly<{
178
273
  keepComments?: boolean,
179
274
  },
180
275
  ) => void,
276
+ }>;
181
277
 
182
- /**
183
- * Removes a given node from the AST.
184
- */
185
- removeStatement: (node: ModuleDeclaration | Statement) => void,
186
-
187
- //
188
- // Comment APIs
189
- //
190
-
191
- /**
192
- * Gets all of the comments attached to the given node.
193
- */
194
- getComments: (node: ESNode) => Array<Comment>,
195
-
196
- /**
197
- * Gets the leading comments attached to the given node.
198
- */
199
- getLeadingComments: (node: ESNode) => Array<Comment>,
200
-
201
- /**
202
- * Gets the trailing comments attached to the given node.
203
- */
204
- getTrailingComments: (node: ESNode) => Array<Comment>,
205
-
206
- /**
207
- * Clones all of the comments from the target node to the destination node.
208
- * As its name suggest - this will clone the comments, duplicating them
209
- * entirely. It will not remove the comments from the target node afterward.
210
- */
211
- cloneCommentsTo: (
212
- target: ESNode,
213
- destination: ESNode | DetachedNode<ESNode>,
214
- ) => void,
278
+ export type TransformContext = $ReadOnly<{
279
+ mutations: $ReadOnlyArray<Mutation>,
280
+ astWasMutated: boolean,
215
281
 
216
282
  /**
217
- * Add leading comments to the specified node.
283
+ * Creates a full code frame for the node along with the message.
284
+ *
285
+ * i.e. `context.buildCodeFrame(node, 'foo')` will create a string like:
286
+ * ```
287
+ * 56 | function () {
288
+ * | ^^^^^^^^^^^^^
289
+ * 57 | }.bind(this)
290
+ * | ^^ foo
291
+ * ```
218
292
  */
219
- addLeadingComments: (
220
- node: ESNode | DetachedNode<ESNode>,
221
- comments: SingleOrArray<Comment>,
222
- ) => void,
293
+ buildCodeFrame: (node: ESNode, message: string) => string,
223
294
 
224
295
  /**
225
- * Add trailing comments to the specified node.
296
+ * Creates a simple code frame for the node along with the message.
297
+ * Use this if you want a condensed marker for your message.
298
+ *
299
+ * i.e. `context.logWithNode(node, 'foo')` will create a string like:
300
+ * ```
301
+ * [FunctionExpression:56:44] foo
302
+ * ```
303
+ * (where 56:44 represents L56, Col44)
226
304
  */
227
- addTrailingComments: (
228
- node: ESNode | DetachedNode<ESNode>,
229
- comments: SingleOrArray<Comment>,
230
- ) => void,
305
+ buildSimpleCodeFrame: (node: ESNode, message: string) => string,
231
306
 
232
- /**
233
- * Removes the specified comments
234
- */
235
- removeComments: (comments: SingleOrArray<Comment>) => void,
307
+ ...TransformCommentAPIs,
308
+ ...TransformCloneAPIs,
309
+ ...TransformInsertAPIs,
310
+ ...TransformRemoveAPIs,
311
+ ...TransformReplaceAPIs,
236
312
  }>;
237
313
 
238
- export function getTransformContext(): TransformContext {
314
+ export function getTransformContext(code: string): TransformContext {
239
315
  /**
240
316
  * The mutations in order of collection.
241
317
  */
@@ -246,27 +322,31 @@ export function getTransformContext(): TransformContext {
246
322
  }
247
323
  }
248
324
 
249
- return {
250
- mutations,
325
+ const cloneAPIs: TransformCloneAPIs = {
326
+ // $FlowExpectedError[incompatible-exact]
327
+ shallowCloneNode: ((
328
+ node: ?ESNode,
329
+ ): // $FlowExpectedError[incompatible-cast]
330
+ ?DetachedNode<ESNode> => {
331
+ if (node == null) {
332
+ return null;
333
+ }
251
334
 
252
- // $FlowExpectedError[unsafe-getters-setters]
253
- get astWasMutated(): boolean {
254
- return mutations.length > 0;
255
- },
335
+ return shallowCloneNode(node);
336
+ }: TransformCloneAPIs['shallowCloneNode']),
256
337
 
257
338
  // $FlowExpectedError[incompatible-exact]
258
- shallowCloneNode: ((
339
+ shallowCloneNodeWithOverrides: ((
259
340
  node: ?ESNode,
260
341
  newProps?: $ReadOnly<{...}>,
261
342
  ): // $FlowExpectedError[incompatible-cast]
262
- // $FlowExpectedError[prop-missing]
263
343
  ?DetachedNode<ESNode> => {
264
344
  if (node == null) {
265
345
  return null;
266
346
  }
267
347
 
268
348
  return shallowCloneNode(node, newProps);
269
- }: TransformContext['shallowCloneNode']),
349
+ }: TransformCloneAPIs['shallowCloneNodeWithOverrides']),
270
350
 
271
351
  shallowCloneArray: (<T: ESNode>(
272
352
  nodes: ?$ReadOnlyArray<T>,
@@ -277,34 +357,87 @@ export function getTransformContext(): TransformContext {
277
357
  }
278
358
 
279
359
  return nodes.map(node => shallowCloneNode<T>(node));
280
- }: TransformContext['shallowCloneArray']),
360
+ }: TransformCloneAPIs['shallowCloneArray']),
281
361
 
282
362
  // $FlowExpectedError[incompatible-exact]
283
363
  deepCloneNode: ((
284
364
  node: ?ESNode,
365
+ ): // $FlowExpectedError[incompatible-cast]
366
+ ?DetachedNode<ESNode> => {
367
+ if (node == null) {
368
+ return null;
369
+ }
370
+
371
+ return deepCloneNode(node);
372
+ }: TransformCloneAPIs['deepCloneNode']),
373
+
374
+ // $FlowExpectedError[incompatible-exact]
375
+ deepCloneNodeWithOverrides: ((
376
+ node: ?ESNode,
285
377
  newProps?: $ReadOnly<{...}>,
286
378
  ): // $FlowExpectedError[incompatible-cast]
287
- // $FlowExpectedError[prop-missing]
288
379
  ?DetachedNode<ESNode> => {
289
380
  if (node == null) {
290
381
  return null;
291
382
  }
292
383
 
293
384
  return deepCloneNode(node, newProps);
294
- }: TransformContext['deepCloneNode']),
385
+ }: TransformCloneAPIs['deepCloneNodeWithOverrides']),
386
+ };
387
+ const commentAPIs: TransformCommentAPIs = {
388
+ getComments: ((node): Array<Comment> => {
389
+ return [...getCommentsForNode(node)];
390
+ }: TransformCommentAPIs['getComments']),
391
+
392
+ getLeadingComments: ((node): Array<Comment> => {
393
+ return getCommentsForNode(node).filter(isLeadingComment);
394
+ }: TransformCommentAPIs['getLeadingComments']),
395
+
396
+ getTrailingComments: ((node): Array<Comment> => {
397
+ return getCommentsForNode(node).filter(isTrailingComment);
398
+ }: TransformCommentAPIs['getTrailingComments']),
399
+
400
+ cloneCommentsTo: ((target, destination): void => {
401
+ pushMutation(createCloneCommentsToMutation(target, destination));
402
+ }: TransformCommentAPIs['cloneCommentsTo']),
295
403
 
404
+ addLeadingComments: ((node, comments): void => {
405
+ pushMutation(createAddLeadingCommentsMutation(node, toArray(comments)));
406
+ }: TransformCommentAPIs['addLeadingComments']),
407
+
408
+ addTrailingComments: ((node, comments): void => {
409
+ pushMutation(createAddTrailingCommentsMutation(node, toArray(comments)));
410
+ }: TransformCommentAPIs['addTrailingComments']),
411
+
412
+ removeComments: ((comments): void => {
413
+ toArray(comments).forEach(comment => {
414
+ pushMutation(createRemoveCommentMutation(comment));
415
+ });
416
+ }: TransformCommentAPIs['removeComments']),
417
+ };
418
+ const insertAPIs: TransformInsertAPIs = {
296
419
  insertAfterStatement: ((target, nodesToInsert): void => {
297
420
  pushMutation(
298
421
  createInsertStatementMutation('after', target, toArray(nodesToInsert)),
299
422
  );
300
- }: TransformContext['insertBeforeStatement']),
423
+ }: TransformInsertAPIs['insertBeforeStatement']),
301
424
 
302
425
  insertBeforeStatement: ((target, nodesToInsert): void => {
303
426
  pushMutation(
304
427
  createInsertStatementMutation('before', target, toArray(nodesToInsert)),
305
428
  );
306
- }: TransformContext['insertBeforeStatement']),
429
+ }: TransformInsertAPIs['insertBeforeStatement']),
430
+ };
431
+ const removeAPIs: TransformRemoveAPIs = {
432
+ removeNode: ((node): void => {
433
+ pushMutation(createRemoveNodeMutation(node));
434
+ }: TransformRemoveAPIs['removeNode']),
307
435
 
436
+ removeStatement: ((node): void => {
437
+ pushMutation(createRemoveStatementMutation(node));
438
+ }: TransformRemoveAPIs['removeStatement']),
439
+ };
440
+ const replaceAPIs: TransformReplaceAPIs = {
308
441
  replaceNode: ((
309
442
  target: ESNode,
310
443
  nodeToReplaceWith: DetachedNode<ESNode>,
@@ -313,7 +446,7 @@ export function getTransformContext(): TransformContext {
313
446
  pushMutation(
314
447
  createReplaceNodeMutation(target, nodeToReplaceWith, options),
315
448
  );
316
- }: TransformContext['replaceNode']),
449
+ }: TransformReplaceAPIs['replaceNode']),
317
450
 
318
451
  replaceStatementWithMany: ((
319
452
  target,
@@ -327,45 +460,46 @@ export function getTransformContext(): TransformContext {
327
460
  options,
328
461
  ),
329
462
  );
330
- }: TransformContext['replaceStatementWithMany']),
331
-
332
- removeStatement: ((node): void => {
333
- pushMutation(createRemoveStatementMutation(node));
334
- }: TransformContext['removeStatement']),
335
-
336
- //
337
- // Comment APIs
338
- //
339
-
340
- getComments: ((node): Array<Comment> => {
341
- return [...getCommentsForNode(node)];
342
- }: TransformContext['getComments']),
343
-
344
- getLeadingComments: ((node): Array<Comment> => {
345
- return getCommentsForNode(node).filter(isLeadingComment);
346
- }: TransformContext['getLeadingComments']),
463
+ }: TransformReplaceAPIs['replaceStatementWithMany']),
464
+ };
347
465
 
348
- getTrailingComments: ((node): Array<Comment> => {
349
- return getCommentsForNode(node).filter(isTrailingComment);
350
- }: TransformContext['getTrailingComments']),
466
+ return {
467
+ mutations,
351
468
 
352
- cloneCommentsTo: ((target, destination): void => {
353
- pushMutation(createCloneCommentsToMutation(target, destination));
354
- }: TransformContext['cloneCommentsTo']),
469
+ // $FlowExpectedError[unsafe-getters-setters]
470
+ get astWasMutated(): boolean {
471
+ return mutations.length > 0;
472
+ },
355
473
 
356
- addLeadingComments: ((node, comments): void => {
357
- pushMutation(createAddLeadingCommentsMutation(node, toArray(comments)));
358
- }: TransformContext['addLeadingComments']),
474
+ buildCodeFrame: (node: ESNode, message: string): string => {
475
+ // babel uses 1-indexed columns
476
+ const locForBabel = {
477
+ start: {
478
+ line: node.loc.start.line,
479
+ column: node.loc.start.column + 1,
480
+ },
481
+ end: {
482
+ line: node.loc.end.line,
483
+ column: node.loc.end.column + 1,
484
+ },
485
+ };
486
+ return codeFrameColumns(code, locForBabel, {
487
+ linesAbove: 0,
488
+ linesBelow: 0,
489
+ highlightCode: process.env.NODE_ENV !== 'test',
490
+ message: message,
491
+ });
492
+ },
359
493
 
360
- addTrailingComments: ((node, comments): void => {
361
- pushMutation(createAddTrailingCommentsMutation(node, toArray(comments)));
362
- }: TransformContext['addTrailingComments']),
494
+ buildSimpleCodeFrame: (node: ESNode, message: string): string => {
495
+ return `[${node.type}:${node.loc.start.line}:${node.loc.start.column}] ${message}`;
496
+ },
363
497
 
364
- removeComments: ((comments): void => {
365
- toArray(comments).forEach(comment => {
366
- pushMutation(createRemoveCommentMutation(comment));
367
- });
368
- }: TransformContext['removeComments']),
498
+ ...cloneAPIs,
499
+ ...commentAPIs,
500
+ ...insertAPIs,
501
+ ...removeAPIs,
502
+ ...replaceAPIs,
369
503
  };
370
504
  }
371
505
 
@@ -24,25 +24,25 @@ var _printerEstree = _interopRequireDefault(require("./prettier/language-js/prin
24
24
 
25
25
  var _util = require("./prettier/common/util");
26
26
 
27
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
28
-
29
- function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
30
-
31
- 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."); }
32
-
33
- 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); }
34
-
35
- function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
36
-
37
- function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
38
-
39
- 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; }
40
-
27
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
28
+
29
+ /**
30
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
31
+ *
32
+ * This source code is licensed under the MIT license found in the
33
+ * LICENSE file in the root directory of this source tree.
34
+ *
35
+ *
36
+ * @format
37
+ */
38
+ // $FlowExpectedError[untyped-import]
39
+ // $FlowExpectedError[untyped-import]
40
+ // $FlowExpectedError[untyped-import]
41
41
  function attachComments(comments, ast, text) {
42
42
  (0, _comments.attach)(comments, ast, text, {
43
43
  locStart: _loc.locStart,
44
44
  locEnd: _loc.locEnd,
45
- printer: _printerEstree["default"]
45
+ printer: _printerEstree.default
46
46
  });
47
47
  }
48
48
 
@@ -57,19 +57,19 @@ function setCommentsOnNode(node, comments) {
57
57
  }
58
58
 
59
59
  function addCommentsToNode(node, comments) {
60
- var _node$comments, _ref;
60
+ var _node$comments;
61
61
 
62
62
  // $FlowExpectedError - this property is secretly added by prettier.
63
- node.comments = (_node$comments = node.comments) !== null && _node$comments !== void 0 ? _node$comments : []; // $FlowExpectedError
63
+ node.comments = (_node$comments = node.comments) != null ? _node$comments : []; // $FlowExpectedError
64
64
 
65
- (_ref = node.comments).push.apply(_ref, _toConsumableArray(comments));
65
+ node.comments.push(...comments);
66
66
  }
67
67
 
68
68
  function getCommentsForNode(node) {
69
69
  var _node$comments2;
70
70
 
71
71
  // $FlowExpectedError - this property is secretly added by prettier.
72
- return (_node$comments2 = node.comments) !== null && _node$comments2 !== void 0 ? _node$comments2 : [];
72
+ return (_node$comments2 = node.comments) != null ? _node$comments2 : [];
73
73
  }
74
74
 
75
75
  function isLeadingComment(comment) {
@@ -117,23 +117,23 @@ function appendCommentToSource(code, comment) {
117
117
  // https://github.com/prettier/prettier/blob/5f0ee39fa03532c85bd1c35291450fe7ac3667b3/src/language-js/print/comment.js#L15-L20
118
118
  // this means that we need to have any appended comments directly in the
119
119
  // source code or else prettier will slice nothing and bork up the transform
120
- var commentText = comment.value;
120
+ let commentText = comment.value;
121
121
 
122
122
  switch (comment.type) {
123
123
  case 'Block':
124
- commentText = "/*".concat(commentText, "*/");
124
+ commentText = `/*${commentText}*/`;
125
125
  break;
126
126
 
127
127
  case 'Line':
128
- commentText = "//".concat(commentText);
128
+ commentText = `//${commentText}`;
129
129
  break;
130
130
  }
131
131
 
132
- var newCode = code;
132
+ let newCode = code;
133
133
  newCode += '\n';
134
- var start = newCode.length;
134
+ const start = newCode.length;
135
135
  newCode += commentText;
136
- var end = newCode.length; // $FlowExpectedError[cannot-write]
136
+ const end = newCode.length; // $FlowExpectedError[cannot-write]
137
137
 
138
138
  comment.range = [start, end];
139
139
  return newCode;
@@ -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.