Skip to content

Commit

Permalink
feat: query for TypeScript strings
Browse files Browse the repository at this point in the history
Sadly, quite ugly, as template strings match their
ENTIRE contents, not only the string literal part, as possible in C#
  • Loading branch information
alexpovel committed Nov 27, 2023
1 parent 7ab5dff commit 37de0d4
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/scoping/langs/csharp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub type CSharpQuery = CodeQuery<CustomCSharpQuery, PremadeCSharpQuery>;
pub enum PremadeCSharpQuery {
/// Comments (including XML, inline, doc comments).
Comments,
/// Strings (literal, verbatim, interpolated).
/// Strings (incl. verbatim, interpolated; incl. quotes, except for interpolated).
///
/// Raw strings are not yet supported
/// (https://github.com/tree-sitter/tree-sitter-c-sharp/pull/240 not released yet).
Expand Down
4 changes: 1 addition & 3 deletions src/scoping/langs/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ pub type PythonQuery = CodeQuery<CustomPythonQuery, PremadePythonQuery>;
pub enum PremadePythonQuery {
/// Comments.
Comments,
/// Docstrings.
///
/// Does not cover multi-line strings.
/// Docstrings (not including multi-line strings).
DocStrings,
/// Function names, at the definition site.
FunctionNames,
Expand Down
11 changes: 11 additions & 0 deletions src/scoping/langs/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ pub type TypeScriptQuery = CodeQuery<CustomTypeScriptQuery, PremadeTypeScriptQue
pub enum PremadeTypeScriptQuery {
/// Comments.
Comments,
/// Strings (literal, template; includes quote characters).
Strings,
}

impl From<PremadeTypeScriptQuery> for TSQuery {
Expand All @@ -22,6 +24,15 @@ impl From<PremadeTypeScriptQuery> for TSQuery {
TypeScript::lang(),
match value {
PremadeTypeScriptQuery::Comments => "(comment) @comment",
PremadeTypeScriptQuery::Strings => {
r#"
[
(template_string)
(string)
]
@string
"#
}
},
)
.expect("Premade queries to be valid")
Expand Down
17 changes: 17 additions & 0 deletions tests/langs/typescript/in/strings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Single-quoted __T__string
let singleQuoted: string = 'He__T__llo';

// Double-quoted __T__string
let doubleQuoted__T__: string = "Wor__T__ld";

// Template literal __T__(backticks)
// CAREFUL: variable name is included and will be modified
let templateLiteral: string = `Hello__T__ ${doubleQuoted__T__}`;

// Tagged __T__template literal
function tag(strings: TemplateStringsArray, ...values: any[]) {
return strings.reduce((result, str, i) => result + str + (values[i] || ''), '');
}

// CAREFUL: variable name is included and will be modified
let taggedTemplate: string = tag`This__T__ is ${singleQuoted} and ${doubleQuoted__T__}`;
8 changes: 4 additions & 4 deletions tests/langs/typescript/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ use super::{get_input_output, nuke_target};
"comments.ts",
TypeScriptQuery::Premade(PremadeTypeScriptQuery::Comments)
)]
// #[case(
// "strings.cs",
// TypeScriptQuery::Premade(PremadeTypeScriptQuery::Strings)
// )]
#[case(
"strings.ts",
TypeScriptQuery::Premade(PremadeTypeScriptQuery::Strings)
)]
fn test_typescript_nuke(#[case] file: &str, #[case] query: TypeScriptQuery) {
let lang = TypeScript::new(query);

Expand Down
17 changes: 17 additions & 0 deletions tests/langs/typescript/out/strings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Single-quoted __T__string
let singleQuoted: string = 'Hello';

// Double-quoted __T__string
let doubleQuoted__T__: string = "World";

// Template literal __T__(backticks)
// CAREFUL: variable name is included and will be modified
let templateLiteral: string = `Hello ${doubleQuoted}`;

// Tagged __T__template literal
function tag(strings: TemplateStringsArray, ...values: any[]) {
return strings.reduce((result, str, i) => result + str + (values[i] || ''), '');
}

// CAREFUL: variable name is included and will be modified
let taggedTemplate: string = tag`This is ${singleQuoted} and ${doubleQuoted}`;

0 comments on commit 37de0d4

Please sign in to comment.