hermes-transform 0.9.0 → 0.10.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 +9 -27
- package/dist/detachedNode.js.flow +18 -31
- package/dist/generated/TransformCloneSignatures.js.flow +18 -0
- package/dist/generated/TransformModifySignatures.js.flow +14 -0
- package/dist/generated/TransformReplaceSignatures.js.flow +15 -1
- package/dist/generated/node-types.js +253 -59
- package/dist/generated/node-types.js.flow +257 -102
- package/dist/generated/special-case-node-types/Comment.js +36 -0
- package/dist/generated/special-case-node-types/Comment.js.flow +36 -0
- package/dist/generated/special-case-node-types/DeclareExportDeclaration.js +55 -0
- package/dist/generated/special-case-node-types/DeclareExportDeclaration.js.flow +97 -0
- package/dist/generated/special-case-node-types/ExportNamedDeclaration.js +42 -0
- package/dist/generated/special-case-node-types/ExportNamedDeclaration.js.flow +75 -0
- package/dist/generated/special-case-node-types/Literal.js +97 -0
- package/dist/generated/special-case-node-types/Literal.js.flow +139 -0
- package/dist/generated/special-case-node-types/ObjectTypeProperty.js +73 -0
- package/dist/generated/special-case-node-types/ObjectTypeProperty.js.flow +107 -0
- package/dist/generated/special-case-node-types/Property.js +136 -0
- package/dist/generated/special-case-node-types/Property.js.flow +237 -0
- package/dist/generated/special-case-node-types/misc.js +119 -0
- package/dist/generated/special-case-node-types/misc.js.flow +205 -0
- package/dist/generated/special-case-node-types.js +42 -174
- package/dist/generated/special-case-node-types.js.flow +7 -274
- package/dist/index.js +19 -3
- package/dist/index.js.flow +6 -2
- package/dist/transform/TransformContext.js +1 -1
- package/dist/transform/TransformContext.js.flow +2 -2
- package/dist/transform/comments/comments.js +11 -0
- package/dist/transform/comments/comments.js.flow +16 -1
- package/dist/transform/comments/prettier/main/comments.js +1 -1
- package/dist/transform/comments/prettier/main/comments.js.flow +2 -1
- package/dist/transform/mutations/InsertStatement.js +3 -3
- package/dist/transform/mutations/InsertStatement.js.flow +3 -3
- package/dist/transform/mutations/RemoveComment.js +3 -3
- package/dist/transform/mutations/RemoveComment.js.flow +3 -5
- package/dist/transform/mutations/RemoveNode.js +2 -2
- package/dist/transform/mutations/RemoveNode.js.flow +2 -2
- package/dist/transform/mutations/RemoveStatement.js +2 -2
- package/dist/transform/mutations/RemoveStatement.js.flow +2 -2
- package/dist/transform/mutations/ReplaceNode.js +4 -6
- package/dist/transform/mutations/ReplaceNode.js.flow +2 -3
- package/dist/transform/mutations/ReplaceStatementWithMany.js +2 -2
- package/dist/transform/mutations/ReplaceStatementWithMany.js.flow +2 -2
- package/dist/transform/mutations/utils/getStatementParent.js +3 -2
- package/dist/transform/mutations/utils/getStatementParent.js.flow +5 -2
- package/dist/transform/parse.js +55 -0
- package/dist/transform/parse.js.flow +55 -0
- package/dist/transform/print.js +160 -0
- package/dist/transform/print.js.flow +176 -0
- package/dist/transform/transform.js +6 -67
- package/dist/transform/transform.js.flow +6 -69
- package/dist/transform/{getTransformedAST.js → transformAST.js} +7 -31
- package/dist/transform/{getTransformedAST.js.flow → transformAST.js.flow} +7 -32
- package/dist/traverse/traverse.js +36 -35
- package/dist/traverse/traverse.js.flow +45 -26
- package/package.json +5 -4
- package/dist/getVisitorKeys.js +0 -33
- package/dist/getVisitorKeys.js.flow +0 -31
- package/dist/transform/mutations/utils/arrayUtils.js +0 -43
- package/dist/transform/mutations/utils/arrayUtils.js.flow +0 -50
- package/dist/traverse/SimpleTraverser.js +0 -118
- package/dist/traverse/SimpleTraverser.js.flow +0 -112
|
@@ -14,277 +14,10 @@ The list of exported functions here must be kept in sync with the `NODES_WITH_SP
|
|
|
14
14
|
list in `scripts/genTransformNodeTypes` to ensure there's no duplicates
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
Identifier as IdentifierType,
|
|
25
|
-
LineComment as LineCommentType,
|
|
26
|
-
NullLiteral as NullLiteralType,
|
|
27
|
-
NumericLiteral as NumericLiteralType,
|
|
28
|
-
RegExpLiteral as RegExpLiteralType,
|
|
29
|
-
StringLiteral as StringLiteralType,
|
|
30
|
-
TemplateElement as TemplateElementType,
|
|
31
|
-
} from 'hermes-estree';
|
|
32
|
-
import type {DetachedNode, MaybeDetachedNode} from '../detachedNode';
|
|
33
|
-
|
|
34
|
-
import {
|
|
35
|
-
asDetachedNode,
|
|
36
|
-
detachedProps,
|
|
37
|
-
setParentPointersInDirectChildren,
|
|
38
|
-
} from '../detachedNode';
|
|
39
|
-
|
|
40
|
-
// hermes adds an `id` prop which is always null, and it adds an `expression`
|
|
41
|
-
// boolean which is true when the body isn't a BlockStatement.
|
|
42
|
-
// No need to make consumers set these
|
|
43
|
-
export type ArrowFunctionExpressionProps = {
|
|
44
|
-
+params: $ReadOnlyArray<
|
|
45
|
-
MaybeDetachedNode<ArrowFunctionExpressionType['params'][number]>,
|
|
46
|
-
>,
|
|
47
|
-
+body: MaybeDetachedNode<ArrowFunctionExpressionType['body']>,
|
|
48
|
-
+typeParameters?: ?MaybeDetachedNode<
|
|
49
|
-
ArrowFunctionExpressionType['typeParameters'],
|
|
50
|
-
>,
|
|
51
|
-
+returnType?: ?MaybeDetachedNode<ArrowFunctionExpressionType['returnType']>,
|
|
52
|
-
+predicate?: ?MaybeDetachedNode<ArrowFunctionExpressionType['predicate']>,
|
|
53
|
-
+async: ArrowFunctionExpressionType['async'],
|
|
54
|
-
};
|
|
55
|
-
export function ArrowFunctionExpression(props: {
|
|
56
|
-
...$ReadOnly<ArrowFunctionExpressionProps>,
|
|
57
|
-
+parent?: ESNode,
|
|
58
|
-
}): DetachedNode<ArrowFunctionExpressionType> {
|
|
59
|
-
const node = detachedProps<ArrowFunctionExpressionType>(props.parent, {
|
|
60
|
-
type: 'ArrowFunctionExpression',
|
|
61
|
-
id: null,
|
|
62
|
-
// $FlowExpectedError[incompatible-use]
|
|
63
|
-
expression: props.body.type !== 'BlockStatement',
|
|
64
|
-
params: props.params.map(n => asDetachedNode(n)),
|
|
65
|
-
body: asDetachedNode(props.body),
|
|
66
|
-
typeParameters: asDetachedNode(props.typeParameters),
|
|
67
|
-
returnType: asDetachedNode(props.returnType),
|
|
68
|
-
predicate: asDetachedNode(props.predicate),
|
|
69
|
-
async: props.async,
|
|
70
|
-
});
|
|
71
|
-
setParentPointersInDirectChildren(node);
|
|
72
|
-
return node;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
export type ClassDeclarationProps = {
|
|
76
|
-
+id?: ?MaybeDetachedNode<ClassDeclarationType['id']>,
|
|
77
|
-
+typeParameters?: ?MaybeDetachedNode<ClassDeclarationType['typeParameters']>,
|
|
78
|
-
+superClass?: ?MaybeDetachedNode<ClassDeclarationType['superClass']>,
|
|
79
|
-
+superTypeParameters?: ?MaybeDetachedNode<
|
|
80
|
-
ClassDeclarationType['superTypeParameters'],
|
|
81
|
-
>,
|
|
82
|
-
// make this optional as it's rarer that people would want to include them
|
|
83
|
-
+implements?: $ReadOnlyArray<
|
|
84
|
-
MaybeDetachedNode<ClassDeclarationType['implements'][number]>,
|
|
85
|
-
>,
|
|
86
|
-
// make this optional as it's rarer that people would want to include them
|
|
87
|
-
+decorators?: $ReadOnlyArray<
|
|
88
|
-
MaybeDetachedNode<ClassDeclarationType['decorators'][number]>,
|
|
89
|
-
>,
|
|
90
|
-
+body: MaybeDetachedNode<ClassDeclarationType['body']>,
|
|
91
|
-
};
|
|
92
|
-
export function ClassDeclaration(props: {
|
|
93
|
-
...$ReadOnly<ClassDeclarationProps>,
|
|
94
|
-
+parent?: ESNode,
|
|
95
|
-
}): DetachedNode<ClassDeclarationType> {
|
|
96
|
-
const node = detachedProps<ClassDeclarationType>(props.parent, {
|
|
97
|
-
type: 'ClassDeclaration',
|
|
98
|
-
id: asDetachedNode(props.id),
|
|
99
|
-
typeParameters: asDetachedNode(props.typeParameters),
|
|
100
|
-
superClass: asDetachedNode(props.superClass),
|
|
101
|
-
superTypeParameters: asDetachedNode(props.superTypeParameters),
|
|
102
|
-
decorators: (props.decorators ?? []).map(n => asDetachedNode(n)),
|
|
103
|
-
implements: (props.implements ?? []).map(n => asDetachedNode(n)),
|
|
104
|
-
body: asDetachedNode(props.body),
|
|
105
|
-
});
|
|
106
|
-
setParentPointersInDirectChildren(node);
|
|
107
|
-
return node;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
// pattern/flags are on a subobject in the estree spec, but are flat on the hermes types
|
|
111
|
-
// also the value is supposed to be a RegExp instance
|
|
112
|
-
export type RegExpLiteralProps = {
|
|
113
|
-
+pattern: RegExpLiteralType['regex']['pattern'],
|
|
114
|
-
+flags: RegExpLiteralType['regex']['flags'],
|
|
115
|
-
};
|
|
116
|
-
export function RegExpLiteral(props: {
|
|
117
|
-
...$ReadOnly<RegExpLiteralProps>,
|
|
118
|
-
+parent?: ESNode,
|
|
119
|
-
}): DetachedNode<RegExpLiteralType> {
|
|
120
|
-
const value = new RegExp(props.pattern, props.flags);
|
|
121
|
-
return detachedProps<RegExpLiteralType>(props.parent, {
|
|
122
|
-
type: 'Literal',
|
|
123
|
-
value,
|
|
124
|
-
raw: value.toString(),
|
|
125
|
-
regex: {
|
|
126
|
-
pattern: props.pattern,
|
|
127
|
-
flags: props.flags,
|
|
128
|
-
},
|
|
129
|
-
});
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
// raw/cooked are on a subobject in the estree spec, but are flat on the hermes types
|
|
133
|
-
export type TemplateElementProps = {
|
|
134
|
-
+tail: TemplateElementType['tail'],
|
|
135
|
-
+cooked: TemplateElementType['value']['cooked'],
|
|
136
|
-
+raw: TemplateElementType['value']['raw'],
|
|
137
|
-
};
|
|
138
|
-
export function TemplateElement(props: {
|
|
139
|
-
...$ReadOnly<TemplateElementProps>,
|
|
140
|
-
+parent?: ESNode,
|
|
141
|
-
}): DetachedNode<TemplateElementType> {
|
|
142
|
-
return detachedProps<TemplateElementType>(props.parent, {
|
|
143
|
-
type: 'TemplateElement',
|
|
144
|
-
tail: props.tail,
|
|
145
|
-
value: {
|
|
146
|
-
cooked: props.cooked,
|
|
147
|
-
raw: props.raw,
|
|
148
|
-
},
|
|
149
|
-
});
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
// Identifier has a bunch of stuff that usually you don't want to provide - so we have
|
|
153
|
-
// this manual def to allow us to default some values
|
|
154
|
-
export type IdentifierProps = {
|
|
155
|
-
+name: IdentifierType['name'],
|
|
156
|
-
+typeAnnotation?: ?MaybeDetachedNode<IdentifierType['typeAnnotation']>,
|
|
157
|
-
+optional?: IdentifierType['optional'],
|
|
158
|
-
};
|
|
159
|
-
export function Identifier(props: {
|
|
160
|
-
...$ReadOnly<IdentifierProps>,
|
|
161
|
-
+parent?: ESNode,
|
|
162
|
-
}): DetachedNode<IdentifierType> {
|
|
163
|
-
const node = detachedProps<IdentifierType>(props.parent, {
|
|
164
|
-
type: 'Identifier',
|
|
165
|
-
name: props.name,
|
|
166
|
-
optional: props.optional ?? false,
|
|
167
|
-
typeAnnotation: asDetachedNode(props.typeAnnotation),
|
|
168
|
-
});
|
|
169
|
-
setParentPointersInDirectChildren(node);
|
|
170
|
-
return node;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
//
|
|
174
|
-
// Literals require a "raw" which is added by the estree transform, not hermes.
|
|
175
|
-
//
|
|
176
|
-
|
|
177
|
-
export type BigIntLiteralProps = {
|
|
178
|
-
+value: $FlowFixMe /* bigint | null */,
|
|
179
|
-
/**
|
|
180
|
-
* Only set this if you want to use a source-code representation like 1_1n, etc.
|
|
181
|
-
* By default "raw" will just be the exact number you've given.
|
|
182
|
-
*/
|
|
183
|
-
+raw?: NumericLiteralType['raw'],
|
|
184
|
-
};
|
|
185
|
-
export function BigIntLiteral(props: {
|
|
186
|
-
...$ReadOnly<BigIntLiteralProps>,
|
|
187
|
-
+parent?: ESNode,
|
|
188
|
-
}): DetachedNode<BigIntLiteralType> {
|
|
189
|
-
const node = detachedProps<BigIntLiteralType>(props.parent, {
|
|
190
|
-
type: 'Literal',
|
|
191
|
-
value: props.value,
|
|
192
|
-
raw: props.raw ?? `${props.value}n`,
|
|
193
|
-
bigint: `${props.value}`,
|
|
194
|
-
});
|
|
195
|
-
setParentPointersInDirectChildren(node);
|
|
196
|
-
return node;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
export type BooleanLiteralProps = {
|
|
200
|
-
+value: BooleanLiteralType['value'],
|
|
201
|
-
};
|
|
202
|
-
export function BooleanLiteral(props: {
|
|
203
|
-
...$ReadOnly<BooleanLiteralProps>,
|
|
204
|
-
+parent?: ESNode,
|
|
205
|
-
}): DetachedNode<BooleanLiteralType> {
|
|
206
|
-
return detachedProps<BooleanLiteralType>(props.parent, {
|
|
207
|
-
type: 'Literal',
|
|
208
|
-
raw: props.value ? 'true' : 'false',
|
|
209
|
-
value: props.value,
|
|
210
|
-
});
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
export type NumericLiteralProps = {
|
|
214
|
-
+value: NumericLiteralType['value'],
|
|
215
|
-
/**
|
|
216
|
-
* Only set this if you want to use a source-code representation like 1e100, 0x11, 1_1, etc.
|
|
217
|
-
* By default "raw" will just be the exact number you've given.
|
|
218
|
-
*/
|
|
219
|
-
+raw?: NumericLiteralType['raw'],
|
|
220
|
-
};
|
|
221
|
-
export function NumericLiteral(props: {
|
|
222
|
-
...$ReadOnly<NumericLiteralProps>,
|
|
223
|
-
+parent?: ESNode,
|
|
224
|
-
}): DetachedNode<NumericLiteralType> {
|
|
225
|
-
return detachedProps<NumericLiteralType>(props.parent, {
|
|
226
|
-
type: 'Literal',
|
|
227
|
-
value: props.value,
|
|
228
|
-
raw: props.raw ?? `${props.value}`,
|
|
229
|
-
});
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
export type NullLiteralProps = {};
|
|
233
|
-
export function NullLiteral(
|
|
234
|
-
props: {
|
|
235
|
-
+parent?: ESNode,
|
|
236
|
-
} = {...null},
|
|
237
|
-
): DetachedNode<NullLiteralType> {
|
|
238
|
-
return detachedProps<NullLiteralType>(props.parent, {
|
|
239
|
-
type: 'Literal',
|
|
240
|
-
value: null,
|
|
241
|
-
raw: 'null',
|
|
242
|
-
});
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
export type StringLiteralProps = {
|
|
246
|
-
+value: StringLiteralType['value'],
|
|
247
|
-
+raw?: StringLiteralType['raw'],
|
|
248
|
-
};
|
|
249
|
-
export function StringLiteral(props: {
|
|
250
|
-
...$ReadOnly<StringLiteralProps>,
|
|
251
|
-
+parent?: ESNode,
|
|
252
|
-
}): DetachedNode<StringLiteralType> {
|
|
253
|
-
const hasSingleQuote = props.value.includes('"');
|
|
254
|
-
const hasDoubleQuote = props.value.includes("'");
|
|
255
|
-
let raw = props.raw;
|
|
256
|
-
if (raw == null) {
|
|
257
|
-
if (hasSingleQuote && hasDoubleQuote) {
|
|
258
|
-
raw = `'${props.value.replace(/'/g, "\\'")}'`;
|
|
259
|
-
} else if (hasSingleQuote) {
|
|
260
|
-
raw = `"${props.value}"`;
|
|
261
|
-
} else {
|
|
262
|
-
raw = `'${props.value}'`;
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
return detachedProps<StringLiteralType>(props.parent, {
|
|
266
|
-
type: 'Literal',
|
|
267
|
-
raw,
|
|
268
|
-
value: props.value,
|
|
269
|
-
});
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
export type LineCommentProps = {+value: string};
|
|
273
|
-
export function LineComment(props: LineCommentProps): LineCommentType {
|
|
274
|
-
// $FlowExpectedError[prop-missing]
|
|
275
|
-
// $FlowExpectedError[incompatible-return]
|
|
276
|
-
return detachedProps<LineCommentType>(undefined, {
|
|
277
|
-
type: 'Line',
|
|
278
|
-
value: props.value,
|
|
279
|
-
});
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
export type BlockCommentProps = {+value: string};
|
|
283
|
-
export function BlockComment(props: BlockCommentProps): BlockCommentType {
|
|
284
|
-
// $FlowExpectedError[prop-missing]
|
|
285
|
-
// $FlowExpectedError[incompatible-return]
|
|
286
|
-
return detachedProps<BlockCommentType>(undefined, {
|
|
287
|
-
type: 'Block',
|
|
288
|
-
value: props.value,
|
|
289
|
-
});
|
|
290
|
-
}
|
|
17
|
+
export * from './special-case-node-types/Comment';
|
|
18
|
+
export * from './special-case-node-types/DeclareExportDeclaration';
|
|
19
|
+
export * from './special-case-node-types/ExportNamedDeclaration';
|
|
20
|
+
export * from './special-case-node-types/Literal';
|
|
21
|
+
export * from './special-case-node-types/ObjectTypeProperty';
|
|
22
|
+
export * from './special-case-node-types/misc';
|
|
23
|
+
export * from './special-case-node-types/Property';
|
package/dist/index.js
CHANGED
|
@@ -12,11 +12,11 @@
|
|
|
12
12
|
Object.defineProperty(exports, "__esModule", {
|
|
13
13
|
value: true
|
|
14
14
|
});
|
|
15
|
-
exports.traverseWithContext = exports.traverse = exports.transform = exports.t = exports.SimpleTraverser = void 0;
|
|
15
|
+
exports.traverseWithContext = exports.traverse = exports.transform = exports.t = exports.print = exports.parse = exports.cloneJSDocCommentsToNewNode = exports.asDetachedNode = exports.SimpleTraverser = void 0;
|
|
16
16
|
|
|
17
|
-
var
|
|
17
|
+
var _hermesParser = require("hermes-parser");
|
|
18
18
|
|
|
19
|
-
exports.SimpleTraverser =
|
|
19
|
+
exports.SimpleTraverser = _hermesParser.SimpleTraverser;
|
|
20
20
|
|
|
21
21
|
var _traverse = require("./traverse/traverse");
|
|
22
22
|
|
|
@@ -27,10 +27,26 @@ var _transform = require("./transform/transform");
|
|
|
27
27
|
|
|
28
28
|
exports.transform = _transform.transform;
|
|
29
29
|
|
|
30
|
+
var _parse = require("./transform/parse");
|
|
31
|
+
|
|
32
|
+
exports.parse = _parse.parse;
|
|
33
|
+
|
|
34
|
+
var _print = require("./transform/print");
|
|
35
|
+
|
|
36
|
+
exports.print = _print.print;
|
|
37
|
+
|
|
30
38
|
var _t = _interopRequireWildcard(require("./generated/node-types"));
|
|
31
39
|
|
|
32
40
|
exports.t = _t;
|
|
33
41
|
|
|
42
|
+
var _detachedNode = require("./detachedNode");
|
|
43
|
+
|
|
44
|
+
exports.asDetachedNode = _detachedNode.asDetachedNode;
|
|
45
|
+
|
|
46
|
+
var _comments = require("./transform/comments/comments");
|
|
47
|
+
|
|
48
|
+
exports.cloneJSDocCommentsToNewNode = _comments.cloneJSDocCommentsToNewNode;
|
|
49
|
+
|
|
34
50
|
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
35
51
|
|
|
36
52
|
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
package/dist/index.js.flow
CHANGED
|
@@ -13,9 +13,13 @@
|
|
|
13
13
|
export type {TraversalContextBase, Visitor} from './traverse/traverse';
|
|
14
14
|
export type {TransformVisitor} from './transform/transform';
|
|
15
15
|
export type {TransformContext} from './transform/TransformContext';
|
|
16
|
-
export type {DetachedNode} from './detachedNode';
|
|
16
|
+
export type {DetachedNode, MaybeDetachedNode} from './detachedNode';
|
|
17
17
|
|
|
18
|
-
export {SimpleTraverser} from '
|
|
18
|
+
export {SimpleTraverser} from 'hermes-parser';
|
|
19
19
|
export {traverse, traverseWithContext} from './traverse/traverse';
|
|
20
20
|
export {transform} from './transform/transform';
|
|
21
|
+
export {parse} from './transform/parse';
|
|
22
|
+
export {print} from './transform/print';
|
|
21
23
|
export * as t from './generated/node-types';
|
|
24
|
+
export {asDetachedNode} from './detachedNode';
|
|
25
|
+
export {cloneJSDocCommentsToNewNode} from './transform/comments/comments';
|
|
@@ -362,9 +362,9 @@ export function getTransformContext(): TransformContextAdditions {
|
|
|
362
362
|
return null;
|
|
363
363
|
}
|
|
364
364
|
|
|
365
|
-
return nodes.map(node => {
|
|
365
|
+
return nodes.map((node): DetachedNode<?ESNode> => {
|
|
366
366
|
if (node == null) {
|
|
367
|
-
// $FlowExpectedError[incompatible-
|
|
367
|
+
// $FlowExpectedError[incompatible-return]
|
|
368
368
|
return node;
|
|
369
369
|
}
|
|
370
370
|
return shallowCloneNode<T>(node, {});
|
|
@@ -10,6 +10,7 @@ exports.appendCommentToSource = appendCommentToSource;
|
|
|
10
10
|
exports.attachComments = attachComments;
|
|
11
11
|
exports.cloneComment = cloneComment;
|
|
12
12
|
exports.cloneCommentWithMarkers = cloneCommentWithMarkers;
|
|
13
|
+
exports.cloneJSDocCommentsToNewNode = cloneJSDocCommentsToNewNode;
|
|
13
14
|
exports.getCommentsForNode = getCommentsForNode;
|
|
14
15
|
exports.getLeadingCommentsForNode = getLeadingCommentsForNode;
|
|
15
16
|
exports.getTrailingCommentsForNode = getTrailingCommentsForNode;
|
|
@@ -26,6 +27,8 @@ var _printerEstree = _interopRequireDefault(require("./prettier/language-js/prin
|
|
|
26
27
|
|
|
27
28
|
var _util = require("./prettier/common/util");
|
|
28
29
|
|
|
30
|
+
var _hermesEstree = require("hermes-estree");
|
|
31
|
+
|
|
29
32
|
var _os = require("os");
|
|
30
33
|
|
|
31
34
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
@@ -59,6 +62,14 @@ function moveCommentsToNewNode(oldNode, newNode) {
|
|
|
59
62
|
setCommentsOnNode(oldNode, []);
|
|
60
63
|
}
|
|
61
64
|
|
|
65
|
+
function cloneJSDocCommentsToNewNode(oldNode, newNode) {
|
|
66
|
+
const comments = getCommentsForNode(oldNode).filter(comment => {
|
|
67
|
+
return (0, _hermesEstree.isBlockComment)(comment) && // JSDoc comments always start with an extra asterisk
|
|
68
|
+
comment.value.startsWith('*');
|
|
69
|
+
});
|
|
70
|
+
setCommentsOnNode(newNode, comments.map(cloneCommentWithMarkers));
|
|
71
|
+
}
|
|
72
|
+
|
|
62
73
|
function setCommentsOnNode(node, comments) {
|
|
63
74
|
// $FlowExpectedError - this property is secretly added by prettier.
|
|
64
75
|
node.comments = comments;
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
import type {Comment, ESNode, Program} from 'hermes-estree';
|
|
12
|
-
import type {DetachedNode} from '../../detachedNode';
|
|
12
|
+
import type {DetachedNode, MaybeDetachedNode} from '../../detachedNode';
|
|
13
13
|
|
|
14
14
|
// $FlowExpectedError[untyped-import]
|
|
15
15
|
import {attach as untypedAttach} from './prettier/main/comments';
|
|
@@ -23,6 +23,7 @@ import {
|
|
|
23
23
|
// $FlowExpectedError[untyped-import]
|
|
24
24
|
addTrailingComment as untypedAddTrailingComment,
|
|
25
25
|
} from './prettier/common/util';
|
|
26
|
+
import {isBlockComment} from 'hermes-estree';
|
|
26
27
|
import {EOL} from 'os';
|
|
27
28
|
|
|
28
29
|
export type Options = $ReadOnly<{}>;
|
|
@@ -54,6 +55,20 @@ export function moveCommentsToNewNode(
|
|
|
54
55
|
setCommentsOnNode(oldNode, []);
|
|
55
56
|
}
|
|
56
57
|
|
|
58
|
+
export function cloneJSDocCommentsToNewNode(
|
|
59
|
+
oldNode: ESNode,
|
|
60
|
+
newNode: MaybeDetachedNode<ESNode>,
|
|
61
|
+
): void {
|
|
62
|
+
const comments = getCommentsForNode(oldNode).filter(comment => {
|
|
63
|
+
return (
|
|
64
|
+
isBlockComment(comment) &&
|
|
65
|
+
// JSDoc comments always start with an extra asterisk
|
|
66
|
+
comment.value.startsWith('*')
|
|
67
|
+
);
|
|
68
|
+
});
|
|
69
|
+
setCommentsOnNode(newNode, comments.map(cloneCommentWithMarkers));
|
|
70
|
+
}
|
|
71
|
+
|
|
57
72
|
export function setCommentsOnNode(
|
|
58
73
|
node: ESNode | DetachedNode<ESNode>,
|
|
59
74
|
comments: $ReadOnlyArray<Comment>,
|
|
@@ -50,7 +50,7 @@ function getSortedChildNodes(node, options, resultArray) {
|
|
|
50
50
|
return childNodesCache.get(node);
|
|
51
51
|
}
|
|
52
52
|
|
|
53
|
-
const childNodes = printer.getCommentChildNodes && printer.getCommentChildNodes(node, options) || typeof node === 'object' && Object.entries(node).filter(([key]) => key !== 'enclosingNode' && key !== 'precedingNode' && key !== 'followingNode' && key !== 'tokens' && key !== 'comments').map(([, value]) => value);
|
|
53
|
+
const childNodes = printer.getCommentChildNodes && printer.getCommentChildNodes(node, options) || typeof node === 'object' && Object.entries(node).filter(([key]) => key !== 'enclosingNode' && key !== 'precedingNode' && key !== 'followingNode' && key !== 'tokens' && key !== 'comments' && key !== 'parent').map(([, value]) => value);
|
|
54
54
|
|
|
55
55
|
if (!childNodes) {
|
|
56
56
|
return;
|
|
@@ -6,7 +6,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports.createInsertStatementMutation = createInsertStatementMutation;
|
|
7
7
|
exports.performInsertStatementMutation = performInsertStatementMutation;
|
|
8
8
|
|
|
9
|
-
var
|
|
9
|
+
var _hermesParser = require("hermes-parser");
|
|
10
10
|
|
|
11
11
|
var _getStatementParent = require("./utils/getStatementParent");
|
|
12
12
|
|
|
@@ -58,13 +58,13 @@ function performInsertStatementMutation(mutationContext, mutation) {
|
|
|
58
58
|
switch (mutation.side) {
|
|
59
59
|
case 'before':
|
|
60
60
|
{
|
|
61
|
-
parent[insertionParent.key] =
|
|
61
|
+
parent[insertionParent.key] = _hermesParser.astArrayMutationHelpers.insertInArray(parent[insertionParent.key], insertionParent.targetIndex, mutation.nodesToInsert);
|
|
62
62
|
break;
|
|
63
63
|
}
|
|
64
64
|
|
|
65
65
|
case 'after':
|
|
66
66
|
{
|
|
67
|
-
parent[insertionParent.key] =
|
|
67
|
+
parent[insertionParent.key] = _hermesParser.astArrayMutationHelpers.insertInArray(parent[insertionParent.key], insertionParent.targetIndex + 1, mutation.nodesToInsert);
|
|
68
68
|
break;
|
|
69
69
|
}
|
|
70
70
|
}
|
|
@@ -12,7 +12,7 @@ import type {ESNode, ModuleDeclaration, Statement} from 'hermes-estree';
|
|
|
12
12
|
import type {MutationContext} from '../MutationContext';
|
|
13
13
|
import type {DetachedNode} from '../../detachedNode';
|
|
14
14
|
|
|
15
|
-
import {
|
|
15
|
+
import {astArrayMutationHelpers} from 'hermes-parser';
|
|
16
16
|
import {getStatementParent} from './utils/getStatementParent';
|
|
17
17
|
import {isValidModuleDeclarationParent} from './utils/isValidModuleDeclarationParent';
|
|
18
18
|
import {InvalidInsertionError} from '../Errors';
|
|
@@ -73,7 +73,7 @@ export function performInsertStatementMutation(
|
|
|
73
73
|
} = insertionParent.parent;
|
|
74
74
|
switch (mutation.side) {
|
|
75
75
|
case 'before': {
|
|
76
|
-
parent[insertionParent.key] = insertInArray(
|
|
76
|
+
parent[insertionParent.key] = astArrayMutationHelpers.insertInArray(
|
|
77
77
|
parent[insertionParent.key],
|
|
78
78
|
insertionParent.targetIndex,
|
|
79
79
|
mutation.nodesToInsert,
|
|
@@ -82,7 +82,7 @@ export function performInsertStatementMutation(
|
|
|
82
82
|
}
|
|
83
83
|
|
|
84
84
|
case 'after': {
|
|
85
|
-
parent[insertionParent.key] = insertInArray(
|
|
85
|
+
parent[insertionParent.key] = astArrayMutationHelpers.insertInArray(
|
|
86
86
|
parent[insertionParent.key],
|
|
87
87
|
insertionParent.targetIndex + 1,
|
|
88
88
|
mutation.nodesToInsert,
|
|
@@ -6,7 +6,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports.createRemoveCommentMutation = createRemoveCommentMutation;
|
|
7
7
|
exports.performRemoveCommentMutations = performRemoveCommentMutations;
|
|
8
8
|
|
|
9
|
-
var
|
|
9
|
+
var _hermesParser = require("hermes-parser");
|
|
10
10
|
|
|
11
11
|
var _comments = require("../comments/comments");
|
|
12
12
|
|
|
@@ -33,7 +33,7 @@ function performRemoveCommentMutations(ast, mutations) {
|
|
|
33
33
|
|
|
34
34
|
const commentsToRemove = new Set(mutations.map(m => m.comment));
|
|
35
35
|
|
|
36
|
-
|
|
36
|
+
_hermesParser.SimpleTraverser.traverse(ast, {
|
|
37
37
|
enter(node) {
|
|
38
38
|
if (node === ast) {
|
|
39
39
|
return;
|
|
@@ -56,7 +56,7 @@ function performRemoveCommentMutations(ast, mutations) {
|
|
|
56
56
|
|
|
57
57
|
if (commentsToRemove.size === 0) {
|
|
58
58
|
// no more comments to process - so we can exit traversal
|
|
59
|
-
throw
|
|
59
|
+
throw _hermesParser.SimpleTraverserBreak;
|
|
60
60
|
}
|
|
61
61
|
},
|
|
62
62
|
|
|
@@ -9,10 +9,8 @@
|
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
import type {Comment, Program} from 'hermes-estree';
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
SimpleTraverserBreak,
|
|
15
|
-
} from '../../traverse/SimpleTraverser';
|
|
12
|
+
|
|
13
|
+
import {SimpleTraverser, SimpleTraverserBreak} from 'hermes-parser';
|
|
16
14
|
import {getCommentsForNode, setCommentsOnNode} from '../comments/comments';
|
|
17
15
|
|
|
18
16
|
export type RemoveCommentMutation = $ReadOnly<{
|
|
@@ -70,7 +68,7 @@ function intersectSets<T>(
|
|
|
70
68
|
first: $ReadOnlySet<T>,
|
|
71
69
|
other: $ReadOnlySet<T>,
|
|
72
70
|
): Set<T> {
|
|
73
|
-
const ret = new Set();
|
|
71
|
+
const ret = new Set<T>();
|
|
74
72
|
for (const value of first) {
|
|
75
73
|
if (other.has(value)) {
|
|
76
74
|
ret.add(value);
|
|
@@ -6,7 +6,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports.createRemoveNodeMutation = createRemoveNodeMutation;
|
|
7
7
|
exports.performRemoveNodeMutation = performRemoveNodeMutation;
|
|
8
8
|
|
|
9
|
-
var
|
|
9
|
+
var _hermesParser = require("hermes-parser");
|
|
10
10
|
|
|
11
11
|
var _Errors = require("../Errors");
|
|
12
12
|
|
|
@@ -187,6 +187,6 @@ function performRemoveNodeMutation(mutationContext, mutation) {
|
|
|
187
187
|
mutationContext.markDeletion(mutation.node);
|
|
188
188
|
mutationContext.markMutation(removalParent.parent, removalParent.key);
|
|
189
189
|
const parent = removalParent.parent;
|
|
190
|
-
parent[removalParent.key] =
|
|
190
|
+
parent[removalParent.key] = _hermesParser.astArrayMutationHelpers.removeFromArray(parent[removalParent.key], removalParent.targetIndex);
|
|
191
191
|
return removalParent.parent;
|
|
192
192
|
}
|
|
@@ -30,7 +30,7 @@ import type {
|
|
|
30
30
|
import type {MutationContext} from '../MutationContext';
|
|
31
31
|
import type {DetachedNode} from '../../detachedNode';
|
|
32
32
|
|
|
33
|
-
import {
|
|
33
|
+
import {astArrayMutationHelpers} from 'hermes-parser';
|
|
34
34
|
import {InvalidRemovalError} from '../Errors';
|
|
35
35
|
|
|
36
36
|
export type RemoveNodeMutation = $ReadOnly<{
|
|
@@ -281,7 +281,7 @@ export function performRemoveNodeMutation(
|
|
|
281
281
|
const parent: interface {
|
|
282
282
|
[string]: $ReadOnlyArray<DetachedNode<RemoveNodeMutation['node']>>,
|
|
283
283
|
} = removalParent.parent;
|
|
284
|
-
parent[removalParent.key] = removeFromArray(
|
|
284
|
+
parent[removalParent.key] = astArrayMutationHelpers.removeFromArray(
|
|
285
285
|
parent[removalParent.key],
|
|
286
286
|
removalParent.targetIndex,
|
|
287
287
|
);
|
|
@@ -6,7 +6,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports.createRemoveStatementMutation = createRemoveStatementMutation;
|
|
7
7
|
exports.performRemoveStatementMutation = performRemoveStatementMutation;
|
|
8
8
|
|
|
9
|
-
var
|
|
9
|
+
var _hermesParser = require("hermes-parser");
|
|
10
10
|
|
|
11
11
|
var _getStatementParent = require("./utils/getStatementParent");
|
|
12
12
|
|
|
@@ -39,7 +39,7 @@ function performRemoveStatementMutation(mutationContext, mutation) {
|
|
|
39
39
|
|
|
40
40
|
if (removalParent.type === 'array') {
|
|
41
41
|
const parent = removalParent.parent;
|
|
42
|
-
parent[removalParent.key] =
|
|
42
|
+
parent[removalParent.key] = _hermesParser.astArrayMutationHelpers.removeFromArray(parent[removalParent.key], removalParent.targetIndex);
|
|
43
43
|
} else {
|
|
44
44
|
// The parent has a 1:1 relationship on this key, so we can't just
|
|
45
45
|
// remove the node. Instead we replace it with an empty block statement.
|
|
@@ -12,7 +12,7 @@ import type {ESNode, ModuleDeclaration, Statement} from 'hermes-estree';
|
|
|
12
12
|
import type {MutationContext} from '../MutationContext';
|
|
13
13
|
import type {DetachedNode} from '../../detachedNode';
|
|
14
14
|
|
|
15
|
-
import {
|
|
15
|
+
import {astArrayMutationHelpers} from 'hermes-parser';
|
|
16
16
|
import {getStatementParent} from './utils/getStatementParent';
|
|
17
17
|
import * as t from '../../generated/node-types';
|
|
18
18
|
|
|
@@ -43,7 +43,7 @@ export function performRemoveStatementMutation(
|
|
|
43
43
|
const parent: interface {
|
|
44
44
|
[string]: $ReadOnlyArray<DetachedNode<Statement | ModuleDeclaration>>,
|
|
45
45
|
} = removalParent.parent;
|
|
46
|
-
parent[removalParent.key] = removeFromArray(
|
|
46
|
+
parent[removalParent.key] = astArrayMutationHelpers.removeFromArray(
|
|
47
47
|
parent[removalParent.key],
|
|
48
48
|
removalParent.targetIndex,
|
|
49
49
|
);
|