1
- use crate :: fixer:: { Fixer , FixerError , SerializableFixer } ;
2
- use crate :: rule:: { RuleSerializeError , SerializableRule } ;
3
- use crate :: transform:: Transformation ;
4
- use crate :: DeserializeEnv ;
1
+ use crate :: fixer:: Fixer ;
5
2
use crate :: GlobalRules ;
6
3
7
4
pub use crate :: rule_core:: {
8
- try_deserialize_matchers, RuleWithConstraint , SerializableMetaVarMatcher ,
9
- SerializeConstraintsError ,
5
+ try_deserialize_matchers, RuleConfigError , RuleWithConstraint , SerializableMetaVarMatcher ,
6
+ SerializableRuleCore , SerializeConstraintsError ,
10
7
} ;
11
8
use ast_grep_core:: language:: Language ;
12
- use ast_grep_core:: meta_var:: MetaVarMatchers ;
13
9
use ast_grep_core:: replacer:: { IndentSensitive , Replacer } ;
14
10
use ast_grep_core:: { NodeMatch , StrDoc } ;
15
11
use schemars:: JsonSchema ;
16
12
use serde:: { Deserialize , Serialize } ;
17
- use serde_yaml:: { with:: singleton_map_recursive:: deserialize, Deserializer , Error as YamlError } ;
18
- use thiserror:: Error ;
13
+ use serde_yaml:: { with:: singleton_map_recursive:: deserialize, Deserializer } ;
19
14
20
15
use std:: collections:: HashMap ;
21
16
use std:: ops:: { Deref , DerefMut } ;
@@ -36,58 +31,6 @@ pub enum Severity {
36
31
Off ,
37
32
}
38
33
39
- #[ derive( Serialize , Deserialize , Clone , JsonSchema ) ]
40
- pub struct SerializableRuleCore < L : Language > {
41
- /// Specify the language to parse and the file extension to include in matching.
42
- pub language : L ,
43
- /// A rule object to find matching AST nodes
44
- pub rule : SerializableRule ,
45
- /// Additional meta variables pattern to filter matching
46
- pub constraints : Option < HashMap < String , SerializableMetaVarMatcher > > ,
47
- /// Utility rules that can be used in `matches`
48
- pub utils : Option < HashMap < String , SerializableRule > > ,
49
- /// A dictionary for metavariable manipulation. Dict key is the new variable name.
50
- /// Dict value is a [transformation] that specifies how meta var is processed.
51
- /// See [transformation doc](https://ast-grep.github.io/reference/yaml/transformation.html).
52
- pub transform : Option < HashMap < String , Transformation > > ,
53
- /// A pattern string or a FixConfig object to auto fix the issue.
54
- /// It can reference metavariables appeared in rule.
55
- /// See details in fix [object reference](https://ast-grep.github.io/reference/yaml/fix.html#fixconfig).
56
- pub fix : Option < SerializableFixer > ,
57
- }
58
-
59
- impl < L : Language > SerializableRuleCore < L > {
60
- fn get_deserialize_env ( & self , globals : & GlobalRules < L > ) -> RResult < DeserializeEnv < L > > {
61
- let env = DeserializeEnv :: new ( self . language . clone ( ) ) . with_globals ( globals) ;
62
- if let Some ( utils) = & self . utils {
63
- let env = env. register_local_utils ( utils) ?;
64
- Ok ( env)
65
- } else {
66
- Ok ( env)
67
- }
68
- }
69
-
70
- fn get_meta_var_matchers ( & self ) -> RResult < MetaVarMatchers < StrDoc < L > > > {
71
- Ok ( if let Some ( constraints) = self . constraints . clone ( ) {
72
- try_deserialize_matchers ( constraints, self . language . clone ( ) ) ?
73
- } else {
74
- MetaVarMatchers :: default ( )
75
- } )
76
- }
77
-
78
- pub fn get_matcher ( & self , globals : & GlobalRules < L > ) -> RResult < RuleWithConstraint < L > > {
79
- let env = self . get_deserialize_env ( globals) ?;
80
- let rule = env. deserialize_rule ( self . rule . clone ( ) ) ?;
81
- let matchers = self . get_meta_var_matchers ( ) ?;
82
- let transform = self . transform . clone ( ) ;
83
- Ok (
84
- RuleWithConstraint :: new ( rule)
85
- . with_matchers ( matchers)
86
- . with_utils ( env. registration )
87
- . with_transform ( transform) ,
88
- )
89
- }
90
- }
91
34
#[ derive( Serialize , Deserialize , Clone , JsonSchema ) ]
92
35
pub struct SerializableRuleCoreWithId < L : Language > {
93
36
#[ serde( flatten) ]
@@ -160,18 +103,6 @@ impl<L: Language> DerefMut for SerializableRuleConfig<L> {
160
103
}
161
104
}
162
105
163
- #[ derive( Debug , Error ) ]
164
- pub enum RuleConfigError {
165
- #[ error( "Fail to parse yaml as RuleConfig" ) ]
166
- Yaml ( #[ from] YamlError ) ,
167
- #[ error( "Rule is not configured correctly." ) ]
168
- Rule ( #[ from] RuleSerializeError ) ,
169
- #[ error( "fix pattern is invalid." ) ]
170
- Fixer ( #[ from] FixerError ) ,
171
- #[ error( "constraints is not configured correctly." ) ]
172
- Constraints ( #[ from] SerializeConstraintsError ) ,
173
- }
174
-
175
106
pub struct RuleConfig < L : Language > {
176
107
inner : SerializableRuleConfig < L > ,
177
108
pub matcher : RuleWithConstraint < L > ,
@@ -218,6 +149,7 @@ impl<L: Language> Deref for RuleConfig<L> {
218
149
mod test {
219
150
use super :: * ;
220
151
use crate :: from_str;
152
+ use crate :: rule:: SerializableRule ;
221
153
use crate :: test:: TypeScript ;
222
154
223
155
fn ts_rule_config ( rule : SerializableRule ) -> SerializableRuleConfig < TypeScript > {
0 commit comments