|
| 1 | +//! Extract SQL string literals from JavaScript and TypeScript source. |
| 2 | +//! |
| 3 | +//! Both grammars expose the same string-literal shapes: |
| 4 | +//! |
| 5 | +//! * `string` — single- or double-quoted, with backslash escapes. |
| 6 | +//! * `template_string` — backtick-quoted; may contain `${...}` |
| 7 | +//! `template_substitution` children. |
| 8 | +//! |
| 9 | +//! For template strings we follow the Python finder's pattern: each |
| 10 | +//! `template_substitution` becomes a literal `1`, which keeps the SQL |
| 11 | +//! parsable when substitutions stand in for static values. |
| 12 | +
|
| 13 | +pub fn extract_query_string_from_node(node: &tree_sitter::Node, code: &[u8]) -> Option<String> { |
| 14 | + match node.kind() { |
| 15 | + // string_inner_text already decodes any escape_sequence children; |
| 16 | + // its output is the final SQL text. |
| 17 | + "string" => string_inner_text(node, code), |
| 18 | + "template_string" => Some(extract_template(node, code)), |
| 19 | + _ => None, |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +/// Get the text between the quotes of a `string` node. Tree-sitter's JS |
| 24 | +/// grammar emits `string_fragment` children for the raw content, so prefer |
| 25 | +/// concatenating those when present; fall back to slicing between the outer |
| 26 | +/// quote characters otherwise. |
| 27 | +fn string_inner_text(node: &tree_sitter::Node, code: &[u8]) -> Option<String> { |
| 28 | + let mut cursor = node.walk(); |
| 29 | + let mut content = String::new(); |
| 30 | + let mut found_fragment = false; |
| 31 | + for child in node.children(&mut cursor) { |
| 32 | + match child.kind() { |
| 33 | + "string_fragment" => { |
| 34 | + found_fragment = true; |
| 35 | + content.push_str(&String::from_utf8_lossy( |
| 36 | + &code[child.start_byte()..child.end_byte()], |
| 37 | + )); |
| 38 | + } |
| 39 | + "escape_sequence" => { |
| 40 | + found_fragment = true; |
| 41 | + let raw = &code[child.start_byte()..child.end_byte()]; |
| 42 | + let text = std::str::from_utf8(raw).ok()?; |
| 43 | + // Decode the single escape inline so the caller's pass is a |
| 44 | + // no-op for whatever we produce here. |
| 45 | + content.push_str(&decode_js_escapes(text)); |
| 46 | + } |
| 47 | + _ => {} |
| 48 | + } |
| 49 | + } |
| 50 | + if found_fragment { |
| 51 | + return Some(content); |
| 52 | + } |
| 53 | + // Empty string or older grammar shape: slice between the outer quotes. |
| 54 | + let raw = &code[node.start_byte()..node.end_byte()]; |
| 55 | + let text = std::str::from_utf8(raw).ok()?; |
| 56 | + let bytes = text.as_bytes(); |
| 57 | + let quote = bytes.first().copied()?; |
| 58 | + if quote != b'"' && quote != b'\'' { |
| 59 | + return None; |
| 60 | + } |
| 61 | + let first = text.find(quote as char)?; |
| 62 | + let last = text.rfind(quote as char)?; |
| 63 | + if first >= last { |
| 64 | + return None; |
| 65 | + } |
| 66 | + Some(text[first + 1..last].to_string()) |
| 67 | +} |
| 68 | + |
| 69 | +fn extract_template(node: &tree_sitter::Node, code: &[u8]) -> String { |
| 70 | + let mut cursor = node.walk(); |
| 71 | + let mut out = String::new(); |
| 72 | + for child in node.children(&mut cursor) { |
| 73 | + match child.kind() { |
| 74 | + "string_fragment" => { |
| 75 | + out.push_str(&String::from_utf8_lossy( |
| 76 | + &code[child.start_byte()..child.end_byte()], |
| 77 | + )); |
| 78 | + } |
| 79 | + "escape_sequence" => { |
| 80 | + let raw = &code[child.start_byte()..child.end_byte()]; |
| 81 | + if let Ok(text) = std::str::from_utf8(raw) { |
| 82 | + out.push_str(&decode_js_escapes(text)); |
| 83 | + } |
| 84 | + } |
| 85 | + "template_substitution" => { |
| 86 | + // Replace ${...} with `1` — same trick as the Python finder. |
| 87 | + out.push('1'); |
| 88 | + } |
| 89 | + _ => {} |
| 90 | + } |
| 91 | + } |
| 92 | + out |
| 93 | +} |
| 94 | + |
| 95 | +fn decode_js_escapes(s: &str) -> String { |
| 96 | + let mut out = String::with_capacity(s.len()); |
| 97 | + let mut chars = s.chars(); |
| 98 | + while let Some(c) = chars.next() { |
| 99 | + if c != '\\' { |
| 100 | + out.push(c); |
| 101 | + continue; |
| 102 | + } |
| 103 | + match chars.next() { |
| 104 | + Some('"') => out.push('"'), |
| 105 | + Some('\'') => out.push('\''), |
| 106 | + Some('`') => out.push('`'), |
| 107 | + Some('\\') => out.push('\\'), |
| 108 | + Some('n') => out.push('\n'), |
| 109 | + Some('t') => out.push('\t'), |
| 110 | + Some('r') => out.push('\r'), |
| 111 | + Some('0') => out.push('\0'), |
| 112 | + // `\b`, `\f`, `\v`, `\xNN`, `\uNNNN`, `\u{...}`, octal: keep |
| 113 | + // literal text. Half-decoding adds risk without payoff. |
| 114 | + Some(other) => { |
| 115 | + out.push('\\'); |
| 116 | + out.push(other); |
| 117 | + } |
| 118 | + None => out.push('\\'), |
| 119 | + } |
| 120 | + } |
| 121 | + out |
| 122 | +} |
0 commit comments