Skip to content

Commit

Permalink
Merge pull request #1 from Justinidlerz/fix/correct_replace
Browse files Browse the repository at this point in the history
correct replace
  • Loading branch information
Justinidlerz authored Oct 21, 2024
2 parents 8776d2d + 0cdfc70 commit 56b9cc8
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 61 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
],
"preferUnplugged": true,
"devDependencies": {
"@swc/core": "^1.7.11",
"@swc/core": "^1.7.36",
"@types/node": "^22.3.0",
"prettier": "^3.3.3",
"typescript": "^5.5.4",
Expand Down
74 changes: 37 additions & 37 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

81 changes: 63 additions & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ use regex::Regex;
use serde::Deserialize;
use serde_json::from_str;
use swc_core::atoms::Atom;
use swc_core::common::DUMMY_SP;
use swc_core::ecma::ast::{CallExpr, ExportAll, ExportDecl, ExportDefaultDecl, FnExpr, Import, ImportDecl, ImportSpecifier, NamedExport, TplElement};
use swc_core::ecma::ast::{CallExpr, ExportAll, ExportDefaultDecl, Import, ImportDecl, ImportSpecifier, NamedExport, TplElement};

#[derive(Deserialize, Default)]
struct Config {
Expand All @@ -33,15 +32,19 @@ impl RemoveInvalidContent {
}
}

fn replace_with<'h>(&self, matcher: &Regex, str: &'h str) -> Cow<'h, str> {
matcher.replace_all(str, |caps: &regex::Captures| {
fn replace_with<'h>(&self, matcher: &Regex, str: &'h str) -> Result<Cow<'h, str>, bool> {
if !matcher.is_match(str) {
return Err(false);
}

Ok(matcher.replace_all(str, |caps: &regex::Captures| {
if self.replace_with.is_empty() {
return "".to_string();
}

let matched_str = &caps[0];
self.replace_with.repeat(matched_str.len())
})
}))
}
}

Expand All @@ -62,27 +65,30 @@ impl VisitMut for RemoveInvalidContent {

fn visit_mut_str(&mut self, node: &mut Str) {
for matcher in self.matchers.iter() {
let new_value = self.replace_with(matcher, node.value.as_str());

let new_content = new_value.to_string();

node.clone_from(&Str::from(new_content))
if let Ok(new_value) = self.replace_with(matcher, node.value.as_str()) {
node.clone_from(&Str::from(new_value.to_string()))
}
}
node.visit_mut_children_with(self);
}

fn visit_mut_tpl_element(&mut self, node: &mut TplElement) {
for matcher in self.matchers.iter() {
let new_value = self.replace_with(matcher, node.raw.as_str());
if let (Ok(cooked_value), Ok(raw_value)) = (
self.replace_with(matcher, node.cooked.clone().unwrap().as_str()), self.replace_with(matcher, node.raw.as_str())) {

let tpl_element = TplElement {
span: DUMMY_SP,
tail: false,
cooked: Some(Atom::from(new_value.to_string())),
raw: Atom::from(new_value.to_string()),
};
let cooked_value = cooked_value.clone().into();
let new_atom = Atom::from(raw_value);

node.clone_from(&tpl_element)
let tpl_element = TplElement {
span: node.span,
tail: node.tail,
raw: new_atom,
cooked: Some(cooked_value),
};

node.clone_from(&tpl_element)
}
}
node.visit_mut_children_with(self);
}
Expand Down Expand Up @@ -190,6 +196,45 @@ test_inline!(
r#"console.log("https:///faker-url");"#
);

test_inline!(
Default::default(),
|_| as_folder(RemoveInvalidContent::new(
Config{
matches: vec![r"[\u4E00-\u9FFF]".to_string()],
..Default::default()
}
)),
should_not_remove_slack,
r#"const a = {
cde: {
code: 1,
message: "\\\\",
}
}"#,
r#"const a = {
cde: {
code: 1,
message: "\\\\",
}
}"#
);


test_inline!(
Default::default(),
|_| as_folder(RemoveInvalidContent::new(
Config{
matches: vec![r"[\u4E00-\u9FFF]".to_string()],
..Default::default()
}
)),
should_not_remove_slack_from_tpl,
r#"const a = `\\中文${b}`"#,
r#"const a = `\\${b}`"#
);



test_inline!(
Default::default(),
|_| as_folder(RemoveInvalidContent::new(Config{
Expand Down
Loading

0 comments on commit 56b9cc8

Please sign in to comment.