hermes-transform 0.36.0 → 0.37.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/detachedNode.js +3 -57
- package/dist/detachedNode.js.flow +12 -12
- package/dist/generated/TransformCloneSignatures.js.flow +1 -1
- package/dist/generated/TransformModifySignatures.js.flow +1 -1
- package/dist/generated/TransformReplaceSignatures.js.flow +207 -207
- package/dist/generated/node-types.js +0 -21
- package/dist/generated/node-types.js.flow +1086 -978
- package/dist/generated/special-case-node-types/Comment.js +0 -13
- package/dist/generated/special-case-node-types/Comment.js.flow +2 -2
- package/dist/generated/special-case-node-types/DeclareExportDeclaration.js +0 -12
- package/dist/generated/special-case-node-types/DeclareExportDeclaration.js.flow +10 -10
- package/dist/generated/special-case-node-types/DeclareHook.js +0 -9
- package/dist/generated/special-case-node-types/DeclareHook.js.flow +5 -5
- package/dist/generated/special-case-node-types/ExportNamedDeclaration.js +0 -11
- package/dist/generated/special-case-node-types/ExportNamedDeclaration.js.flow +9 -9
- package/dist/generated/special-case-node-types/Literal.js +1 -12
- package/dist/generated/special-case-node-types/Literal.js.flow +20 -20
- package/dist/generated/special-case-node-types/ObjectTypeProperty.js +0 -10
- package/dist/generated/special-case-node-types/ObjectTypeProperty.js.flow +23 -21
- package/dist/generated/special-case-node-types/Property.js +0 -9
- package/dist/generated/special-case-node-types/Property.js.flow +50 -42
- package/dist/generated/special-case-node-types/misc.js +6 -35
- package/dist/generated/special-case-node-types/misc.js.flow +56 -48
- package/dist/index.js +0 -9
- package/dist/src/detachedNode.js +3 -57
- package/dist/src/generated/node-types.js +0 -21
- package/dist/src/generated/special-case-node-types/Comment.js +0 -13
- package/dist/src/generated/special-case-node-types/DeclareExportDeclaration.js +0 -12
- package/dist/src/generated/special-case-node-types/DeclareHook.js +0 -9
- package/dist/src/generated/special-case-node-types/ExportNamedDeclaration.js +0 -11
- package/dist/src/generated/special-case-node-types/Literal.js +1 -12
- package/dist/src/generated/special-case-node-types/ObjectTypeProperty.js +0 -10
- package/dist/src/generated/special-case-node-types/Property.js +0 -9
- package/dist/src/generated/special-case-node-types/misc.js +6 -35
- package/dist/src/index.js +0 -9
- package/dist/src/transform/Errors.js +0 -9
- package/dist/src/transform/MutationContext.js +1 -28
- package/dist/src/transform/TransformContext.js +0 -18
- package/dist/src/transform/comments/comments.js +12 -78
- package/dist/src/transform/comments/prettier/common/util.js +4 -133
- package/dist/src/transform/comments/prettier/language-js/comments.js +12 -137
- package/dist/src/transform/comments/prettier/language-js/loc.js +1 -13
- package/dist/src/transform/comments/prettier/language-js/printer-estree.js +1 -10
- package/dist/src/transform/comments/prettier/language-js/utils.js +3 -33
- package/dist/src/transform/comments/prettier/main/comments.js +11 -77
- package/dist/src/transform/comments/prettier/utils/get-last.js +0 -8
- package/dist/src/transform/mutations/AddComments.js +0 -9
- package/dist/src/transform/mutations/CloneCommentsTo.js +0 -9
- package/dist/src/transform/mutations/InsertStatement.js +4 -16
- package/dist/src/transform/mutations/ModifyNodeInPlace.js +4 -17
- package/dist/src/transform/mutations/RemoveComment.js +0 -10
- package/dist/src/transform/mutations/RemoveNode.js +1 -23
- package/dist/src/transform/mutations/RemoveStatement.js +0 -16
- package/dist/src/transform/mutations/ReplaceNode.js +2 -16
- package/dist/src/transform/mutations/ReplaceStatementWithMany.js +2 -14
- package/dist/src/transform/mutations/utils/getStatementParent.js +3 -19
- package/dist/src/transform/mutations/utils/isValidModuleDeclarationParent.js +5 -20
- package/dist/src/transform/parse.js +6 -25
- package/dist/src/transform/print.js +9 -35
- package/dist/src/transform/transform.js +0 -9
- package/dist/src/transform/transformAST.js +3 -31
- package/dist/src/traverse/NodeEventGenerator.js +2 -108
- package/dist/src/traverse/SafeEmitter.js +0 -19
- package/dist/src/traverse/esquery.js +0 -18
- package/dist/src/traverse/traverse.js +1 -22
- package/dist/transform/Errors.js +0 -9
- package/dist/transform/MutationContext.js +1 -28
- package/dist/transform/MutationContext.js.flow +3 -3
- package/dist/transform/TransformContext.js +0 -18
- package/dist/transform/TransformContext.js.flow +60 -54
- package/dist/transform/comments/comments.js +12 -78
- package/dist/transform/comments/comments.js.flow +8 -8
- package/dist/transform/comments/prettier/common/util.js +4 -133
- package/dist/transform/comments/prettier/language-js/comments.js +12 -137
- package/dist/transform/comments/prettier/language-js/loc.js +1 -13
- package/dist/transform/comments/prettier/language-js/printer-estree.js +1 -10
- package/dist/transform/comments/prettier/language-js/utils.js +3 -33
- package/dist/transform/comments/prettier/main/comments.js +11 -77
- package/dist/transform/comments/prettier/utils/get-last.js +0 -8
- package/dist/transform/mutations/AddComments.js +0 -9
- package/dist/transform/mutations/AddComments.js.flow +2 -2
- package/dist/transform/mutations/CloneCommentsTo.js +0 -9
- package/dist/transform/mutations/CloneCommentsTo.js.flow +1 -1
- package/dist/transform/mutations/InsertStatement.js +4 -16
- package/dist/transform/mutations/InsertStatement.js.flow +7 -6
- package/dist/transform/mutations/ModifyNodeInPlace.js +4 -17
- package/dist/transform/mutations/ModifyNodeInPlace.js.flow +2 -2
- package/dist/transform/mutations/RemoveComment.js +0 -10
- package/dist/transform/mutations/RemoveComment.js.flow +4 -4
- package/dist/transform/mutations/RemoveNode.js +1 -23
- package/dist/transform/mutations/RemoveNode.js.flow +9 -9
- package/dist/transform/mutations/RemoveStatement.js +0 -16
- package/dist/transform/mutations/RemoveStatement.js.flow +3 -3
- package/dist/transform/mutations/ReplaceNode.js +2 -16
- package/dist/transform/mutations/ReplaceNode.js.flow +6 -6
- package/dist/transform/mutations/ReplaceStatementWithMany.js +2 -14
- package/dist/transform/mutations/ReplaceStatementWithMany.js.flow +7 -8
- package/dist/transform/mutations/utils/getStatementParent.js +3 -19
- package/dist/transform/mutations/utils/getStatementParent.js.flow +5 -6
- package/dist/transform/mutations/utils/isValidModuleDeclarationParent.js +5 -20
- package/dist/transform/mutations/utils/isValidModuleDeclarationParent.js.flow +4 -3
- package/dist/transform/parse.js +6 -25
- package/dist/transform/print.js +9 -35
- package/dist/transform/print.js.flow +7 -15
- package/dist/transform/transform.js +0 -9
- package/dist/transform/transformAST.js +3 -31
- package/dist/traverse/NodeEventGenerator.js +2 -108
- package/dist/traverse/NodeEventGenerator.js.flow +13 -9
- package/dist/traverse/SafeEmitter.js +0 -19
- package/dist/traverse/SafeEmitter.js.flow +2 -2
- package/dist/traverse/esquery.js +0 -18
- package/dist/traverse/esquery.js.flow +10 -10
- package/dist/traverse/traverse.js +1 -22
- package/dist/traverse/traverse.js.flow +4 -4
- package/package.json +7 -12
|
@@ -29,19 +29,7 @@ var _ReplaceStatementWithMany = require("./mutations/ReplaceStatementWithMany");
|
|
|
29
29
|
|
|
30
30
|
var _ModifyNodeInPlace = require("./mutations/ModifyNodeInPlace");
|
|
31
31
|
|
|
32
|
-
/**
|
|
33
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
34
|
-
*
|
|
35
|
-
* This source code is licensed under the MIT license found in the
|
|
36
|
-
* LICENSE file in the root directory of this source tree.
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
* @format
|
|
40
|
-
*/
|
|
41
32
|
function getTransformContext() {
|
|
42
|
-
/**
|
|
43
|
-
* The mutations in order of collection.
|
|
44
|
-
*/
|
|
45
33
|
const mutations = [];
|
|
46
34
|
|
|
47
35
|
function pushMutation(mutation) {
|
|
@@ -51,7 +39,6 @@ function getTransformContext() {
|
|
|
51
39
|
}
|
|
52
40
|
|
|
53
41
|
const cloneAPIs = {
|
|
54
|
-
// $FlowFixMe[incompatible-exact]
|
|
55
42
|
shallowCloneNode: node => {
|
|
56
43
|
if (node == null) {
|
|
57
44
|
return null;
|
|
@@ -66,7 +53,6 @@ function getTransformContext() {
|
|
|
66
53
|
|
|
67
54
|
return (0, _detachedNode.shallowCloneNode)(node, newProps);
|
|
68
55
|
},
|
|
69
|
-
// $FlowFixMe[incompatible-exact]
|
|
70
56
|
shallowCloneArray: nodes => {
|
|
71
57
|
if (nodes == null) {
|
|
72
58
|
return null;
|
|
@@ -76,14 +62,12 @@ function getTransformContext() {
|
|
|
76
62
|
const node = node_;
|
|
77
63
|
|
|
78
64
|
if (node == null) {
|
|
79
|
-
// $FlowExpectedError[incompatible-type]
|
|
80
65
|
return node;
|
|
81
66
|
}
|
|
82
67
|
|
|
83
68
|
return (0, _detachedNode.shallowCloneNode)(node, {});
|
|
84
69
|
});
|
|
85
70
|
},
|
|
86
|
-
// $FlowFixMe[incompatible-exact]
|
|
87
71
|
deepCloneNode: node => {
|
|
88
72
|
if (node == null) {
|
|
89
73
|
return null;
|
|
@@ -176,7 +160,6 @@ function getTransformContext() {
|
|
|
176
160
|
|
|
177
161
|
for (const [key, value] of Object.entries(newProps)) {
|
|
178
162
|
if ((0, _hermesParser.isNode)(value)) {
|
|
179
|
-
// $FlowFixMe[incompatible-type]
|
|
180
163
|
const node = value;
|
|
181
164
|
detachedProps[key] = (0, _detachedNode.asDetachedNode)(node);
|
|
182
165
|
} else {
|
|
@@ -190,7 +173,6 @@ function getTransformContext() {
|
|
|
190
173
|
return {
|
|
191
174
|
mutations,
|
|
192
175
|
|
|
193
|
-
// $FlowExpectedError[unsafe-getters-setters]
|
|
194
176
|
get astWasMutated() {
|
|
195
177
|
return mutations.length > 0;
|
|
196
178
|
},
|
|
@@ -54,7 +54,7 @@ import {createReplaceNodeMutation} from './mutations/ReplaceNode';
|
|
|
54
54
|
import {createReplaceStatementWithManyMutation} from './mutations/ReplaceStatementWithMany';
|
|
55
55
|
import {createModifyNodeInPlaceMutation} from './mutations/ModifyNodeInPlace';
|
|
56
56
|
|
|
57
|
-
type Mutation =
|
|
57
|
+
type Mutation = Readonly<
|
|
58
58
|
| AddCommentsMutation
|
|
59
59
|
| CloneCommentsToMutation
|
|
60
60
|
| InsertStatementMutation
|
|
@@ -66,9 +66,9 @@ type Mutation = $ReadOnly<
|
|
|
66
66
|
| ModifyNodeInPlaceMutation,
|
|
67
67
|
>;
|
|
68
68
|
|
|
69
|
-
type SingleOrArray
|
|
69
|
+
type SingleOrArray<out T> = T | ReadonlyArray<T>;
|
|
70
70
|
|
|
71
|
-
type ReplaceNodeOptions =
|
|
71
|
+
type ReplaceNodeOptions = Readonly<{
|
|
72
72
|
/**
|
|
73
73
|
* Moves the comments from the target node to the nodetoReplaceWith.
|
|
74
74
|
* Note that this does not *clone* comments, it moves them and clears out
|
|
@@ -77,7 +77,7 @@ type ReplaceNodeOptions = $ReadOnly<{
|
|
|
77
77
|
keepComments?: boolean,
|
|
78
78
|
}>;
|
|
79
79
|
|
|
80
|
-
type TransformCloneAPIs =
|
|
80
|
+
type TransformCloneAPIs = Readonly<{
|
|
81
81
|
/**
|
|
82
82
|
* Shallowly clones the given node.
|
|
83
83
|
*
|
|
@@ -95,8 +95,8 @@ type TransformCloneAPIs = $ReadOnly<{
|
|
|
95
95
|
* in the AST, then use `deepCloneNode` instead.
|
|
96
96
|
*/
|
|
97
97
|
shallowCloneNode: {
|
|
98
|
-
<T
|
|
99
|
-
<T
|
|
98
|
+
<T extends ESNode>(node: T): DetachedNode<T>,
|
|
99
|
+
<T extends ESNode>(node: ?T): DetachedNode<T> | null,
|
|
100
100
|
},
|
|
101
101
|
|
|
102
102
|
/**
|
|
@@ -109,10 +109,16 @@ type TransformCloneAPIs = $ReadOnly<{
|
|
|
109
109
|
* {@see shallowCloneNode}
|
|
110
110
|
*/
|
|
111
111
|
shallowCloneArray: {
|
|
112
|
-
<T
|
|
113
|
-
<T
|
|
114
|
-
|
|
115
|
-
|
|
112
|
+
<T extends ESNode>(node: ReadonlyArray<T>): ReadonlyArray<DetachedNode<T>>,
|
|
113
|
+
<T extends ESNode>(
|
|
114
|
+
node: ?ReadonlyArray<T>,
|
|
115
|
+
): ?ReadonlyArray<DetachedNode<T>>,
|
|
116
|
+
<T extends ESNode>(
|
|
117
|
+
node: ReadonlyArray<?T>,
|
|
118
|
+
): ReadonlyArray<DetachedNode<?T>>,
|
|
119
|
+
<T extends ESNode>(
|
|
120
|
+
node: ?ReadonlyArray<?T>,
|
|
121
|
+
): ?ReadonlyArray<DetachedNode<?T>>,
|
|
116
122
|
},
|
|
117
123
|
|
|
118
124
|
/**
|
|
@@ -123,8 +129,8 @@ type TransformCloneAPIs = $ReadOnly<{
|
|
|
123
129
|
* result in a lot of work being done.
|
|
124
130
|
*/
|
|
125
131
|
deepCloneNode: {
|
|
126
|
-
<T
|
|
127
|
-
<T
|
|
132
|
+
<T extends ESNode>(node: T): DetachedNode<T>,
|
|
133
|
+
<T extends ESNode>(node: ?T): DetachedNode<T> | null,
|
|
128
134
|
},
|
|
129
135
|
|
|
130
136
|
/**
|
|
@@ -135,7 +141,7 @@ type TransformCloneAPIs = $ReadOnly<{
|
|
|
135
141
|
deepCloneNodeWithOverrides: TransformCloneSignatures,
|
|
136
142
|
}>;
|
|
137
143
|
|
|
138
|
-
type TransformCommentAPIs =
|
|
144
|
+
type TransformCommentAPIs = Readonly<{
|
|
139
145
|
/**
|
|
140
146
|
* Gets all of the comments attached to the given node.
|
|
141
147
|
*/
|
|
@@ -199,7 +205,7 @@ type TransformCommentAPIs = $ReadOnly<{
|
|
|
199
205
|
removeComments: (comments: SingleOrArray<Comment>) => void,
|
|
200
206
|
}>;
|
|
201
207
|
|
|
202
|
-
type TransformInsertAPIs =
|
|
208
|
+
type TransformInsertAPIs = Readonly<{
|
|
203
209
|
/**
|
|
204
210
|
* Insert `nodeToInsert` after the `target` statement.
|
|
205
211
|
* The inserted nodes will be kept in the order given.
|
|
@@ -223,7 +229,7 @@ type TransformInsertAPIs = $ReadOnly<{
|
|
|
223
229
|
) => void,
|
|
224
230
|
}>;
|
|
225
231
|
|
|
226
|
-
type TransformModifyAPIs =
|
|
232
|
+
type TransformModifyAPIs = Readonly<{
|
|
227
233
|
/**
|
|
228
234
|
* Modifies a given node in place.
|
|
229
235
|
* This is equivalent to doing a replace with a shallow clone with overrides.
|
|
@@ -231,7 +237,7 @@ type TransformModifyAPIs = $ReadOnly<{
|
|
|
231
237
|
modifyNodeInPlace: TransformModifySignatures,
|
|
232
238
|
}>;
|
|
233
239
|
|
|
234
|
-
type TransformRemoveAPIs =
|
|
240
|
+
type TransformRemoveAPIs = Readonly<{
|
|
235
241
|
/**
|
|
236
242
|
* Removes a given node from the AST.
|
|
237
243
|
* The set of thigns that can be removed is intentionally restricted by types.
|
|
@@ -245,7 +251,7 @@ type TransformRemoveAPIs = $ReadOnly<{
|
|
|
245
251
|
removeStatement: (node: RemoveStatementMutation['node']) => void,
|
|
246
252
|
}>;
|
|
247
253
|
|
|
248
|
-
type TransformReplaceAPIs =
|
|
254
|
+
type TransformReplaceAPIs = Readonly<{
|
|
249
255
|
/**
|
|
250
256
|
* Replace the `target` node with the `nodeToReplaceWith` node.
|
|
251
257
|
* This simply does an in-place replacement in the AST.
|
|
@@ -295,7 +301,7 @@ type TransformReplaceAPIs = $ReadOnly<{
|
|
|
295
301
|
*/
|
|
296
302
|
replaceStatementWithMany: (
|
|
297
303
|
target: ReplaceStatementWithManyMutation['target'],
|
|
298
|
-
nodesToReplaceWith:
|
|
304
|
+
nodesToReplaceWith: ReadonlyArray<
|
|
299
305
|
MaybeDetachedNode<ReplaceStatementWithManyMutationNodes>,
|
|
300
306
|
>,
|
|
301
307
|
options?: {
|
|
@@ -309,8 +315,8 @@ type TransformReplaceAPIs = $ReadOnly<{
|
|
|
309
315
|
) => void,
|
|
310
316
|
}>;
|
|
311
317
|
|
|
312
|
-
export type TransformContextAdditions =
|
|
313
|
-
mutations:
|
|
318
|
+
export type TransformContextAdditions = Readonly<{
|
|
319
|
+
mutations: ReadonlyArray<Mutation>,
|
|
314
320
|
astWasMutated: boolean,
|
|
315
321
|
|
|
316
322
|
...TransformCommentAPIs,
|
|
@@ -335,17 +341,17 @@ export function getTransformContext(): TransformContextAdditions {
|
|
|
335
341
|
|
|
336
342
|
const cloneAPIs: TransformCloneAPIs = {
|
|
337
343
|
// $FlowFixMe[incompatible-exact]
|
|
338
|
-
shallowCloneNode: ((
|
|
344
|
+
shallowCloneNode: ((node: ?ESNode): ?DetachedNode<ESNode> => {
|
|
339
345
|
if (node == null) {
|
|
340
346
|
return null;
|
|
341
347
|
}
|
|
342
348
|
|
|
343
349
|
return shallowCloneNode(node, {});
|
|
344
|
-
}
|
|
350
|
+
}) as $FlowFixMe as TransformCloneAPIs['shallowCloneNode'],
|
|
345
351
|
|
|
346
|
-
shallowCloneNodeWithOverrides: ((
|
|
352
|
+
shallowCloneNodeWithOverrides: ((
|
|
347
353
|
node: ?ESNode,
|
|
348
|
-
newProps?:
|
|
354
|
+
newProps?: Readonly<{...}> = {},
|
|
349
355
|
): // $FlowExpectedError[prop-missing]
|
|
350
356
|
?DetachedNode<ESNode> => {
|
|
351
357
|
if (node == null) {
|
|
@@ -353,12 +359,12 @@ export function getTransformContext(): TransformContextAdditions {
|
|
|
353
359
|
}
|
|
354
360
|
|
|
355
361
|
return shallowCloneNode(node, newProps);
|
|
356
|
-
}
|
|
362
|
+
}) as $FlowFixMe as TransformCloneAPIs['shallowCloneNodeWithOverrides'],
|
|
357
363
|
|
|
358
364
|
// $FlowFixMe[incompatible-exact]
|
|
359
|
-
shallowCloneArray: (
|
|
360
|
-
nodes:
|
|
361
|
-
):
|
|
365
|
+
shallowCloneArray: (<T extends ESNode>(
|
|
366
|
+
nodes: ?ReadonlyArray<?T>,
|
|
367
|
+
): ?ReadonlyArray<DetachedNode<?ESNode>> => {
|
|
362
368
|
if (nodes == null) {
|
|
363
369
|
return null;
|
|
364
370
|
}
|
|
@@ -371,20 +377,20 @@ export function getTransformContext(): TransformContextAdditions {
|
|
|
371
377
|
}
|
|
372
378
|
return shallowCloneNode<ESNode>(node, {});
|
|
373
379
|
});
|
|
374
|
-
}
|
|
380
|
+
}) as $FlowFixMe as TransformCloneAPIs['shallowCloneArray'],
|
|
375
381
|
|
|
376
382
|
// $FlowFixMe[incompatible-exact]
|
|
377
|
-
deepCloneNode: ((
|
|
383
|
+
deepCloneNode: ((node: ?ESNode): ?DetachedNode<ESNode> => {
|
|
378
384
|
if (node == null) {
|
|
379
385
|
return null;
|
|
380
386
|
}
|
|
381
387
|
|
|
382
388
|
return deepCloneNode(node, {});
|
|
383
|
-
}
|
|
389
|
+
}) as $FlowFixMe as TransformCloneAPIs['deepCloneNode'],
|
|
384
390
|
|
|
385
|
-
deepCloneNodeWithOverrides: ((
|
|
391
|
+
deepCloneNodeWithOverrides: ((
|
|
386
392
|
node: ?ESNode,
|
|
387
|
-
newProps?:
|
|
393
|
+
newProps?: Readonly<{...}> = {},
|
|
388
394
|
): // $FlowExpectedError[prop-missing]
|
|
389
395
|
?DetachedNode<ESNode> => {
|
|
390
396
|
if (node == null) {
|
|
@@ -392,24 +398,24 @@ export function getTransformContext(): TransformContextAdditions {
|
|
|
392
398
|
}
|
|
393
399
|
|
|
394
400
|
return deepCloneNode(node, newProps);
|
|
395
|
-
}
|
|
401
|
+
}) as $FlowFixMe as TransformCloneAPIs['deepCloneNodeWithOverrides'],
|
|
396
402
|
};
|
|
397
403
|
const commentAPIs: TransformCommentAPIs = {
|
|
398
404
|
getComments: ((node): Array<Comment> => {
|
|
399
405
|
return [...getCommentsForNode(node)];
|
|
400
|
-
}
|
|
406
|
+
}) as TransformCommentAPIs['getComments'],
|
|
401
407
|
|
|
402
408
|
getLeadingComments: ((node): Array<Comment> => {
|
|
403
409
|
return getCommentsForNode(node).filter(isLeadingComment);
|
|
404
|
-
}
|
|
410
|
+
}) as TransformCommentAPIs['getLeadingComments'],
|
|
405
411
|
|
|
406
412
|
getTrailingComments: ((node): Array<Comment> => {
|
|
407
413
|
return getCommentsForNode(node).filter(isTrailingComment);
|
|
408
|
-
}
|
|
414
|
+
}) as TransformCommentAPIs['getTrailingComments'],
|
|
409
415
|
|
|
410
416
|
cloneCommentsTo: ((target, destination): void => {
|
|
411
417
|
pushMutation(createCloneCommentsToMutation(target, destination));
|
|
412
|
-
}
|
|
418
|
+
}) as TransformCommentAPIs['cloneCommentsTo'],
|
|
413
419
|
|
|
414
420
|
addLeadingComments: ((node, comments): void => {
|
|
415
421
|
pushMutation(
|
|
@@ -421,7 +427,7 @@ export function getTransformContext(): TransformContextAdditions {
|
|
|
421
427
|
})),
|
|
422
428
|
),
|
|
423
429
|
);
|
|
424
|
-
}
|
|
430
|
+
}) as TransformCommentAPIs['addLeadingComments'],
|
|
425
431
|
|
|
426
432
|
addLeadingInlineComments: ((node, comments): void => {
|
|
427
433
|
pushMutation(
|
|
@@ -433,7 +439,7 @@ export function getTransformContext(): TransformContextAdditions {
|
|
|
433
439
|
})),
|
|
434
440
|
),
|
|
435
441
|
);
|
|
436
|
-
}
|
|
442
|
+
}) as TransformCommentAPIs['addLeadingInlineComments'],
|
|
437
443
|
|
|
438
444
|
addTrailingComments: ((node, comments): void => {
|
|
439
445
|
pushMutation(
|
|
@@ -445,7 +451,7 @@ export function getTransformContext(): TransformContextAdditions {
|
|
|
445
451
|
})),
|
|
446
452
|
),
|
|
447
453
|
);
|
|
448
|
-
}
|
|
454
|
+
}) as TransformCommentAPIs['addTrailingComments'],
|
|
449
455
|
|
|
450
456
|
addTrailingInlineComments: ((node, comments): void => {
|
|
451
457
|
pushMutation(
|
|
@@ -457,13 +463,13 @@ export function getTransformContext(): TransformContextAdditions {
|
|
|
457
463
|
})),
|
|
458
464
|
),
|
|
459
465
|
);
|
|
460
|
-
}
|
|
466
|
+
}) as TransformCommentAPIs['addTrailingInlineComments'],
|
|
461
467
|
|
|
462
468
|
removeComments: ((comments): void => {
|
|
463
469
|
toArray(comments).forEach(comment => {
|
|
464
470
|
pushMutation(createRemoveCommentMutation(comment));
|
|
465
471
|
});
|
|
466
|
-
}
|
|
472
|
+
}) as TransformCommentAPIs['removeComments'],
|
|
467
473
|
};
|
|
468
474
|
const insertAPIs: TransformInsertAPIs = {
|
|
469
475
|
insertAfterStatement: ((target, nodesToInsert): void => {
|
|
@@ -476,7 +482,7 @@ export function getTransformContext(): TransformContextAdditions {
|
|
|
476
482
|
),
|
|
477
483
|
),
|
|
478
484
|
);
|
|
479
|
-
}
|
|
485
|
+
}) as TransformInsertAPIs['insertBeforeStatement'],
|
|
480
486
|
|
|
481
487
|
insertBeforeStatement: ((target, nodesToInsert): void => {
|
|
482
488
|
pushMutation(
|
|
@@ -488,19 +494,19 @@ export function getTransformContext(): TransformContextAdditions {
|
|
|
488
494
|
),
|
|
489
495
|
),
|
|
490
496
|
);
|
|
491
|
-
}
|
|
497
|
+
}) as TransformInsertAPIs['insertBeforeStatement'],
|
|
492
498
|
};
|
|
493
499
|
const removeAPIs: TransformRemoveAPIs = {
|
|
494
500
|
removeNode: ((node): void => {
|
|
495
501
|
pushMutation(createRemoveNodeMutation(node));
|
|
496
|
-
}
|
|
502
|
+
}) as TransformRemoveAPIs['removeNode'],
|
|
497
503
|
|
|
498
504
|
removeStatement: ((node): void => {
|
|
499
505
|
pushMutation(createRemoveStatementMutation(node));
|
|
500
|
-
}
|
|
506
|
+
}) as TransformRemoveAPIs['removeStatement'],
|
|
501
507
|
};
|
|
502
508
|
const replaceAPIs: TransformReplaceAPIs = {
|
|
503
|
-
replaceNode: ((
|
|
509
|
+
replaceNode: ((
|
|
504
510
|
target: ESNode,
|
|
505
511
|
nodeToReplaceWith: MaybeDetachedNode<ESNode>,
|
|
506
512
|
options?: ReplaceNodeOptions,
|
|
@@ -512,7 +518,7 @@ export function getTransformContext(): TransformContextAdditions {
|
|
|
512
518
|
options,
|
|
513
519
|
),
|
|
514
520
|
);
|
|
515
|
-
}
|
|
521
|
+
}) as $FlowFixMe as TransformReplaceAPIs['replaceNode'],
|
|
516
522
|
|
|
517
523
|
replaceStatementWithMany: ((
|
|
518
524
|
target,
|
|
@@ -526,10 +532,10 @@ export function getTransformContext(): TransformContextAdditions {
|
|
|
526
532
|
options,
|
|
527
533
|
),
|
|
528
534
|
);
|
|
529
|
-
}
|
|
535
|
+
}) as TransformReplaceAPIs['replaceStatementWithMany'],
|
|
530
536
|
};
|
|
531
537
|
const modifyAPIs: TransformModifyAPIs = {
|
|
532
|
-
modifyNodeInPlace: ((target: ESNode, newProps:
|
|
538
|
+
modifyNodeInPlace: ((target: ESNode, newProps: Readonly<{...}>): void => {
|
|
533
539
|
const detachedProps = {};
|
|
534
540
|
for (const [key, value] of Object.entries(newProps)) {
|
|
535
541
|
if (isNode(value)) {
|
|
@@ -542,7 +548,7 @@ export function getTransformContext(): TransformContextAdditions {
|
|
|
542
548
|
}
|
|
543
549
|
|
|
544
550
|
pushMutation(createModifyNodeInPlaceMutation(target, detachedProps));
|
|
545
|
-
}
|
|
551
|
+
}) as TransformModifyAPIs['modifyNodeInPlace'],
|
|
546
552
|
};
|
|
547
553
|
|
|
548
554
|
return {
|
|
@@ -562,9 +568,9 @@ export function getTransformContext(): TransformContextAdditions {
|
|
|
562
568
|
};
|
|
563
569
|
}
|
|
564
570
|
|
|
565
|
-
function toArray<T>(thing: SingleOrArray<T>):
|
|
571
|
+
function toArray<T>(thing: SingleOrArray<T>): ReadonlyArray<T> {
|
|
566
572
|
if (Array.isArray(thing)) {
|
|
567
|
-
return
|
|
573
|
+
return thing as $FlowFixMe;
|
|
568
574
|
}
|
|
569
575
|
return [thing];
|
|
570
576
|
}
|
|
@@ -36,18 +36,6 @@ var _os = require("os");
|
|
|
36
36
|
|
|
37
37
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
38
38
|
|
|
39
|
-
/**
|
|
40
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
41
|
-
*
|
|
42
|
-
* This source code is licensed under the MIT license found in the
|
|
43
|
-
* LICENSE file in the root directory of this source tree.
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
* @format
|
|
47
|
-
*/
|
|
48
|
-
// $FlowExpectedError[untyped-import]
|
|
49
|
-
// $FlowExpectedError[untyped-import]
|
|
50
|
-
// $FlowExpectedError[untyped-import]
|
|
51
39
|
const CommentPlacement = require("flow-enums-runtime").Mirrored(["LEADING_OWN_LINE", "LEADING_INLINE", "TRAILING_OWN_LINE", "TRAILING_INLINE"]);
|
|
52
40
|
|
|
53
41
|
exports.CommentPlacement = CommentPlacement;
|
|
@@ -61,30 +49,18 @@ function attachComments(comments, ast, text) {
|
|
|
61
49
|
}
|
|
62
50
|
|
|
63
51
|
function mutateESTreeASTCommentsForPrettier(program, text) {
|
|
64
|
-
let code = text;
|
|
65
|
-
|
|
66
|
-
// comments on each node, borking the output
|
|
67
|
-
// $FlowExpectedError[cannot-write]
|
|
68
|
-
|
|
69
|
-
delete program.comments; // The docblock comment is never attached to any AST nodes, since its technically
|
|
70
|
-
// attached to the program. However this is specific to our AST and in order for
|
|
71
|
-
// prettier to correctly print it we need to attach it to the first node in the
|
|
72
|
-
// program body.
|
|
52
|
+
let code = text;
|
|
53
|
+
delete program.comments;
|
|
73
54
|
|
|
74
55
|
if (program.docblock != null && program.docblock.comment != null) {
|
|
75
56
|
const docblockComment = program.docblock.comment;
|
|
76
57
|
const isDocblockCommentNew = !isAttachedComment(docblockComment);
|
|
77
58
|
|
|
78
59
|
if (isDocblockCommentNew) {
|
|
79
|
-
|
|
80
|
-
docblockComment.
|
|
81
|
-
|
|
82
|
-
docblockComment.leading = true; // $FlowExpectedError[prop-missing]
|
|
83
|
-
|
|
60
|
+
docblockComment.printed = false;
|
|
61
|
+
docblockComment.leading = true;
|
|
84
62
|
docblockComment.trailing = false;
|
|
85
|
-
}
|
|
86
|
-
// otherwise set it on the program.
|
|
87
|
-
|
|
63
|
+
}
|
|
88
64
|
|
|
89
65
|
if (program.body.length > 0) {
|
|
90
66
|
const firstStatement = program.body[0];
|
|
@@ -94,17 +70,13 @@ function mutateESTreeASTCommentsForPrettier(program, text) {
|
|
|
94
70
|
setCommentsOnNode(firstStatement, [docblockComment, ...getCommentsForNode(firstStatement)]);
|
|
95
71
|
|
|
96
72
|
if (isDocblockCommentNew) {
|
|
97
|
-
// Add markers to the code block to ensure docblock comment is printed on its
|
|
98
|
-
// own line.
|
|
99
73
|
code = makeCommentOwnLine(code, docblockComment);
|
|
100
74
|
}
|
|
101
75
|
}
|
|
102
76
|
} else {
|
|
103
77
|
setCommentsOnNode(program, [docblockComment]);
|
|
104
78
|
}
|
|
105
|
-
}
|
|
106
|
-
// $FlowExpectedError[cannot-write]
|
|
107
|
-
|
|
79
|
+
}
|
|
108
80
|
|
|
109
81
|
delete program.docblock;
|
|
110
82
|
return code;
|
|
@@ -121,40 +93,30 @@ function cloneCommentsToNewNode(oldNode, newNode) {
|
|
|
121
93
|
|
|
122
94
|
function cloneJSDocCommentsToNewNode(oldNode, newNode) {
|
|
123
95
|
const comments = getCommentsForNode(oldNode).filter(comment => {
|
|
124
|
-
return (0, _hermesEstree.isBlockComment)(comment) &&
|
|
125
|
-
comment.value.startsWith('*');
|
|
96
|
+
return (0, _hermesEstree.isBlockComment)(comment) && comment.value.startsWith('*');
|
|
126
97
|
});
|
|
127
98
|
setCommentsOnNode(newNode, [...getCommentsForNode(newNode), ...comments.map(cloneCommentWithMarkers)]);
|
|
128
99
|
}
|
|
129
100
|
|
|
130
101
|
function setCommentsOnNode(node, comments) {
|
|
131
|
-
// $FlowExpectedError[prop-missing] - this property is secretly added by prettier.
|
|
132
|
-
// $FlowExpectedError[incompatible-use] - this property is secretly added by prettier.
|
|
133
|
-
// $FlowExpectedError[cannot-write] - this property is secretly added by prettier.
|
|
134
102
|
node.comments = comments;
|
|
135
103
|
}
|
|
136
104
|
|
|
137
105
|
function getCommentsForNode(node) {
|
|
138
106
|
var _node$comments;
|
|
139
107
|
|
|
140
|
-
// $FlowExpectedError[prop-missing] - this property is secretly added by prettier.
|
|
141
|
-
// $FlowExpectedError[incompatible-use] - this property is secretly added by prettier.
|
|
142
108
|
return (_node$comments = node.comments) != null ? _node$comments : [];
|
|
143
109
|
}
|
|
144
110
|
|
|
145
111
|
function isAttachedComment(comment) {
|
|
146
|
-
// Prettier adds this property to comments that have been attached.
|
|
147
|
-
// $FlowExpectedError[prop-missing] - this property is secretly added by prettier.
|
|
148
112
|
return comment.printed === false;
|
|
149
113
|
}
|
|
150
114
|
|
|
151
115
|
function isLeadingComment(comment) {
|
|
152
|
-
// $FlowExpectedError[prop-missing] - this property is secretly added by prettier.
|
|
153
116
|
return comment.leading === true;
|
|
154
117
|
}
|
|
155
118
|
|
|
156
119
|
function isTrailingComment(comment) {
|
|
157
|
-
// $FlowExpectedError[prop-missing] - this property is secretly added by prettier.
|
|
158
120
|
return comment.trailing === true;
|
|
159
121
|
}
|
|
160
122
|
|
|
@@ -185,7 +147,6 @@ function addComment(node, comment, placement) {
|
|
|
185
147
|
}
|
|
186
148
|
|
|
187
149
|
function cloneComment(comment) {
|
|
188
|
-
// $FlowExpectedError[incompatible-type]
|
|
189
150
|
return {
|
|
190
151
|
type: comment.type,
|
|
191
152
|
value: comment.value,
|
|
@@ -195,7 +156,6 @@ function cloneComment(comment) {
|
|
|
195
156
|
}
|
|
196
157
|
|
|
197
158
|
function cloneCommentWithMarkers(comment) {
|
|
198
|
-
// $FlowExpectedError[incompatible-type]
|
|
199
159
|
return {
|
|
200
160
|
type: comment.type,
|
|
201
161
|
value: comment.value,
|
|
@@ -215,19 +175,13 @@ function getFirstNonWhitespaceIndex(code) {
|
|
|
215
175
|
}
|
|
216
176
|
|
|
217
177
|
function makeCommentOwnLine(code, comment) {
|
|
218
|
-
let newCode = code;
|
|
219
|
-
// searching out from either side of the comment range.
|
|
220
|
-
|
|
178
|
+
let newCode = code;
|
|
221
179
|
let firstNewline = getFirstNewlineIndex(code);
|
|
222
180
|
|
|
223
181
|
if (firstNewline === -1) {
|
|
224
|
-
// No newline in file, lets add one.
|
|
225
182
|
newCode += _os.EOL;
|
|
226
183
|
firstNewline = newCode.length;
|
|
227
|
-
}
|
|
228
|
-
// range is safe.
|
|
229
|
-
// $FlowExpectedError[cannot-write]
|
|
230
|
-
|
|
184
|
+
}
|
|
231
185
|
|
|
232
186
|
comment.range = [firstNewline + 1, firstNewline];
|
|
233
187
|
return newCode;
|
|
@@ -239,11 +193,6 @@ function appendCommentToSource(code, comment, placement) {
|
|
|
239
193
|
switch (comment.type) {
|
|
240
194
|
case 'Block':
|
|
241
195
|
{
|
|
242
|
-
// Prettier decides if a newline is necessary between the comment and its node by looking
|
|
243
|
-
// to see if a newline seperates them in the source text. We can trick prettier into
|
|
244
|
-
// formatting how we want for new comments by placing the range such that a newline
|
|
245
|
-
// will (OWN_LINE) or will not (INLINE) be found when searching from the specified range
|
|
246
|
-
// position.
|
|
247
196
|
switch (placement) {
|
|
248
197
|
case CommentPlacement.LEADING_OWN_LINE:
|
|
249
198
|
case CommentPlacement.TRAILING_OWN_LINE:
|
|
@@ -255,18 +204,13 @@ function appendCommentToSource(code, comment, placement) {
|
|
|
255
204
|
case CommentPlacement.LEADING_INLINE:
|
|
256
205
|
case CommentPlacement.TRAILING_INLINE:
|
|
257
206
|
{
|
|
258
|
-
// Since we don't want a line break we need to ensure a non whitespace char is
|
|
259
|
-
// always found before a newline when searching out from either side of the
|
|
260
|
-
// comment range.
|
|
261
207
|
let firstNonWhitespace = getFirstNonWhitespaceIndex(code);
|
|
262
208
|
|
|
263
209
|
if (firstNonWhitespace === -1) {
|
|
264
|
-
// No non whitespace chars in file, lets add an identifiable statement for prettier to find.
|
|
265
210
|
newCode += '$FORCE_INLINE_ON_EMPTY_FILE_TOKEN$;';
|
|
266
211
|
firstNonWhitespace = newCode.length;
|
|
267
212
|
break;
|
|
268
|
-
}
|
|
269
|
-
|
|
213
|
+
}
|
|
270
214
|
|
|
271
215
|
comment.range = [firstNonWhitespace + 1, firstNonWhitespace];
|
|
272
216
|
break;
|
|
@@ -278,30 +222,20 @@ function appendCommentToSource(code, comment, placement) {
|
|
|
278
222
|
|
|
279
223
|
case 'Line':
|
|
280
224
|
{
|
|
281
|
-
// For `Line` comments prettier slices comments directly from the source code when printing
|
|
282
|
-
// https://github.com/prettier/prettier/blob/5f0ee39fa03532c85bd1c35291450fe7ac3667b3/src/language-js/print/comment.js#L15-L20
|
|
283
|
-
// this means that we need to have any appended comments directly in the
|
|
284
|
-
// source code or else prettier will slice nothing and bork up the transform
|
|
285
225
|
const commentText = `//${comment.value}`;
|
|
286
226
|
const lastChar = newCode[newCode.length - 1];
|
|
287
227
|
|
|
288
228
|
if (lastChar !== '\n' && lastChar !== '\r') {
|
|
289
229
|
newCode += _os.EOL;
|
|
290
|
-
}
|
|
291
|
-
|
|
230
|
+
}
|
|
292
231
|
|
|
293
232
|
if (placement === CommentPlacement.TRAILING_INLINE) {
|
|
294
|
-
// Prettier determines an "end of line" comment by walking backwards from
|
|
295
|
-
// the comment start range through the source code to see if it finds a non
|
|
296
|
-
// newline token. In order to trick prettier for new comments we need to
|
|
297
|
-
// insert fake source code for it to find.
|
|
298
233
|
newCode += '$FORCE_END_OF_LINE_COMMENT_TOKEN$;';
|
|
299
234
|
}
|
|
300
235
|
|
|
301
236
|
const start = newCode.length;
|
|
302
237
|
newCode += commentText;
|
|
303
|
-
const end = newCode.length;
|
|
304
|
-
|
|
238
|
+
const end = newCode.length;
|
|
305
239
|
comment.range = [start, end];
|
|
306
240
|
break;
|
|
307
241
|
}
|