Skip to content

Commit 9a1fa84

Browse files
feat(es/parser): Add an error for empty type args for generic (#11164)
**Related issue:** - Closes #11119 --------- Signed-off-by: Yash Suthar <yashsuthar983@gmail.com>
1 parent 7356594 commit 9a1fa84

File tree

5 files changed

+22
-0
lines changed

5 files changed

+22
-0
lines changed

.changeset/afraid-coats-judge.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
swc_ecma_parser: patch
3+
swc_core: patch
4+
---
5+
6+
Fixed Parser error for empty type args for generic

crates/swc_ecma_lexer/src/common/parser/typescript.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,12 @@ pub fn parse_ts_type_args<'a, P: Parser<'a>>(p: &mut P) -> PResult<Box<TsTypePar
488488
p.input_mut().set_expr_allowed(false);
489489
p.expect_without_advance(&P::Token::GREATER)?;
490490
let span = Span::new_with_checked(start, p.input().cur_span().hi);
491+
492+
// Report grammar error for empty type argument list like `I<>`.
493+
if params.is_empty() {
494+
p.emit_err(span, SyntaxError::EmptyTypeArgumentList);
495+
}
496+
491497
Ok(Box::new(TsTypeParamInstantiation { span, params }))
492498
}
493499

crates/swc_ecma_lexer/src/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ pub enum SyntaxError {
290290

291291
ReservedTypeAssertion,
292292
ReservedArrowTypeParam,
293+
EmptyTypeArgumentList,
293294
}
294295

295296
impl SyntaxError {
@@ -744,6 +745,7 @@ impl SyntaxError {
744745
.mts or .cts extension. Add a trailing comma, \
745746
as in `<T,>() => ...`."
746747
.into(),
748+
SyntaxError::EmptyTypeArgumentList => "Type argument list cannot be empty.".into(),
747749
SyntaxError::InvalidAssignTarget => "Invalid assignment target".into(),
748750
}
749751
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class I<T> { }
2+
var x: I<>;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
x Type argument list cannot be empty.
2+
,-[$DIR/tests/typescript-errors/type-args-empty/input.ts:2:1]
3+
1 | class I<T> { }
4+
2 | var x: I<>;
5+
: ^^
6+
`----

0 commit comments

Comments
 (0)