Skip to content

Commit

Permalink
fix(parser): fix crash on TSTemplateLiteralType in function return po…
Browse files Browse the repository at this point in the history
…sition (#2089)

```
interface Helpers {
  inspect(): `~~~~\n${string}\n~~~~`;
}
```
  • Loading branch information
Boshen committed Jan 19, 2024
1 parent 721a869 commit 2f5afff
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 17 deletions.
11 changes: 0 additions & 11 deletions crates/oxc_ast/src/ast/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
8 changes: 6 additions & 2 deletions crates/oxc_parser/src/ts/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions tasks/coverage/codegen_misc.snap
Original file line number Diff line number Diff line change
@@ -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%)
3 changes: 3 additions & 0 deletions tasks/coverage/misc/pass/oxc-2087.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
interface Helpers {
inspect(): `~~~~\n${string}\n~~~~`;
}
4 changes: 2 additions & 2 deletions tasks/coverage/parser_misc.snap
Original file line number Diff line number Diff line change
@@ -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]
Expand Down

0 comments on commit 2f5afff

Please sign in to comment.