Skip to content

Commit

Permalink
refactor: rename RuleWithConstraints to RuleCore
Browse files Browse the repository at this point in the history
BREAKING CHANGE: RuleCore is the new name of RuleWithConstraints.
The renaming reflects RuleCore = Rule + trans + fix + constraints.

fix #862
  • Loading branch information
HerringtonDarkholme committed Jan 15, 2024
1 parent 315f40a commit da7010b
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 31 deletions.
3 changes: 1 addition & 2 deletions crates/config/src/lib.rs
Expand Up @@ -19,8 +19,7 @@ pub use rule::DeserializeEnv;
pub use rule::{Rule, RuleSerializeError, SerializableRule};
pub use rule_collection::RuleCollection;
pub use rule_config::{
RuleConfig, RuleConfigError, RuleWithConstraint, SerializableRuleConfig, SerializableRuleCore,
Severity,
RuleConfig, RuleConfigError, RuleCore, SerializableRuleConfig, SerializableRuleCore, Severity,
};
pub use transform::Transformation;

Expand Down
14 changes: 7 additions & 7 deletions crates/config/src/rule/referent_rule.rs
@@ -1,4 +1,4 @@
use crate::{Rule, RuleWithConstraint};
use crate::{Rule, RuleCore};

use ast_grep_core::language::Language;
use ast_grep_core::meta_var::MetaVarEnv;
Expand Down Expand Up @@ -27,10 +27,10 @@ impl<R> Registration<R> {
self.0.write().unwrap()
}
}
pub type GlobalRules<L> = Registration<RuleWithConstraint<L>>;
pub type GlobalRules<L> = Registration<RuleCore<L>>;

