Skip to content

Commit

Permalink
feat: add not rule check and test
Browse files Browse the repository at this point in the history
  • Loading branch information
HerringtonDarkholme committed May 6, 2024
1 parent ad32172 commit 5f4758c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
23 changes: 21 additions & 2 deletions crates/config/src/rule/deserialize_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ fn visit_dependent_rule_ids<'a, T: DependentRule>(
visit_dependent_rule_ids(sub, sort)?;
}
}
if let Maybe::Present(_not) = &rule.not {
// TODO: check cyclic here
if let Maybe::Present(not) = &rule.not {
visit_dependent_rule_ids(not, sort)?;
}
Ok(())
}
Expand Down Expand Up @@ -305,4 +305,23 @@ local-rule-c:
assert!(ret.is_err());
Ok(())
}

#[test]
fn test_cyclic_not() -> Result<()> {
let utils = from_str(
"
local-rule-a:
not: {matches: local-rule-b}
local-rule-b:
matches: local-rule-a",
)?;
let ret = DeserializeEnv::new(TypeScript::Tsx).register_local_utils(&utils);
assert!(matches!(
ret,
Err(RuleSerializeError::MatchesReference(
ReferentRuleError::CyclicRule(_)
))
));
Ok(())
}
}
10 changes: 10 additions & 0 deletions crates/config/src/rule/referent_rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,16 @@ mod test {
Ok(())
}

#[test]
fn test_cyclic_not() -> Result {
let registration = RuleRegistration::<TS>::default();
let rule = ReferentRule::try_new("test".into(), &registration)?;
let rule = Rule::Not(Box::new(o::Not::new(Rule::Matches(rule))));
let error = registration.insert_local("test", rule);
assert!(matches!(error, Err(ReferentRuleError::CyclicRule(_))));
Ok(())
}

#[test]
fn test_success_rule() -> Result {
let registration = RuleRegistration::<TS>::default();
Expand Down

0 comments on commit 5f4758c

Please sign in to comment.