hermes-parser 0.32.0 → 0.33.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/README.md +3 -0
- package/dist/HermesASTAdapter.js +1 -1
- package/dist/HermesASTAdapter.js.flow +1 -1
- package/dist/HermesParser.js +2 -2
- package/dist/HermesParser.js.flow +2 -0
- package/dist/HermesParserDeserializer.js +0 -1
- package/dist/HermesParserDeserializer.js.flow +0 -1
- package/dist/HermesParserNodeDeserializers.js +109 -5
- package/dist/HermesParserWASM.js +1 -1
- package/dist/HermesParserWASM.js.flow +11 -1
- package/dist/ParserOptions.js +1 -1
- package/dist/ParserOptions.js.flow +10 -0
- package/dist/babel/TransformESTreeToBabel.js +79 -3
- package/dist/babel/TransformESTreeToBabel.js.flow +76 -2
- package/dist/estree/StripFlowTypes.js +1 -1
- package/dist/estree/StripFlowTypes.js.flow +1 -1
- package/dist/estree/{StripComponentSyntax.js → TransformComponentSyntax.js} +1 -1
- package/dist/estree/{StripComponentSyntax.js.flow → TransformComponentSyntax.js.flow} +1 -1
- package/dist/estree/TransformEnumSyntax.js +106 -0
- package/dist/estree/TransformEnumSyntax.js.flow +125 -0
- package/dist/estree/TransformMatchSyntax.js +124 -56
- package/dist/estree/TransformMatchSyntax.js.flow +124 -46
- package/dist/estree/TransformRecordSyntax.js +294 -0
- package/dist/estree/TransformRecordSyntax.js.flow +308 -0
- package/dist/generated/ESTreeVisitorKeys.js +16 -4
- package/dist/generated/ParserVisitorKeys.js +45 -4
- package/dist/index.js +13 -3
- package/dist/index.js.flow +13 -3
- package/dist/src/HermesASTAdapter.js +1 -1
- package/dist/src/HermesParser.js +2 -2
- package/dist/src/HermesParserDeserializer.js +0 -1
- package/dist/src/HermesParserNodeDeserializers.js +109 -5
- package/dist/src/ParserOptions.js +1 -1
- package/dist/src/babel/TransformESTreeToBabel.js +79 -3
- package/dist/src/estree/StripFlowTypes.js +1 -1
- package/dist/src/estree/{StripComponentSyntax.js → TransformComponentSyntax.js} +1 -1
- package/dist/src/estree/TransformEnumSyntax.js +106 -0
- package/dist/src/estree/TransformMatchSyntax.js +124 -56
- package/dist/src/estree/TransformRecordSyntax.js +294 -0
- package/dist/src/generated/ESTreeVisitorKeys.js +16 -4
- package/dist/src/generated/ParserVisitorKeys.js +45 -4
- package/dist/src/index.js +13 -3
- package/dist/src/transform/SimpleTransform.js +20 -4
- package/dist/src/transform/astNodeMutationHelpers.js +7 -2
- package/dist/src/utils/GenID.js +28 -23
- package/dist/src/utils/isReservedWord.js +62 -0
- package/dist/transform/SimpleTransform.js +20 -4
- package/dist/transform/SimpleTransform.js.flow +34 -8
- package/dist/transform/astNodeMutationHelpers.js +7 -2
- package/dist/transform/astNodeMutationHelpers.js.flow +10 -2
- package/dist/utils/GenID.js +28 -23
- package/dist/utils/GenID.js.flow +23 -22
- package/dist/utils/isReservedWord.js +62 -0
- package/dist/utils/isReservedWord.js.flow +57 -0
- package/package.json +2 -2
|
@@ -26,7 +26,7 @@ module.exports = {
|
|
|
26
26
|
ArrayExpression: ['elements'],
|
|
27
27
|
ArrayPattern: ['elements', 'typeAnnotation'],
|
|
28
28
|
ArrayTypeAnnotation: ['elementType'],
|
|
29
|
-
ArrowFunctionExpression: ['
|
|
29
|
+
ArrowFunctionExpression: ['params', 'body', 'typeParameters', 'returnType', 'predicate'],
|
|
30
30
|
AsConstExpression: ['expression'],
|
|
31
31
|
AsExpression: ['expression', 'typeAnnotation'],
|
|
32
32
|
AssignmentExpression: ['left', 'right'],
|
|
@@ -43,8 +43,8 @@ module.exports = {
|
|
|
43
43
|
CatchClause: ['param', 'body'],
|
|
44
44
|
ChainExpression: ['expression'],
|
|
45
45
|
ClassBody: ['body'],
|
|
46
|
-
ClassDeclaration: ['id', 'typeParameters', 'superClass', '
|
|
47
|
-
ClassExpression: ['id', 'typeParameters', 'superClass', '
|
|
46
|
+
ClassDeclaration: ['id', 'typeParameters', 'superClass', 'superTypeArguments', 'implements', 'decorators', 'body'],
|
|
47
|
+
ClassExpression: ['id', 'typeParameters', 'superClass', 'superTypeArguments', 'implements', 'decorators', 'body'],
|
|
48
48
|
ClassImplements: ['id', 'typeParameters'],
|
|
49
49
|
ComponentDeclaration: ['id', 'params', 'body', 'typeParameters', 'rendersType'],
|
|
50
50
|
ComponentParameter: ['name', 'local'],
|
|
@@ -138,6 +138,8 @@ module.exports = {
|
|
|
138
138
|
MatchExpression: ['argument', 'cases'],
|
|
139
139
|
MatchExpressionCase: ['pattern', 'body', 'guard'],
|
|
140
140
|
MatchIdentifierPattern: ['id'],
|
|
141
|
+
MatchInstanceObjectPattern: ['properties', 'rest'],
|
|
142
|
+
MatchInstancePattern: ['targetConstructor', 'properties'],
|
|
141
143
|
MatchLiteralPattern: ['literal'],
|
|
142
144
|
MatchMemberPattern: ['base', 'property'],
|
|
143
145
|
MatchObjectPattern: ['properties', 'rest'],
|
|
@@ -152,6 +154,7 @@ module.exports = {
|
|
|
152
154
|
MetaProperty: ['meta', 'property'],
|
|
153
155
|
MethodDefinition: ['key', 'value'],
|
|
154
156
|
MixedTypeAnnotation: [],
|
|
157
|
+
NeverTypeAnnotation: [],
|
|
155
158
|
NewExpression: ['callee', 'typeArguments', 'arguments'],
|
|
156
159
|
NullableTypeAnnotation: ['typeAnnotation'],
|
|
157
160
|
NullLiteralTypeAnnotation: [],
|
|
@@ -174,6 +177,13 @@ module.exports = {
|
|
|
174
177
|
PropertyDefinition: ['key', 'value', 'variance', 'typeAnnotation'],
|
|
175
178
|
QualifiedTypeIdentifier: ['qualification', 'id'],
|
|
176
179
|
QualifiedTypeofIdentifier: ['qualification', 'id'],
|
|
180
|
+
RecordDeclaration: ['id', 'typeParameters', 'implements', 'body'],
|
|
181
|
+
RecordDeclarationBody: ['elements'],
|
|
182
|
+
RecordDeclarationImplements: ['id', 'typeArguments'],
|
|
183
|
+
RecordDeclarationProperty: ['key', 'typeAnnotation', 'defaultValue'],
|
|
184
|
+
RecordDeclarationStaticProperty: ['key', 'typeAnnotation', 'value'],
|
|
185
|
+
RecordExpression: ['recordConstructor', 'typeArguments', 'properties'],
|
|
186
|
+
RecordExpressionProperties: ['properties'],
|
|
177
187
|
RestElement: ['argument'],
|
|
178
188
|
ReturnStatement: ['argument'],
|
|
179
189
|
SequenceExpression: ['expressions'],
|
|
@@ -192,7 +202,7 @@ module.exports = {
|
|
|
192
202
|
ThisTypeAnnotation: [],
|
|
193
203
|
ThrowStatement: ['argument'],
|
|
194
204
|
TryStatement: ['block', 'handler', 'finalizer'],
|
|
195
|
-
TupleTypeAnnotation: ['
|
|
205
|
+
TupleTypeAnnotation: ['elementTypes'],
|
|
196
206
|
TupleTypeLabeledElement: ['label', 'elementType', 'variance'],
|
|
197
207
|
TupleTypeSpreadElement: ['label', 'typeAnnotation'],
|
|
198
208
|
TypeAlias: ['id', 'typeParameters', 'right'],
|
|
@@ -205,7 +215,9 @@ module.exports = {
|
|
|
205
215
|
TypeParameterInstantiation: ['params'],
|
|
206
216
|
TypePredicate: ['parameterName', 'typeAnnotation'],
|
|
207
217
|
UnaryExpression: ['argument'],
|
|
218
|
+
UndefinedTypeAnnotation: [],
|
|
208
219
|
UnionTypeAnnotation: ['types'],
|
|
220
|
+
UnknownTypeAnnotation: [],
|
|
209
221
|
UpdateExpression: ['argument'],
|
|
210
222
|
VariableDeclaration: ['declarations'],
|
|
211
223
|
VariableDeclarator: ['id', 'init'],
|
|
@@ -42,7 +42,6 @@ const HERMES_AST_VISITOR_KEYS = {
|
|
|
42
42
|
elementType: 'Node'
|
|
43
43
|
},
|
|
44
44
|
ArrowFunctionExpression: {
|
|
45
|
-
id: 'Node',
|
|
46
45
|
params: 'NodeList',
|
|
47
46
|
body: 'Node',
|
|
48
47
|
typeParameters: 'Node',
|
|
@@ -102,7 +101,7 @@ const HERMES_AST_VISITOR_KEYS = {
|
|
|
102
101
|
id: 'Node',
|
|
103
102
|
typeParameters: 'Node',
|
|
104
103
|
superClass: 'Node',
|
|
105
|
-
|
|
104
|
+
superTypeArguments: 'Node',
|
|
106
105
|
implements: 'NodeList',
|
|
107
106
|
decorators: 'NodeList',
|
|
108
107
|
body: 'Node'
|
|
@@ -111,7 +110,7 @@ const HERMES_AST_VISITOR_KEYS = {
|
|
|
111
110
|
id: 'Node',
|
|
112
111
|
typeParameters: 'Node',
|
|
113
112
|
superClass: 'Node',
|
|
114
|
-
|
|
113
|
+
superTypeArguments: 'Node',
|
|
115
114
|
implements: 'NodeList',
|
|
116
115
|
decorators: 'NodeList',
|
|
117
116
|
body: 'Node'
|
|
@@ -484,6 +483,14 @@ const HERMES_AST_VISITOR_KEYS = {
|
|
|
484
483
|
MatchIdentifierPattern: {
|
|
485
484
|
id: 'Node'
|
|
486
485
|
},
|
|
486
|
+
MatchInstanceObjectPattern: {
|
|
487
|
+
properties: 'NodeList',
|
|
488
|
+
rest: 'Node'
|
|
489
|
+
},
|
|
490
|
+
MatchInstancePattern: {
|
|
491
|
+
targetConstructor: 'Node',
|
|
492
|
+
properties: 'Node'
|
|
493
|
+
},
|
|
487
494
|
MatchLiteralPattern: {
|
|
488
495
|
literal: 'Node'
|
|
489
496
|
},
|
|
@@ -531,6 +538,7 @@ const HERMES_AST_VISITOR_KEYS = {
|
|
|
531
538
|
value: 'Node'
|
|
532
539
|
},
|
|
533
540
|
MixedTypeAnnotation: {},
|
|
541
|
+
NeverTypeAnnotation: {},
|
|
534
542
|
NewExpression: {
|
|
535
543
|
callee: 'Node',
|
|
536
544
|
typeArguments: 'Node',
|
|
@@ -618,6 +626,37 @@ const HERMES_AST_VISITOR_KEYS = {
|
|
|
618
626
|
qualification: 'Node',
|
|
619
627
|
id: 'Node'
|
|
620
628
|
},
|
|
629
|
+
RecordDeclaration: {
|
|
630
|
+
id: 'Node',
|
|
631
|
+
typeParameters: 'Node',
|
|
632
|
+
implements: 'NodeList',
|
|
633
|
+
body: 'Node'
|
|
634
|
+
},
|
|
635
|
+
RecordDeclarationBody: {
|
|
636
|
+
elements: 'NodeList'
|
|
637
|
+
},
|
|
638
|
+
RecordDeclarationImplements: {
|
|
639
|
+
id: 'Node',
|
|
640
|
+
typeArguments: 'Node'
|
|
641
|
+
},
|
|
642
|
+
RecordDeclarationProperty: {
|
|
643
|
+
key: 'Node',
|
|
644
|
+
typeAnnotation: 'Node',
|
|
645
|
+
defaultValue: 'Node'
|
|
646
|
+
},
|
|
647
|
+
RecordDeclarationStaticProperty: {
|
|
648
|
+
key: 'Node',
|
|
649
|
+
typeAnnotation: 'Node',
|
|
650
|
+
value: 'Node'
|
|
651
|
+
},
|
|
652
|
+
RecordExpression: {
|
|
653
|
+
recordConstructor: 'Node',
|
|
654
|
+
typeArguments: 'Node',
|
|
655
|
+
properties: 'Node'
|
|
656
|
+
},
|
|
657
|
+
RecordExpressionProperties: {
|
|
658
|
+
properties: 'NodeList'
|
|
659
|
+
},
|
|
621
660
|
RegExpLiteral: {},
|
|
622
661
|
RestElement: {
|
|
623
662
|
argument: 'Node'
|
|
@@ -667,7 +706,7 @@ const HERMES_AST_VISITOR_KEYS = {
|
|
|
667
706
|
finalizer: 'Node'
|
|
668
707
|
},
|
|
669
708
|
TupleTypeAnnotation: {
|
|
670
|
-
|
|
709
|
+
elementTypes: 'NodeList'
|
|
671
710
|
},
|
|
672
711
|
TupleTypeLabeledElement: {
|
|
673
712
|
label: 'Node',
|
|
@@ -715,9 +754,11 @@ const HERMES_AST_VISITOR_KEYS = {
|
|
|
715
754
|
UnaryExpression: {
|
|
716
755
|
argument: 'Node'
|
|
717
756
|
},
|
|
757
|
+
UndefinedTypeAnnotation: {},
|
|
718
758
|
UnionTypeAnnotation: {
|
|
719
759
|
types: 'NodeList'
|
|
720
760
|
},
|
|
761
|
+
UnknownTypeAnnotation: {},
|
|
721
762
|
UpdateExpression: {
|
|
722
763
|
argument: 'Node'
|
|
723
764
|
},
|
package/dist/src/index.js
CHANGED
|
@@ -31,10 +31,14 @@ var _ESTreeVisitorKeys = _interopRequireDefault(require("./generated/ESTreeVisit
|
|
|
31
31
|
|
|
32
32
|
exports.FlowVisitorKeys = _ESTreeVisitorKeys.default;
|
|
33
33
|
|
|
34
|
-
var
|
|
34
|
+
var TransformComponentSyntax = _interopRequireWildcard(require("./estree/TransformComponentSyntax"));
|
|
35
|
+
|
|
36
|
+
var TransformEnumSyntax = _interopRequireWildcard(require("./estree/TransformEnumSyntax"));
|
|
35
37
|
|
|
36
38
|
var TransformMatchSyntax = _interopRequireWildcard(require("./estree/TransformMatchSyntax"));
|
|
37
39
|
|
|
40
|
+
var TransformRecordSyntax = _interopRequireWildcard(require("./estree/TransformRecordSyntax"));
|
|
41
|
+
|
|
38
42
|
var StripFlowTypesForBabel = _interopRequireWildcard(require("./estree/StripFlowTypesForBabel"));
|
|
39
43
|
|
|
40
44
|
var TransformESTreeToBabel = _interopRequireWildcard(require("./babel/TransformESTreeToBabel"));
|
|
@@ -124,6 +128,10 @@ function getOptions(options = { ...DEFAULTS
|
|
|
124
128
|
options.enableExperimentalFlowMatchSyntax = true; // Enable by default
|
|
125
129
|
}
|
|
126
130
|
|
|
131
|
+
if (options.enableExperimentalFlowRecordSyntax == null) {
|
|
132
|
+
options.enableExperimentalFlowRecordSyntax = true; // Enable by default
|
|
133
|
+
}
|
|
134
|
+
|
|
127
135
|
options.tokens = options.tokens === true;
|
|
128
136
|
options.allowReturnOutsideFunction = options.allowReturnOutsideFunction === true;
|
|
129
137
|
return options;
|
|
@@ -140,13 +148,15 @@ function parse(code, opts) {
|
|
|
140
148
|
return estreeAST;
|
|
141
149
|
}
|
|
142
150
|
|
|
143
|
-
const loweredESTreeAST = [TransformMatchSyntax.transformProgram,
|
|
151
|
+
const loweredESTreeAST = [TransformEnumSyntax.transformProgram, TransformMatchSyntax.transformProgram, TransformComponentSyntax.transformProgram, TransformRecordSyntax.transformProgram, StripFlowTypesForBabel.transformProgram].reduce((ast, transform) => transform(ast, options), estreeAST);
|
|
144
152
|
return TransformESTreeToBabel.transformProgram(loweredESTreeAST, options);
|
|
145
153
|
}
|
|
146
154
|
|
|
147
155
|
const Transforms = {
|
|
156
|
+
transformEnumSyntax: TransformEnumSyntax.transformProgram,
|
|
148
157
|
transformMatchSyntax: TransformMatchSyntax.transformProgram,
|
|
149
|
-
|
|
158
|
+
transformComponentSyntax: TransformComponentSyntax.transformProgram,
|
|
159
|
+
transformRecordSyntax: TransformRecordSyntax.transformProgram,
|
|
150
160
|
stripFlowTypesForBabel: StripFlowTypesForBabel.transformProgram,
|
|
151
161
|
stripFlowTypes: StripFlowTypes.transformProgram
|
|
152
162
|
};
|
|
@@ -20,8 +20,15 @@ var _astNodeMutationHelpers = require("./astNodeMutationHelpers");
|
|
|
20
20
|
|
|
21
21
|
function setParentPointer(node, parent) {
|
|
22
22
|
if (parent != null) {
|
|
23
|
-
|
|
24
|
-
|
|
23
|
+
if (Array.isArray(node)) {
|
|
24
|
+
for (const item of node) {
|
|
25
|
+
// $FlowExpectedError[cannot-write]
|
|
26
|
+
item.parent = parent;
|
|
27
|
+
}
|
|
28
|
+
} else {
|
|
29
|
+
// $FlowExpectedError[cannot-write]
|
|
30
|
+
node.parent = parent;
|
|
31
|
+
}
|
|
25
32
|
}
|
|
26
33
|
}
|
|
27
34
|
/**
|
|
@@ -51,7 +58,12 @@ class SimpleTransform {
|
|
|
51
58
|
if (resultNode != null) {
|
|
52
59
|
// Ensure the new node has the correct parent pointers before recursing again.
|
|
53
60
|
setParentPointer(resultNode, parent);
|
|
54
|
-
|
|
61
|
+
|
|
62
|
+
if (Array.isArray(resultNode)) {
|
|
63
|
+
traversedResultNode = resultNode.map(item => this.transform(item, options)).filter(item => item != null);
|
|
64
|
+
} else {
|
|
65
|
+
traversedResultNode = this.transform(resultNode, options);
|
|
66
|
+
}
|
|
55
67
|
}
|
|
56
68
|
|
|
57
69
|
if (parent == null) {
|
|
@@ -59,7 +71,11 @@ class SimpleTransform {
|
|
|
59
71
|
throw new Error('SimpleTransform infra error: Parent not set on non root node, this should not be possible');
|
|
60
72
|
}
|
|
61
73
|
|
|
62
|
-
|
|
74
|
+
if (Array.isArray(traversedResultNode)) {
|
|
75
|
+
throw new Error('SimpleTransform: invalid array result for root node');
|
|
76
|
+
} else {
|
|
77
|
+
resultRootNode = traversedResultNode;
|
|
78
|
+
}
|
|
63
79
|
} else if (traversedResultNode == null) {
|
|
64
80
|
(0, _astNodeMutationHelpers.removeNodeOnParent)(node, parent, options.visitorKeys);
|
|
65
81
|
} else {
|
|
@@ -34,6 +34,7 @@ function getParentKey(target, parent, visitorKeys) {
|
|
|
34
34
|
for (const key of (0, _getVisitorKeys.getVisitorKeys)(parent, visitorKeys)) {
|
|
35
35
|
if ((0, _getVisitorKeys.isNode)( // $FlowExpectedError[prop-missing]
|
|
36
36
|
parent[key])) {
|
|
37
|
+
// $FlowFixMe[invalid-compare]
|
|
37
38
|
if (parent[key] === target) {
|
|
38
39
|
return {
|
|
39
40
|
type: 'single',
|
|
@@ -73,9 +74,13 @@ function replaceNodeOnParent(originalNode, originalNodeParent, nodeToReplaceWith
|
|
|
73
74
|
if (replacementParent.type === 'array') {
|
|
74
75
|
// $FlowExpectedError[prop-missing]
|
|
75
76
|
parent[replacementParent.key] = (0, _astArrayMutationHelpers.replaceInArray)( // $FlowExpectedError[prop-missing]
|
|
76
|
-
parent[replacementParent.key], replacementParent.targetIndex, [nodeToReplaceWith]);
|
|
77
|
+
parent[replacementParent.key], replacementParent.targetIndex, Array.isArray(nodeToReplaceWith) ? nodeToReplaceWith : [nodeToReplaceWith]);
|
|
77
78
|
} else {
|
|
78
|
-
|
|
79
|
+
if (Array.isArray(nodeToReplaceWith)) {
|
|
80
|
+
throw new Error(`Cannot insert array into non-array parent type: ${parent.type}`);
|
|
81
|
+
} // $FlowExpectedError[prop-missing]
|
|
82
|
+
|
|
83
|
+
|
|
79
84
|
parent[replacementParent.key] = nodeToReplaceWith;
|
|
80
85
|
}
|
|
81
86
|
}
|
package/dist/src/utils/GenID.js
CHANGED
|
@@ -12,30 +12,35 @@
|
|
|
12
12
|
Object.defineProperty(exports, "__esModule", {
|
|
13
13
|
value: true
|
|
14
14
|
});
|
|
15
|
-
exports.
|
|
15
|
+
exports.default = void 0;
|
|
16
16
|
const genPrefix = '$$gen$';
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
18
|
+
class GenID {
|
|
19
|
+
constructor(uniqueTransformPrefix) {
|
|
20
|
+
this.genN = 0;
|
|
21
|
+
this.used = new Set();
|
|
22
|
+
this.prefix = void 0;
|
|
23
|
+
this.prefix = `${genPrefix}${uniqueTransformPrefix}`;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
id() {
|
|
27
|
+
let name;
|
|
28
|
+
|
|
29
|
+
do {
|
|
30
|
+
name = `${this.prefix}${this.genN}`;
|
|
31
|
+
this.genN++;
|
|
32
|
+
} while (this.used.has(name));
|
|
33
|
+
|
|
34
|
+
this.used.add(name);
|
|
35
|
+
return name;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
addUsage(name) {
|
|
39
|
+
if (name.startsWith(this.prefix)) {
|
|
40
|
+
this.used.add(name);
|
|
38
41
|
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
}
|
|
39
45
|
|
|
40
|
-
|
|
41
|
-
}
|
|
46
|
+
exports.default = GenID;
|
|
@@ -0,0 +1,62 @@
|
|
|
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
|
+
'use strict';
|
|
11
|
+
|
|
12
|
+
Object.defineProperty(exports, "__esModule", {
|
|
13
|
+
value: true
|
|
14
|
+
});
|
|
15
|
+
exports.default = isReservedWord;
|
|
16
|
+
|
|
17
|
+
function isReservedWord(name) {
|
|
18
|
+
switch (name) {
|
|
19
|
+
case 'await':
|
|
20
|
+
case 'break':
|
|
21
|
+
case 'case':
|
|
22
|
+
case 'catch':
|
|
23
|
+
case 'class':
|
|
24
|
+
case 'const':
|
|
25
|
+
case 'continue':
|
|
26
|
+
case 'debugger':
|
|
27
|
+
case 'default':
|
|
28
|
+
case 'delete':
|
|
29
|
+
case 'do':
|
|
30
|
+
case 'else':
|
|
31
|
+
case 'enum':
|
|
32
|
+
case 'export':
|
|
33
|
+
case 'extends':
|
|
34
|
+
case 'false':
|
|
35
|
+
case 'finally':
|
|
36
|
+
case 'for':
|
|
37
|
+
case 'function':
|
|
38
|
+
case 'if':
|
|
39
|
+
case 'import':
|
|
40
|
+
case 'in':
|
|
41
|
+
case 'instanceof':
|
|
42
|
+
case 'new':
|
|
43
|
+
case 'null':
|
|
44
|
+
case 'return':
|
|
45
|
+
case 'super':
|
|
46
|
+
case 'switch':
|
|
47
|
+
case 'this':
|
|
48
|
+
case 'throw':
|
|
49
|
+
case 'true':
|
|
50
|
+
case 'try':
|
|
51
|
+
case 'typeof':
|
|
52
|
+
case 'var':
|
|
53
|
+
case 'void':
|
|
54
|
+
case 'while':
|
|
55
|
+
case 'with':
|
|
56
|
+
case 'yield':
|
|
57
|
+
return true;
|
|
58
|
+
|
|
59
|
+
default:
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
@@ -20,8 +20,15 @@ var _astNodeMutationHelpers = require("./astNodeMutationHelpers");
|
|
|
20
20
|
|
|
21
21
|
function setParentPointer(node, parent) {
|
|
22
22
|
if (parent != null) {
|
|
23
|
-
|
|
24
|
-
|
|
23
|
+
if (Array.isArray(node)) {
|
|
24
|
+
for (const item of node) {
|
|
25
|
+
// $FlowExpectedError[cannot-write]
|
|
26
|
+
item.parent = parent;
|
|
27
|
+
}
|
|
28
|
+
} else {
|
|
29
|
+
// $FlowExpectedError[cannot-write]
|
|
30
|
+
node.parent = parent;
|
|
31
|
+
}
|
|
25
32
|
}
|
|
26
33
|
}
|
|
27
34
|
/**
|
|
@@ -51,7 +58,12 @@ class SimpleTransform {
|
|
|
51
58
|
if (resultNode != null) {
|
|
52
59
|
// Ensure the new node has the correct parent pointers before recursing again.
|
|
53
60
|
setParentPointer(resultNode, parent);
|
|
54
|
-
|
|
61
|
+
|
|
62
|
+
if (Array.isArray(resultNode)) {
|
|
63
|
+
traversedResultNode = resultNode.map(item => this.transform(item, options)).filter(item => item != null);
|
|
64
|
+
} else {
|
|
65
|
+
traversedResultNode = this.transform(resultNode, options);
|
|
66
|
+
}
|
|
55
67
|
}
|
|
56
68
|
|
|
57
69
|
if (parent == null) {
|
|
@@ -59,7 +71,11 @@ class SimpleTransform {
|
|
|
59
71
|
throw new Error('SimpleTransform infra error: Parent not set on non root node, this should not be possible');
|
|
60
72
|
}
|
|
61
73
|
|
|
62
|
-
|
|
74
|
+
if (Array.isArray(traversedResultNode)) {
|
|
75
|
+
throw new Error('SimpleTransform: invalid array result for root node');
|
|
76
|
+
} else {
|
|
77
|
+
resultRootNode = traversedResultNode;
|
|
78
|
+
}
|
|
63
79
|
} else if (traversedResultNode == null) {
|
|
64
80
|
(0, _astNodeMutationHelpers.removeNodeOnParent)(node, parent, options.visitorKeys);
|
|
65
81
|
} else {
|
|
@@ -29,7 +29,9 @@ import {
|
|
|
29
29
|
* children are then traversed.
|
|
30
30
|
* - return null, signals the node should be deleted from the AST.
|
|
31
31
|
*/
|
|
32
|
-
export type TransformCallback = (
|
|
32
|
+
export type TransformCallback = (
|
|
33
|
+
node: ESNode,
|
|
34
|
+
) => ESNode | $ReadOnlyArray<ESNode> | null;
|
|
33
35
|
|
|
34
36
|
export type TransformOptions = $ReadOnly<{
|
|
35
37
|
/** The callback function which is called on entering each node. */
|
|
@@ -39,10 +41,20 @@ export type TransformOptions = $ReadOnly<{
|
|
|
39
41
|
visitorKeys?: ?VisitorKeysType,
|
|
40
42
|
}>;
|
|
41
43
|
|
|
42
|
-
function setParentPointer(
|
|
44
|
+
function setParentPointer(
|
|
45
|
+
node: ESNode | $ReadOnlyArray<ESNode>,
|
|
46
|
+
parent: ?ESNode,
|
|
47
|
+
): void {
|
|
43
48
|
if (parent != null) {
|
|
44
|
-
|
|
45
|
-
|
|
49
|
+
if (Array.isArray(node)) {
|
|
50
|
+
for (const item of node) {
|
|
51
|
+
// $FlowExpectedError[cannot-write]
|
|
52
|
+
item.parent = parent;
|
|
53
|
+
}
|
|
54
|
+
} else {
|
|
55
|
+
// $FlowExpectedError[cannot-write]
|
|
56
|
+
node.parent = parent;
|
|
57
|
+
}
|
|
46
58
|
}
|
|
47
59
|
}
|
|
48
60
|
|
|
@@ -63,15 +75,23 @@ export class SimpleTransform {
|
|
|
63
75
|
// Ensure the parent pointers are correctly set before entering the node.
|
|
64
76
|
setParentPointer(node, parent);
|
|
65
77
|
|
|
66
|
-
const resultNode =
|
|
78
|
+
const resultNode: ESNode | $ReadOnlyArray<ESNode> | null =
|
|
79
|
+
options.transform(node);
|
|
67
80
|
if (resultNode !== node) {
|
|
68
|
-
let traversedResultNode
|
|
81
|
+
let traversedResultNode: ESNode | $ReadOnlyArray<ESNode> | null =
|
|
82
|
+
null;
|
|
69
83
|
|
|
70
84
|
if (resultNode != null) {
|
|
71
85
|
// Ensure the new node has the correct parent pointers before recursing again.
|
|
72
86
|
setParentPointer(resultNode, parent);
|
|
73
87
|
|
|
74
|
-
|
|
88
|
+
if (Array.isArray(resultNode)) {
|
|
89
|
+
traversedResultNode = resultNode
|
|
90
|
+
.map(item => this.transform(item, options))
|
|
91
|
+
.filter(item => item != null);
|
|
92
|
+
} else {
|
|
93
|
+
traversedResultNode = this.transform(resultNode, options);
|
|
94
|
+
}
|
|
75
95
|
}
|
|
76
96
|
|
|
77
97
|
if (parent == null) {
|
|
@@ -80,7 +100,13 @@ export class SimpleTransform {
|
|
|
80
100
|
'SimpleTransform infra error: Parent not set on non root node, this should not be possible',
|
|
81
101
|
);
|
|
82
102
|
}
|
|
83
|
-
|
|
103
|
+
if (Array.isArray(traversedResultNode)) {
|
|
104
|
+
throw new Error(
|
|
105
|
+
'SimpleTransform: invalid array result for root node',
|
|
106
|
+
);
|
|
107
|
+
} else {
|
|
108
|
+
resultRootNode = traversedResultNode;
|
|
109
|
+
}
|
|
84
110
|
} else if (traversedResultNode == null) {
|
|
85
111
|
removeNodeOnParent(node, parent, options.visitorKeys);
|
|
86
112
|
} else {
|
|
@@ -34,6 +34,7 @@ function getParentKey(target, parent, visitorKeys) {
|
|
|
34
34
|
for (const key of (0, _getVisitorKeys.getVisitorKeys)(parent, visitorKeys)) {
|
|
35
35
|
if ((0, _getVisitorKeys.isNode)( // $FlowExpectedError[prop-missing]
|
|
36
36
|
parent[key])) {
|
|
37
|
+
// $FlowFixMe[invalid-compare]
|
|
37
38
|
if (parent[key] === target) {
|
|
38
39
|
return {
|
|
39
40
|
type: 'single',
|
|
@@ -73,9 +74,13 @@ function replaceNodeOnParent(originalNode, originalNodeParent, nodeToReplaceWith
|
|
|
73
74
|
if (replacementParent.type === 'array') {
|
|
74
75
|
// $FlowExpectedError[prop-missing]
|
|
75
76
|
parent[replacementParent.key] = (0, _astArrayMutationHelpers.replaceInArray)( // $FlowExpectedError[prop-missing]
|
|
76
|
-
parent[replacementParent.key], replacementParent.targetIndex, [nodeToReplaceWith]);
|
|
77
|
+
parent[replacementParent.key], replacementParent.targetIndex, Array.isArray(nodeToReplaceWith) ? nodeToReplaceWith : [nodeToReplaceWith]);
|
|
77
78
|
} else {
|
|
78
|
-
|
|
79
|
+
if (Array.isArray(nodeToReplaceWith)) {
|
|
80
|
+
throw new Error(`Cannot insert array into non-array parent type: ${parent.type}`);
|
|
81
|
+
} // $FlowExpectedError[prop-missing]
|
|
82
|
+
|
|
83
|
+
|
|
79
84
|
parent[replacementParent.key] = nodeToReplaceWith;
|
|
80
85
|
}
|
|
81
86
|
}
|
|
@@ -48,6 +48,7 @@ function getParentKey(
|
|
|
48
48
|
parent[key],
|
|
49
49
|
)
|
|
50
50
|
) {
|
|
51
|
+
// $FlowFixMe[invalid-compare]
|
|
51
52
|
if (parent[key] === target) {
|
|
52
53
|
return {type: 'single', node: parent, key};
|
|
53
54
|
}
|
|
@@ -74,7 +75,7 @@ function getParentKey(
|
|
|
74
75
|
export function replaceNodeOnParent(
|
|
75
76
|
originalNode: ESNode,
|
|
76
77
|
originalNodeParent: ESNode,
|
|
77
|
-
nodeToReplaceWith: ESNode
|
|
78
|
+
nodeToReplaceWith: ESNode | $ReadOnlyArray<ESNode>,
|
|
78
79
|
visitorKeys?: ?VisitorKeysType,
|
|
79
80
|
): void {
|
|
80
81
|
const replacementParent = getParentKey(
|
|
@@ -89,9 +90,16 @@ export function replaceNodeOnParent(
|
|
|
89
90
|
// $FlowExpectedError[prop-missing]
|
|
90
91
|
parent[replacementParent.key],
|
|
91
92
|
replacementParent.targetIndex,
|
|
92
|
-
|
|
93
|
+
Array.isArray(nodeToReplaceWith)
|
|
94
|
+
? nodeToReplaceWith
|
|
95
|
+
: [nodeToReplaceWith],
|
|
93
96
|
);
|
|
94
97
|
} else {
|
|
98
|
+
if (Array.isArray(nodeToReplaceWith)) {
|
|
99
|
+
throw new Error(
|
|
100
|
+
`Cannot insert array into non-array parent type: ${parent.type}`,
|
|
101
|
+
);
|
|
102
|
+
}
|
|
95
103
|
// $FlowExpectedError[prop-missing]
|
|
96
104
|
parent[replacementParent.key] = nodeToReplaceWith;
|
|
97
105
|
}
|
package/dist/utils/GenID.js
CHANGED
|
@@ -12,30 +12,35 @@
|
|
|
12
12
|
Object.defineProperty(exports, "__esModule", {
|
|
13
13
|
value: true
|
|
14
14
|
});
|
|
15
|
-
exports.
|
|
15
|
+
exports.default = void 0;
|
|
16
16
|
const genPrefix = '$$gen$';
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
18
|
+
class GenID {
|
|
19
|
+
constructor(uniqueTransformPrefix) {
|
|
20
|
+
this.genN = 0;
|
|
21
|
+
this.used = new Set();
|
|
22
|
+
this.prefix = void 0;
|
|
23
|
+
this.prefix = `${genPrefix}${uniqueTransformPrefix}`;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
id() {
|
|
27
|
+
let name;
|
|
28
|
+
|
|
29
|
+
do {
|
|
30
|
+
name = `${this.prefix}${this.genN}`;
|
|
31
|
+
this.genN++;
|
|
32
|
+
} while (this.used.has(name));
|
|
33
|
+
|
|
34
|
+
this.used.add(name);
|
|
35
|
+
return name;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
addUsage(name) {
|
|
39
|
+
if (name.startsWith(this.prefix)) {
|
|
40
|
+
this.used.add(name);
|
|
38
41
|
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
}
|
|
39
45
|
|
|
40
|
-
|
|
41
|
-
}
|
|
46
|
+
exports.default = GenID;
|