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
|
@@ -9,19 +9,9 @@ exports.DeclareExportDefaultDeclaration = DeclareExportDefaultDeclaration;
|
|
|
9
9
|
|
|
10
10
|
var _detachedNode = require("../../detachedNode");
|
|
11
11
|
|
|
12
|
-
/**
|
|
13
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
14
|
-
*
|
|
15
|
-
* This source code is licensed under the MIT license found in the
|
|
16
|
-
* LICENSE file in the root directory of this source tree.
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
* @format
|
|
20
|
-
*/
|
|
21
12
|
function DeclareExportDefaultDeclaration(props) {
|
|
22
13
|
const node = (0, _detachedNode.detachedProps)(props.parent, {
|
|
23
14
|
type: 'DeclareExportDeclaration',
|
|
24
|
-
// $FlowFixMe[incompatible-type]
|
|
25
15
|
declaration: (0, _detachedNode.asDetachedNode)(props.declaration),
|
|
26
16
|
specifiers: [],
|
|
27
17
|
source: null,
|
|
@@ -34,7 +24,6 @@ function DeclareExportDefaultDeclaration(props) {
|
|
|
34
24
|
function DeclareExportDeclarationNamedWithDeclaration(props) {
|
|
35
25
|
const node = (0, _detachedNode.detachedProps)(props.parent, {
|
|
36
26
|
type: 'DeclareExportDeclaration',
|
|
37
|
-
// $FlowFixMe[incompatible-type]
|
|
38
27
|
declaration: (0, _detachedNode.asDetachedNode)(props.declaration),
|
|
39
28
|
specifiers: [],
|
|
40
29
|
source: null,
|
|
@@ -49,7 +38,6 @@ function DeclareExportDeclarationNamedWithSpecifiers(props) {
|
|
|
49
38
|
type: 'DeclareExportDeclaration',
|
|
50
39
|
declaration: null,
|
|
51
40
|
specifiers: props.specifiers.map(n => (0, _detachedNode.asDetachedNode)(n)),
|
|
52
|
-
// $FlowFixMe[incompatible-type]
|
|
53
41
|
source: (0, _detachedNode.asDetachedNode)(props.source),
|
|
54
42
|
default: false
|
|
55
43
|
});
|
|
@@ -7,15 +7,6 @@ exports.DeclareHook = DeclareHook;
|
|
|
7
7
|
|
|
8
8
|
var _detachedNode = require("../../detachedNode");
|
|
9
9
|
|
|
10
|
-
/**
|
|
11
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
12
|
-
*
|
|
13
|
-
* This source code is licensed under the MIT license found in the
|
|
14
|
-
* LICENSE file in the root directory of this source tree.
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
* @format
|
|
18
|
-
*/
|
|
19
10
|
function DeclareHook(props) {
|
|
20
11
|
const node = (0, _detachedNode.detachedProps)(props.parent, {
|
|
21
12
|
type: 'DeclareHook',
|
|
@@ -8,19 +8,9 @@ exports.ExportNamedDeclarationWithSpecifiers = ExportNamedDeclarationWithSpecifi
|
|
|
8
8
|
|
|
9
9
|
var _detachedNode = require("../../detachedNode");
|
|
10
10
|
|
|
11
|
-
/**
|
|
12
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
13
|
-
*
|
|
14
|
-
* This source code is licensed under the MIT license found in the
|
|
15
|
-
* LICENSE file in the root directory of this source tree.
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
* @format
|
|
19
|
-
*/
|
|
20
11
|
function ExportNamedDeclarationWithDeclaration(props) {
|
|
21
12
|
const node = (0, _detachedNode.detachedProps)(props.parent, {
|
|
22
13
|
type: 'ExportNamedDeclaration',
|
|
23
|
-
// $FlowFixMe[incompatible-type]
|
|
24
14
|
declaration: (0, _detachedNode.asDetachedNode)(props.declaration),
|
|
25
15
|
specifiers: [],
|
|
26
16
|
source: null,
|
|
@@ -35,7 +25,6 @@ function ExportNamedDeclarationWithSpecifiers(props) {
|
|
|
35
25
|
type: 'ExportNamedDeclaration',
|
|
36
26
|
declaration: null,
|
|
37
27
|
specifiers: props.specifiers.map(n => (0, _detachedNode.asDetachedNode)(n)),
|
|
38
|
-
// $FlowFixMe[incompatible-type]
|
|
39
28
|
source: (0, _detachedNode.asDetachedNode)(props.source),
|
|
40
29
|
exportKind: props.exportKind
|
|
41
30
|
});
|
|
@@ -12,15 +12,6 @@ exports.StringLiteral = StringLiteral;
|
|
|
12
12
|
|
|
13
13
|
var _detachedNode = require("../../detachedNode");
|
|
14
14
|
|
|
15
|
-
/**
|
|
16
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
17
|
-
*
|
|
18
|
-
* This source code is licensed under the MIT license found in the
|
|
19
|
-
* LICENSE file in the root directory of this source tree.
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
* @format
|
|
23
|
-
*/
|
|
24
15
|
function BigIntLiteral(props) {
|
|
25
16
|
var _props$raw;
|
|
26
17
|
|
|
@@ -57,9 +48,7 @@ function NullLiteral(props = { ...null
|
|
|
57
48
|
value: null,
|
|
58
49
|
raw: 'null'
|
|
59
50
|
});
|
|
60
|
-
}
|
|
61
|
-
// also the value is supposed to be a RegExp instance
|
|
62
|
-
|
|
51
|
+
}
|
|
63
52
|
|
|
64
53
|
function RegExpLiteral(props) {
|
|
65
54
|
const value = new RegExp(props.pattern, props.flags);
|
|
@@ -9,15 +9,6 @@ exports.ObjectTypePropertySignature = ObjectTypePropertySignature;
|
|
|
9
9
|
|
|
10
10
|
var _detachedNode = require("../../detachedNode");
|
|
11
11
|
|
|
12
|
-
/**
|
|
13
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
14
|
-
*
|
|
15
|
-
* This source code is licensed under the MIT license found in the
|
|
16
|
-
* LICENSE file in the root directory of this source tree.
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
* @format
|
|
20
|
-
*/
|
|
21
12
|
function ObjectTypeMethodSignature(props) {
|
|
22
13
|
var _props$proto, _props$static;
|
|
23
14
|
|
|
@@ -48,7 +39,6 @@ function ObjectTypePropertySignature(props) {
|
|
|
48
39
|
proto: (_props$proto2 = props.proto) != null ? _props$proto2 : false,
|
|
49
40
|
static: (_props$static2 = props.static) != null ? _props$static2 : false,
|
|
50
41
|
value: (0, _detachedNode.asDetachedNode)(props.value),
|
|
51
|
-
// $FlowFixMe[incompatible-type]
|
|
52
42
|
variance: (0, _detachedNode.asDetachedNode)(props.variance)
|
|
53
43
|
});
|
|
54
44
|
(0, _detachedNode.setParentPointersInDirectChildren)(node);
|
|
@@ -14,15 +14,6 @@ exports.ObjectPropertyWithShorthandStaticName = ObjectPropertyWithShorthandStati
|
|
|
14
14
|
|
|
15
15
|
var _detachedNode = require("../../detachedNode");
|
|
16
16
|
|
|
17
|
-
/**
|
|
18
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
19
|
-
*
|
|
20
|
-
* This source code is licensed under the MIT license found in the
|
|
21
|
-
* LICENSE file in the root directory of this source tree.
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
* @format
|
|
25
|
-
*/
|
|
26
17
|
function DestructuringObjectProperty(props) {
|
|
27
18
|
const node = (0, _detachedNode.detachedProps)(props.parent, {
|
|
28
19
|
type: 'Property',
|
|
@@ -14,28 +14,15 @@ exports.TemplateElement = TemplateElement;
|
|
|
14
14
|
|
|
15
15
|
var _detachedNode = require("../../detachedNode");
|
|
16
16
|
|
|
17
|
-
/**
|
|
18
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
19
|
-
*
|
|
20
|
-
* This source code is licensed under the MIT license found in the
|
|
21
|
-
* LICENSE file in the root directory of this source tree.
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
* @format
|
|
25
|
-
*/
|
|
26
17
|
function ArrowFunctionExpression(props) {
|
|
27
18
|
const node = (0, _detachedNode.detachedProps)(props.parent, {
|
|
28
19
|
type: 'ArrowFunctionExpression',
|
|
29
20
|
id: null,
|
|
30
|
-
// $FlowExpectedError[incompatible-use]
|
|
31
21
|
expression: props.body.type !== 'BlockStatement',
|
|
32
22
|
params: props.params.map(n => (0, _detachedNode.asDetachedNode)(n)),
|
|
33
23
|
body: (0, _detachedNode.asDetachedNode)(props.body),
|
|
34
|
-
// $FlowFixMe[incompatible-type]
|
|
35
24
|
typeParameters: (0, _detachedNode.asDetachedNode)(props.typeParameters),
|
|
36
|
-
// $FlowFixMe[incompatible-type]
|
|
37
25
|
returnType: (0, _detachedNode.asDetachedNode)(props.returnType),
|
|
38
|
-
// $FlowFixMe[incompatible-type]
|
|
39
26
|
predicate: (0, _detachedNode.asDetachedNode)(props.predicate),
|
|
40
27
|
async: props.async
|
|
41
28
|
});
|
|
@@ -48,13 +35,9 @@ function ClassDeclaration(props) {
|
|
|
48
35
|
|
|
49
36
|
const node = (0, _detachedNode.detachedProps)(props.parent, {
|
|
50
37
|
type: 'ClassDeclaration',
|
|
51
|
-
// $FlowFixMe[incompatible-type]
|
|
52
38
|
id: (0, _detachedNode.asDetachedNode)(props.id),
|
|
53
|
-
// $FlowFixMe[incompatible-type]
|
|
54
39
|
typeParameters: (0, _detachedNode.asDetachedNode)(props.typeParameters),
|
|
55
|
-
// $FlowFixMe[incompatible-type]
|
|
56
40
|
superClass: (0, _detachedNode.asDetachedNode)(props.superClass),
|
|
57
|
-
// $FlowFixMe[incompatible-type]
|
|
58
41
|
superTypeArguments: (0, _detachedNode.asDetachedNode)(props.superTypeArguments),
|
|
59
42
|
decorators: ((_props$decorators = props.decorators) != null ? _props$decorators : []).map(n => (0, _detachedNode.asDetachedNode)(n)),
|
|
60
43
|
implements: ((_props$implements = props.implements) != null ? _props$implements : []).map(n => (0, _detachedNode.asDetachedNode)(n)),
|
|
@@ -62,8 +45,7 @@ function ClassDeclaration(props) {
|
|
|
62
45
|
});
|
|
63
46
|
(0, _detachedNode.setParentPointersInDirectChildren)(node);
|
|
64
47
|
return node;
|
|
65
|
-
}
|
|
66
|
-
|
|
48
|
+
}
|
|
67
49
|
|
|
68
50
|
function TemplateElement(props) {
|
|
69
51
|
return (0, _detachedNode.detachedProps)(props.parent, {
|
|
@@ -74,9 +56,7 @@ function TemplateElement(props) {
|
|
|
74
56
|
raw: props.raw
|
|
75
57
|
}
|
|
76
58
|
});
|
|
77
|
-
}
|
|
78
|
-
// this manual def to allow us to default some values
|
|
79
|
-
|
|
59
|
+
}
|
|
80
60
|
|
|
81
61
|
function Identifier(props) {
|
|
82
62
|
var _props$optional;
|
|
@@ -85,14 +65,11 @@ function Identifier(props) {
|
|
|
85
65
|
type: 'Identifier',
|
|
86
66
|
name: props.name,
|
|
87
67
|
optional: (_props$optional = props.optional) != null ? _props$optional : false,
|
|
88
|
-
// $FlowFixMe[incompatible-type]
|
|
89
68
|
typeAnnotation: (0, _detachedNode.asDetachedNode)(props.typeAnnotation)
|
|
90
69
|
});
|
|
91
70
|
(0, _detachedNode.setParentPointersInDirectChildren)(node);
|
|
92
71
|
return node;
|
|
93
|
-
}
|
|
94
|
-
// this manual def to allow us to default some values
|
|
95
|
-
|
|
72
|
+
}
|
|
96
73
|
|
|
97
74
|
function Program(props) {
|
|
98
75
|
var _props$sourceType, _props$tokens, _props$comments;
|
|
@@ -103,17 +80,13 @@ function Program(props) {
|
|
|
103
80
|
body: props.body.map(n => (0, _detachedNode.asDetachedNode)(n)),
|
|
104
81
|
tokens: (_props$tokens = props.tokens) != null ? _props$tokens : [],
|
|
105
82
|
comments: (_props$comments = props.comments) != null ? _props$comments : [],
|
|
106
|
-
interpreter: props.interpreter != null ?
|
|
107
|
-
(0, _detachedNode.asDetachedNode)({
|
|
83
|
+
interpreter: props.interpreter != null ? (0, _detachedNode.asDetachedNode)({
|
|
108
84
|
type: 'InterpreterDirective',
|
|
109
85
|
value: props.interpreter
|
|
110
86
|
}) : null,
|
|
111
87
|
docblock: props.docblock
|
|
112
88
|
});
|
|
113
|
-
}
|
|
114
|
-
// which is super awkward to work with and type - so we flatten the input
|
|
115
|
-
// and put it in the right spot after
|
|
116
|
-
|
|
89
|
+
}
|
|
117
90
|
|
|
118
91
|
function DeclareFunction(props) {
|
|
119
92
|
const node = (0, _detachedNode.detachedProps)(props.parent, {
|
|
@@ -126,7 +99,6 @@ function DeclareFunction(props) {
|
|
|
126
99
|
typeAnnotation: (0, _detachedNode.asDetachedNode)(props.functionType)
|
|
127
100
|
})
|
|
128
101
|
}),
|
|
129
|
-
// $FlowFixMe[incompatible-type]
|
|
130
102
|
predicate: (0, _detachedNode.asDetachedNode)(props.predicate)
|
|
131
103
|
});
|
|
132
104
|
(0, _detachedNode.setParentPointersInDirectChildren)(node);
|
|
@@ -145,8 +117,7 @@ function MemberExpression(props) {
|
|
|
145
117
|
});
|
|
146
118
|
(0, _detachedNode.setParentPointersInDirectChildren)(node);
|
|
147
119
|
return node;
|
|
148
|
-
}
|
|
149
|
-
|
|
120
|
+
}
|
|
150
121
|
|
|
151
122
|
function BlockStatement(props) {
|
|
152
123
|
const node = (0, _detachedNode.detachedProps)(props.parent, {
|
package/dist/src/index.js
CHANGED
|
@@ -1,12 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
-
*
|
|
4
|
-
* This source code is licensed under the MIT license found in the
|
|
5
|
-
* LICENSE file in the root directory of this source tree.
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* @format
|
|
9
|
-
*/
|
|
10
1
|
'use strict';
|
|
11
2
|
|
|
12
3
|
Object.defineProperty(exports, "__esModule", {
|
|
@@ -5,15 +5,6 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.UnexpectedTransformationState = exports.NodeIsMutatedError = exports.NodeIsDeletedError = exports.InvalidStatementError = exports.InvalidReplacementError = exports.InvalidRemovalError = exports.InvalidInsertionError = void 0;
|
|
7
7
|
|
|
8
|
-
/**
|
|
9
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
10
|
-
*
|
|
11
|
-
* This source code is licensed under the MIT license found in the
|
|
12
|
-
* LICENSE file in the root directory of this source tree.
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
* @format
|
|
16
|
-
*/
|
|
17
8
|
class NodeIsDeletedError extends Error {}
|
|
18
9
|
|
|
19
10
|
exports.NodeIsDeletedError = NodeIsDeletedError;
|
|
@@ -9,17 +9,7 @@ var _Errors = require("./Errors");
|
|
|
9
9
|
|
|
10
10
|
var _comments = require("./comments/comments");
|
|
11
11
|
|
|
12
|
-
/**
|
|
13
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
14
|
-
*
|
|
15
|
-
* This source code is licensed under the MIT license found in the
|
|
16
|
-
* LICENSE file in the root directory of this source tree.
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
* @format
|
|
20
|
-
*/
|
|
21
12
|
class MutationContext {
|
|
22
|
-
// TODO - do we care about this? Arrays are pretty safe to concurrently mutate
|
|
23
13
|
constructor(code) {
|
|
24
14
|
this._deletedNodes = new Set();
|
|
25
15
|
this._mutatedKeys = new Map();
|
|
@@ -27,42 +17,25 @@ class MutationContext {
|
|
|
27
17
|
this.code = void 0;
|
|
28
18
|
this.code = code;
|
|
29
19
|
}
|
|
30
|
-
/**
|
|
31
|
-
* Marks a node and its entire subtree as deleted.
|
|
32
|
-
*/
|
|
33
|
-
|
|
34
20
|
|
|
35
21
|
markDeletion(node) {
|
|
36
22
|
this._deletedNodes.add(node);
|
|
37
23
|
}
|
|
38
|
-
/**
|
|
39
|
-
* Marks the key of the node as having been mutated.
|
|
40
|
-
*/
|
|
41
|
-
|
|
42
24
|
|
|
43
25
|
markMutation(node, key) {
|
|
44
26
|
var _map$get$add, _map$get;
|
|
45
27
|
|
|
46
28
|
this.assertNotDeleted(node, `Attempted to mutate a \`${node.type}.${key}\` on a deleted node.`);
|
|
47
29
|
this.assertNotMutated(node, key, `Attempted to mutate a \`${node.type}.${key}\` when it has already been mutated.`);
|
|
48
|
-
const map = Array.isArray(
|
|
49
|
-
node[key]) ? this._mutatedArrays : this._mutatedKeys;
|
|
30
|
+
const map = Array.isArray(node[key]) ? this._mutatedArrays : this._mutatedKeys;
|
|
50
31
|
map.set(node, (_map$get$add = (_map$get = map.get(node)) == null ? void 0 : _map$get.add(key)) != null ? _map$get$add : new Set([key]));
|
|
51
32
|
}
|
|
52
|
-
/**
|
|
53
|
-
* Throws if the node has been deleted
|
|
54
|
-
*/
|
|
55
|
-
|
|
56
33
|
|
|
57
34
|
assertNotDeleted(node, message) {
|
|
58
35
|
if (this._deletedNodes.has(node)) {
|
|
59
36
|
throw new _Errors.NodeIsDeletedError(message);
|
|
60
37
|
}
|
|
61
38
|
}
|
|
62
|
-
/**
|
|
63
|
-
* Throws if the key of the node has been mutated
|
|
64
|
-
*/
|
|
65
|
-
|
|
66
39
|
|
|
67
40
|
assertNotMutated(node, key, message) {
|
|
68
41
|
var _this$_mutatedKeys$ge;
|
|
@@ -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
|
},
|
|
@@ -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
|
}
|