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
|
@@ -0,0 +1,106 @@
|
|
|
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
|
+
* Transform Flow Enum declarations (https://flow.org/en/docs/enums/).
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
Object.defineProperty(exports, "__esModule", {
|
|
16
|
+
value: true
|
|
17
|
+
});
|
|
18
|
+
exports.transformProgram = transformProgram;
|
|
19
|
+
|
|
20
|
+
var _hermesEstree = require("hermes-estree");
|
|
21
|
+
|
|
22
|
+
var _SimpleTransform = require("../transform/SimpleTransform");
|
|
23
|
+
|
|
24
|
+
var _Builders = require("../utils/Builders");
|
|
25
|
+
|
|
26
|
+
function mapEnumDeclaration(node, options) {
|
|
27
|
+
var _options$transformOpt, _options$transformOpt2;
|
|
28
|
+
|
|
29
|
+
const {
|
|
30
|
+
body
|
|
31
|
+
} = node;
|
|
32
|
+
const {
|
|
33
|
+
members
|
|
34
|
+
} = body;
|
|
35
|
+
const getRuntime = (_options$transformOpt = options.transformOptions) == null ? void 0 : (_options$transformOpt2 = _options$transformOpt.TransformEnumSyntax) == null ? void 0 : _options$transformOpt2.getRuntime;
|
|
36
|
+
const enumModule = typeof getRuntime === 'function' ? getRuntime() : (0, _Builders.callExpression)((0, _Builders.ident)('require'), [(0, _Builders.stringLiteral)('flow-enums-runtime')]);
|
|
37
|
+
const mirrored = body.type === 'EnumStringBody' && (!members.length || members[0].type === 'EnumDefaultedMember');
|
|
38
|
+
const enumExpression = mirrored ? (0, _Builders.callExpression)({
|
|
39
|
+
type: 'MemberExpression',
|
|
40
|
+
object: enumModule,
|
|
41
|
+
property: (0, _Builders.ident)('Mirrored'),
|
|
42
|
+
computed: false,
|
|
43
|
+
optional: false,
|
|
44
|
+
...(0, _Builders.etc)()
|
|
45
|
+
}, [{
|
|
46
|
+
type: 'ArrayExpression',
|
|
47
|
+
elements: members.map(member => (0, _Builders.stringLiteral)(member.id.name)),
|
|
48
|
+
trailingComma: false,
|
|
49
|
+
...(0, _Builders.etc)()
|
|
50
|
+
}]) : (0, _Builders.callExpression)(enumModule, [{
|
|
51
|
+
type: 'ObjectExpression',
|
|
52
|
+
properties: members.map(member => ({
|
|
53
|
+
type: 'Property',
|
|
54
|
+
key: member.id,
|
|
55
|
+
value: // String enums with `EnumDefaultedMember` are handled above by
|
|
56
|
+
// calculation of `mirrored`.
|
|
57
|
+
member.type === 'EnumDefaultedMember' ? (0, _Builders.callExpression)((0, _Builders.ident)('Symbol'), [(0, _Builders.stringLiteral)(member.id.name)]) : member.init,
|
|
58
|
+
kind: 'init',
|
|
59
|
+
method: false,
|
|
60
|
+
shorthand: false,
|
|
61
|
+
computed: false,
|
|
62
|
+
...(0, _Builders.etc)(),
|
|
63
|
+
parent: _Builders.EMPTY_PARENT
|
|
64
|
+
})),
|
|
65
|
+
...(0, _Builders.etc)()
|
|
66
|
+
}]);
|
|
67
|
+
return (0, _Builders.variableDeclaration)('const', node.id, enumExpression);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function transformProgram(program, options) {
|
|
71
|
+
return _SimpleTransform.SimpleTransform.transformProgram(program, {
|
|
72
|
+
transform(node) {
|
|
73
|
+
switch (node.type) {
|
|
74
|
+
case 'EnumDeclaration':
|
|
75
|
+
{
|
|
76
|
+
return mapEnumDeclaration(node, options);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
case 'ExportDefaultDeclaration':
|
|
80
|
+
{
|
|
81
|
+
const {
|
|
82
|
+
declaration
|
|
83
|
+
} = node;
|
|
84
|
+
|
|
85
|
+
if ((0, _hermesEstree.isEnumDeclaration)(declaration)) {
|
|
86
|
+
const enumDeclaration = mapEnumDeclaration(declaration, options);
|
|
87
|
+
|
|
88
|
+
const exportDefault = _SimpleTransform.SimpleTransform.nodeWith(node, {
|
|
89
|
+
declaration: (0, _Builders.ident)(declaration.id.name)
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
return [enumDeclaration, exportDefault];
|
|
93
|
+
} else {
|
|
94
|
+
return node;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
default:
|
|
99
|
+
{
|
|
100
|
+
return node;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
});
|
|
106
|
+
}
|
|
@@ -25,20 +25,22 @@ var _createSyntaxError = require("../utils/createSyntaxError");
|
|
|
25
25
|
|
|
26
26
|
var _Builders = require("../utils/Builders");
|
|
27
27
|
|
|
28
|
-
var _GenID = require("../utils/GenID");
|
|
28
|
+
var _GenID = _interopRequireDefault(require("../utils/GenID"));
|
|
29
|
+
|
|
30
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
29
31
|
|
|
30
32
|
/**
|
|
31
33
|
* Generated identifiers.
|
|
32
34
|
* `GenID` is initialized in the transform.
|
|
33
35
|
*/
|
|
34
|
-
let
|
|
36
|
+
let genID = null;
|
|
35
37
|
|
|
36
38
|
function genIdent() {
|
|
37
|
-
if (
|
|
39
|
+
if (genID == null) {
|
|
38
40
|
throw Error('GenID must be initialized at the start of the transform.');
|
|
39
41
|
}
|
|
40
42
|
|
|
41
|
-
return (0, _Builders.ident)(
|
|
43
|
+
return (0, _Builders.ident)(genID.id());
|
|
42
44
|
}
|
|
43
45
|
/**
|
|
44
46
|
* A series of properties.
|
|
@@ -138,6 +140,7 @@ function needsPropExistsCond(pattern) {
|
|
|
138
140
|
case 'MatchLiteralPattern':
|
|
139
141
|
case 'MatchUnaryPattern':
|
|
140
142
|
case 'MatchObjectPattern':
|
|
143
|
+
case 'MatchInstancePattern':
|
|
141
144
|
case 'MatchArrayPattern':
|
|
142
145
|
return false;
|
|
143
146
|
|
|
@@ -158,6 +161,78 @@ function needsPropExistsCond(pattern) {
|
|
|
158
161
|
}
|
|
159
162
|
}
|
|
160
163
|
}
|
|
164
|
+
/**
|
|
165
|
+
* Analyzes properties of both object patterns and instance patterns.
|
|
166
|
+
*/
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
function analyzeProperties(key, pattern, seenBindingNames, properties, rest) {
|
|
170
|
+
const conditions = [];
|
|
171
|
+
const bindings = [];
|
|
172
|
+
const objKeys = [];
|
|
173
|
+
const seenNames = new Set();
|
|
174
|
+
properties.forEach(prop => {
|
|
175
|
+
const {
|
|
176
|
+
key: objKey,
|
|
177
|
+
pattern: propPattern
|
|
178
|
+
} = prop;
|
|
179
|
+
objKeys.push(objKey);
|
|
180
|
+
const name = objKeyToString(objKey);
|
|
181
|
+
|
|
182
|
+
if (seenNames.has(name)) {
|
|
183
|
+
throw (0, _createSyntaxError.createSyntaxError)(propPattern, `Duplicate property name '${name}' in match object pattern.`);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
seenNames.add(name);
|
|
187
|
+
const propKey = key.concat(objKey);
|
|
188
|
+
|
|
189
|
+
if (needsPropExistsCond(propPattern)) {
|
|
190
|
+
conditions.push({
|
|
191
|
+
type: 'prop-exists',
|
|
192
|
+
key,
|
|
193
|
+
propName: name
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const {
|
|
198
|
+
conditions: childConditions,
|
|
199
|
+
bindings: childBindings
|
|
200
|
+
} = analyzePattern(propPattern, propKey, seenBindingNames);
|
|
201
|
+
conditions.push(...childConditions);
|
|
202
|
+
bindings.push(...childBindings);
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
if (rest != null && rest.argument != null) {
|
|
206
|
+
const {
|
|
207
|
+
id,
|
|
208
|
+
kind
|
|
209
|
+
} = rest.argument;
|
|
210
|
+
checkDuplicateBindingName(seenBindingNames, rest.argument, id.name);
|
|
211
|
+
checkBindingKind(pattern, kind);
|
|
212
|
+
bindings.push({
|
|
213
|
+
type: 'object-rest',
|
|
214
|
+
key,
|
|
215
|
+
exclude: objKeys,
|
|
216
|
+
kind,
|
|
217
|
+
id
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
return {
|
|
222
|
+
conditions,
|
|
223
|
+
bindings
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function constructorExpression(constructor) {
|
|
228
|
+
switch (constructor.type) {
|
|
229
|
+
case 'MatchIdentifierPattern':
|
|
230
|
+
return constructor.id;
|
|
231
|
+
|
|
232
|
+
case 'MatchMemberPattern':
|
|
233
|
+
return convertMemberPattern(constructor);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
161
236
|
/**
|
|
162
237
|
* Analyzes a match pattern, and produced both the conditions and bindings
|
|
163
238
|
* produced by that pattern.
|
|
@@ -360,60 +435,38 @@ function analyzePattern(pattern, key, seenBindingNames) {
|
|
|
360
435
|
properties,
|
|
361
436
|
rest
|
|
362
437
|
} = pattern;
|
|
438
|
+
const {
|
|
439
|
+
conditions: propertyConditions,
|
|
440
|
+
bindings
|
|
441
|
+
} = analyzeProperties(key, pattern, seenBindingNames, properties, rest);
|
|
363
442
|
const conditions = [{
|
|
364
443
|
type: 'object',
|
|
365
444
|
key
|
|
366
|
-
}];
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
key: objKey,
|
|
373
|
-
pattern: propPattern
|
|
374
|
-
} = prop;
|
|
375
|
-
objKeys.push(objKey);
|
|
376
|
-
const name = objKeyToString(objKey);
|
|
377
|
-
|
|
378
|
-
if (seenNames.has(name)) {
|
|
379
|
-
throw (0, _createSyntaxError.createSyntaxError)(propPattern, `Duplicate property name '${name}' in match object pattern.`);
|
|
380
|
-
}
|
|
381
|
-
|
|
382
|
-
seenNames.add(name);
|
|
383
|
-
const propKey = key.concat(objKey);
|
|
445
|
+
}, ...propertyConditions];
|
|
446
|
+
return {
|
|
447
|
+
conditions,
|
|
448
|
+
bindings
|
|
449
|
+
};
|
|
450
|
+
}
|
|
384
451
|
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
452
|
+
case 'MatchInstancePattern':
|
|
453
|
+
{
|
|
454
|
+
const {
|
|
455
|
+
targetConstructor,
|
|
456
|
+
properties: {
|
|
457
|
+
properties,
|
|
458
|
+
rest
|
|
391
459
|
}
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
const {
|
|
403
|
-
id,
|
|
404
|
-
kind
|
|
405
|
-
} = rest.argument;
|
|
406
|
-
checkDuplicateBindingName(seenBindingNames, rest.argument, id.name);
|
|
407
|
-
checkBindingKind(pattern, kind);
|
|
408
|
-
bindings.push({
|
|
409
|
-
type: 'object-rest',
|
|
410
|
-
key,
|
|
411
|
-
exclude: objKeys,
|
|
412
|
-
kind,
|
|
413
|
-
id
|
|
414
|
-
});
|
|
415
|
-
}
|
|
416
|
-
|
|
460
|
+
} = pattern;
|
|
461
|
+
const {
|
|
462
|
+
conditions: propertyConditions,
|
|
463
|
+
bindings
|
|
464
|
+
} = analyzeProperties(key, pattern, seenBindingNames, properties, rest);
|
|
465
|
+
const conditions = [{
|
|
466
|
+
type: 'instanceof',
|
|
467
|
+
key,
|
|
468
|
+
constructor: constructorExpression(targetConstructor)
|
|
469
|
+
}, ...propertyConditions];
|
|
417
470
|
return {
|
|
418
471
|
conditions,
|
|
419
472
|
bindings
|
|
@@ -567,6 +620,21 @@ function testsOfCondition(root, condition) {
|
|
|
567
620
|
return [(0, _Builders.disjunction)([(0, _Builders.conjunction)([typeofObject, notNull]), typeofFunction])];
|
|
568
621
|
}
|
|
569
622
|
|
|
623
|
+
case 'instanceof':
|
|
624
|
+
{
|
|
625
|
+
const {
|
|
626
|
+
key,
|
|
627
|
+
constructor
|
|
628
|
+
} = condition;
|
|
629
|
+
return [{
|
|
630
|
+
type: 'BinaryExpression',
|
|
631
|
+
left: expressionOfKey(root, key),
|
|
632
|
+
right: constructor,
|
|
633
|
+
operator: 'instanceof',
|
|
634
|
+
...(0, _Builders.etc)()
|
|
635
|
+
}];
|
|
636
|
+
}
|
|
637
|
+
|
|
570
638
|
case 'prop-exists':
|
|
571
639
|
{
|
|
572
640
|
// <propName> in <x>
|
|
@@ -968,7 +1036,7 @@ function mapMatchStatement(node) {
|
|
|
968
1036
|
function transformProgram(program, _options) {
|
|
969
1037
|
// Initialize so each file transformed starts freshly incrementing the
|
|
970
1038
|
// variable name counter, and has its own usage tracking.
|
|
971
|
-
|
|
1039
|
+
genID = new _GenID.default('m');
|
|
972
1040
|
return _SimpleTransform.SimpleTransform.transformProgram(program, {
|
|
973
1041
|
transform(node) {
|
|
974
1042
|
switch (node.type) {
|
|
@@ -987,11 +1055,11 @@ function transformProgram(program, _options) {
|
|
|
987
1055
|
// A rudimentary check to avoid some collisions with our generated
|
|
988
1056
|
// variable names. Ideally, we would have access a scope analyzer
|
|
989
1057
|
// inside the transform instead.
|
|
990
|
-
if (
|
|
1058
|
+
if (genID == null) {
|
|
991
1059
|
throw Error('GenID must be initialized at the start of the transform.');
|
|
992
1060
|
}
|
|
993
1061
|
|
|
994
|
-
|
|
1062
|
+
genID.addUsage(node.name);
|
|
995
1063
|
return node;
|
|
996
1064
|
}
|
|
997
1065
|
|
|
@@ -0,0 +1,294 @@
|
|
|
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
|
+
* Transform record declarations.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
Object.defineProperty(exports, "__esModule", {
|
|
16
|
+
value: true
|
|
17
|
+
});
|
|
18
|
+
exports.transformProgram = transformProgram;
|
|
19
|
+
|
|
20
|
+
var _hermesEstree = require("hermes-estree");
|
|
21
|
+
|
|
22
|
+
var _SimpleTransform = require("../transform/SimpleTransform");
|
|
23
|
+
|
|
24
|
+
var _astNodeMutationHelpers = require("../transform/astNodeMutationHelpers");
|
|
25
|
+
|
|
26
|
+
var _Builders = require("../utils/Builders");
|
|
27
|
+
|
|
28
|
+
var _isReservedWord = _interopRequireDefault(require("../utils/isReservedWord"));
|
|
29
|
+
|
|
30
|
+
var _GenID = _interopRequireDefault(require("../utils/GenID"));
|
|
31
|
+
|
|
32
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
33
|
+
|
|
34
|
+
function nameOfKey(key) {
|
|
35
|
+
switch (key.type) {
|
|
36
|
+
case 'Identifier':
|
|
37
|
+
return key.name;
|
|
38
|
+
|
|
39
|
+
case 'Literal':
|
|
40
|
+
if ((0, _hermesEstree.isBigIntLiteral)(key)) {
|
|
41
|
+
return key.bigint;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return String(key.value);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function mapRecordDeclaration(genID, node) {
|
|
49
|
+
const ownProperties = [];
|
|
50
|
+
const staticProperties = [];
|
|
51
|
+
const methods = [];
|
|
52
|
+
const staticMethods = [];
|
|
53
|
+
|
|
54
|
+
for (const element of node.body.elements) {
|
|
55
|
+
switch (element.type) {
|
|
56
|
+
case 'RecordDeclarationProperty':
|
|
57
|
+
ownProperties.push(element);
|
|
58
|
+
break;
|
|
59
|
+
|
|
60
|
+
case 'RecordDeclarationStaticProperty':
|
|
61
|
+
staticProperties.push(element);
|
|
62
|
+
break;
|
|
63
|
+
|
|
64
|
+
case 'MethodDefinition':
|
|
65
|
+
if (element.static) {
|
|
66
|
+
staticMethods.push(element);
|
|
67
|
+
} else {
|
|
68
|
+
methods.push(element);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const reservedPropNames = new Map(); // Create constructor parameter as an object pattern with all properties
|
|
76
|
+
|
|
77
|
+
const constructorParam = {
|
|
78
|
+
type: 'ObjectPattern',
|
|
79
|
+
properties: ownProperties.map(prop => {
|
|
80
|
+
const {
|
|
81
|
+
key,
|
|
82
|
+
defaultValue
|
|
83
|
+
} = prop;
|
|
84
|
+
const keyName = nameOfKey(key);
|
|
85
|
+
|
|
86
|
+
const getValue = bindingIdent => defaultValue != null ? {
|
|
87
|
+
type: 'AssignmentPattern',
|
|
88
|
+
left: bindingIdent,
|
|
89
|
+
right: (0, _astNodeMutationHelpers.deepCloneNode)(defaultValue),
|
|
90
|
+
...(0, _Builders.etc)()
|
|
91
|
+
} : bindingIdent;
|
|
92
|
+
|
|
93
|
+
switch (key.type) {
|
|
94
|
+
case 'Identifier':
|
|
95
|
+
{
|
|
96
|
+
const needsNewBinding = (0, _isReservedWord.default)(keyName);
|
|
97
|
+
const bindingName = needsNewBinding ? genID.id() : keyName;
|
|
98
|
+
const bindingIdent = (0, _Builders.ident)(bindingName);
|
|
99
|
+
|
|
100
|
+
if (needsNewBinding) {
|
|
101
|
+
reservedPropNames.set(keyName, bindingName);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (needsNewBinding) {
|
|
105
|
+
return {
|
|
106
|
+
type: 'Property',
|
|
107
|
+
kind: 'init',
|
|
108
|
+
key: (0, _astNodeMutationHelpers.shallowCloneNode)(key),
|
|
109
|
+
value: getValue(bindingIdent),
|
|
110
|
+
shorthand: false,
|
|
111
|
+
method: false,
|
|
112
|
+
computed: false,
|
|
113
|
+
...(0, _Builders.etc)(),
|
|
114
|
+
parent: _Builders.EMPTY_PARENT
|
|
115
|
+
};
|
|
116
|
+
} else {
|
|
117
|
+
return {
|
|
118
|
+
type: 'Property',
|
|
119
|
+
kind: 'init',
|
|
120
|
+
key: (0, _astNodeMutationHelpers.shallowCloneNode)(key),
|
|
121
|
+
value: getValue(bindingIdent),
|
|
122
|
+
shorthand: true,
|
|
123
|
+
method: false,
|
|
124
|
+
computed: false,
|
|
125
|
+
...(0, _Builders.etc)(),
|
|
126
|
+
parent: _Builders.EMPTY_PARENT
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
case 'Literal':
|
|
132
|
+
{
|
|
133
|
+
const bindingName = genID.id();
|
|
134
|
+
const bindingIdent = (0, _Builders.ident)(bindingName);
|
|
135
|
+
reservedPropNames.set(keyName, bindingName);
|
|
136
|
+
return {
|
|
137
|
+
type: 'Property',
|
|
138
|
+
kind: 'init',
|
|
139
|
+
key: (0, _astNodeMutationHelpers.shallowCloneNode)(key),
|
|
140
|
+
value: getValue(bindingIdent),
|
|
141
|
+
shorthand: false,
|
|
142
|
+
method: false,
|
|
143
|
+
computed: false,
|
|
144
|
+
...(0, _Builders.etc)(),
|
|
145
|
+
parent: _Builders.EMPTY_PARENT
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}),
|
|
150
|
+
typeAnnotation: null,
|
|
151
|
+
...(0, _Builders.etc)()
|
|
152
|
+
}; // Create the constructor method
|
|
153
|
+
|
|
154
|
+
const constructor = {
|
|
155
|
+
type: 'MethodDefinition',
|
|
156
|
+
key: (0, _Builders.ident)('constructor'),
|
|
157
|
+
kind: 'constructor',
|
|
158
|
+
computed: false,
|
|
159
|
+
static: false,
|
|
160
|
+
value: {
|
|
161
|
+
type: 'FunctionExpression',
|
|
162
|
+
id: null,
|
|
163
|
+
params: [constructorParam],
|
|
164
|
+
body: {
|
|
165
|
+
type: 'BlockStatement',
|
|
166
|
+
body: ownProperties.map(({
|
|
167
|
+
key
|
|
168
|
+
}) => {
|
|
169
|
+
var _reservedPropNames$ge;
|
|
170
|
+
|
|
171
|
+
const keyName = nameOfKey(key);
|
|
172
|
+
const bindingIdent = (0, _Builders.ident)((_reservedPropNames$ge = reservedPropNames.get(keyName)) != null ? _reservedPropNames$ge : keyName);
|
|
173
|
+
const object = {
|
|
174
|
+
type: 'ThisExpression',
|
|
175
|
+
...(0, _Builders.etc)()
|
|
176
|
+
};
|
|
177
|
+
const memberExpression = key.type === 'Identifier' ? {
|
|
178
|
+
type: 'MemberExpression',
|
|
179
|
+
object,
|
|
180
|
+
property: (0, _astNodeMutationHelpers.shallowCloneNode)(key),
|
|
181
|
+
computed: false,
|
|
182
|
+
optional: false,
|
|
183
|
+
...(0, _Builders.etc)()
|
|
184
|
+
} : {
|
|
185
|
+
type: 'MemberExpression',
|
|
186
|
+
object,
|
|
187
|
+
property: (0, _astNodeMutationHelpers.shallowCloneNode)(key),
|
|
188
|
+
computed: true,
|
|
189
|
+
optional: false,
|
|
190
|
+
...(0, _Builders.etc)()
|
|
191
|
+
};
|
|
192
|
+
return {
|
|
193
|
+
type: 'ExpressionStatement',
|
|
194
|
+
expression: {
|
|
195
|
+
type: 'AssignmentExpression',
|
|
196
|
+
operator: '=',
|
|
197
|
+
left: memberExpression,
|
|
198
|
+
right: bindingIdent,
|
|
199
|
+
...(0, _Builders.etc)()
|
|
200
|
+
},
|
|
201
|
+
directive: null,
|
|
202
|
+
...(0, _Builders.etc)()
|
|
203
|
+
};
|
|
204
|
+
}),
|
|
205
|
+
...(0, _Builders.etc)()
|
|
206
|
+
},
|
|
207
|
+
generator: false,
|
|
208
|
+
async: false,
|
|
209
|
+
predicate: null,
|
|
210
|
+
returnType: null,
|
|
211
|
+
typeParameters: null,
|
|
212
|
+
...(0, _Builders.etc)()
|
|
213
|
+
},
|
|
214
|
+
...(0, _Builders.etc)(),
|
|
215
|
+
parent: _Builders.EMPTY_PARENT
|
|
216
|
+
};
|
|
217
|
+
const classStaticProperties = staticProperties.map(prop => ({
|
|
218
|
+
type: 'PropertyDefinition',
|
|
219
|
+
key: (0, _astNodeMutationHelpers.shallowCloneNode)(prop.key),
|
|
220
|
+
value: (0, _astNodeMutationHelpers.deepCloneNode)(prop.value),
|
|
221
|
+
static: true,
|
|
222
|
+
typeAnnotation: null,
|
|
223
|
+
variance: null,
|
|
224
|
+
computed: false,
|
|
225
|
+
declare: false,
|
|
226
|
+
optional: false,
|
|
227
|
+
...(0, _Builders.etc)(),
|
|
228
|
+
parent: _Builders.EMPTY_PARENT
|
|
229
|
+
}));
|
|
230
|
+
const classBodyElements = [constructor, ...methods, ...classStaticProperties, ...staticMethods];
|
|
231
|
+
return {
|
|
232
|
+
type: 'ClassDeclaration',
|
|
233
|
+
id: (0, _astNodeMutationHelpers.shallowCloneNode)(node.id),
|
|
234
|
+
body: {
|
|
235
|
+
type: 'ClassBody',
|
|
236
|
+
body: classBodyElements,
|
|
237
|
+
...(0, _Builders.etc)(),
|
|
238
|
+
parent: _Builders.EMPTY_PARENT
|
|
239
|
+
},
|
|
240
|
+
superClass: null,
|
|
241
|
+
typeParameters: null,
|
|
242
|
+
superTypeArguments: null,
|
|
243
|
+
implements: [],
|
|
244
|
+
decorators: [],
|
|
245
|
+
...(0, _Builders.etc)()
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function mapRecordExpression(node) {
|
|
250
|
+
const obj = {
|
|
251
|
+
type: 'ObjectExpression',
|
|
252
|
+
properties: node.properties.properties,
|
|
253
|
+
...(0, _Builders.etc)()
|
|
254
|
+
};
|
|
255
|
+
return {
|
|
256
|
+
type: 'NewExpression',
|
|
257
|
+
callee: node.recordConstructor,
|
|
258
|
+
arguments: [obj],
|
|
259
|
+
typeArguments: null,
|
|
260
|
+
...(0, _Builders.etc)()
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
function transformProgram(program, _options) {
|
|
265
|
+
const genID = new _GenID.default('r');
|
|
266
|
+
return _SimpleTransform.SimpleTransform.transformProgram(program, {
|
|
267
|
+
transform(node) {
|
|
268
|
+
switch (node.type) {
|
|
269
|
+
case 'RecordDeclaration':
|
|
270
|
+
{
|
|
271
|
+
return mapRecordDeclaration(genID, node);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
case 'RecordExpression':
|
|
275
|
+
{
|
|
276
|
+
return mapRecordExpression(node);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
case 'Identifier':
|
|
280
|
+
{
|
|
281
|
+
// A rudimentary check to avoid some collisions with our generated
|
|
282
|
+
// variable names. Ideally, we would have access a scope analyzer
|
|
283
|
+
// inside the transform instead.
|
|
284
|
+
genID.addUsage(node.name);
|
|
285
|
+
return node;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
default:
|
|
289
|
+
return node;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
});
|
|
294
|
+
}
|