fix(es/ast): Fix definition of Expr::TsInstantiation (#3657)

This commit is contained in:
Donny/강동윤 2022-02-21 22:03:11 +09:00 committed by GitHub
parent e08a2c2c17
commit 68a155165b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 97 additions and 64 deletions

View File

@ -15,7 +15,7 @@ use crate::{
prop::Prop,
stmt::BlockStmt,
typescript::{
TsAsExpr, TsConstAssertion, TsExprWithTypeArgs, TsNonNullExpr, TsTypeAnn, TsTypeAssertion,
TsAsExpr, TsConstAssertion, TsInstantiation, TsNonNullExpr, TsTypeAnn, TsTypeAssertion,
TsTypeParamDecl, TsTypeParamInstantiation,
},
ComputedPropName, Invalid,
@ -147,8 +147,8 @@ pub enum Expr {
#[tag("TsAsExpression")]
TsAs(TsAsExpr),
#[tag("TsExpressionWithTypeArguments")]
TsInstantiation(TsExprWithTypeArgs),
#[tag("TsInstantiation")]
TsInstantiation(TsInstantiation),
#[tag("PrivateName")]
PrivateName(PrivateName),

View File

@ -60,9 +60,9 @@ pub use self::{
TsEntityName, TsEnumDecl, TsEnumMember, TsEnumMemberId, TsExportAssignment,
TsExprWithTypeArgs, TsExternalModuleRef, TsFnOrConstructorType, TsFnParam, TsFnType,
TsGetterSignature, TsImportEqualsDecl, TsImportType, TsIndexSignature, TsIndexedAccessType,
TsInferType, TsInterfaceBody, TsInterfaceDecl, TsIntersectionType, TsKeywordType,
TsKeywordTypeKind, TsLit, TsLitType, TsMappedType, TsMethodSignature, TsModuleBlock,
TsModuleDecl, TsModuleName, TsModuleRef, TsNamespaceBody, TsNamespaceDecl,
TsInferType, TsInstantiation, TsInterfaceBody, TsInterfaceDecl, TsIntersectionType,
TsKeywordType, TsKeywordTypeKind, TsLit, TsLitType, TsMappedType, TsMethodSignature,
TsModuleBlock, TsModuleDecl, TsModuleName, TsModuleRef, TsNamespaceBody, TsNamespaceDecl,
TsNamespaceExportDecl, TsNonNullExpr, TsOptionalType, TsParamProp, TsParamPropParam,
TsParenthesizedType, TsPropertySignature, TsQualifiedName, TsRestType, TsSetterSignature,
TsThisType, TsThisTypeOrIdent, TsTplLitType, TsTupleElement, TsTupleType, TsType,

View File

@ -1030,3 +1030,14 @@ pub struct TsConstAssertion {
#[serde(rename = "expression")]
pub expr: Box<Expr>,
}
#[ast_node("TsInstantiation")]
#[derive(Eq, Hash, EqIgnoreSpan)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct TsInstantiation {
pub span: Span,
#[serde(rename = "expression")]
pub expr: Box<Expr>,
#[serde(rename = "typeArguments")]
pub type_args: TsTypeParamInstantiation,
}

View File

@ -1050,6 +1050,15 @@ where
self.emit_list(n.span, Some(&n.types), ListFormat::UnionTypeConstituents)?;
}
#[emitter]
fn emit_ts_instantiation(&mut self, n: &TsInstantiation) -> Result {
self.emit_leading_comments_of_span(n.span(), false)?;
emit!(n.expr);
emit!(n.type_args);
}
}
#[cfg(test)]

View File

@ -292,7 +292,7 @@ impl StartsWithAlphaNum for Expr {
Expr::TsNonNull(TsNonNullExpr { ref expr, .. })
| Expr::TsAs(TsAsExpr { ref expr, .. })
| Expr::TsConstAssertion(TsConstAssertion { ref expr, .. })
| Expr::TsInstantiation(TsExprWithTypeArgs { ref expr, .. }) => {
| Expr::TsInstantiation(TsInstantiation { ref expr, .. }) => {
expr.starts_with_alpha_num()
}

View File

@ -179,9 +179,9 @@ impl<'a, I: Tokens> Parser<I> {
fn parse_super_class(&mut self) -> PResult<(Box<Expr>, Option<TsTypeParamInstantiation>)> {
let super_class = self.parse_lhs_expr()?;
match *super_class {
Expr::TsInstantiation(TsExprWithTypeArgs {
Expr::TsInstantiation(TsInstantiation {
expr, type_args, ..
}) => Ok((expr, type_args)),
}) => Ok((expr, Some(type_args))),
_ => {
// We still need to parse TS type arguments,
// because in some cases "super class" returned by `parse_lhs_expr`

View File

@ -576,8 +576,8 @@ impl<'a, I: Tokens> Parser<I> {
} else {
None
};
let obj = if type_args.is_some() {
Box::new(Expr::TsInstantiation(TsExprWithTypeArgs {
let obj = if let Some(type_args) = type_args {
Box::new(Expr::TsInstantiation(TsInstantiation {
expr: obj,
type_args,
span: span!(self, start),
@ -1189,8 +1189,8 @@ impl<'a, I: Tokens> Parser<I> {
Expr::Member(expr)
};
if type_args.is_some() {
Expr::TsInstantiation(TsExprWithTypeArgs {
if let Some(type_args) = type_args {
Expr::TsInstantiation(TsInstantiation {
expr: Box::new(expr),
type_args,
span: span!(self, start),
@ -1328,8 +1328,8 @@ impl<'a, I: Tokens> Parser<I> {
} else {
Expr::Member(expr)
};
if type_args.is_some() {
Expr::TsInstantiation(TsExprWithTypeArgs {
if let Some(type_args) = type_args {
Expr::TsInstantiation(TsInstantiation {
expr: Box::new(expr),
type_args,
span: span!(self, start),
@ -1345,8 +1345,8 @@ impl<'a, I: Tokens> Parser<I> {
match obj {
Callee::Expr(expr) => {
let expr = if type_args.is_some() {
Box::new(Expr::TsInstantiation(TsExprWithTypeArgs {
let expr = if let Some(type_args) = type_args {
Box::new(Expr::TsInstantiation(TsInstantiation {
expr,
type_args,
span: span!(self, start),

View File

@ -943,7 +943,11 @@ impl<I: Tokens> Parser<I> {
}
match *expr {
Expr::TsInstantiation(ts_expr_with_type_args) => Ok(ts_expr_with_type_args),
Expr::TsInstantiation(v) => Ok(TsExprWithTypeArgs {
span: v.span,
expr: v.expr,
type_args: Some(v.type_args),
}),
_ => {
let type_args = if is!(self, '<') {
Some(self.parse_ts_type_args()?)

View File

@ -298,7 +298,7 @@ pub(super) trait ExprExt {
Expr::TsNonNull(TsNonNullExpr { ref expr, .. })
| Expr::TsTypeAssertion(TsTypeAssertion { ref expr, .. })
| Expr::TsAs(TsAsExpr { ref expr, .. })
| Expr::TsInstantiation(TsExprWithTypeArgs { ref expr, .. }) => {
| Expr::TsInstantiation(TsInstantiation { ref expr, .. }) => {
expr.is_valid_simple_assignment_target(strict)
}

View File

@ -204,7 +204,7 @@
"typeAnnotation": null
},
"init": {
"type": "TsExpressionWithTypeArguments",
"type": "TsInstantiation",
"span": {
"start": 207,
"end": 214,

View File

@ -42,7 +42,7 @@
"ctxt": 0
},
"callee": {
"type": "TsExpressionWithTypeArguments",
"type": "TsInstantiation",
"span": {
"start": 13,
"end": 23,
@ -114,7 +114,7 @@
"typeAnnotation": null
},
"init": {
"type": "TsExpressionWithTypeArguments",
"type": "TsInstantiation",
"span": {
"start": 37,
"end": 46,
@ -183,7 +183,7 @@
"typeAnnotation": null
},
"init": {
"type": "TsExpressionWithTypeArguments",
"type": "TsInstantiation",
"span": {
"start": 59,
"end": 70,
@ -277,7 +277,7 @@
"ctxt": 0
},
"object": {
"type": "TsExpressionWithTypeArguments",
"type": "TsInstantiation",
"span": {
"start": 83,
"end": 92,
@ -357,7 +357,7 @@
"typeAnnotation": null
},
"init": {
"type": "TsExpressionWithTypeArguments",
"type": "TsInstantiation",
"span": {
"start": 107,
"end": 126,
@ -371,7 +371,7 @@
"ctxt": 0
},
"object": {
"type": "TsExpressionWithTypeArguments",
"type": "TsInstantiation",
"span": {
"start": 107,
"end": 116,
@ -471,7 +471,7 @@
"typeAnnotation": null
},
"init": {
"type": "TsExpressionWithTypeArguments",
"type": "TsInstantiation",
"span": {
"start": 139,
"end": 153,
@ -570,7 +570,7 @@
"typeAnnotation": null
},
"init": {
"type": "TsExpressionWithTypeArguments",
"type": "TsInstantiation",
"span": {
"start": 166,
"end": 185,
@ -584,7 +584,7 @@
"ctxt": 0
},
"expression": {
"type": "TsExpressionWithTypeArguments",
"type": "TsInstantiation",
"span": {
"start": 167,
"end": 176,

View File

@ -22,7 +22,7 @@
},
"expressions": [
{
"type": "TsExpressionWithTypeArguments",
"type": "TsInstantiation",
"span": {
"start": 0,
"end": 4,
@ -69,7 +69,7 @@
}
},
{
"type": "TsExpressionWithTypeArguments",
"type": "TsInstantiation",
"span": {
"start": 6,
"end": 10,
@ -136,7 +136,7 @@
{
"spread": null,
"expression": {
"type": "TsExpressionWithTypeArguments",
"type": "TsInstantiation",
"span": {
"start": 13,
"end": 17,
@ -201,7 +201,7 @@
"ctxt": 0
},
"test": {
"type": "TsExpressionWithTypeArguments",
"type": "TsInstantiation",
"span": {
"start": 20,
"end": 24,
@ -248,7 +248,7 @@
}
},
"consequent": {
"type": "TsExpressionWithTypeArguments",
"type": "TsInstantiation",
"span": {
"start": 27,
"end": 31,
@ -295,7 +295,7 @@
}
},
"alternate": {
"type": "TsExpressionWithTypeArguments",
"type": "TsInstantiation",
"span": {
"start": 34,
"end": 38,
@ -359,7 +359,7 @@
},
"operator": "^",
"left": {
"type": "TsExpressionWithTypeArguments",
"type": "TsInstantiation",
"span": {
"start": 40,
"end": 44,
@ -406,7 +406,7 @@
}
},
"right": {
"type": "TsExpressionWithTypeArguments",
"type": "TsInstantiation",
"span": {
"start": 47,
"end": 51,
@ -470,7 +470,7 @@
},
"operator": "&",
"left": {
"type": "TsExpressionWithTypeArguments",
"type": "TsInstantiation",
"span": {
"start": 53,
"end": 57,
@ -517,7 +517,7 @@
}
},
"right": {
"type": "TsExpressionWithTypeArguments",
"type": "TsInstantiation",
"span": {
"start": 60,
"end": 64,
@ -581,7 +581,7 @@
},
"operator": "|",
"left": {
"type": "TsExpressionWithTypeArguments",
"type": "TsInstantiation",
"span": {
"start": 66,
"end": 70,
@ -628,7 +628,7 @@
}
},
"right": {
"type": "TsExpressionWithTypeArguments",
"type": "TsInstantiation",
"span": {
"start": 73,
"end": 77,
@ -692,7 +692,7 @@
},
"operator": "&&",
"left": {
"type": "TsExpressionWithTypeArguments",
"type": "TsInstantiation",
"span": {
"start": 79,
"end": 83,
@ -739,7 +739,7 @@
}
},
"right": {
"type": "TsExpressionWithTypeArguments",
"type": "TsInstantiation",
"span": {
"start": 87,
"end": 91,
@ -803,7 +803,7 @@
},
"operator": "||",
"left": {
"type": "TsExpressionWithTypeArguments",
"type": "TsInstantiation",
"span": {
"start": 93,
"end": 97,
@ -850,7 +850,7 @@
}
},
"right": {
"type": "TsExpressionWithTypeArguments",
"type": "TsInstantiation",
"span": {
"start": 101,
"end": 105,
@ -914,7 +914,7 @@
"ctxt": 0
},
"expression": {
"type": "TsExpressionWithTypeArguments",
"type": "TsInstantiation",
"span": {
"start": 109,
"end": 113,
@ -979,7 +979,7 @@
},
"operator": "??",
"left": {
"type": "TsExpressionWithTypeArguments",
"type": "TsInstantiation",
"span": {
"start": 116,
"end": 120,
@ -1026,7 +1026,7 @@
}
},
"right": {
"type": "TsExpressionWithTypeArguments",
"type": "TsInstantiation",
"span": {
"start": 124,
"end": 128,
@ -1090,7 +1090,7 @@
},
"operator": "==",
"left": {
"type": "TsExpressionWithTypeArguments",
"type": "TsInstantiation",
"span": {
"start": 130,
"end": 134,
@ -1137,7 +1137,7 @@
}
},
"right": {
"type": "TsExpressionWithTypeArguments",
"type": "TsInstantiation",
"span": {
"start": 138,
"end": 142,
@ -1201,7 +1201,7 @@
},
"operator": "===",
"left": {
"type": "TsExpressionWithTypeArguments",
"type": "TsInstantiation",
"span": {
"start": 144,
"end": 148,
@ -1248,7 +1248,7 @@
}
},
"right": {
"type": "TsExpressionWithTypeArguments",
"type": "TsInstantiation",
"span": {
"start": 153,
"end": 157,
@ -1312,7 +1312,7 @@
},
"operator": "!=",
"left": {
"type": "TsExpressionWithTypeArguments",
"type": "TsInstantiation",
"span": {
"start": 159,
"end": 163,
@ -1359,7 +1359,7 @@
}
},
"right": {
"type": "TsExpressionWithTypeArguments",
"type": "TsInstantiation",
"span": {
"start": 167,
"end": 171,
@ -1423,7 +1423,7 @@
},
"operator": "!==",
"left": {
"type": "TsExpressionWithTypeArguments",
"type": "TsInstantiation",
"span": {
"start": 173,
"end": 177,
@ -1470,7 +1470,7 @@
}
},
"right": {
"type": "TsExpressionWithTypeArguments",
"type": "TsInstantiation",
"span": {
"start": 182,
"end": 186,

View File

@ -54,7 +54,7 @@
"ctxt": 0
},
"callee": {
"type": "TsExpressionWithTypeArguments",
"type": "TsInstantiation",
"span": {
"start": 11,
"end": 20,

View File

@ -1194,7 +1194,7 @@ fn can_be_null(e: &Expr) -> bool {
Expr::TsAs(TsAsExpr { ref expr, .. })
| Expr::TsTypeAssertion(TsTypeAssertion { ref expr, .. })
| Expr::TsConstAssertion(TsConstAssertion { ref expr, .. })
| Expr::TsInstantiation(TsExprWithTypeArgs { ref expr, .. }) => can_be_null(expr),
| Expr::TsInstantiation(TsInstantiation { ref expr, .. }) => can_be_null(expr),
Expr::Invalid(..) => unreachable!(),
}

View File

@ -732,7 +732,8 @@ where
Expr::TsAs(TsAsExpr { expr, .. })
| Expr::TsNonNull(TsNonNullExpr { expr, .. })
| Expr::TsTypeAssertion(TsTypeAssertion { expr, .. })
| Expr::TsConstAssertion(TsConstAssertion { expr, .. }) => {
| Expr::TsConstAssertion(TsConstAssertion { expr, .. })
| Expr::TsInstantiation(TsInstantiation { expr, .. }) => {
expr.visit_mut_with(self);
let expr = *expr.take();
*n = expr;

View File

@ -1233,7 +1233,7 @@ pub trait ExprExt {
Expr::TsAs(TsAsExpr { ref expr, .. })
| Expr::TsNonNull(TsNonNullExpr { ref expr, .. })
| Expr::TsTypeAssertion(TsTypeAssertion { ref expr, .. })
| Expr::TsInstantiation(TsExprWithTypeArgs { ref expr, .. }) => {
| Expr::TsInstantiation(TsInstantiation { ref expr, .. }) => {
expr.may_have_side_effects()
}
@ -2087,9 +2087,7 @@ pub fn extract_side_effects_to(to: &mut Vec<Box<Expr>>, expr: Box<Expr>) {
| Expr::TsNonNull(TsNonNullExpr { expr, .. })
| Expr::TsAs(TsAsExpr { expr, .. })
| Expr::TsConstAssertion(TsConstAssertion { expr, .. })
| Expr::TsInstantiation(TsExprWithTypeArgs { expr, .. }) => {
extract_side_effects_to(to, expr)
}
| Expr::TsInstantiation(TsInstantiation { expr, .. }) => extract_side_effects_to(to, expr),
Expr::OptChain(OptChainExpr { base: child, .. }) => {
extract_side_effects_to(to, Box::new(child.into()))
}

View File

@ -663,7 +663,7 @@ define!({
TsConstAssertion(TsConstAssertion),
TsNonNull(TsNonNullExpr),
TsAs(TsAsExpr),
TsInstantiation(TsExprWithTypeArgs),
TsInstantiation(TsInstantiation),
PrivateName(PrivateName),
OptChain(OptChainExpr),
Invalid(Invalid),
@ -1806,6 +1806,12 @@ define!({
pub span: Span,
pub expr: Box<Expr>,
}
pub struct TsInstantiation {
pub span: Span,
pub expr: Box<Expr>,
pub type_args: TsTypeParamInstantiation,
}
});
#[macro_export]

View File

@ -35,6 +35,10 @@ impl VisitMut for TsRemover {
*e = *expr.expr.take();
}
Expr::TsInstantiation(expr) => {
*e = *expr.expr.take();
}
_ => {}
}
}