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,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
|
+
}
|
|
@@ -0,0 +1,308 @@
|
|
|
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
|
+
* @flow strict
|
|
8
|
+
* @format
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
'use strict';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Transform record declarations.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import type {ParserOptions} from '../ParserOptions';
|
|
18
|
+
import type {
|
|
19
|
+
AssignmentPattern,
|
|
20
|
+
ClassDeclaration,
|
|
21
|
+
ClassMember,
|
|
22
|
+
ESNode,
|
|
23
|
+
Identifier,
|
|
24
|
+
MemberExpression,
|
|
25
|
+
MethodDefinition,
|
|
26
|
+
NewExpression,
|
|
27
|
+
ObjectExpression,
|
|
28
|
+
ObjectPattern,
|
|
29
|
+
ObjectPropertyKey,
|
|
30
|
+
Program,
|
|
31
|
+
RecordDeclaration,
|
|
32
|
+
RecordDeclarationProperty,
|
|
33
|
+
RecordDeclarationStaticProperty,
|
|
34
|
+
RecordExpression,
|
|
35
|
+
ThisExpression,
|
|
36
|
+
} from 'hermes-estree';
|
|
37
|
+
|
|
38
|
+
import {isBigIntLiteral} from 'hermes-estree';
|
|
39
|
+
import {SimpleTransform} from '../transform/SimpleTransform';
|
|
40
|
+
import {
|
|
41
|
+
deepCloneNode,
|
|
42
|
+
shallowCloneNode,
|
|
43
|
+
} from '../transform/astNodeMutationHelpers';
|
|
44
|
+
import {EMPTY_PARENT, etc, ident} from '../utils/Builders';
|
|
45
|
+
import isReservedWord from '../utils/isReservedWord';
|
|
46
|
+
import GenID from '../utils/GenID';
|
|
47
|
+
|
|
48
|
+
function nameOfKey(key: ObjectPropertyKey): string {
|
|
49
|
+
switch (key.type) {
|
|
50
|
+
case 'Identifier':
|
|
51
|
+
return key.name;
|
|
52
|
+
case 'Literal':
|
|
53
|
+
if (isBigIntLiteral(key)) {
|
|
54
|
+
return key.bigint;
|
|
55
|
+
}
|
|
56
|
+
return String(key.value);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function mapRecordDeclaration(
|
|
61
|
+
genID: GenID,
|
|
62
|
+
node: RecordDeclaration,
|
|
63
|
+
): ClassDeclaration {
|
|
64
|
+
const ownProperties: Array<RecordDeclarationProperty> = [];
|
|
65
|
+
const staticProperties: Array<RecordDeclarationStaticProperty> = [];
|
|
66
|
+
const methods: Array<MethodDefinition> = [];
|
|
67
|
+
const staticMethods: Array<MethodDefinition> = [];
|
|
68
|
+
|
|
69
|
+
for (const element of node.body.elements) {
|
|
70
|
+
switch (element.type) {
|
|
71
|
+
case 'RecordDeclarationProperty':
|
|
72
|
+
ownProperties.push(element);
|
|
73
|
+
break;
|
|
74
|
+
case 'RecordDeclarationStaticProperty':
|
|
75
|
+
staticProperties.push(element);
|
|
76
|
+
break;
|
|
77
|
+
case 'MethodDefinition':
|
|
78
|
+
if (element.static) {
|
|
79
|
+
staticMethods.push(element);
|
|
80
|
+
} else {
|
|
81
|
+
methods.push(element);
|
|
82
|
+
}
|
|
83
|
+
break;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const reservedPropNames = new Map<string, string>();
|
|
88
|
+
|
|
89
|
+
// Create constructor parameter as an object pattern with all properties
|
|
90
|
+
const constructorParam: ObjectPattern = {
|
|
91
|
+
type: 'ObjectPattern',
|
|
92
|
+
properties: ownProperties.map(prop => {
|
|
93
|
+
const {key, defaultValue} = prop;
|
|
94
|
+
const keyName = nameOfKey(key);
|
|
95
|
+
|
|
96
|
+
const getValue = (
|
|
97
|
+
bindingIdent: Identifier,
|
|
98
|
+
): AssignmentPattern | Identifier =>
|
|
99
|
+
defaultValue != null
|
|
100
|
+
? {
|
|
101
|
+
type: 'AssignmentPattern',
|
|
102
|
+
left: bindingIdent,
|
|
103
|
+
right: deepCloneNode(defaultValue),
|
|
104
|
+
...etc(),
|
|
105
|
+
}
|
|
106
|
+
: bindingIdent;
|
|
107
|
+
|
|
108
|
+
switch (key.type) {
|
|
109
|
+
case 'Identifier': {
|
|
110
|
+
const needsNewBinding = isReservedWord(keyName);
|
|
111
|
+
const bindingName = needsNewBinding ? genID.id() : keyName;
|
|
112
|
+
const bindingIdent = ident(bindingName);
|
|
113
|
+
if (needsNewBinding) {
|
|
114
|
+
reservedPropNames.set(keyName, bindingName);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (needsNewBinding) {
|
|
118
|
+
return {
|
|
119
|
+
type: 'Property',
|
|
120
|
+
kind: 'init',
|
|
121
|
+
key: shallowCloneNode(key),
|
|
122
|
+
value: getValue(bindingIdent),
|
|
123
|
+
shorthand: false,
|
|
124
|
+
method: false,
|
|
125
|
+
computed: false,
|
|
126
|
+
...etc(),
|
|
127
|
+
parent: EMPTY_PARENT,
|
|
128
|
+
};
|
|
129
|
+
} else {
|
|
130
|
+
return {
|
|
131
|
+
type: 'Property',
|
|
132
|
+
kind: 'init',
|
|
133
|
+
key: shallowCloneNode(key),
|
|
134
|
+
value: getValue(bindingIdent),
|
|
135
|
+
shorthand: true,
|
|
136
|
+
method: false,
|
|
137
|
+
computed: false,
|
|
138
|
+
...etc(),
|
|
139
|
+
parent: EMPTY_PARENT,
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
case 'Literal': {
|
|
144
|
+
const bindingName = genID.id();
|
|
145
|
+
const bindingIdent = ident(bindingName);
|
|
146
|
+
reservedPropNames.set(keyName, bindingName);
|
|
147
|
+
|
|
148
|
+
return {
|
|
149
|
+
type: 'Property',
|
|
150
|
+
kind: 'init',
|
|
151
|
+
key: shallowCloneNode(key),
|
|
152
|
+
value: getValue(bindingIdent),
|
|
153
|
+
shorthand: false,
|
|
154
|
+
method: false,
|
|
155
|
+
computed: false,
|
|
156
|
+
...etc(),
|
|
157
|
+
parent: EMPTY_PARENT,
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}),
|
|
162
|
+
typeAnnotation: null,
|
|
163
|
+
...etc(),
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
// Create the constructor method
|
|
167
|
+
const constructor: MethodDefinition = {
|
|
168
|
+
type: 'MethodDefinition',
|
|
169
|
+
key: ident('constructor'),
|
|
170
|
+
kind: 'constructor',
|
|
171
|
+
computed: false,
|
|
172
|
+
static: false,
|
|
173
|
+
value: {
|
|
174
|
+
type: 'FunctionExpression',
|
|
175
|
+
id: null,
|
|
176
|
+
params: [constructorParam],
|
|
177
|
+
body: {
|
|
178
|
+
type: 'BlockStatement',
|
|
179
|
+
body: ownProperties.map(({key}) => {
|
|
180
|
+
const keyName = nameOfKey(key);
|
|
181
|
+
const bindingIdent = ident(reservedPropNames.get(keyName) ?? keyName);
|
|
182
|
+
const object: ThisExpression = {type: 'ThisExpression', ...etc()};
|
|
183
|
+
const memberExpression: MemberExpression =
|
|
184
|
+
key.type === 'Identifier'
|
|
185
|
+
? {
|
|
186
|
+
type: 'MemberExpression',
|
|
187
|
+
object,
|
|
188
|
+
property: shallowCloneNode(key),
|
|
189
|
+
computed: false,
|
|
190
|
+
optional: false,
|
|
191
|
+
...etc(),
|
|
192
|
+
}
|
|
193
|
+
: {
|
|
194
|
+
type: 'MemberExpression',
|
|
195
|
+
object,
|
|
196
|
+
property: shallowCloneNode(key),
|
|
197
|
+
computed: true,
|
|
198
|
+
optional: false,
|
|
199
|
+
...etc(),
|
|
200
|
+
};
|
|
201
|
+
return {
|
|
202
|
+
type: 'ExpressionStatement',
|
|
203
|
+
expression: {
|
|
204
|
+
type: 'AssignmentExpression',
|
|
205
|
+
operator: '=',
|
|
206
|
+
left: memberExpression,
|
|
207
|
+
right: bindingIdent,
|
|
208
|
+
...etc(),
|
|
209
|
+
},
|
|
210
|
+
directive: null,
|
|
211
|
+
...etc(),
|
|
212
|
+
};
|
|
213
|
+
}),
|
|
214
|
+
...etc(),
|
|
215
|
+
},
|
|
216
|
+
generator: false,
|
|
217
|
+
async: false,
|
|
218
|
+
predicate: null,
|
|
219
|
+
returnType: null,
|
|
220
|
+
typeParameters: null,
|
|
221
|
+
...etc(),
|
|
222
|
+
},
|
|
223
|
+
...etc(),
|
|
224
|
+
parent: EMPTY_PARENT,
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
const classStaticProperties: ReadonlyArray<ClassMember> =
|
|
228
|
+
staticProperties.map(prop => ({
|
|
229
|
+
type: 'PropertyDefinition',
|
|
230
|
+
key: shallowCloneNode(prop.key),
|
|
231
|
+
value: deepCloneNode(prop.value),
|
|
232
|
+
static: true,
|
|
233
|
+
typeAnnotation: null,
|
|
234
|
+
variance: null,
|
|
235
|
+
computed: false,
|
|
236
|
+
declare: false,
|
|
237
|
+
optional: false,
|
|
238
|
+
...etc(),
|
|
239
|
+
parent: EMPTY_PARENT,
|
|
240
|
+
}));
|
|
241
|
+
|
|
242
|
+
const classBodyElements: ReadonlyArray<ClassMember> = [
|
|
243
|
+
constructor,
|
|
244
|
+
...methods,
|
|
245
|
+
...classStaticProperties,
|
|
246
|
+
...staticMethods,
|
|
247
|
+
];
|
|
248
|
+
|
|
249
|
+
return {
|
|
250
|
+
type: 'ClassDeclaration',
|
|
251
|
+
id: shallowCloneNode(node.id),
|
|
252
|
+
body: {
|
|
253
|
+
type: 'ClassBody',
|
|
254
|
+
body: classBodyElements,
|
|
255
|
+
...etc(),
|
|
256
|
+
parent: EMPTY_PARENT,
|
|
257
|
+
},
|
|
258
|
+
superClass: null,
|
|
259
|
+
typeParameters: null,
|
|
260
|
+
superTypeArguments: null,
|
|
261
|
+
implements: [],
|
|
262
|
+
decorators: [],
|
|
263
|
+
...etc(),
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function mapRecordExpression(node: RecordExpression): NewExpression {
|
|
268
|
+
const obj: ObjectExpression = {
|
|
269
|
+
type: 'ObjectExpression',
|
|
270
|
+
properties: node.properties.properties,
|
|
271
|
+
...etc(),
|
|
272
|
+
};
|
|
273
|
+
return {
|
|
274
|
+
type: 'NewExpression',
|
|
275
|
+
callee: node.recordConstructor,
|
|
276
|
+
arguments: [obj],
|
|
277
|
+
typeArguments: null,
|
|
278
|
+
...etc(),
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
export function transformProgram(
|
|
283
|
+
program: Program,
|
|
284
|
+
_options: ParserOptions,
|
|
285
|
+
): Program {
|
|
286
|
+
const genID = new GenID('r');
|
|
287
|
+
return SimpleTransform.transformProgram(program, {
|
|
288
|
+
transform(node: ESNode) {
|
|
289
|
+
switch (node.type) {
|
|
290
|
+
case 'RecordDeclaration': {
|
|
291
|
+
return mapRecordDeclaration(genID, node);
|
|
292
|
+
}
|
|
293
|
+
case 'RecordExpression': {
|
|
294
|
+
return mapRecordExpression(node);
|
|
295
|
+
}
|
|
296
|
+
case 'Identifier': {
|
|
297
|
+
// A rudimentary check to avoid some collisions with our generated
|
|
298
|
+
// variable names. Ideally, we would have access a scope analyzer
|
|
299
|
+
// inside the transform instead.
|
|
300
|
+
genID.addUsage(node.name);
|
|
301
|
+
return node;
|
|
302
|
+
}
|
|
303
|
+
default:
|
|
304
|
+
return node;
|
|
305
|
+
}
|
|
306
|
+
},
|
|
307
|
+
});
|
|
308
|
+
}
|
|
@@ -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'],
|