Skip to content

Commit

Permalink
fix(es/parser): Fix parsing of TS satisfies expressions (#6354)
Browse files Browse the repository at this point in the history
  • Loading branch information
nissy-dev committed Nov 7, 2022
1 parent 981dc88 commit f6faeb3
Show file tree
Hide file tree
Showing 29 changed files with 43 additions and 43 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/CI.yml
Expand Up @@ -717,6 +717,7 @@ jobs:
run: |
echo '[patch.crates-io]' >> bindings/Cargo.toml
./scripts/cargo/patch-section.sh >> bindings/Cargo.toml
cd bindings && cargo update
- name: Cache
uses: actions/cache@v3
Expand Down Expand Up @@ -777,6 +778,7 @@ jobs:
run: |
echo '[patch.crates-io]' >> bindings/Cargo.toml
./scripts/cargo/patch-section.sh >> bindings/Cargo.toml
cd bindings && cargo update
- name: Prepare
run: |
Expand Down
8 changes: 4 additions & 4 deletions crates/swc_ecma_ast/src/expr.rs
Expand Up @@ -22,7 +22,7 @@ use crate::{
prop::Prop,
stmt::BlockStmt,
typescript::{
TsAsExpr, TsConstAssertion, TsInstantiation, TsNonNullExpr, TsSatisfactionExpr, TsTypeAnn,
TsAsExpr, TsConstAssertion, TsInstantiation, TsNonNullExpr, TsSatisfiesExpr, TsTypeAnn,
TsTypeAssertion, TsTypeParamDecl, TsTypeParamInstantiation,
},
ComputedPropName, Id, Invalid,
Expand Down Expand Up @@ -157,8 +157,8 @@ pub enum Expr {
#[tag("TsInstantiation")]
TsInstantiation(TsInstantiation),

#[tag("TsSatisfactionExpr")]
TsSatisfaction(TsSatisfactionExpr),
#[tag("TsSatisfiesExpression")]
TsSatisfies(TsSatisfiesExpr),

#[tag("PrivateName")]
PrivateName(PrivateName),
Expand Down Expand Up @@ -271,7 +271,7 @@ impl Clone for Expr {
PrivateName(e) => PrivateName(e.clone()),
OptChain(e) => OptChain(e.clone()),
Invalid(e) => Invalid(e.clone()),
TsSatisfaction(e) => TsSatisfaction(e.clone()),
TsSatisfies(e) => TsSatisfies(e.clone()),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_ast/src/lib.rs
Expand Up @@ -68,7 +68,7 @@ pub use self::{
TsKeywordType, TsKeywordTypeKind, TsLit, TsLitType, TsMappedType, TsMethodSignature,
TsModuleBlock, TsModuleDecl, TsModuleName, TsModuleRef, TsNamespaceBody, TsNamespaceDecl,
TsNamespaceExportDecl, TsNonNullExpr, TsOptionalType, TsParamProp, TsParamPropParam,
TsParenthesizedType, TsPropertySignature, TsQualifiedName, TsRestType, TsSatisfactionExpr,
TsParenthesizedType, TsPropertySignature, TsQualifiedName, TsRestType, TsSatisfiesExpr,
TsSetterSignature, TsThisType, TsThisTypeOrIdent, TsTplLitType, TsTupleElement,
TsTupleType, TsType, TsTypeAliasDecl, TsTypeAnn, TsTypeAssertion, TsTypeElement, TsTypeLit,
TsTypeOperator, TsTypeOperatorOp, TsTypeParam, TsTypeParamDecl, TsTypeParamInstantiation,
Expand Down
4 changes: 2 additions & 2 deletions crates/swc_ecma_ast/src/typescript.rs
Expand Up @@ -1055,10 +1055,10 @@ pub struct TsNonNullExpr {
pub expr: Box<Expr>,
}

#[ast_node("TsSatisfactionExpr")]
#[ast_node("TsSatisfiesExpression")]
#[derive(Eq, Hash, EqIgnoreSpan)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct TsSatisfactionExpr {
pub struct TsSatisfiesExpr {
pub span: Span,
#[serde(rename = "expression")]
pub expr: Box<Expr>,
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_codegen/src/lib.rs
Expand Up @@ -794,7 +794,7 @@ where
Expr::TsInstantiation(ref n) => emit!(n),
Expr::OptChain(ref n) => emit!(n),
Expr::Invalid(ref n) => emit!(n),
Expr::TsSatisfaction(n) => {
Expr::TsSatisfies(n) => {
emit!(n)
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_codegen/src/typescript.rs
Expand Up @@ -40,7 +40,7 @@ where
}

#[emitter]
fn emit_ts_satisfaction_expr(&mut self, n: &TsSatisfactionExpr) -> Result {
fn emit_ts_satisfies_expr(&mut self, n: &TsSatisfiesExpr) -> Result {
self.emit_leading_comments_of_span(n.span(), false)?;

emit!(n.expr);
Expand Down
4 changes: 1 addition & 3 deletions crates/swc_ecma_codegen/src/util.rs
Expand Up @@ -142,9 +142,7 @@ impl StartsWithAlphaNum for Expr {
| Expr::TsAs(TsAsExpr { ref expr, .. })
| Expr::TsConstAssertion(TsConstAssertion { ref expr, .. })
| Expr::TsInstantiation(TsInstantiation { ref expr, .. })
| Expr::TsSatisfaction(TsSatisfactionExpr { ref expr, .. }) => {
expr.starts_with_alpha_num()
}
| Expr::TsSatisfies(TsSatisfiesExpr { ref expr, .. }) => expr.starts_with_alpha_num(),

Expr::OptChain(OptChainExpr {
base: OptChainBase::Member(MemberExpr { obj: expr, .. }),
Expand Down
Expand Up @@ -1451,7 +1451,7 @@ where
| Expr::TsNonNull(_)
| Expr::TsAs(_)
| Expr::TsInstantiation(_)
| Expr::TsSatisfaction(_) => false,
| Expr::TsSatisfies(_) => false,
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_minifier/src/util/size.rs
Expand Up @@ -198,7 +198,7 @@ impl SizeWithCtxt for Expr {
Expr::TsNonNull(_) => TODO,
Expr::TsAs(_) => TODO,
Expr::TsInstantiation(_) => TODO,
Expr::TsSatisfaction(_) => TODO,
Expr::TsSatisfies(_) => TODO,
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_parser/src/parser/expr/ops.rs
Expand Up @@ -122,7 +122,7 @@ impl<I: Tokens> Parser<I> {
let expr = left;
let node = {
let type_ann = self.next_then_parse_ts_type()?;
Box::new(Expr::TsAs(TsAsExpr {
Box::new(Expr::TsSatisfies(TsSatisfiesExpr {
span: span!(self, start),
expr,
type_ann,
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_parser/src/parser/util.rs
Expand Up @@ -315,7 +315,7 @@ pub(super) trait ExprExt {
| Expr::TsTypeAssertion(TsTypeAssertion { ref expr, .. })
| Expr::TsAs(TsAsExpr { ref expr, .. })
| Expr::TsInstantiation(TsInstantiation { ref expr, .. })
| Expr::TsSatisfaction(TsSatisfactionExpr { ref expr, .. }) => {
| Expr::TsSatisfies(TsSatisfiesExpr { ref expr, .. }) => {
expr.is_valid_simple_assignment_target(strict)
}

Expand Down
16 changes: 8 additions & 8 deletions crates/swc_ecma_parser/tests/tsc/typeSatisfaction.json
Expand Up @@ -290,7 +290,7 @@
"typeAnnotation": null
},
"init": {
"type": "TsAsExpression",
"type": "TsSatisfiesExpression",
"span": {
"start": 110,
"end": 131,
Expand Down Expand Up @@ -382,7 +382,7 @@
"typeAnnotation": null
},
"init": {
"type": "TsAsExpression",
"type": "TsSatisfiesExpression",
"span": {
"start": 150,
"end": 177,
Expand Down Expand Up @@ -497,7 +497,7 @@
"typeAnnotation": null
},
"init": {
"type": "TsAsExpression",
"type": "TsSatisfiesExpression",
"span": {
"start": 199,
"end": 215,
Expand Down Expand Up @@ -591,7 +591,7 @@
}
},
"init": {
"type": "TsAsExpression",
"type": "TsSatisfiesExpression",
"span": {
"start": 242,
"end": 265,
Expand Down Expand Up @@ -683,7 +683,7 @@
"typeAnnotation": null
},
"init": {
"type": "TsAsExpression",
"type": "TsSatisfiesExpression",
"span": {
"start": 284,
"end": 318,
Expand Down Expand Up @@ -827,7 +827,7 @@
"typeAnnotation": null
},
"init": {
"type": "TsAsExpression",
"type": "TsSatisfiesExpression",
"span": {
"start": 338,
"end": 371,
Expand Down Expand Up @@ -1021,7 +1021,7 @@
"typeAnnotation": null
},
"init": {
"type": "TsAsExpression",
"type": "TsSatisfiesExpression",
"span": {
"start": 413,
"end": 438,
Expand Down Expand Up @@ -1113,7 +1113,7 @@
"typeAnnotation": null
},
"init": {
"type": "TsAsExpression",
"type": "TsSatisfiesExpression",
"span": {
"start": 449,
"end": 485,
Expand Down
Expand Up @@ -167,7 +167,7 @@
"typeAnnotation": null
},
"init": {
"type": "TsAsExpression",
"type": "TsSatisfiesExpression",
"span": {
"start": 71,
"end": 153,
Expand Down
Expand Up @@ -133,7 +133,7 @@
"typeAnnotation": null
},
"init": {
"type": "TsAsExpression",
"type": "TsSatisfiesExpression",
"span": {
"start": 68,
"end": 198,
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_parser/tests/tsc/typeSatisfaction_js.json
Expand Up @@ -35,7 +35,7 @@
"typeAnnotation": null
},
"init": {
"type": "TsAsExpression",
"type": "TsSatisfiesExpression",
"span": {
"start": 71,
"end": 92,
Expand Down
Expand Up @@ -147,7 +147,7 @@
"typeAnnotation": null
},
"init": {
"type": "TsAsExpression",
"type": "TsSatisfiesExpression",
"span": {
"start": 103,
"end": 139,
Expand Down
Expand Up @@ -137,7 +137,7 @@
"typeAnnotation": null
},
"init": {
"type": "TsAsExpression",
"type": "TsSatisfiesExpression",
"span": {
"start": 47,
"end": 163,
Expand Down
Expand Up @@ -137,7 +137,7 @@
"typeAnnotation": null
},
"init": {
"type": "TsAsExpression",
"type": "TsSatisfiesExpression",
"span": {
"start": 47,
"end": 154,
Expand Down
Expand Up @@ -681,7 +681,7 @@
"typeAnnotation": null
},
"init": {
"type": "TsAsExpression",
"type": "TsSatisfiesExpression",
"span": {
"start": 476,
"end": 523,
Expand Down
Expand Up @@ -681,7 +681,7 @@
"typeAnnotation": null
},
"init": {
"type": "TsAsExpression",
"type": "TsSatisfiesExpression",
"span": {
"start": 457,
"end": 504,
Expand Down
Expand Up @@ -203,7 +203,7 @@
"typeAnnotation": null
},
"init": {
"type": "TsAsExpression",
"type": "TsSatisfiesExpression",
"span": {
"start": 150,
"end": 323,
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_quote_macros/src/ast/expr.rs
Expand Up @@ -38,7 +38,7 @@ impl_enum!(
TsNonNull,
TsAs,
TsInstantiation,
TsSatisfaction,
TsSatisfies,
PrivateName,
OptChain,
Invalid
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_quote_macros/src/ast/typescript.rs
Expand Up @@ -19,4 +19,4 @@ fail_todo!(TsTypeParamDecl);
fail_todo!(TsExprWithTypeArgs);
fail_todo!(TsIndexSignature);
fail_todo!(TsParamProp);
fail_todo!(TsSatisfactionExpr);
fail_todo!(TsSatisfiesExpr);
Expand Up @@ -1210,7 +1210,7 @@ fn can_be_null(e: &Expr) -> bool {
| Expr::TsTypeAssertion(TsTypeAssertion { ref expr, .. })
| Expr::TsConstAssertion(TsConstAssertion { ref expr, .. })
| Expr::TsInstantiation(TsInstantiation { ref expr, .. })
| Expr::TsSatisfaction(TsSatisfactionExpr { ref expr, .. }) => can_be_null(expr),
| Expr::TsSatisfies(TsSatisfiesExpr { ref expr, .. }) => can_be_null(expr),

Expr::Invalid(..) => unreachable!(),
}
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_transforms_typescript/src/strip.rs
Expand Up @@ -437,7 +437,7 @@ where
| Expr::TsTypeAssertion(TsTypeAssertion { expr, .. })
| Expr::TsConstAssertion(TsConstAssertion { expr, .. })
| Expr::TsInstantiation(TsInstantiation { expr, .. })
| Expr::TsSatisfaction(TsSatisfactionExpr { expr, .. }) => {
| Expr::TsSatisfies(TsSatisfiesExpr { expr, .. }) => {
expr.visit_mut_with(self);
let expr = *expr.take();
*n = expr;
Expand Down
4 changes: 2 additions & 2 deletions crates/swc_ecma_utils/src/lib.rs
Expand Up @@ -1489,7 +1489,7 @@ pub trait ExprExt {
| Expr::TsNonNull(TsNonNullExpr { ref expr, .. })
| Expr::TsTypeAssertion(TsTypeAssertion { ref expr, .. })
| Expr::TsInstantiation(TsInstantiation { ref expr, .. })
| Expr::TsSatisfaction(TsSatisfactionExpr { ref expr, .. }) => {
| Expr::TsSatisfies(TsSatisfiesExpr { ref expr, .. }) => {
expr.may_have_side_effects(ctx)
}

Expand Down Expand Up @@ -2454,7 +2454,7 @@ impl ExprCtx {
| Expr::TsAs(TsAsExpr { expr, .. })
| Expr::TsConstAssertion(TsConstAssertion { expr, .. })
| Expr::TsInstantiation(TsInstantiation { expr, .. })
| Expr::TsSatisfaction(TsSatisfactionExpr { expr, .. }) => {
| Expr::TsSatisfies(TsSatisfiesExpr { expr, .. }) => {
self.extract_side_effects_to(to, *expr)
}
Expr::OptChain(OptChainExpr { base: child, .. }) => {
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_visit/src/lib.rs
Expand Up @@ -660,7 +660,7 @@ define!({
TsConstAssertion(TsConstAssertion),
TsNonNull(TsNonNullExpr),
TsAs(TsAsExpr),
TsSatisfaction(TsSatisfactionExpr),
TsSatisfies(TsSatisfiesExpr),
TsInstantiation(TsInstantiation),
PrivateName(PrivateName),
OptChain(OptChainExpr),
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_estree_compat/src/babelify/expr.rs
Expand Up @@ -157,7 +157,7 @@ impl Babelify for Expr {
"illegal conversion: Cannot convert {:?} to ExprOutput - babel has no equivalent",
&self
),
Expr::TsSatisfaction(_) => panic!(
Expr::TsSatisfies(_) => panic!(
"illegal conversion: Cannot convert {:?} to ExprOutput - babel has no equivalent",
&self
),
Expand Down
4 changes: 2 additions & 2 deletions node-swc/__tests__/transform/issue_4730_test.mjs
Expand Up @@ -37,9 +37,9 @@ Object.defineProperty(exports, \\"__esModule\\", {
value: true
});
const _interopRequireWildcard = require(\\"@swc/helpers/lib/_interop_require_wildcard.js\\").default;
const _b = require(\\"../packages/b/src/index\\");
const _b = require(\\"../packages/b/src/index.ts\\");
async function display() {
const displayA = await Promise.resolve().then(()=>/*#__PURE__*/ _interopRequireWildcard(require(\\"../packages/a/src/index\\"))).then((c)=>c.displayA);
const displayA = await Promise.resolve().then(()=>/*#__PURE__*/ _interopRequireWildcard(require(\\"../packages/a/src/index.ts\\"))).then((c)=>c.displayA);
console.log(displayA());
console.log((0, _b.displayB)());
}
Expand Down

1 comment on commit f6faeb3

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: f6faeb3 Previous: 9820122 Ratio
es/full/bugs-1 358795 ns/iter (± 38395) 373287 ns/iter (± 28346) 0.96
es/full/minify/libraries/antd 1965605898 ns/iter (± 88356385) 2001873169 ns/iter (± 56900320) 0.98
es/full/minify/libraries/d3 452632052 ns/iter (± 31919466) 401146522 ns/iter (± 10371101) 1.13
es/full/minify/libraries/echarts 1605755668 ns/iter (± 41635012) 1751536448 ns/iter (± 53773341) 0.92
es/full/minify/libraries/jquery 110594200 ns/iter (± 5662236) 114711300 ns/iter (± 8910224) 0.96
es/full/minify/libraries/lodash 125023605 ns/iter (± 5543173) 131769259 ns/iter (± 8526458) 0.95
es/full/minify/libraries/moment 64092084 ns/iter (± 4223605) 82814708 ns/iter (± 4260741) 0.77
es/full/minify/libraries/react 21261153 ns/iter (± 508100) 26593976 ns/iter (± 1810720) 0.80
es/full/minify/libraries/terser 321203060 ns/iter (± 6926155) 365326682 ns/iter (± 28101789) 0.88
es/full/minify/libraries/three 582566997 ns/iter (± 19542169) 704037778 ns/iter (± 25687217) 0.83
es/full/minify/libraries/typescript 3421487029 ns/iter (± 55811320) 4179611782 ns/iter (± 152277217) 0.82
es/full/minify/libraries/victory 837362326 ns/iter (± 31719325) 937062722 ns/iter (± 39953239) 0.89
es/full/minify/libraries/vue 161798266 ns/iter (± 7650905) 173936249 ns/iter (± 16561809) 0.93
es/full/codegen/es3 33745 ns/iter (± 1460) 34789 ns/iter (± 4909) 0.97
es/full/codegen/es5 34310 ns/iter (± 1316) 34478 ns/iter (± 4670) 1.00
es/full/codegen/es2015 33953 ns/iter (± 1194) 34797 ns/iter (± 5091) 0.98
es/full/codegen/es2016 34042 ns/iter (± 1193) 41129 ns/iter (± 7043) 0.83
es/full/codegen/es2017 33959 ns/iter (± 898) 41023 ns/iter (± 6930) 0.83
es/full/codegen/es2018 34151 ns/iter (± 4651) 34731 ns/iter (± 2874) 0.98
es/full/codegen/es2019 33894 ns/iter (± 1971) 35554 ns/iter (± 5941) 0.95
es/full/codegen/es2020 34172 ns/iter (± 1611) 34846 ns/iter (± 3993) 0.98
es/full/all/es3 194612803 ns/iter (± 11102168) 214985700 ns/iter (± 18702738) 0.91
es/full/all/es5 184162391 ns/iter (± 11583273) 243046724 ns/iter (± 21642421) 0.76
es/full/all/es2015 171168236 ns/iter (± 15028097) 192316368 ns/iter (± 14462713) 0.89
es/full/all/es2016 169181090 ns/iter (± 15750745) 186180612 ns/iter (± 19579409) 0.91
es/full/all/es2017 169214651 ns/iter (± 14932569) 166654404 ns/iter (± 23847007) 1.02
es/full/all/es2018 167720537 ns/iter (± 11380504) 183651979 ns/iter (± 21273439) 0.91
es/full/all/es2019 163547817 ns/iter (± 12153217) 182808659 ns/iter (± 19231820) 0.89
es/full/all/es2020 157577858 ns/iter (± 12681569) 180358610 ns/iter (± 20130818) 0.87
es/full/parser 750360 ns/iter (± 52555) 850885 ns/iter (± 102704) 0.88
es/full/base/fixer 27844 ns/iter (± 2392) 26902 ns/iter (± 2434) 1.04
es/full/base/resolver_and_hygiene 94929 ns/iter (± 9596) 96786 ns/iter (± 10883) 0.98
serialization of ast node 223 ns/iter (± 9) 230 ns/iter (± 39) 0.97
serialization of serde 222 ns/iter (± 24) 283 ns/iter (± 47) 0.78

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.