Skip to content

Commit

Permalink
fix(rulegen): support creating cases in outdent (#1704)
Browse files Browse the repository at this point in the history
fix: #1694
  • Loading branch information
mysteryven authored Dec 17, 2023
1 parent 9597036 commit a96e428
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
9 changes: 4 additions & 5 deletions tasks/rulegen/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@ use std::borrow::Cow;

use lazy_static::lazy_static;

/// Convert a javascript object literal to JSON by wrapping the property keys in double quote
pub fn wrap_property_in_quotes(object: &str) -> String {
/// Convert a javascript object literal to JSON by wrapping the property keys in double quote,
/// and convert the single quote to a double quote.
pub fn convert_config_to_json_literal(object: &str) -> String {
use regex::{Captures, Regex};

lazy_static! {
static ref IDENT_MATCHER: Regex = Regex::new(r"(?P<ident>[[:alpha:]]\w*\s*):").unwrap();
static ref DUP_QUOTE_MATCHER: Regex =
Regex::new(r#"(?P<outer>"(?P<inner>"\w+")")"#).unwrap();
}

let add_quote = IDENT_MATCHER
.replace_all(object, |capture: &Captures| {
let ident = &capture["ident"];
Cow::Owned(format!(r#""{ident}":"#))
})
.into_owned();
.replace('\'', "\"");
add_quote
}
8 changes: 5 additions & 3 deletions tasks/rulegen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,12 @@ impl<'a> Visit<'a> for TestCase<'a> {
self.code = match &prop.value {
Expression::StringLiteral(s) => Some(s.value.to_string()),
// eslint-plugin-jest use dedent to strips indentation from multi-line strings
// eslint-plugin-unicon use outdent to removes leading indentation from ES6 template strings
Expression::TaggedTemplateExpression(tag_expr) => {
let Expression::Identifier(ident) = &tag_expr.tag else {
continue;
};
if ident.name != "dedent" {
if ident.name != "dedent" && ident.name != "outdent" {
continue;
}
tag_expr.quasi.quasi().map(ToString::to_string)
Expand Down Expand Up @@ -176,7 +177,8 @@ impl<'a> Visit<'a> for TestCase<'a> {
PropertyKey::Identifier(ident) if ident.name == "options" => {
let span = prop.value.span();
let option_text = &self.source_text[span.start as usize..span.end as usize];
self.config = Some(Cow::Owned(json::wrap_property_in_quotes(option_text)));
self.config =
Some(Cow::Owned(json::convert_config_to_json_literal(option_text)));
}
_ => continue,
},
Expand All @@ -199,7 +201,7 @@ impl<'a> Visit<'a> for TestCase<'a> {
let Expression::Identifier(ident) = &expr.tag else {
return;
};
if ident.name != "dedent" {
if ident.name != "dedent" && ident.name != "outdent" {
return;
}
self.code = expr.quasi.quasi().map(std::string::ToString::to_string);
Expand Down

0 comments on commit a96e428

Please sign in to comment.