Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: move fixer to RuleCore #860

Merged
merged 6 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/config/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
mod combined;
mod constraints;
mod fixer;
mod maybe;
mod rule;
mod rule_collection;
mod rule_config;
mod rule_core;
mod transform;

use serde::Deserialize;
Expand Down
4 changes: 2 additions & 2 deletions crates/config/src/rule/deserialize_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::referent_rule::{GlobalRules, ReferentRuleError, RuleRegistration};
use crate::maybe::Maybe;
use crate::rule::{self, Rule, RuleSerializeError, SerializableRule};
use crate::rule_config::{
into_map, RuleConfigError, SerializableRuleConfigCore, SerializableRuleCore,
into_map, RuleConfigError, SerializableRuleCore, SerializableRuleCoreWithId,
HerringtonDarkholme marked this conversation as resolved.
Show resolved Hide resolved
};

use ast_grep_core::language::Language;
Expand Down Expand Up @@ -144,7 +144,7 @@ impl<L: Language> DeserializeEnv<L> {

/// register global utils rule discovered in the config.
pub fn parse_global_utils(
utils: Vec<SerializableRuleConfigCore<L>>,
utils: Vec<SerializableRuleCoreWithId<L>>,
) -> Result<GlobalRules<L>, RuleConfigError> {
let registration = GlobalRules::default();
let utils = into_map(utils);
Expand Down
16 changes: 9 additions & 7 deletions crates/config/src/rule_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::transform::Transformation;
use crate::DeserializeEnv;
use crate::GlobalRules;

pub use crate::constraints::{
pub use crate::rule_core::{
try_deserialize_matchers, RuleWithConstraint, SerializableMetaVarMatcher,
SerializeConstraintsError,
};
Expand Down Expand Up @@ -48,8 +48,12 @@ pub struct SerializableRuleCore<L: Language> {
pub utils: Option<HashMap<String, SerializableRule>>,
/// A dictionary for metavariable manipulation. Dict key is the new variable name.
/// Dict value is a [transformation] that specifies how meta var is processed.
/// Warning: this is experimental option. [`https://github.com/ast-grep/ast-grep/issues/436`]
/// See [transformation doc](https://ast-grep.github.io/reference/yaml/transformation.html).
pub transform: Option<HashMap<String, Transformation>>,
/// A pattern string or a FixConfig object to auto fix the issue.
/// It can reference metavariables appeared in rule.
/// See details in fix [object reference](https://ast-grep.github.io/reference/yaml/fix.html#fixconfig).
pub fix: Option<SerializableFixer>,
}

impl<L: Language> SerializableRuleCore<L> {
Expand Down Expand Up @@ -85,15 +89,15 @@ impl<L: Language> SerializableRuleCore<L> {
}
}
#[derive(Serialize, Deserialize, Clone, JsonSchema)]
pub struct SerializableRuleConfigCore<L: Language> {
pub struct SerializableRuleCoreWithId<L: Language> {
#[serde(flatten)]
pub core: SerializableRuleCore<L>,
/// Unique, descriptive identifier, e.g., no-unused-variable
pub id: String,
}

pub fn into_map<L: Language>(
rules: Vec<SerializableRuleConfigCore<L>>,
rules: Vec<SerializableRuleCoreWithId<L>>,
) -> HashMap<String, SerializableRuleCore<L>> {
rules.into_iter().map(|r| (r.id, r.core)).collect()
}
Expand All @@ -113,8 +117,6 @@ pub struct SerializableRuleConfig<L: Language> {
/// One of: hint, info, warning, or error
#[serde(default)]
pub severity: Severity,
/// A pattern to auto fix the issue. It can reference metavariables appeared in rule.
pub fix: Option<SerializableFixer>,
/// Glob patterns to specify that the rule only applies to matching files
pub files: Option<Vec<String>>,
/// Glob patterns that exclude rules from applying to files
Expand Down Expand Up @@ -225,14 +227,14 @@ mod test {
constraints: None,
transform: None,
utils: None,
fix: None,
};
SerializableRuleConfig {
core,
id: "".into(),
message: "".into(),
note: None,
severity: Severity::Hint,
fix: None,
files: None,
ignores: None,
url: None,
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions crates/napi/src/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ impl NapiConfig {
constraints: self.constraints.map(serde_json::from_value).transpose()?,
transform: self.transform.map(serde_json::from_value).transpose()?,
utils: self.utils.map(serde_json::from_value).transpose()?,
fix: None,
};
rule.get_matcher(&Default::default()).map_err(|e| {
let error = Error::from(e)
Expand Down
1 change: 1 addition & 0 deletions crates/pyo3/src/py_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ fn config_from_rule(
constraints: None,
utils: None,
transform: None,
fix: None,
})
}

Expand Down