From 37de0d4989e5751c05419be9ab16c4ff46ac8f0c Mon Sep 17 00:00:00 2001 From: Alex Povel Date: Mon, 27 Nov 2023 21:03:26 +0100 Subject: [PATCH] feat: query for TypeScript strings Sadly, quite ugly, as template strings match their ENTIRE contents, not only the string literal part, as possible in C# --- src/scoping/langs/csharp.rs | 2 +- src/scoping/langs/python.rs | 4 +--- src/scoping/langs/typescript.rs | 11 +++++++++++ tests/langs/typescript/in/strings.ts | 17 +++++++++++++++++ tests/langs/typescript/mod.rs | 8 ++++---- tests/langs/typescript/out/strings.ts | 17 +++++++++++++++++ 6 files changed, 51 insertions(+), 8 deletions(-) create mode 100644 tests/langs/typescript/in/strings.ts create mode 100644 tests/langs/typescript/out/strings.ts diff --git a/src/scoping/langs/csharp.rs b/src/scoping/langs/csharp.rs index a3cff7ea..f38fe9b0 100644 --- a/src/scoping/langs/csharp.rs +++ b/src/scoping/langs/csharp.rs @@ -14,7 +14,7 @@ pub type CSharpQuery = CodeQuery; 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). diff --git a/src/scoping/langs/python.rs b/src/scoping/langs/python.rs index 96eb84f5..0ef623c1 100644 --- a/src/scoping/langs/python.rs +++ b/src/scoping/langs/python.rs @@ -14,9 +14,7 @@ pub type PythonQuery = CodeQuery; 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, diff --git a/src/scoping/langs/typescript.rs b/src/scoping/langs/typescript.rs index 771b9235..2f2253d4 100644 --- a/src/scoping/langs/typescript.rs +++ b/src/scoping/langs/typescript.rs @@ -14,6 +14,8 @@ pub type TypeScriptQuery = CodeQuery for TSQuery { @@ -22,6 +24,15 @@ impl From for TSQuery { TypeScript::lang(), match value { PremadeTypeScriptQuery::Comments => "(comment) @comment", + PremadeTypeScriptQuery::Strings => { + r#" + [ + (template_string) + (string) + ] + @string + "# + } }, ) .expect("Premade queries to be valid") diff --git a/tests/langs/typescript/in/strings.ts b/tests/langs/typescript/in/strings.ts new file mode 100644 index 00000000..ad5de425 --- /dev/null +++ b/tests/langs/typescript/in/strings.ts @@ -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__}`; diff --git a/tests/langs/typescript/mod.rs b/tests/langs/typescript/mod.rs index 92c3a9e3..7dd34d7a 100644 --- a/tests/langs/typescript/mod.rs +++ b/tests/langs/typescript/mod.rs @@ -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); diff --git a/tests/langs/typescript/out/strings.ts b/tests/langs/typescript/out/strings.ts new file mode 100644 index 00000000..1b8cef0c --- /dev/null +++ b/tests/langs/typescript/out/strings.ts @@ -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}`;