Skip to content

Commit

Permalink
feat(typescript): Scope constructor method definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpovel committed Aug 17, 2024
1 parent f5ddc52 commit afafbbf
Show file tree
Hide file tree
Showing 4 changed files with 65 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
- method: Method definitions
- constructor: `constructor` method definitions
- class: `class` definitions
- enum: `enum` definitions
- interface: `interface` definitions
Expand Down
7 changes: 7 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,
/// Method definitions.
Method,
/// `constructor` method definitions.
Constructor,
/// `class` definitions.
Class,
/// `enum` definitions.
Expand Down Expand Up @@ -68,6 +70,11 @@ impl From<PreparedTypeScriptQuery> for TSQuery {
)"#
}
PreparedTypeScriptQuery::Method => "(method_definition) @method",
PreparedTypeScriptQuery::Constructor => {
r#"(method_definition
name: (_) @name (#eq? @name "constructor")
) @constructor"#
}
PreparedTypeScriptQuery::Class => "(class_declaration) @class",
PreparedTypeScriptQuery::Enum => "(enum_declaration) @enum",
PreparedTypeScriptQuery::Interface => "(interface_declaration) @interface",
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::Method)),
)]
#[case(
"base.ts_constructor",
include_str!("typescript/base.ts"),
TypeScript::new(CodeQuery::Prepared(PreparedTypeScriptQuery::Constructor)),
)]
#[case(
"base.ts_class",
include_str!("typescript/base.ts"),
Expand Down
52 changes: 52 additions & 0 deletions tests/langs/snapshots/r#mod__langs__base.ts_constructor.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
source: tests/langs/mod.rs
expression: inscope_parts
---
- n: 84
l: " constructor(h: number, m: number) { }\n"
m: " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ "
- n: 106
l: " constructor(message: string) {\n"
m: " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"
- n: 107
l: " this.greeting = message;\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 108
l: " }\n"
m: "^^^^^ "
- n: 117
l: " constructor(theName: string) { this.name = theName; }\n"
m: " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ "
- n: 124
l: " constructor(name: string) { super(name); }\n"
m: " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ "
- n: 132
l: " constructor(name: string) { super(name); }\n"
m: " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ "
- n: 146
l: " constructor(name: string) { this.name = name; }\n"
m: " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ "
- n: 153
l: " constructor (theName: string) {\n"
m: " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"
- n: 154
l: " this.name = theName;\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 155
l: " }\n"
m: "^^^^^ "
- n: 178
l: " constructor(public name: string) {\n"
m: " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"
- n: 179
l: " }\n"
m: "^^^^^ "
- n: 282
l: " constructor(message: string) {\n"
m: " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"
- n: 283
l: " this.greeting = message;\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 284
l: " }\n"
m: "^^^^^ "

0 comments on commit afafbbf

Please sign in to comment.