[rust] More es2015 ast types
Adds almost all the remaining AST types from ES2015 to `estree`, and updates the swc->estree->hir conversions accordingly. In a few places i punted with a `Diagnostic::todo()`.
Joe Savona committed
Jul 25, 2023 at 14:08 UTC
c19286fce7a8354b2995a1b722647ddb74b83803
6 files changed
+902
-98
compiler/forget/crates/forget_build_hir/src/build.rs
+36
-20
@@ -56,6 +56,12 @@ pub fn build<'a>(
56
)?;
57
params.push(identifier);
58
}
59
+ _ => {
60
+ return Err(Diagnostic::todo(
61
+ "Support non-identifier params",
62
+ param.range(),
63
+ ));
64
+ }
65
}
66
}
67
@@ -174,27 +180,37 @@ fn lower_statement<'a>(
180
value,
181
)?;
182
} else {
177
- if let Pattern::Identifier(id) = declaration.id {
178
- // TODO: handle unbound variables
179
- let binding = builder.resolve_identifier(&id)?;
180
- let identifier = match binding {
181
- Binding::Local(identifier) => identifier,
182
- _ => {
183
- return Err(Diagnostic::invariant(
184
- BuildHIRError::VariableDeclarationBindingIsNonLocal,
185
- id.range,
186
- ));
187
- }
188
- };
189
- builder.push(InstructionValue::DeclareLocal(forget_hir::DeclareLocal {
190
- lvalue: LValue {
191
- identifier: IdentifierOperand {
192
- identifier,
193
- effect: None,
183
+ match declaration.id {
184
+ Pattern::Identifier(id) => {
185
+ // TODO: handle unbound variables
186
+ let binding = builder.resolve_identifier(&id)?;
187
+ let identifier = match binding {
188
+ Binding::Local(identifier) => identifier,
189
+ _ => {
190
+ return Err(Diagnostic::invariant(
191
+ BuildHIRError::VariableDeclarationBindingIsNonLocal,
192
+ id.range,
193
+ ));
194
+ }
195
+ };
196
+ builder.push(InstructionValue::DeclareLocal(
197
+ forget_hir::DeclareLocal {
198
+ lvalue: LValue {
199
+ identifier: IdentifierOperand {
200
+ identifier,
201
+ effect: None,
202
+ },
203
+ kind,
204
+ },
205
},
195
- kind,
196
- },
197
- }));
206
+ ));
207
+ }
208
+ _ => {
209
+ return Err(Diagnostic::todo(
210
+ "Handle non-identifier variable declarations",
211
+ declaration.range,
212
+ ));
213
+ }
214
}
215
}
216
}
compiler/forget/crates/forget_estree/src/generated.rs
+556
-56
@@ -257,6 +257,57 @@ pub struct FunctionDeclaration {
257
pub range: Option<SourceRange>,
258
}
259
#[derive(Serialize, Deserialize, Clone, Debug)]
260
+pub struct Class {
261
+ pub id: Option<Identifier>,
262
+ #[serde(rename = "superClass")]
263
+ pub super_class: Option<Expression>,
264
+ pub body: ClassBody,
265
+ #[serde(default)]
266
+ pub loc: Option<SourceLocation>,
267
+ #[serde(default)]
268
+ pub range: Option<SourceRange>,
269
+}
270
+#[derive(Serialize, Deserialize, Clone, Debug)]
271
+pub struct ClassDeclaration {
272
+ #[serde(flatten)]
273
+ pub class: Class,
274
+ #[serde(default)]
275
+ pub loc: Option<SourceLocation>,
276
+ #[serde(default)]
277
+ pub range: Option<SourceRange>,
278
+}
279
+#[derive(Serialize, Deserialize, Clone, Debug)]
280
+pub struct ClassExpression {
281
+ #[serde(flatten)]
282
+ pub class: Class,
283
+ #[serde(default)]
284
+ pub loc: Option<SourceLocation>,
285
+ #[serde(default)]
286
+ pub range: Option<SourceRange>,
287
+}
288
+#[derive(Serialize, Deserialize, Clone, Debug)]
289
+pub struct ClassBody {
290
+ pub body: Vec<MethodDefinition>,
291
+ #[serde(default)]
292
+ pub loc: Option<SourceLocation>,
293
+ #[serde(default)]
294
+ pub range: Option<SourceRange>,
295
+}
296
+#[derive(Serialize, Deserialize, Clone, Debug)]
297
+pub struct MethodDefinition {
298
+ pub key: Expression,
299
+ pub value: FunctionExpression,
300
+ pub kind: MethodKind,
301
+ #[serde(rename = "computed")]
302
+ pub is_computed: bool,
303
+ #[serde(rename = "static")]
304
+ pub is_static: bool,
305
+ #[serde(default)]
306
+ pub loc: Option<SourceLocation>,
307
+ #[serde(default)]
308
+ pub range: Option<SourceRange>,
309
+}
310
+#[derive(Serialize, Deserialize, Clone, Debug)]
311
pub struct VariableDeclaration {
312
pub kind: VariableDeclarationKind,
313
pub declarations: Vec<VariableDeclarator>,
@@ -299,9 +350,15 @@ pub struct ObjectExpression {
350
}
351
#[derive(Serialize, Deserialize, Clone, Debug)]
352
pub struct Property {
302
- pub key: PropertyKey,
353
+ pub key: Expression,
354
pub value: Expression,
355
pub kind: PropertyKind,
356
+ #[serde(rename = "method")]
357
+ pub is_method: bool,
358
+ #[serde(rename = "shorthand")]
359
+ pub is_shorthand: bool,
360
+ #[serde(rename = "computed")]
361
+ pub is_computed: bool,
362
#[serde(default)]
363
pub loc: Option<SourceLocation>,
364
#[serde(default)]
@@ -484,6 +541,40 @@ pub struct ImportNamespaceSpecifier {
541
pub range: Option<SourceRange>,
542
}
543
#[derive(Serialize, Deserialize, Clone, Debug)]
544
+pub struct ExportNamedDeclaration {
545
+ pub declaration: Option<Declaration>,
546
+ pub specifiers: Vec<ExportSpecifier>,
547
+ pub source: Option<Literal>,
548
+ #[serde(default)]
549
+ pub loc: Option<SourceLocation>,
550
+ #[serde(default)]
551
+ pub range: Option<SourceRange>,
552
+}
553
+#[derive(Serialize, Deserialize, Clone, Debug)]
554
+pub struct ExportSpecifier {
555
+ pub exported: Identifier,
556
+ #[serde(default)]
557
+ pub loc: Option<SourceLocation>,
558
+ #[serde(default)]
559
+ pub range: Option<SourceRange>,
560
+}
561
+#[derive(Serialize, Deserialize, Clone, Debug)]
562
+pub struct ExportDefaultDeclaration {
563
+ pub declaration: Declaration,
564
+ #[serde(default)]
565
+ pub loc: Option<SourceLocation>,
566
+ #[serde(default)]
567
+ pub range: Option<SourceRange>,
568
+}
569
+#[derive(Serialize, Deserialize, Clone, Debug)]
570
+pub struct ExportAllDeclaration {
571
+ pub source: Literal,
572
+ #[serde(default)]
573
+ pub loc: Option<SourceLocation>,
574
+ #[serde(default)]
575
+ pub range: Option<SourceRange>,
576
+}
577
+#[derive(Serialize, Deserialize, Clone, Debug)]
578
pub struct JSXIdentifier {
579
pub name: String,
580
#[serde(default)]
@@ -615,11 +706,56 @@ pub struct JSXClosingFragment {
706
#[serde(default)]
707
pub range: Option<SourceRange>,
708
}
709
+#[derive(Serialize, Deserialize, Clone, Debug)]
710
+pub struct ArrayPattern {
711
+ pub elements: Vec<Option<Pattern>>,
712
+ #[serde(default)]
713
+ pub loc: Option<SourceLocation>,
714
+ #[serde(default)]
715
+ pub range: Option<SourceRange>,
716
+}
717
+#[derive(Serialize, Deserialize, Clone, Debug)]
718
+pub struct ObjectPattern {
719
+ pub properties: Vec<AssignmentProperty>,
720
+ #[serde(default)]
721
+ pub loc: Option<SourceLocation>,
722
+ #[serde(default)]
723
+ pub range: Option<SourceRange>,
724
+}
725
+#[derive(Serialize, Deserialize, Clone, Debug)]
726
+pub struct AssignmentProperty {
727
+ pub key: PropertyKey,
728
+ pub value: Pattern,
729
+ pub kind: PropertyKind,
730
+ pub method: bool,
731
+ #[serde(default)]
732
+ pub loc: Option<SourceLocation>,
733
+ #[serde(default)]
734
+ pub range: Option<SourceRange>,
735
+}
736
+#[derive(Serialize, Deserialize, Clone, Debug)]
737
+pub struct RestElement {
738
+ pub argument: Pattern,
739
+ #[serde(default)]
740
+ pub loc: Option<SourceLocation>,
741
+ #[serde(default)]
742
+ pub range: Option<SourceRange>,
743
+}
744
+#[derive(Serialize, Deserialize, Clone, Debug)]
745
+pub struct AssignmentPattern {
746
+ pub left: Pattern,
747
+ pub right: Expression,
748
+ #[serde(default)]
749
+ pub loc: Option<SourceLocation>,
750
+ #[serde(default)]
751
+ pub range: Option<SourceRange>,
752
+}
753
#[derive(Serialize, Clone, Debug)]
754
#[serde(tag = "type")]
755
pub enum Statement {
756
BlockStatement(Box<BlockStatement>),
757
BreakStatement(Box<BreakStatement>),
758
+ ClassDeclaration(Box<ClassDeclaration>),
759
ContinueStatement(Box<ContinueStatement>),
760
DebuggerStatement(Box<DebuggerStatement>),
761
DoWhileStatement(Box<DoWhileStatement>),
@@ -643,6 +779,7 @@ pub enum Statement {
779
enum __StatementTag {
780
BlockStatement,
781
BreakStatement,
782
+ ClassDeclaration,
783
ContinueStatement,
784
DebuggerStatement,
785
DoWhileStatement,
@@ -690,6 +827,14 @@ impl<'de> serde::Deserialize<'de> for Statement {
827
)?;
828
Ok(Statement::BreakStatement(node))
829
}
830
+ __StatementTag::ClassDeclaration => {
831
+ let node: Box<ClassDeclaration> = <Box<
832
+ ClassDeclaration,
833
+ > as Deserialize>::deserialize(
834
+ serde::__private::de::ContentDeserializer::<D::Error>::new(tagged.1),
835
+ )?;
836
+ Ok(Statement::ClassDeclaration(node))
837
+ }
838
__StatementTag::ContinueStatement => {
839
let node: Box<ContinueStatement> = <Box<
840
ContinueStatement,
@@ -845,6 +990,7 @@ pub enum Expression {
990
AssignmentExpression(Box<AssignmentExpression>),
991
BinaryExpression(Box<BinaryExpression>),
992
CallExpression(Box<CallExpression>),
993
+ ClassExpression(Box<ClassExpression>),
994
ConditionalExpression(Box<ConditionalExpression>),
995
FunctionExpression(Box<FunctionExpression>),
996
Identifier(Box<Identifier>),
@@ -867,9 +1013,11 @@ enum __ExpressionTag {
1013
AssignmentExpression,
1014
BinaryExpression,
1015
CallExpression,
1016
+ ClassExpression,
1017
ConditionalExpression,
1018
FunctionExpression,
1019
Identifier,
1020
+ JSXElement,
1021
Literal,
1022
LogicalExpression,
1023
MemberExpression,
@@ -880,7 +1028,6 @@ enum __ExpressionTag {
1028
UnaryExpression,
1029
UpdateExpression,
1030
YieldExpression,
883
- JSXElement,
1031
}
1032
impl<'de> serde::Deserialize<'de> for Expression {
1033
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
@@ -934,6 +1081,14 @@ impl<'de> serde::Deserialize<'de> for Expression {
1081
)?;
1082
Ok(Expression::CallExpression(node))
1083
}
1084
+ __ExpressionTag::ClassExpression => {
1085
+ let node: Box<ClassExpression> = <Box<
1086
+ ClassExpression,
1087
+ > as Deserialize>::deserialize(
1088
+ serde::__private::de::ContentDeserializer::<D::Error>::new(tagged.1),
1089
+ )?;
1090
+ Ok(Expression::ClassExpression(node))
1091
+ }
1092
__ExpressionTag::ConditionalExpression => {
1093
let node: Box<ConditionalExpression> = <Box<
1094
ConditionalExpression,
@@ -958,6 +1113,14 @@ impl<'de> serde::Deserialize<'de> for Expression {
1113
)?;
1114
Ok(Expression::Identifier(node))
1115
}
1116
+ __ExpressionTag::JSXElement => {
1117
+ let node: Box<JSXElement> = <Box<
1118
+ JSXElement,
1119
+ > as Deserialize>::deserialize(
1120
+ serde::__private::de::ContentDeserializer::<D::Error>::new(tagged.1),
1121
+ )?;
1122
+ Ok(Expression::JSXElement(node))
1123
+ }
1124
__ExpressionTag::Literal => {
1125
let node: Box<Literal> = <Box<
1126
Literal,
@@ -1038,13 +1201,57 @@ impl<'de> serde::Deserialize<'de> for Expression {
1201
)?;
1202
Ok(Expression::YieldExpression(node))
1203
}
1041
- __ExpressionTag::JSXElement => {
1042
- let node: Box<JSXElement> = <Box<
1043
- JSXElement,
1204
+ }
1205
+ }
1206
+}
1207
+#[derive(Serialize, Clone, Debug)]
1208
+#[serde(tag = "type")]
1209
+pub enum Declaration {
1210
+ ClassDeclaration(Box<ClassDeclaration>),
1211
+ FunctionDeclaration(Box<FunctionDeclaration>),
1212
+ VariableDeclaration(Box<VariableDeclaration>),
1213
+}
1214
+#[derive(Deserialize, Debug)]
1215
+enum __DeclarationTag {
1216
+ ClassDeclaration,
1217
+ FunctionDeclaration,
1218
+ VariableDeclaration,
1219
+}
1220
+impl<'de> serde::Deserialize<'de> for Declaration {
1221
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
1222
+ where
1223
+ D: serde::Deserializer<'de>,
1224
+ {
1225
+ let tagged = serde::Deserializer::deserialize_any(
1226
+ deserializer,
1227
+ serde::__private::de::TaggedContentVisitor::<
1228
+ __DeclarationTag,
1229
+ >::new("type", "Pattern"),
1230
+ )?;
1231
+ match tagged.0 {
1232
+ __DeclarationTag::ClassDeclaration => {
1233
+ let node: Box<ClassDeclaration> = <Box<
1234
+ ClassDeclaration,
1235
> as Deserialize>::deserialize(
1236
serde::__private::de::ContentDeserializer::<D::Error>::new(tagged.1),
1237
)?;
1047
- Ok(Expression::JSXElement(node))
1238
+ Ok(Declaration::ClassDeclaration(node))
1239
+ }
1240
+ __DeclarationTag::FunctionDeclaration => {
1241
+ let node: Box<FunctionDeclaration> = <Box<
1242
+ FunctionDeclaration,
1243
+ > as Deserialize>::deserialize(
1244
+ serde::__private::de::ContentDeserializer::<D::Error>::new(tagged.1),
1245
+ )?;
1246
+ Ok(Declaration::FunctionDeclaration(node))
1247
+ }
1248
+ __DeclarationTag::VariableDeclaration => {
1249
+ let node: Box<VariableDeclaration> = <Box<
1250
+ VariableDeclaration,
1251
+ > as Deserialize>::deserialize(
1252
+ serde::__private::de::ContentDeserializer::<D::Error>::new(tagged.1),
1253
+ )?;
1254
+ Ok(Declaration::VariableDeclaration(node))
1255
}
1256
}
1257
}
@@ -1110,8 +1317,12 @@ pub enum ModuleItem {
1317
#[derive(Deserialize, Debug)]
1318
enum __ModuleItemTag {
1319
ImportDeclaration,
1320
+ ExportNamedDeclaration,
1321
+ ExportDefaultDeclaration,
1322
+ ExportAllDeclaration,
1323
BlockStatement,
1324
BreakStatement,
1325
+ ClassDeclaration,
1326
ContinueStatement,
1327
DebuggerStatement,
1328
DoWhileStatement,
@@ -1155,6 +1366,42 @@ impl<'de> serde::Deserialize<'de> for ModuleItem {
1366
),
1367
)
1368
}
1369
+ __ModuleItemTag::ExportNamedDeclaration => {
1370
+ let node: Box<ExportNamedDeclaration> = <Box<
1371
+ ExportNamedDeclaration,
1372
+ > as Deserialize>::deserialize(
1373
+ serde::__private::de::ContentDeserializer::<D::Error>::new(tagged.1),
1374
+ )?;
1375
+ Ok(
1376
+ ModuleItem::ImportOrExportDeclaration(
1377
+ ImportOrExportDeclaration::ExportNamedDeclaration(node),
1378
+ ),
1379
+ )
1380
+ }
1381
+ __ModuleItemTag::ExportDefaultDeclaration => {
1382
+ let node: Box<ExportDefaultDeclaration> = <Box<
1383
+ ExportDefaultDeclaration,
1384
+ > as Deserialize>::deserialize(
1385
+ serde::__private::de::ContentDeserializer::<D::Error>::new(tagged.1),
1386
+ )?;
1387
+ Ok(
1388
+ ModuleItem::ImportOrExportDeclaration(
1389
+ ImportOrExportDeclaration::ExportDefaultDeclaration(node),
1390
+ ),
1391
+ )
1392
+ }
1393
+ __ModuleItemTag::ExportAllDeclaration => {
1394
+ let node: Box<ExportAllDeclaration> = <Box<
1395
+ ExportAllDeclaration,
1396
+ > as Deserialize>::deserialize(
1397
+ serde::__private::de::ContentDeserializer::<D::Error>::new(tagged.1),
1398
+ )?;
1399
+ Ok(
1400
+ ModuleItem::ImportOrExportDeclaration(
1401
+ ImportOrExportDeclaration::ExportAllDeclaration(node),
1402
+ ),
1403
+ )
1404
+ }
1405
__ModuleItemTag::BlockStatement => {
1406
let node: Box<BlockStatement> = <Box<
1407
BlockStatement,
@@ -1171,6 +1418,14 @@ impl<'de> serde::Deserialize<'de> for ModuleItem {
1418
)?;
1419
Ok(ModuleItem::Statement(Statement::BreakStatement(node)))
1420
}
1421
+ __ModuleItemTag::ClassDeclaration => {
1422
+ let node: Box<ClassDeclaration> = <Box<
1423
+ ClassDeclaration,
1424
+ > as Deserialize>::deserialize(
1425
+ serde::__private::de::ContentDeserializer::<D::Error>::new(tagged.1),
1426
+ )?;
1427
+ Ok(ModuleItem::Statement(Statement::ClassDeclaration(node)))
1428
+ }
1429
__ModuleItemTag::ContinueStatement => {
1430
let node: Box<ContinueStatement> = <Box<
1431
ContinueStatement,
@@ -1321,11 +1576,17 @@ impl<'de> serde::Deserialize<'de> for ModuleItem {
1576
#[derive(Serialize, Clone, Debug)]
1577
#[serde(tag = "type")]
1578
pub enum ImportOrExportDeclaration {
1579
+ ExportAllDeclaration(Box<ExportAllDeclaration>),
1580
+ ExportDefaultDeclaration(Box<ExportDefaultDeclaration>),
1581
+ ExportNamedDeclaration(Box<ExportNamedDeclaration>),
1582
ImportDeclaration(Box<ImportDeclaration>),
1583
}
1584
#[derive(Deserialize, Debug)]
1585
enum __ImportOrExportDeclarationTag {
1586
ImportDeclaration,
1587
+ ExportNamedDeclaration,
1588
+ ExportDefaultDeclaration,
1589
+ ExportAllDeclaration,
1590
}
1591
impl<'de> serde::Deserialize<'de> for ImportOrExportDeclaration {
1592
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
@@ -1347,6 +1608,30 @@ impl<'de> serde::Deserialize<'de> for ImportOrExportDeclaration {
1608
)?;
1609
Ok(ImportOrExportDeclaration::ImportDeclaration(node))
1610
}
1611
+ __ImportOrExportDeclarationTag::ExportNamedDeclaration => {
1612
+ let node: Box<ExportNamedDeclaration> = <Box<
1613
+ ExportNamedDeclaration,
1614
+ > as Deserialize>::deserialize(
1615
+ serde::__private::de::ContentDeserializer::<D::Error>::new(tagged.1),
1616
+ )?;
1617
+ Ok(ImportOrExportDeclaration::ExportNamedDeclaration(node))
1618
+ }
1619
+ __ImportOrExportDeclarationTag::ExportDefaultDeclaration => {
1620
+ let node: Box<ExportDefaultDeclaration> = <Box<
1621
+ ExportDefaultDeclaration,
1622
+ > as Deserialize>::deserialize(
1623
+ serde::__private::de::ContentDeserializer::<D::Error>::new(tagged.1),
1624
+ )?;
1625
+ Ok(ImportOrExportDeclaration::ExportDefaultDeclaration(node))
1626
+ }
1627
+ __ImportOrExportDeclarationTag::ExportAllDeclaration => {
1628
+ let node: Box<ExportAllDeclaration> = <Box<
1629
+ ExportAllDeclaration,
1630
+ > as Deserialize>::deserialize(
1631
+ serde::__private::de::ContentDeserializer::<D::Error>::new(tagged.1),
1632
+ )?;
1633
+ Ok(ImportOrExportDeclaration::ExportAllDeclaration(node))
1634
+ }
1635
}
1636
}
1637
}
@@ -1363,9 +1648,11 @@ enum __ExpressionOrSuperTag {
1648
AssignmentExpression,
1649
BinaryExpression,
1650
CallExpression,
1651
+ ClassExpression,
1652
ConditionalExpression,
1653
FunctionExpression,
1654
Identifier,
1655
+ JSXElement,
1656
Literal,
1657
LogicalExpression,
1658
MemberExpression,
@@ -1376,7 +1663,6 @@ enum __ExpressionOrSuperTag {
1663
UnaryExpression,
1664
UpdateExpression,
1665
YieldExpression,
1379
- JSXElement,
1666
Super,
1667
}
1668
impl<'de> serde::Deserialize<'de> for ExpressionOrSuper {
@@ -1435,6 +1721,14 @@ impl<'de> serde::Deserialize<'de> for ExpressionOrSuper {
1721
)?;
1722
Ok(ExpressionOrSuper::Expression(Expression::CallExpression(node)))
1723
}
1724
+ __ExpressionOrSuperTag::ClassExpression => {
1725
+ let node: Box<ClassExpression> = <Box<
1726
+ ClassExpression,
1727
+ > as Deserialize>::deserialize(
1728
+ serde::__private::de::ContentDeserializer::<D::Error>::new(tagged.1),
1729
+ )?;
1730
+ Ok(ExpressionOrSuper::Expression(Expression::ClassExpression(node)))
1731
+ }
1732
__ExpressionOrSuperTag::ConditionalExpression => {
1733
let node: Box<ConditionalExpression> = <Box<
1734
ConditionalExpression,
@@ -1463,6 +1757,14 @@ impl<'de> serde::Deserialize<'de> for ExpressionOrSuper {
1757
)?;
1758
Ok(ExpressionOrSuper::Expression(Expression::Identifier(node)))
1759
}
1760
+ __ExpressionOrSuperTag::JSXElement => {
1761
+ let node: Box<JSXElement> = <Box<
1762
+ JSXElement,
1763
+ > as Deserialize>::deserialize(
1764
+ serde::__private::de::ContentDeserializer::<D::Error>::new(tagged.1),
1765
+ )?;
1766
+ Ok(ExpressionOrSuper::Expression(Expression::JSXElement(node)))
1767
+ }
1768
__ExpressionOrSuperTag::Literal => {
1769
let node: Box<Literal> = <Box<
1770
Literal,
@@ -1543,14 +1845,6 @@ impl<'de> serde::Deserialize<'de> for ExpressionOrSuper {
1845
)?;
1846
Ok(ExpressionOrSuper::Expression(Expression::YieldExpression(node)))
1847
}
1546
- __ExpressionOrSuperTag::JSXElement => {
1547
- let node: Box<JSXElement> = <Box<
1548
- JSXElement,
1549
- > as Deserialize>::deserialize(
1550
- serde::__private::de::ContentDeserializer::<D::Error>::new(tagged.1),
1551
- )?;
1552
- Ok(ExpressionOrSuper::Expression(Expression::JSXElement(node)))
1553
- }
1848
__ExpressionOrSuperTag::Super => {
1849
let node: Box<Super> = <Box<
1850
Super,
@@ -1575,9 +1869,11 @@ enum __ExpressionOrSpreadTag {
1869
AssignmentExpression,
1870
BinaryExpression,
1871
CallExpression,
1872
+ ClassExpression,
1873
ConditionalExpression,
1874
FunctionExpression,
1875
Identifier,
1876
+ JSXElement,
1877
Literal,
1878
LogicalExpression,
1879
MemberExpression,
@@ -1588,7 +1884,6 @@ enum __ExpressionOrSpreadTag {
1884
UnaryExpression,
1885
UpdateExpression,
1886
YieldExpression,
1591
- JSXElement,
1887
SpreadElement,
1888
}
1889
impl<'de> serde::Deserialize<'de> for ExpressionOrSpread {
@@ -1651,6 +1946,14 @@ impl<'de> serde::Deserialize<'de> for ExpressionOrSpread {
1946
)?;
1947
Ok(ExpressionOrSpread::Expression(Expression::CallExpression(node)))
1948
}
1949
+ __ExpressionOrSpreadTag::ClassExpression => {
1950
+ let node: Box<ClassExpression> = <Box<
1951
+ ClassExpression,
1952
+ > as Deserialize>::deserialize(
1953
+ serde::__private::de::ContentDeserializer::<D::Error>::new(tagged.1),
1954
+ )?;
1955
+ Ok(ExpressionOrSpread::Expression(Expression::ClassExpression(node)))
1956
+ }
1957
__ExpressionOrSpreadTag::ConditionalExpression => {
1958
let node: Box<ConditionalExpression> = <Box<
1959
ConditionalExpression,
@@ -1679,6 +1982,14 @@ impl<'de> serde::Deserialize<'de> for ExpressionOrSpread {
1982
)?;
1983
Ok(ExpressionOrSpread::Expression(Expression::Identifier(node)))
1984
}
1985
+ __ExpressionOrSpreadTag::JSXElement => {
1986
+ let node: Box<JSXElement> = <Box<
1987
+ JSXElement,
1988
+ > as Deserialize>::deserialize(
1989
+ serde::__private::de::ContentDeserializer::<D::Error>::new(tagged.1),
1990
+ )?;
1991
+ Ok(ExpressionOrSpread::Expression(Expression::JSXElement(node)))
1992
+ }
1993
__ExpressionOrSpreadTag::Literal => {
1994
let node: Box<Literal> = <Box<
1995
Literal,
@@ -1759,14 +2070,6 @@ impl<'de> serde::Deserialize<'de> for ExpressionOrSpread {
2070
)?;
2071
Ok(ExpressionOrSpread::Expression(Expression::YieldExpression(node)))
2072
}
1762
- __ExpressionOrSpreadTag::JSXElement => {
1763
- let node: Box<JSXElement> = <Box<
1764
- JSXElement,
1765
- > as Deserialize>::deserialize(
1766
- serde::__private::de::ContentDeserializer::<D::Error>::new(tagged.1),
1767
- )?;
1768
- Ok(ExpressionOrSpread::Expression(Expression::JSXElement(node)))
1769
- }
2073
__ExpressionOrSpreadTag::SpreadElement => {
2074
let node: Box<SpreadElement> = <Box<
2075
SpreadElement,
@@ -1792,9 +2095,11 @@ enum __FunctionBodyTag {
2095
AssignmentExpression,
2096
BinaryExpression,
2097
CallExpression,
2098
+ ClassExpression,
2099
ConditionalExpression,
2100
FunctionExpression,
2101
Identifier,
2102
+ JSXElement,
2103
Literal,
2104
LogicalExpression,
2105
MemberExpression,
@@ -1805,7 +2110,6 @@ enum __FunctionBodyTag {
2110
UnaryExpression,
2111
UpdateExpression,
2112
YieldExpression,
1808
- JSXElement,
2113
}
2114
impl<'de> serde::Deserialize<'de> for FunctionBody {
2115
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
@@ -1867,6 +2171,14 @@ impl<'de> serde::Deserialize<'de> for FunctionBody {
2171
)?;
2172
Ok(FunctionBody::Expression(Expression::CallExpression(node)))
2173
}
2174
+ __FunctionBodyTag::ClassExpression => {
2175
+ let node: Box<ClassExpression> = <Box<
2176
+ ClassExpression,
2177
+ > as Deserialize>::deserialize(
2178
+ serde::__private::de::ContentDeserializer::<D::Error>::new(tagged.1),
2179
+ )?;
2180
+ Ok(FunctionBody::Expression(Expression::ClassExpression(node)))
2181
+ }
2182
__FunctionBodyTag::ConditionalExpression => {
2183
let node: Box<ConditionalExpression> = <Box<
2184
ConditionalExpression,
@@ -1891,6 +2203,14 @@ impl<'de> serde::Deserialize<'de> for FunctionBody {
2203
)?;
2204
Ok(FunctionBody::Expression(Expression::Identifier(node)))
2205
}
2206
+ __FunctionBodyTag::JSXElement => {
2207
+ let node: Box<JSXElement> = <Box<
2208
+ JSXElement,
2209
+ > as Deserialize>::deserialize(
2210
+ serde::__private::de::ContentDeserializer::<D::Error>::new(tagged.1),
2211
+ )?;
2212
+ Ok(FunctionBody::Expression(Expression::JSXElement(node)))
2213
+ }
2214
__FunctionBodyTag::Literal => {
2215
let node: Box<Literal> = <Box<
2216
Literal,
@@ -1971,25 +2291,25 @@ impl<'de> serde::Deserialize<'de> for FunctionBody {
2291
)?;
2292
Ok(FunctionBody::Expression(Expression::YieldExpression(node)))
2293
}
1974
- __FunctionBodyTag::JSXElement => {
1975
- let node: Box<JSXElement> = <Box<
1976
- JSXElement,
1977
- > as Deserialize>::deserialize(
1978
- serde::__private::de::ContentDeserializer::<D::Error>::new(tagged.1),
1979
- )?;
1980
- Ok(FunctionBody::Expression(Expression::JSXElement(node)))
1981
- }
2294
}
2295
}
2296
}
2297
#[derive(Serialize, Clone, Debug)]
2298
#[serde(tag = "type")]
2299
pub enum Pattern {
2300
+ ArrayPattern(Box<ArrayPattern>),
2301
+ AssignmentPattern(Box<AssignmentPattern>),
2302
Identifier(Box<Identifier>),
2303
+ ObjectPattern(Box<ObjectPattern>),
2304
+ RestElement(Box<RestElement>),
2305
}
2306
#[derive(Deserialize, Debug)]
2307
enum __PatternTag {
2308
Identifier,
2309
+ ArrayPattern,
2310
+ ObjectPattern,
2311
+ RestElement,
2312
+ AssignmentPattern,
2313
}
2314
impl<'de> serde::Deserialize<'de> for Pattern {
2315
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
@@ -2011,6 +2331,38 @@ impl<'de> serde::Deserialize<'de> for Pattern {
2331
)?;
2332
Ok(Pattern::Identifier(node))
2333
}
2334
+ __PatternTag::ArrayPattern => {
2335
+ let node: Box<ArrayPattern> = <Box<
2336
+ ArrayPattern,
2337
+ > as Deserialize>::deserialize(
2338
+ serde::__private::de::ContentDeserializer::<D::Error>::new(tagged.1),
2339
+ )?;
2340
+ Ok(Pattern::ArrayPattern(node))
2341
+ }
2342
+ __PatternTag::ObjectPattern => {
2343
+ let node: Box<ObjectPattern> = <Box<
2344
+ ObjectPattern,
2345
+ > as Deserialize>::deserialize(
2346
+ serde::__private::de::ContentDeserializer::<D::Error>::new(tagged.1),
2347
+ )?;
2348
+ Ok(Pattern::ObjectPattern(node))
2349
+ }
2350
+ __PatternTag::RestElement => {
2351
+ let node: Box<RestElement> = <Box<
2352
+ RestElement,
2353
+ > as Deserialize>::deserialize(
2354
+ serde::__private::de::ContentDeserializer::<D::Error>::new(tagged.1),
2355
+ )?;
2356
+ Ok(Pattern::RestElement(node))
2357
+ }
2358
+ __PatternTag::AssignmentPattern => {
2359
+ let node: Box<AssignmentPattern> = <Box<
2360
+ AssignmentPattern,
2361
+ > as Deserialize>::deserialize(
2362
+ serde::__private::de::ContentDeserializer::<D::Error>::new(tagged.1),
2363
+ )?;
2364
+ Ok(Pattern::AssignmentPattern(node))
2365
+ }
2366
}
2367
}
2368
}
@@ -2027,9 +2379,11 @@ enum __ForInitTag {
2379
AssignmentExpression,
2380
BinaryExpression,
2381
CallExpression,
2382
+ ClassExpression,
2383
ConditionalExpression,
2384
FunctionExpression,
2385
Identifier,
2386
+ JSXElement,
2387
Literal,
2388
LogicalExpression,
2389
MemberExpression,
@@ -2040,7 +2394,6 @@ enum __ForInitTag {
2394
UnaryExpression,
2395
UpdateExpression,
2396
YieldExpression,
2043
- JSXElement,
2397
VariableDeclaration,
2398
}
2399
impl<'de> serde::Deserialize<'de> for ForInit {
@@ -2095,6 +2448,14 @@ impl<'de> serde::Deserialize<'de> for ForInit {
2448
)?;
2449
Ok(ForInit::Expression(Expression::CallExpression(node)))
2450
}
2451
+ __ForInitTag::ClassExpression => {
2452
+ let node: Box<ClassExpression> = <Box<
2453
+ ClassExpression,
2454
+ > as Deserialize>::deserialize(
2455
+ serde::__private::de::ContentDeserializer::<D::Error>::new(tagged.1),
2456
+ )?;
2457
+ Ok(ForInit::Expression(Expression::ClassExpression(node)))
2458
+ }
2459
__ForInitTag::ConditionalExpression => {
2460
let node: Box<ConditionalExpression> = <Box<
2461
ConditionalExpression,
@@ -2119,6 +2480,14 @@ impl<'de> serde::Deserialize<'de> for ForInit {
2480
)?;
2481
Ok(ForInit::Expression(Expression::Identifier(node)))
2482
}
2483
+ __ForInitTag::JSXElement => {
2484
+ let node: Box<JSXElement> = <Box<
2485
+ JSXElement,
2486
+ > as Deserialize>::deserialize(
2487
+ serde::__private::de::ContentDeserializer::<D::Error>::new(tagged.1),
2488
+ )?;
2489
+ Ok(ForInit::Expression(Expression::JSXElement(node)))
2490
+ }
2491
__ForInitTag::Literal => {
2492
let node: Box<Literal> = <Box<
2493
Literal,
@@ -2199,14 +2568,6 @@ impl<'de> serde::Deserialize<'de> for ForInit {
2568
)?;
2569
Ok(ForInit::Expression(Expression::YieldExpression(node)))
2570
}
2202
- __ForInitTag::JSXElement => {
2203
- let node: Box<JSXElement> = <Box<
2204
- JSXElement,
2205
- > as Deserialize>::deserialize(
2206
- serde::__private::de::ContentDeserializer::<D::Error>::new(tagged.1),
2207
- )?;
2208
- Ok(ForInit::Expression(Expression::JSXElement(node)))
2209
- }
2571
__ForInitTag::VariableDeclaration => {
2572
let node: Box<VariableDeclaration> = <Box<
2573
VariableDeclaration,
@@ -2227,6 +2588,10 @@ pub enum ForInInit {
2588
#[derive(Deserialize, Debug)]
2589
enum __ForInInitTag {
2590
Identifier,
2591
+ ArrayPattern,
2592
+ ObjectPattern,
2593
+ RestElement,
2594
+ AssignmentPattern,
2595
VariableDeclaration,
2596
}
2597
impl<'de> serde::Deserialize<'de> for ForInInit {
@@ -2249,6 +2614,38 @@ impl<'de> serde::Deserialize<'de> for ForInInit {
2614
)?;
2615
Ok(ForInInit::Pattern(Pattern::Identifier(node)))
2616
}
2617
+ __ForInInitTag::ArrayPattern => {
2618
+ let node: Box<ArrayPattern> = <Box<
2619
+ ArrayPattern,
2620
+ > as Deserialize>::deserialize(
2621
+ serde::__private::de::ContentDeserializer::<D::Error>::new(tagged.1),
2622
+ )?;
2623
+ Ok(ForInInit::Pattern(Pattern::ArrayPattern(node)))
2624
+ }
2625
+ __ForInInitTag::ObjectPattern => {
2626
+ let node: Box<ObjectPattern> = <Box<
2627
+ ObjectPattern,
2628
+ > as Deserialize>::deserialize(
2629
+ serde::__private::de::ContentDeserializer::<D::Error>::new(tagged.1),
2630
+ )?;
2631
+ Ok(ForInInit::Pattern(Pattern::ObjectPattern(node)))
2632
+ }
2633
+ __ForInInitTag::RestElement => {
2634
+ let node: Box<RestElement> = <Box<
2635
+ RestElement,
2636
+ > as Deserialize>::deserialize(
2637
+ serde::__private::de::ContentDeserializer::<D::Error>::new(tagged.1),
2638
+ )?;
2639
+ Ok(ForInInit::Pattern(Pattern::RestElement(node)))
2640
+ }
2641
+ __ForInInitTag::AssignmentPattern => {
2642
+ let node: Box<AssignmentPattern> = <Box<
2643
+ AssignmentPattern,
2644
+ > as Deserialize>::deserialize(
2645
+ serde::__private::de::ContentDeserializer::<D::Error>::new(tagged.1),
2646
+ )?;
2647
+ Ok(ForInInit::Pattern(Pattern::AssignmentPattern(node)))
2648
+ }
2649
__ForInInitTag::VariableDeclaration => {
2650
let node: Box<VariableDeclaration> = <Box<
2651
VariableDeclaration,
@@ -2315,9 +2712,11 @@ enum __AssignmentTargetTag {
2712
AssignmentExpression,
2713
BinaryExpression,
2714
CallExpression,
2715
+ ClassExpression,
2716
ConditionalExpression,
2717
FunctionExpression,
2718
Identifier,
2719
+ JSXElement,
2720
Literal,
2721
LogicalExpression,
2722
MemberExpression,
@@ -2328,7 +2727,10 @@ enum __AssignmentTargetTag {
2727
UnaryExpression,
2728
UpdateExpression,
2729
YieldExpression,
2331
- JSXElement,
2730
+ ArrayPattern,
2731
+ ObjectPattern,
2732
+ RestElement,
2733
+ AssignmentPattern,
2734
}
2735
impl<'de> serde::Deserialize<'de> for AssignmentTarget {
2736
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
@@ -2386,6 +2788,14 @@ impl<'de> serde::Deserialize<'de> for AssignmentTarget {
2788
)?;
2789
Ok(AssignmentTarget::Expression(Expression::CallExpression(node)))
2790
}
2791
+ __AssignmentTargetTag::ClassExpression => {
2792
+ let node: Box<ClassExpression> = <Box<
2793
+ ClassExpression,
2794
+ > as Deserialize>::deserialize(
2795
+ serde::__private::de::ContentDeserializer::<D::Error>::new(tagged.1),
2796
+ )?;
2797
+ Ok(AssignmentTarget::Expression(Expression::ClassExpression(node)))
2798
+ }
2799
__AssignmentTargetTag::ConditionalExpression => {
2800
let node: Box<ConditionalExpression> = <Box<
2801
ConditionalExpression,
@@ -2410,6 +2820,14 @@ impl<'de> serde::Deserialize<'de> for AssignmentTarget {
2820
)?;
2821
Ok(AssignmentTarget::Expression(Expression::Identifier(node)))
2822
}
2823
+ __AssignmentTargetTag::JSXElement => {
2824
+ let node: Box<JSXElement> = <Box<
2825
+ JSXElement,
2826
+ > as Deserialize>::deserialize(
2827
+ serde::__private::de::ContentDeserializer::<D::Error>::new(tagged.1),
2828
+ )?;
2829
+ Ok(AssignmentTarget::Expression(Expression::JSXElement(node)))
2830
+ }
2831
__AssignmentTargetTag::Literal => {
2832
let node: Box<Literal> = <Box<
2833
Literal,
@@ -2490,13 +2908,37 @@ impl<'de> serde::Deserialize<'de> for AssignmentTarget {
2908
)?;
2909
Ok(AssignmentTarget::Expression(Expression::YieldExpression(node)))
2910
}
2493
- __AssignmentTargetTag::JSXElement => {
2494
- let node: Box<JSXElement> = <Box<
2495
- JSXElement,
2911
+ __AssignmentTargetTag::ArrayPattern => {
2912
+ let node: Box<ArrayPattern> = <Box<
2913
+ ArrayPattern,
2914
> as Deserialize>::deserialize(
2915
serde::__private::de::ContentDeserializer::<D::Error>::new(tagged.1),
2916
)?;
2499
- Ok(AssignmentTarget::Expression(Expression::JSXElement(node)))
2917
+ Ok(AssignmentTarget::Pattern(Pattern::ArrayPattern(node)))
2918
+ }
2919
+ __AssignmentTargetTag::ObjectPattern => {
2920
+ let node: Box<ObjectPattern> = <Box<
2921
+ ObjectPattern,
2922
+ > as Deserialize>::deserialize(
2923
+ serde::__private::de::ContentDeserializer::<D::Error>::new(tagged.1),
2924
+ )?;
2925
+ Ok(AssignmentTarget::Pattern(Pattern::ObjectPattern(node)))
2926
+ }
2927
+ __AssignmentTargetTag::RestElement => {
2928
+ let node: Box<RestElement> = <Box<
2929
+ RestElement,
2930
+ > as Deserialize>::deserialize(
2931
+ serde::__private::de::ContentDeserializer::<D::Error>::new(tagged.1),
2932
+ )?;
2933
+ Ok(AssignmentTarget::Pattern(Pattern::RestElement(node)))
2934
+ }
2935
+ __AssignmentTargetTag::AssignmentPattern => {
2936
+ let node: Box<AssignmentPattern> = <Box<
2937
+ AssignmentPattern,
2938
+ > as Deserialize>::deserialize(
2939
+ serde::__private::de::ContentDeserializer::<D::Error>::new(tagged.1),
2940
+ )?;
2941
+ Ok(AssignmentTarget::Pattern(Pattern::AssignmentPattern(node)))
2942
}
2943
}
2944
}
@@ -2556,9 +2998,11 @@ enum __JSXExpressionOrEmptyTag {
2998
AssignmentExpression,
2999
BinaryExpression,
3000
CallExpression,
3001
+ ClassExpression,
3002
ConditionalExpression,
3003
FunctionExpression,
3004
Identifier,
3005
+ JSXElement,
3006
Literal,
3007
LogicalExpression,
3008
MemberExpression,
@@ -2569,7 +3013,6 @@ enum __JSXExpressionOrEmptyTag {
3013
UnaryExpression,
3014
UpdateExpression,
3015
YieldExpression,
2572
- JSXElement,
3016
JSXEmptyExpression,
3017
}
3018
impl<'de> serde::Deserialize<'de> for JSXExpressionOrEmpty {
@@ -2632,6 +3075,14 @@ impl<'de> serde::Deserialize<'de> for JSXExpressionOrEmpty {
3075
)?;
3076
Ok(JSXExpressionOrEmpty::Expression(Expression::CallExpression(node)))
3077
}
3078
+ __JSXExpressionOrEmptyTag::ClassExpression => {
3079
+ let node: Box<ClassExpression> = <Box<
3080
+ ClassExpression,
3081
+ > as Deserialize>::deserialize(
3082
+ serde::__private::de::ContentDeserializer::<D::Error>::new(tagged.1),
3083
+ )?;
3084
+ Ok(JSXExpressionOrEmpty::Expression(Expression::ClassExpression(node)))
3085
+ }
3086
__JSXExpressionOrEmptyTag::ConditionalExpression => {
3087
let node: Box<ConditionalExpression> = <Box<
3088
ConditionalExpression,
@@ -2664,6 +3115,14 @@ impl<'de> serde::Deserialize<'de> for JSXExpressionOrEmpty {
3115
)?;
3116
Ok(JSXExpressionOrEmpty::Expression(Expression::Identifier(node)))
3117
}
3118
+ __JSXExpressionOrEmptyTag::JSXElement => {
3119
+ let node: Box<JSXElement> = <Box<
3120
+ JSXElement,
3121
+ > as Deserialize>::deserialize(
3122
+ serde::__private::de::ContentDeserializer::<D::Error>::new(tagged.1),
3123
+ )?;
3124
+ Ok(JSXExpressionOrEmpty::Expression(Expression::JSXElement(node)))
3125
+ }
3126
__JSXExpressionOrEmptyTag::Literal => {
3127
let node: Box<Literal> = <Box<
3128
Literal,
@@ -2748,14 +3207,6 @@ impl<'de> serde::Deserialize<'de> for JSXExpressionOrEmpty {
3207
)?;
3208
Ok(JSXExpressionOrEmpty::Expression(Expression::YieldExpression(node)))
3209
}
2751
- __JSXExpressionOrEmptyTag::JSXElement => {
2752
- let node: Box<JSXElement> = <Box<
2753
- JSXElement,
2754
- > as Deserialize>::deserialize(
2755
- serde::__private::de::ContentDeserializer::<D::Error>::new(tagged.1),
2756
- )?;
2757
- Ok(JSXExpressionOrEmpty::Expression(Expression::JSXElement(node)))
2758
- }
3210
__JSXExpressionOrEmptyTag::JSXEmptyExpression => {
3211
let node: Box<JSXEmptyExpression> = <Box<
3212
JSXEmptyExpression,
@@ -3534,3 +3985,52 @@ impl std::str::FromStr for SourceType {
3985
}
3986
}
3987
}
3988
+#[derive(
3989
+ Serialize,
3990
+ Deserialize,
3991
+ Clone,
3992
+ Copy,
3993
+ Eq,
3994
+ PartialEq,
3995
+ Ord,
3996
+ PartialOrd,
3997
+ Hash,
3998
+ Debug
3999
+)]
4000
+pub enum MethodKind {
4001
+ /// constructor
4002
+ #[serde(rename = "constructor")]
4003
+ Constructor,
4004
+ /// get
4005
+ #[serde(rename = "get")]
4006
+ Get,
4007
+ /// method
4008
+ #[serde(rename = "method")]
4009
+ Method,
4010
+ /// set
4011
+ #[serde(rename = "set")]
4012
+ Set,
4013
+}
4014
+impl std::fmt::Display for MethodKind {
4015
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
4016
+ let name = match self {
4017
+ Self::Constructor => "constructor",
4018
+ Self::Get => "get",
4019
+ Self::Method => "method",
4020
+ Self::Set => "set",
4021
+ };
4022
+ f.write_str(name)
4023
+ }
4024
+}
4025
+impl std::str::FromStr for MethodKind {
4026
+ type Err = ();
4027
+ fn from_str(s: &str) -> Result<Self, Self::Err> {
4028
+ match s {
4029
+ "constructor" => Ok(Self::Constructor),
4030
+ "get" => Ok(Self::Get),
4031
+ "method" => Ok(Self::Method),
4032
+ "set" => Ok(Self::Set),
4033
+ _ => Err(()),
4034
+ }
4035
+ }
4036
+}
compiler/forget/crates/forget_estree/src/generated_extensions.rs
+13
-1
@@ -1,8 +1,20 @@
1
// Manual extensions to generated types
2
-use crate::SourceType;
2
+use crate::{Pattern, SourceRange, SourceType};
3
4
impl Default for SourceType {
5
fn default() -> Self {
6
Self::Script
7
}
8
}
9
+
10
+impl Pattern {
11
+ pub fn range(&self) -> Option<SourceRange> {
12
+ match self {
13
+ Self::ArrayPattern(pattern) => pattern.range,
14
+ Self::AssignmentPattern(pattern) => pattern.range,
15
+ Self::Identifier(pattern) => pattern.range,
16
+ Self::ObjectPattern(pattern) => pattern.range,
17
+ Self::RestElement(pattern) => pattern.range,
18
+ }
19
+ }
20
+}
compiler/forget/crates/forget_estree/src/visit.rs
+111
-9
@@ -1,7 +1,9 @@
1
use crate::{
2
- AssignmentTarget, Expression, ExpressionOrSpread, ExpressionOrSuper, ForInInit, ForInit,
3
- Function, FunctionBody, Identifier, ImportDeclarationSpecifier, ImportOrExportDeclaration,
4
- Literal, ModuleItem, Pattern, Program, Statement, SwitchCase, VariableDeclarator,
2
+ AssignmentTarget, Class, Declaration, ExportAllDeclaration, ExportDefaultDeclaration,
3
+ ExportNamedDeclaration, Expression, ExpressionOrSpread, ExpressionOrSuper, ForInInit, ForInit,
4
+ Function, FunctionBody, Identifier, ImportDeclaration, ImportDeclarationSpecifier,
5
+ ImportOrExportDeclaration, Literal, MethodDefinition, ModuleItem, Pattern, Program, Statement,
6
+ SwitchCase, VariableDeclarator,
7
};
8
9
/// Trait for visiting an estree
@@ -52,13 +54,43 @@ pub trait Visitor<'ast> {
54
fn visit_import_or_export_declaration(&mut self, declaration: &'ast ImportOrExportDeclaration) {
55
match declaration {
56
ImportOrExportDeclaration::ImportDeclaration(declaration) => {
55
- self.visit_lvalue(|visitor| {
56
- for specifier in &declaration.specifiers {
57
- visitor.visit_import_declaration_specifier(specifier, &declaration.source)
58
- }
59
- });
60
- self.visit_import_source(&declaration.source);
57
+ self.visit_import_declaration(declaration);
58
+ }
59
+ ImportOrExportDeclaration::ExportAllDeclaration(declaration) => {
60
+ self.visit_export_all_declaration(declaration);
61
+ }
62
+ ImportOrExportDeclaration::ExportDefaultDeclaration(declaration) => {
63
+ self.visit_export_default_declaration(declaration);
64
+ }
65
+ ImportOrExportDeclaration::ExportNamedDeclaration(declaration) => {
66
+ self.visit_export_named_declaration(declaration);
67
+ }
68
+ }
69
+ }
70
+
71
+ fn visit_import_declaration(&mut self, declaration: &'ast ImportDeclaration) {
72
+ self.visit_lvalue(|visitor| {
73
+ for specifier in &declaration.specifiers {
74
+ visitor.visit_import_declaration_specifier(specifier, &declaration.source)
75
}
76
+ });
77
+ self.visit_import_source(&declaration.source);
78
+ }
79
+
80
+ fn visit_export_all_declaration(&mut self, declaration: &'ast ExportAllDeclaration) {
81
+ self.visit_export_source(&declaration.source);
82
+ }
83
+
84
+ fn visit_export_default_declaration(&mut self, declaration: &'ast ExportDefaultDeclaration) {
85
+ self.visit_declaration(&declaration.declaration);
86
+ }
87
+
88
+ fn visit_export_named_declaration(&mut self, declaration: &'ast ExportNamedDeclaration) {
89
+ if let Some(declaration) = &declaration.declaration {
90
+ self.visit_declaration(declaration)
91
+ }
92
+ if let Some(source) = &declaration.source {
93
+ self.visit_export_source(source);
94
}
95
}
96
@@ -80,6 +112,26 @@ pub trait Visitor<'ast> {
112
}
113
}
114
115
+ fn visit_declaration(&mut self, declaration: &'ast Declaration) {
116
+ self.default_visit_declaration(declaration);
117
+ }
118
+
119
+ fn default_visit_declaration(&mut self, declaration: &'ast Declaration) {
120
+ match declaration {
121
+ Declaration::ClassDeclaration(declaration) => {
122
+ self.visit_class(&declaration.class);
123
+ }
124
+ Declaration::FunctionDeclaration(declaration) => {
125
+ self.visit_function(&declaration.function);
126
+ }
127
+ Declaration::VariableDeclaration(declaration) => {
128
+ for declarator in &declaration.declarations {
129
+ self.visit_variable_declarator(declarator)
130
+ }
131
+ }
132
+ }
133
+ }
134
+
135
fn visit_statement(&mut self, stmt: &'ast Statement) {
136
self.default_visit_statement(stmt);
137
}
@@ -100,6 +152,9 @@ pub trait Visitor<'ast> {
152
Statement::DebuggerStatement(_stmt) => {
153
// todo
154
}
155
+ Statement::ClassDeclaration(stmt) => {
156
+ self.visit_class(&stmt.class);
157
+ }
158
Statement::DoWhileStatement(stmt) => {
159
self.visit_statement(&stmt.body);
160
self.visit_expression(&stmt.test);
@@ -191,6 +246,31 @@ pub trait Visitor<'ast> {
246
}
247
}
248
249
+ fn visit_class(&mut self, class: &'ast Class) {
250
+ if let Some(id) = &class.id {
251
+ self.visit_identifier(id)
252
+ }
253
+ if let Some(super_class) = &class.super_class {
254
+ self.visit_expression(super_class);
255
+ }
256
+ for method in &class.body.body {
257
+ self.visit_method_definition(class, method)
258
+ }
259
+ }
260
+
261
+ fn visit_method_definition(&mut self, class: &'ast Class, method: &'ast MethodDefinition) {
262
+ self.default_visit_method_definition(class, method);
263
+ }
264
+
265
+ fn default_visit_method_definition(
266
+ &mut self,
267
+ _class: &'ast Class,
268
+ method: &'ast MethodDefinition,
269
+ ) {
270
+ self.visit_expression(&method.key);
271
+ self.visit_function(&method.value.function);
272
+ }
273
+
274
fn visit_case(&mut self, case_: &'ast SwitchCase) {
275
if let Some(test) = &case_.test {
276
self.visit_expression(test);
@@ -229,6 +309,23 @@ pub trait Visitor<'ast> {
309
fn visit_pattern(&mut self, pattern: &'ast Pattern) {
310
match pattern {
311
Pattern::Identifier(pattern) => self.visit_identifier(pattern),
312
+ Pattern::ArrayPattern(pattern) => {
313
+ for element in &pattern.elements {
314
+ if let Some(element) = element {
315
+ self.visit_pattern(element);
316
+ }
317
+ }
318
+ }
319
+ Pattern::ObjectPattern(pattern) => {
320
+ for property in &pattern.properties {
321
+ self.visit_pattern(&property.value);
322
+ }
323
+ }
324
+ Pattern::RestElement(pattern) => self.visit_pattern(&pattern.argument),
325
+ Pattern::AssignmentPattern(pattern) => {
326
+ self.visit_expression(&pattern.right);
327
+ self.visit_pattern(&pattern.left);
328
+ }
329
}
330
}
331
@@ -280,6 +377,7 @@ pub trait Visitor<'ast> {
377
}
378
Expression::Literal(expr) => self.visit_literal(expr),
379
Expression::FunctionExpression(expr) => self.visit_function(&expr.function),
380
+ Expression::ArrowFunctionExpression(expr) => self.visit_function(&expr.function),
381
Expression::MemberExpression(expr) => {
382
match &expr.object {
383
ExpressionOrSuper::Super(_object) => {
@@ -305,6 +403,10 @@ pub trait Visitor<'ast> {
403
self.visit_literal(literal);
404
}
405
406
+ fn visit_export_source(&mut self, literal: &'ast Literal) {
407
+ self.visit_literal(literal);
408
+ }
409
+
410
fn visit_literal(&mut self, _literal: &'ast Literal) {
411
// nothing to do unless overridden
412
}
compiler/forget/crates/forget_estree_codegen/src/codegen.rs
+6
-6
@@ -21,7 +21,7 @@ pub fn estree() -> String {
21
}
22
23
#[derive(Serialize, Deserialize, Debug)]
24
-#[serde(deny_unknown_fields)]
24
+// #[serde(deny_unknown_fields)]
25
pub struct Grammar {
26
pub objects: IndexMap<String, Object>,
27
pub nodes: IndexMap<String, Node>,
@@ -72,7 +72,7 @@ impl Grammar {
72
}
73
74
#[derive(Serialize, Deserialize, Debug)]
75
-#[serde(deny_unknown_fields)]
75
+// #[serde(deny_unknown_fields)]
76
pub struct Object {
77
#[serde(default)]
78
pub fields: IndexMap<String, Field>,
@@ -97,7 +97,7 @@ impl Object {
97
}
98
99
#[derive(Serialize, Deserialize, Debug)]
100
-#[serde(deny_unknown_fields)]
100
+// #[serde(deny_unknown_fields)]
101
pub struct Node {
102
#[serde(default)]
103
pub fields: IndexMap<String, Field>,
@@ -128,7 +128,7 @@ impl Node {
128
}
129
130
#[derive(Serialize, Deserialize, Debug)]
131
-#[serde(deny_unknown_fields)]
131
+// #[serde(deny_unknown_fields)]
132
pub struct Field {
133
#[serde(rename = "type")]
134
pub type_: String,
@@ -217,7 +217,7 @@ impl Field {
217
218
#[derive(Serialize, Deserialize, Debug)]
219
#[serde(transparent)]
220
-#[serde(deny_unknown_fields)]
220
+// #[serde(deny_unknown_fields)]
221
pub struct Enum {
222
pub variants: Vec<String>,
223
}
@@ -350,7 +350,7 @@ impl Enum {
350
351
#[derive(Serialize, Deserialize, Debug)]
352
#[serde(transparent)]
353
-#[serde(deny_unknown_fields)]
353
+// #[serde(deny_unknown_fields)]
354
pub struct Operator {
355
pub variants: IndexMap<String, String>,
356
}
compiler/forget/crates/forget_estree_codegen/src/ecmascript.json
+180
-6
@@ -299,6 +299,64 @@
299
}
300
}
301
},
302
+ "Class": {
303
+ "fields": {
304
+ "id": {
305
+ "type": "Option<Identifier>"
306
+ },
307
+ "super_class": {
308
+ "type": "Option<Expression>",
309
+ "rename": "superClass"
310
+ },
311
+ "body": {
312
+ "type": "ClassBody"
313
+ }
314
+ }
315
+ },
316
+ "ClassDeclaration": {
317
+ "fields": {
318
+ "class": {
319
+ "type": "Class",
320
+ "flatten": true
321
+ }
322
+ }
323
+ },
324
+ "ClassExpression": {
325
+ "fields": {
326
+ "class": {
327
+ "type": "Class",
328
+ "flatten": true
329
+ }
330
+ }
331
+ },
332
+ "ClassBody": {
333
+ "fields": {
334
+ "body": {
335
+ "type": "Vec<MethodDefinition>"
336
+ }
337
+ }
338
+ },
339
+ "MethodDefinition": {
340
+ "fields": {
341
+ "key": {
342
+ "type": "Expression"
343
+ },
344
+ "value": {
345
+ "type": "FunctionExpression"
346
+ },
347
+ "kind": {
348
+ "type": "MethodKind"
349
+ },
350
+ "is_computed": {
351
+ "type": "bool",
352
+ "rename": "computed"
353
+ },
354
+ "is_static": {
355
+ "type": "bool",
356
+ "rename": "static"
357
+ }
358
+ }
359
+ },
360
"VariableDeclaration": {
361
"fields": {
362
"kind": {
@@ -337,13 +395,25 @@
395
"Property": {
396
"fields": {
397
"key": {
340
- "type": "PropertyKey"
398
+ "type": "Expression"
399
},
400
"value": {
401
"type": "Expression"
402
},
403
"kind": {
404
"type": "PropertyKind"
405
+ },
406
+ "is_method": {
407
+ "type": "bool",
408
+ "rename": "method"
409
+ },
410
+ "is_shorthand": {
411
+ "type": "bool",
412
+ "rename": "shorthand"
413
+ },
414
+ "is_computed": {
415
+ "type": "bool",
416
+ "rename": "computed"
417
}
418
}
419
},
@@ -539,6 +609,40 @@
609
}
610
}
611
},
612
+ "ExportNamedDeclaration": {
613
+ "fields": {
614
+ "declaration": {
615
+ "type": "Option<Declaration>"
616
+ },
617
+ "specifiers": {
618
+ "type": "Vec<ExportSpecifier>"
619
+ },
620
+ "source": {
621
+ "type": "Option<Literal>"
622
+ }
623
+ }
624
+ },
625
+ "ExportSpecifier": {
626
+ "fields": {
627
+ "exported": {
628
+ "type": "Identifier"
629
+ }
630
+ }
631
+ },
632
+ "ExportDefaultDeclaration": {
633
+ "fields": {
634
+ "declaration": {
635
+ "type": "Declaration"
636
+ }
637
+ }
638
+ },
639
+ "ExportAllDeclaration": {
640
+ "fields": {
641
+ "source": {
642
+ "type": "Literal"
643
+ }
644
+ }
645
+ },
646
"JSXIdentifier": {
647
"fields": {
648
"name": {
@@ -660,12 +764,63 @@
764
}
765
},
766
"JSXOpeningFragment": {},
663
- "JSXClosingFragment": {}
767
+ "JSXClosingFragment": {},
768
+ "ArrayPattern": {
769
+ "fields": {
770
+ "elements": {
771
+ "type": "Vec<Option<Pattern>>"
772
+ }
773
+ }
774
+ },
775
+ "ObjectPattern": {
776
+ "fields": {
777
+ "properties": {
778
+ "type": "Vec<AssignmentProperty>"
779
+ }
780
+ }
781
+ },
782
+ "AssignmentProperty": {
783
+ "tag": "Property",
784
+ "fields": {
785
+ "key": {
786
+ "type": "PropertyKey"
787
+ },
788
+ "value": {
789
+ "type": "Pattern"
790
+ },
791
+ "kind": {
792
+ "type": "PropertyKind",
793
+ "TODO": "fixed value `init`"
794
+ },
795
+ "method": {
796
+ "type": "bool",
797
+ "TODO": "fixed value `false`"
798
+ }
799
+ }
800
+ },
801
+ "RestElement": {
802
+ "fields": {
803
+ "argument": {
804
+ "type": "Pattern"
805
+ }
806
+ }
807
+ },
808
+ "AssignmentPattern": {
809
+ "fields": {
810
+ "left": {
811
+ "type": "Pattern"
812
+ },
813
+ "right": {
814
+ "type": "Expression"
815
+ }
816
+ }
817
+ }
818
},
819
"enums": {
820
"Statement": [
821
"BlockStatement",
822
"BreakStatement",
823
+ "ClassDeclaration",
824
"ContinueStatement",
825
"DebuggerStatement",
826
"DoWhileStatement",
@@ -691,9 +846,11 @@
846
"AssignmentExpression",
847
"BinaryExpression",
848
"CallExpression",
849
+ "ClassExpression",
850
"ConditionalExpression",
851
"FunctionExpression",
852
"Identifier",
853
+ "JSXElement",
854
"Literal",
855
"LogicalExpression",
856
"MemberExpression",
@@ -703,8 +860,12 @@
860
"ThisExpression",
861
"UnaryExpression",
862
"UpdateExpression",
706
- "YieldExpression",
707
- "JSXElement"
863
+ "YieldExpression"
864
+ ],
865
+ "Declaration": [
866
+ "ClassDeclaration",
867
+ "FunctionDeclaration",
868
+ "VariableDeclaration"
869
],
870
"ImportDeclarationSpecifier": [
871
"ImportSpecifier",
@@ -716,7 +877,10 @@
877
"Statement"
878
],
879
"ImportOrExportDeclaration": [
719
- "ImportDeclaration"
880
+ "ImportDeclaration",
881
+ "ExportNamedDeclaration",
882
+ "ExportDefaultDeclaration",
883
+ "ExportAllDeclaration"
884
],
885
"ExpressionOrSuper": [
886
"Expression",
@@ -731,7 +895,11 @@
895
"Expression"
896
],
897
"Pattern": [
734
- "Identifier"
898
+ "Identifier",
899
+ "ArrayPattern",
900
+ "ObjectPattern",
901
+ "RestElement",
902
+ "AssignmentPattern"
903
],
904
"ForInit": [
905
"Expression",
@@ -853,6 +1021,12 @@
1021
"SourceType": {
1022
"Script": "script",
1023
"Module": "module"
1024
+ },
1025
+ "MethodKind": {
1026
+ "Constructor": "constructor",
1027
+ "Method": "method",
1028
+ "Get": "get",
1029
+ "Set": "set"
1030
}
1031
}
1032
}
\ No newline at end of file