Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions ast/src/lang/parse/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ use tree_sitter::{Node as TreeNode, QueryMatch};

pub fn trim_quotes(value: &str) -> &str {
let value = value.trim();
if value.starts_with('"') && value.ends_with('"') {
return &value[1..value.len() - 1];
}
if value.starts_with("'") && value.ends_with("'") {
return &value[1..value.len() - 1];
}
if value.starts_with("`") && value.ends_with("`") {
return &value[1..value.len() - 1];
for quote in ['"', '\'', '`'] {
if let Some(inner) = value
.strip_prefix(quote)
.and_then(|v| v.strip_suffix(quote))
{
return inner;
}
}
if let Some(stripped) = value.strip_prefix(':') {
return stripped;
Expand Down
10 changes: 5 additions & 5 deletions ast/src/lang/queries/angular.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,11 +427,11 @@ impl Stack for Angular {
path = path[1..].to_string();
}

if (path.starts_with('"') && path.ends_with('"'))
|| (path.starts_with('\'') && path.ends_with('\''))
|| (path.starts_with('`') && path.ends_with('`'))
{
path = path[1..path.len() - 1].to_string();
for quote in ['"', '\'', '`'] {
if let Some(inner) = path.strip_prefix(quote).and_then(|p| p.strip_suffix(quote)) {
path = inner.to_string();
break;
}
}

path
Expand Down
10 changes: 5 additions & 5 deletions ast/src/lang/queries/react_ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1160,11 +1160,11 @@ impl Stack for TypeScriptReact {
path = path[1..].to_string();
}

if (path.starts_with('"') && path.ends_with('"'))
|| (path.starts_with('\'') && path.ends_with('\''))
|| (path.starts_with('`') && path.ends_with('`'))
{
path = path[1..path.len() - 1].to_string();
for quote in ['"', '\'', '`'] {
if let Some(inner) = path.strip_prefix(quote).and_then(|p| p.strip_suffix(quote)) {
path = inner.to_string();
break;
}
}

// FROM TYPESCRIPT: .js -> .ts replacement
Expand Down
19 changes: 9 additions & 10 deletions ast/src/lang/queries/ruby.rs
Original file line number Diff line number Diff line change
Expand Up @@ -784,8 +784,8 @@ impl Stack for Ruby {
fn resolve_import_path(&self, import_path: &str, _current_file: &str) -> String {
let mut path = import_path.to_string();

if path.starts_with("(") {
path = path[1..path.len() - 1].to_string();
if let Some(inner) = path.strip_prefix('(').and_then(|p| p.strip_suffix(')')) {
path = inner.to_string();
}

if path.contains(":") {
Expand All @@ -797,20 +797,19 @@ impl Stack for Ruby {
fn resolve_import_name(&self, import_name: &str) -> String {
let mut name = import_name.to_string();

if name.starts_with("(") {
name = name[1..name.len() - 1].to_string();
if let Some(inner) = name.strip_prefix('(').and_then(|n| n.strip_suffix(')')) {
name = inner.to_string();
}

if name.contains(":") {
name = name.replace(":", "");
}

if name.starts_with("\"") && name.ends_with("\"") {
name = name[1..name.len() - 1].to_string();
}

if name.starts_with("'") && name.ends_with("'") {
name = name[1..name.len() - 1].to_string();
for quote in ['"', '\''] {
if let Some(inner) = name.strip_prefix(quote).and_then(|n| n.strip_suffix(quote)) {
name = inner.to_string();
break;
}
}

if name.starts_with("File") {
Expand Down
2 changes: 2 additions & 0 deletions ast/src/testing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use annotations::run_fixture_test;
pub mod builder;
#[cfg(test)]
pub mod registry;
#[cfg(test)]
pub mod quotes;

#[cfg(test)]
pub mod coverage;
Expand Down
51 changes: 51 additions & 0 deletions ast/src/testing/quotes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use crate::lang::parse::trim_quotes;
use crate::lang::queries::react_ts::TypeScriptReact;
use crate::lang::queries::ruby::Ruby;
use crate::lang::queries::Stack;

#[test]
fn trim_quotes_strips_matching_quotes() {
assert_eq!(trim_quotes("\"abc\""), "abc");
assert_eq!(trim_quotes("'abc'"), "abc");
assert_eq!(trim_quotes("`abc`"), "abc");
assert_eq!(trim_quotes("\"\""), "");
assert_eq!(trim_quotes(":sym"), "sym");
}

#[test]
fn trim_quotes_leaves_unbalanced_quotes_alone() {
assert_eq!(trim_quotes("\"abc"), "\"abc");
assert_eq!(trim_quotes("\"abc'"), "\"abc'");
}

#[test]
fn trim_quotes_lone_quote_does_not_panic() {
assert_eq!(trim_quotes("\""), "\"");
assert_eq!(trim_quotes("'"), "'");
assert_eq!(trim_quotes("`"), "`");
}

#[test]
fn ruby_import_path_strips_matching_parens_only() {
let ruby = Ruby::default();
assert_eq!(ruby.resolve_import_path("(foo)", ""), "foo");
assert_eq!(ruby.resolve_import_path("(", ""), "(");
assert_eq!(ruby.resolve_import_path("(abc", ""), "(abc");
assert_eq!(ruby.resolve_import_path("(café", ""), "(café");
}

#[test]
fn ruby_import_name_strips_matching_quotes_only() {
let ruby = Ruby::default();
assert_eq!(ruby.resolve_import_name("\"foo\""), "Foo");
assert_eq!(ruby.resolve_import_name("'foo'"), "Foo");
assert_eq!(ruby.resolve_import_name("\""), "\"");
assert_eq!(ruby.resolve_import_name("'"), "'");
}

#[test]
fn react_ts_import_path_lone_quote_does_not_panic() {
let ts = TypeScriptReact::default();
assert_eq!(ts.resolve_import_path("\"x\"", ""), "x");
assert_eq!(ts.resolve_import_path("\"", ""), "\"");
}
Loading