Skip to content

Commit

Permalink
fix(es/parser): Fix parsing of TypeScript type instantiation (#8758)
Browse files Browse the repository at this point in the history
**Related issue:**

 - Closes #8735
  • Loading branch information
kdy1 committed Mar 18, 2024
1 parent 32ed692 commit 2d6de94
Show file tree
Hide file tree
Showing 8 changed files with 782 additions and 2 deletions.
19 changes: 19 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8735/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"jsc": {
"parser": {
"syntax": "typescript",
"tsx": true
},
"target": "es5",
"loose": false,
"minify": {
"compress": false,
"mangle": false
}
},
"module": {
"type": "es6"
},
"minify": false,
"isModule": true
}
5 changes: 5 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8735/input/input.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function getFirstElement<T>(array: T[]): T | null {
return array.length > 0 ? array[0] : null;
}

let myFunction = getFirstElement<number> as any;
4 changes: 4 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8735/output/input.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function getFirstElement(array) {
return array.length > 0 ? array[0] : null;
}
var myFunction = getFirstElement;
4 changes: 2 additions & 2 deletions crates/swc_ecma_parser/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1226,7 +1226,7 @@ impl<I: Tokens> Parser<I> {
)
.map(|expr| (Box::new(Expr::TaggedTpl(expr)), true))
.map(Some)
} else if is!(p, '=') {
} else if is_one_of!(p, '=', "as") {
Ok(Some((
Box::new(Expr::TsInstantiation(TsInstantiation {
span: span!(p, start),
Expand All @@ -1236,7 +1236,7 @@ impl<I: Tokens> Parser<I> {
},
type_args,
})),
true,
false,
)))
} else if no_call {
unexpected!(p, "`")
Expand Down
5 changes: 5 additions & 0 deletions crates/swc_ecma_parser/tests/typescript/issue-8735/1/input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function getFirstElement<T>(array: T[]): T | null {
return array.length > 0 ? array[0] : null;
}

let myFunction = getFirstElement<number> as any;
Loading

0 comments on commit 2d6de94

Please sign in to comment.