Skip to content

Commit

Permalink
feat(typescript): Scope try/catch/finally blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpovel committed Aug 17, 2024
1 parent 91afb9f commit 4c19dd9
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1619,6 +1619,7 @@ Language scopes:
- async-function: `async function` definitions
- sync-function: Non-`async function` definitions
- enum: `enum` definitions
- try-catch: `try`/`catch`/`finally` blocks

--typescript-query <TREE-SITTER-QUERY>
Scope TypeScript code using a custom tree-sitter query.
Expand Down
3 changes: 3 additions & 0 deletions src/scoping/langs/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ pub enum PreparedTypeScriptQuery {
SyncFunction,
/// `enum` definitions.
Enum,
/// `try`/`catch`/`finally` blocks.
TryCatch,
}

impl From<PreparedTypeScriptQuery> for TSQuery {
Expand All @@ -48,6 +50,7 @@ impl From<PreparedTypeScriptQuery> for TSQuery {
)"#
}
PreparedTypeScriptQuery::Enum => "(enum_declaration) @enum",
PreparedTypeScriptQuery::TryCatch => "(try_statement) @try",
},
)
.expect("Prepared queries to be valid")
Expand Down
5 changes: 5 additions & 0 deletions tests/langs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@ impl InScopeLinePart {
include_str!("typescript/base.ts"),
TypeScript::new(CodeQuery::Prepared(PreparedTypeScriptQuery::Enum)),
)]
#[case(
"base.ts_try-block",
include_str!("typescript/base.ts"),
TypeScript::new(CodeQuery::Prepared(PreparedTypeScriptQuery::TryCatch)),
)]
#[case(
"base.rs_strings",
include_str!("rust/base.rs"),
Expand Down
25 changes: 25 additions & 0 deletions tests/langs/snapshots/r#mod__langs__base.ts_try-block.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
source: tests/langs/mod.rs
expression: inscope_parts
---
- n: 28
l: " try {\n"
m: " ^^^^^^^"
- n: 29
l: " throw new Error('This is an error');\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 30
l: " } catch (e) {\n"
m: ^^^^^^^^^^^^^^^^^^^
- n: 31
l: " console.error(e);\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 32
l: " } finally {\n"
m: ^^^^^^^^^^^^^^^^^
- n: 33
l: " console.log('finally');\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 34
l: " }\n"
m: "^^^^^ "

0 comments on commit 4c19dd9

Please sign in to comment.