fix(parser): fix crash on TSTemplateLiteralType in function return position (#2089)

```
interface Helpers {
  inspect(): `~~~~\n${string}\n~~~~`;
}
```
This commit is contained in:
Boshen 2024-01-19 23:14:05 +08:00 committed by GitHub
parent 721a869b6e
commit 2f5afff9bd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 13 additions and 17 deletions

View file

@ -197,17 +197,6 @@ pub enum TSTypeOperator {
Readonly,
}
impl TSTypeOperator {
pub fn from_src(src: &str) -> Option<Self> {
match src {
"keyof" => Some(Self::Keyof),
"unique" => Some(Self::Unique),
"readonly" => Some(Self::Readonly),
_ => None,
}
}
}
/// `let myArray: string[] = ["hello", "world"];`
///
/// <https://www.typescriptlang.org/docs/handbook/2/objects.html#the-array-type>

View file

@ -302,8 +302,12 @@ impl<'a> Parser<'a> {
return self.parse_ts_infer_type();
}
let operator =
if self.at(Kind::Str) { None } else { TSTypeOperator::from_src(self.cur_string()) };
let operator = match self.cur_kind() {
Kind::KeyOf => Some(TSTypeOperator::Keyof),
Kind::Unique => Some(TSTypeOperator::Unique),
Kind::Readonly => Some(TSTypeOperator::Readonly),
_ => None,
};
// test ts ts_type_operator
// type B = keyof A;

View file

@ -1,3 +1,3 @@
codegen_misc Summary:
AST Parsed : 9/9 (100.00%)
Positive Passed: 9/9 (100.00%)
AST Parsed : 10/10 (100.00%)
Positive Passed: 10/10 (100.00%)

View file

@ -0,0 +1,3 @@
interface Helpers {
inspect(): `~~~~\n${string}\n~~~~`;
}

View file

@ -1,6 +1,6 @@
parser_misc Summary:
AST Parsed : 9/9 (100.00%)
Positive Passed: 9/9 (100.00%)
AST Parsed : 10/10 (100.00%)
Positive Passed: 10/10 (100.00%)
Negative Passed: 5/5 (100.00%)
× Unexpected token
╭─[fail/oxc-169.js:1:1]