Skip to content

Commit e0bfaf2

Browse files
test: add test for transformation indentation
1 parent ae82daf commit e0bfaf2

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

crates/config/src/transform/mod.rs

+26
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,30 @@ mod test {
118118
let ret = Transform::deserialize(&trans, &env);
119119
assert!(ret.is_ok());
120120
}
121+
122+
#[test]
123+
fn test_transform_indentation() {
124+
let src = "
125+
if (true) {
126+
let a = {
127+
b: 123
128+
}
129+
}
130+
";
131+
let expected = "{
132+
b: 123
133+
}";
134+
let mut trans = HashMap::new();
135+
let tr = from_str("{ substring: { source: $A } }").expect("should work");
136+
trans.insert("TR".into(), tr);
137+
let grep = TypeScript::Tsx.ast_grep(src);
138+
let root = grep.root();
139+
let mut nm = root.find("let a = $A").expect("should find");
140+
let env = DeserializeEnv::new(TypeScript::Tsx);
141+
let trans = Transform::deserialize(&trans, &env).expect("should deserialize");
142+
trans.apply_transform(nm.get_env_mut(), &Default::default(), &Default::default());
143+
let actual = nm.get_env().get_transformed("TR").expect("should have TR");
144+
let actual = std::str::from_utf8(actual).expect("should work");
145+
assert_eq!(actual, expected);
146+
}
121147
}

crates/config/src/transform/transformation.rs

+20
Original file line numberDiff line numberDiff line change
@@ -435,5 +435,25 @@ mod test {
435435
assert_eq!(actual, "camelcase_not");
436436
Ok(())
437437
}
438+
439+
#[test]
440+
fn test_transform_indentation_with_insertion() -> R {
441+
let src = "
442+
if (true) {
443+
let a = {
444+
b: 123
445+
}
446+
}
447+
";
448+
// note the indentation
449+
let expected = "{
450+
b: 123
451+
}";
452+
let tr = parse("{ substring: { source: $A } }")?;
453+
let actual = get_transformed(src, "let a = $A", &tr).ok_or(())?;
454+
assert_eq!(actual, expected);
455+
Ok(())
456+
}
457+
438458
// TODO: add a symbolic test for Rewrite
439459
}

0 commit comments

Comments
 (0)