diff --git a/crates/gritmodule/fixtures/pattern_files/.grit/grit.yaml b/crates/gritmodule/fixtures/pattern_files/.grit/grit.yaml index 818de7336..d58831a5d 100644 --- a/crates/gritmodule/fixtures/pattern_files/.grit/grit.yaml +++ b/crates/gritmodule/fixtures/pattern_files/.grit/grit.yaml @@ -1,5 +1,7 @@ version: 0.0.1 patterns: + - file: ../docs/guides/version_5_upgrade.md + - file: ../docs/guides/something.md - name: remove_console_error level: error body: | @@ -7,6 +9,3 @@ patterns: language js `console.error($_)` => . -pattern_files: - - docs/guides/version_5_upgrade.md - - docs/guides/something.md diff --git a/crates/gritmodule/src/config.rs b/crates/gritmodule/src/config.rs index 62dcb6564..03735b8c7 100644 --- a/crates/gritmodule/src/config.rs +++ b/crates/gritmodule/src/config.rs @@ -32,17 +32,31 @@ pub struct GritGitHubConfig { pub reviewers: Vec, } +/// Represents a reference to an external pattern file #[derive(Debug, Deserialize)] +pub struct GritPatternFile { + pub file: PathBuf, +} + +/// Pure in-memory representation of the grit config +#[derive(Debug)] pub struct GritConfig { pub patterns: Vec, - pub pattern_files: Option>, + pub pattern_files: Option>, pub github: Option, } +#[derive(Debug, Deserialize)] +#[serde(untagged)] +pub enum GritPatternConfig { + File(GritPatternFile), + Pattern(GritSerializedDefinitionConfig), +} + +/// Compacted / serialized version of the GritConfig #[derive(Debug, Deserialize)] pub struct SerializedGritConfig { - pub patterns: Vec, - pub pattern_files: Option>, + pub patterns: Vec, pub github: Option, } diff --git a/crates/gritmodule/src/snapshots/marzano_gritmodule__resolver__tests__finds_patterns_from_custom_pattern_files.snap b/crates/gritmodule/src/snapshots/marzano_gritmodule__resolver__tests__finds_patterns_from_custom_pattern_files.snap index d656f9e97..1ac553545 100644 --- a/crates/gritmodule/src/snapshots/marzano_gritmodule__resolver__tests__finds_patterns_from_custom_pattern_files.snap +++ b/crates/gritmodule/src/snapshots/marzano_gritmodule__resolver__tests__finds_patterns_from_custom_pattern_files.snap @@ -12,7 +12,7 @@ expression: resolved_patterns samples: ~ path: ".grit/grit.yaml" position: - line: 3 + line: 5 column: 11 raw: ~ module: @@ -136,7 +136,7 @@ expression: resolved_patterns startByte: 1220 endByte: 1262 output_range: ~ - path: fixtures/pattern_files/docs/guides/something.md + path: ".grit/../docs/guides/something.md" position: line: 10 column: 1 @@ -264,7 +264,7 @@ expression: resolved_patterns startByte: 1220 endByte: 1262 output_range: ~ - path: fixtures/pattern_files/docs/guides/version_5_upgrade.md + path: ".grit/../docs/guides/version_5_upgrade.md" position: line: 10 column: 1 diff --git a/crates/gritmodule/src/yaml.rs b/crates/gritmodule/src/yaml.rs index b93260af1..eddf0dcdf 100644 --- a/crates/gritmodule/src/yaml.rs +++ b/crates/gritmodule/src/yaml.rs @@ -28,14 +28,31 @@ pub fn get_grit_config(source: &str, source_path: &str) -> Result { } }; + let mut patterns = Vec::new(); + let mut pattern_files = Vec::new(); + + for pattern in serialized.patterns.into_iter() { + match pattern { + crate::config::GritPatternConfig::File(file) => { + pattern_files.push(file); + } + crate::config::GritPatternConfig::Pattern(p) => { + patterns.push(GritDefinitionConfig::from_serialized( + p, + source_path.to_string(), + )); + } + } + } + let new_config = GritConfig { github: serialized.github, - pattern_files: serialized.pattern_files, - patterns: serialized - .patterns - .into_iter() - .map(|p| GritDefinitionConfig::from_serialized(p, source_path.to_string())) - .collect(), + pattern_files: if pattern_files.is_empty() { + None + } else { + Some(pattern_files) + }, + patterns, }; Ok(new_config) @@ -47,7 +64,8 @@ pub async fn get_patterns_from_yaml( root: &Option, repo_dir: &str, ) -> Result> { - let mut config = get_grit_config(&file.content, &extract_relative_file_path(file, root))?; + let grit_path = extract_relative_file_path(file, root); + let mut config = get_grit_config(&file.content, &grit_path)?; for pattern in config.patterns.iter_mut() { pattern.kind = Some(DefinitionKind::Pattern); @@ -69,7 +87,9 @@ pub async fn get_patterns_from_yaml( let mut file_readers = Vec::new(); for pattern_file in config.pattern_files.unwrap() { - let pattern_file = PathBuf::from(repo_dir).join(&pattern_file); + let pattern_file = PathBuf::from(repo_dir) + .join(REPO_CONFIG_DIR_NAME) + .join(&pattern_file.file); let extension = PatternFileExt::from_path(&pattern_file); if extension.is_none() { continue;