impl<L: Language> GlobalRules<L> {
pub fn insert(&self, id: &str, rule: RuleWithConstraint<L>) -> Result<(), ReferentRuleError> {
pub fn insert(&self, id: &str, rule: RuleCore<L>) -> Result<(), ReferentRuleError> {

Check warning on line 33 in crates/config/src/rule/referent_rule.rs

View check run for this annotation

Codecov / codecov/patch

crates/config/src/rule/referent_rule.rs#L33

Added line #L33 was not covered by tests
let mut map = self.write();
if map.contains_key(id) {
return Err(ReferentRuleError::DuplicateRule(id.into()));
Expand All @@ -55,7 +55,7 @@ impl<R> Default for Registration<R> {
#[derive(Clone)]
pub struct RuleRegistration<L: Language> {
local: Registration<Rule<L>>,
global: Registration<RuleWithConstraint<L>>,
global: Registration<RuleCore<L>>,
}

// these are shit code
Expand All @@ -64,7 +64,7 @@ impl<L: Language> RuleRegistration<L> {
self.local.read()
}

fn get_global(&self) -> RwLockReadGuard<HashMap<String, RuleWithConstraint<L>>> {
fn get_global(&self) -> RwLockReadGuard<HashMap<String, RuleCore<L>>> {
self.global.read()
}

Expand Down Expand Up @@ -107,7 +107,7 @@ impl<L: Language> Default for RuleRegistration<L> {

pub struct RegistrationRef<L: Language> {
local: Weak<RwLock<HashMap<String, Rule<L>>>>,
global: Weak<RwLock<HashMap<String, RuleWithConstraint<L>>>>,
global: Weak<RwLock<HashMap<String, RuleCore<L>>>>,
}
// these are shit code
impl<L: Language> RegistrationRef<L> {
Expand Down Expand Up @@ -156,7 +156,7 @@ impl<L: Language> ReferentRule<L> {

fn eval_global<F, T>(&self, func: F) -> Option<T>
where
F: FnOnce(&RuleWithConstraint<L>) -> T,
F: FnOnce(&RuleCore<L>) -> T,
{
let registration = self.reg_ref.unref();
let rules = registration.get_global();
Expand Down
4 changes: 2 additions & 2 deletions crates/config/src/rule_config.rs
@@ -1,7 +1,7 @@
use crate::GlobalRules;

pub use crate::rule_core::{
try_deserialize_matchers, RuleConfigError, RuleWithConstraint, SerializableMetaVarMatcher,
try_deserialize_matchers, RuleConfigError, RuleCore, SerializableMetaVarMatcher,
SerializableRuleCore, SerializeConstraintsError,
};
use ast_grep_core::language::Language;
Expand Down Expand Up @@ -91,7 +91,7 @@ impl<L: Language> DerefMut for SerializableRuleConfig<L> {

pub struct RuleConfig<L: Language> {
inner: SerializableRuleConfig<L>,
pub matcher: RuleWithConstraint<L>,
pub matcher: RuleCore<L>,
}

impl<L: Language> RuleConfig<L> {
Expand Down
18 changes: 9 additions & 9 deletions crates/config/src/rule_core.rs
Expand Up @@ -133,14 +133,14 @@ impl<L: Language> SerializableRuleCore<L> {
}
}

pub fn get_matcher(&self, globals: &GlobalRules<L>) -> RResult<RuleWithConstraint<L>> {
pub fn get_matcher(&self, globals: &GlobalRules<L>) -> RResult<RuleCore<L>> {
let env = self.get_deserialize_env(globals)?;
let rule = env.deserialize_rule(self.rule.clone())?;
let matchers = self.get_meta_var_matchers()?;
let transform = self.transform.clone();
let fixer = self.get_fixer(globals)?;
Ok(
RuleWithConstraint::new(rule)
RuleCore::new(rule)
.with_matchers(matchers)
.with_utils(env.registration)
.with_transform(transform)
Expand All @@ -149,7 +149,7 @@ impl<L: Language> SerializableRuleCore<L> {
}
}

pub struct RuleWithConstraint<L: Language> {
pub struct RuleCore<L: Language> {
rule: Rule<L>,
matchers: MetaVarMatchers<StrDoc<L>>,
kinds: Option<BitSet>,
Expand All @@ -159,7 +159,7 @@ pub struct RuleWithConstraint<L: Language> {
_utils: RuleRegistration<L>,
}

impl<L: Language> RuleWithConstraint<L> {
impl<L: Language> RuleCore<L> {
#[inline]
pub fn new(rule: Rule<L>) -> Self {
let kinds = rule.potential_kinds();
Expand Down Expand Up @@ -190,14 +190,14 @@ impl<L: Language> RuleWithConstraint<L> {
Self { fixer, ..self }
}
}
impl<L: Language> Deref for RuleWithConstraint<L> {
impl<L: Language> Deref for RuleCore<L> {
type Target = Rule<L>;
fn deref(&self) -> &Self::Target {
&self.rule
}
}

impl<L: Language> Default for RuleWithConstraint<L> {
impl<L: Language> Default for RuleCore<L> {
#[inline]
fn default() -> Self {
Self {
Expand All @@ -211,7 +211,7 @@ impl<L: Language> Default for RuleWithConstraint<L> {
}
}

impl<L: Language> Matcher<L> for RuleWithConstraint<L> {
impl<L: Language> Matcher<L> for RuleCore<L> {
fn match_node_with_env<'tree, D: Doc<Lang = L>>(
&self,
node: Node<'tree, D>,
Expand Down Expand Up @@ -260,8 +260,8 @@ mod test {
"A".to_string(),
MetaVarMatcher::Regex(RegexMatcher::try_new("a").unwrap()),
);
let rule = RuleWithConstraint::new(Rule::Pattern(Pattern::new("$A", TypeScript::Tsx)))
.with_matchers(matchers);
let rule =
RuleCore::new(Rule::Pattern(Pattern::new("$A", TypeScript::Tsx))).with_matchers(matchers);
let grep = TypeScript::Tsx.ast_grep("a");
assert!(grep.root().find(&rule).is_some());
let grep = TypeScript::Tsx.ast_grep("bbb");
Expand Down
7 changes: 2 additions & 5 deletions crates/napi/src/doc.rs
@@ -1,6 +1,6 @@
use crate::FrontEndLanguage;

use ast_grep_config::{RuleWithConstraint, SerializableRuleCore};
use ast_grep_config::{RuleCore, SerializableRuleCore};
use ast_grep_core::replacer::IndentSensitive;
use ast_grep_core::source::{Content, Doc, Edit, TSParseError};
use ast_grep_core::Language;
Expand Down Expand Up @@ -29,10 +29,7 @@ pub struct NapiConfig {
}

impl NapiConfig {
pub fn parse_with(
self,
language: FrontEndLanguage,
) -> NapiResult<RuleWithConstraint<FrontEndLanguage>> {
pub fn parse_with(self, language: FrontEndLanguage) -> NapiResult<RuleCore<FrontEndLanguage>> {
let lang = self.language.unwrap_or(language);
let rule = SerializableRuleCore {
language: lang,
Expand Down
6 changes: 3 additions & 3 deletions crates/napi/src/lib.rs
Expand Up @@ -4,7 +4,7 @@ mod doc;
mod fe_lang;
mod sg_node;

use ast_grep_config::RuleWithConstraint;
use ast_grep_config::RuleCore;
use ast_grep_core::language::Language;
use ast_grep_core::pinned::{NodeData, PinnedNodeData};
use ast_grep_core::{AstGrep, NodeMatch};
Expand Down Expand Up @@ -224,7 +224,7 @@ fn get_root(entry: ignore::DirEntry, lang_option: &LangOption) -> Ret<(AstGrep<J

type FindInFiles = IterateFiles<(
ThreadsafeFunction<PinnedNodes, ErrorStrategy::CalleeHandled>,
RuleWithConstraint<FrontEndLanguage>,
RuleCore<FrontEndLanguage>,
)>;

pub struct PinnedNodes(
Expand Down Expand Up @@ -292,7 +292,7 @@ fn from_pinned_data(pinned: PinnedNodes, env: napi::Env) -> Result<Vec<Vec<SgNod
fn call_sg_node(
(tsfn, rule): &(
ThreadsafeFunction<PinnedNodes, ErrorStrategy::CalleeHandled>,
RuleWithConstraint<FrontEndLanguage>,
RuleCore<FrontEndLanguage>,
),
entry: std::result::Result<ignore::DirEntry, ignore::Error>,
lang_option: &LangOption,
Expand Down
6 changes: 3 additions & 3 deletions crates/pyo3/src/py_node.rs
@@ -1,7 +1,7 @@
use crate::range::Range;
use crate::SgRoot;

use ast_grep_config::{GlobalRules, RuleWithConstraint, SerializableRuleCore};
use ast_grep_config::{GlobalRules, RuleCore, SerializableRuleCore};
use ast_grep_core::{NodeMatch, StrDoc};
use ast_grep_language::SupportLang;

Expand Down Expand Up @@ -269,7 +269,7 @@ impl SgNode {
&self,
config: Option<&PyDict>,
kwargs: Option<&PyDict>,
) -> PyResult<RuleWithConstraint<SupportLang>> {
) -> PyResult<RuleCore<SupportLang>> {
let lang = self.inner.lang();
let config = if let Some(config) = config {
config_from_dict(lang, config)?
Expand Down Expand Up @@ -313,7 +313,7 @@ fn config_from_rule(
fn get_matcher_from_rule(
lang: &SupportLang,
dict: Option<&PyDict>,
) -> PyResult<RuleWithConstraint<SupportLang>> {
) -> PyResult<RuleCore<SupportLang>> {
let rule = dict.ok_or_else(|| PyErr::new::<PyValueError, _>("rule must not be empty"))?;
let config = config_from_rule(lang, rule)?;
let matcher = config
Expand Down

0 comments on commit da7010b

Please sign in to comment.