Skip to content

Commit

Permalink
refactor(migrate): use includes when migrating from eslint and pretti…
Browse files Browse the repository at this point in the history
…er (#5012)
  • Loading branch information
Conaclos authored Feb 2, 2025
1 parent 99f27a2 commit 703bff0
Show file tree
Hide file tree
Showing 26 changed files with 296 additions and 283 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/biome_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ biome_diagnostics = { workspace = true, features = ["std", "bpaf"] }
biome_flags = { workspace = true }
biome_formatter = { workspace = true }
biome_fs = { workspace = true }
biome_glob = { workspace = true }
biome_grit_patterns = { workspace = true }
biome_js_analyze = { workspace = true }
biome_js_formatter = { workspace = true }
Expand Down
16 changes: 4 additions & 12 deletions crates/biome_cli/src/execute/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,11 @@ pub(crate) fn run(migrate_payload: MigratePayload) -> Result<(), CliDiagnostic>
biome_config
.formatter
.get_or_insert(Default::default())
.ignore
.includes
.get_or_insert(Default::default())
.extend(ignore_patterns.patterns);
}
if ignore_patterns.has_negated_patterns {
console.log(markup! {
<Warn><Emphasis>{prettier::IGNORE_FILE}</Emphasis>" contains negated glob patterns that start with "<Emphasis>"!"</Emphasis>".\nThese patterns cannot be migrated because Biome doesn't support them."</Warn>
})
} else if write && biome_config != old_biome_config {
if write && biome_config != old_biome_config {
console.log(markup!{
<Info><Emphasis>{prettier::IGNORE_FILE}</Emphasis>" has been successfully migrated."</Info>
});
Expand Down Expand Up @@ -191,15 +187,11 @@ pub(crate) fn run(migrate_payload: MigratePayload) -> Result<(), CliDiagnostic>
biome_config
.linter
.get_or_insert(Default::default())
.ignore
.includes
.get_or_insert(Default::default())
.extend(ignore_patterns.patterns);
}
if ignore_patterns.has_negated_patterns {
console.log(markup! {
<Warn><Emphasis>{eslint::IGNORE_FILE}</Emphasis>" contains negated glob patterns that start with "<Emphasis>"!"</Emphasis>".\nThese patterns cannot be migrated because Biome doesn't support them."</Warn>
})
} else if write && biome_config != old_biome_config {
if write && biome_config != old_biome_config {
console.log(markup!{
<Info><Emphasis>{eslint::IGNORE_FILE}</Emphasis>" has been successfully migrated."</Info>
});
Expand Down
13 changes: 6 additions & 7 deletions crates/biome_cli/src/execute/migrate/eslint_eslint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,20 +122,19 @@ impl Deref for IgnorePattern {
&self.0
}
}
impl AsRef<str> for IgnorePattern {
fn as_ref(&self) -> &str {
self.0.as_ref()
}
}
impl biome_deserialize::Deserializable for IgnorePattern {
fn deserialize(
ctx: &mut impl DeserializationContext,
value: &impl DeserializableValue,
name: &str,
) -> Option<Self> {
let s = biome_deserialize::Text::deserialize(ctx, value, name)?;
match ignorefile::convert_pattern(s.text()) {
Ok(pattern) => Some(Self(pattern.into_boxed_str())),
Err(msg) => {
ctx.report(DeserializationDiagnostic::new(msg).with_range(value.range()));
None
}
}
Some(Self(ignorefile::convert_pattern(s.text()).into_boxed_str()))
}
}

Expand Down
82 changes: 47 additions & 35 deletions crates/biome_cli/src/execute/migrate/eslint_to_biome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,9 @@ impl eslint_eslint::FlatConfigData {
};
override_pat.javascript = Some(js_config)
}
if !flat_config_object.ignores.is_empty() {
override_pat.ignore =
Some(flat_config_object.ignores.into_iter().collect());
}
if !flat_config_object.files.is_empty() {
override_pat.include = Some(flat_config_object.files.into_iter().collect());
}
let includes =
to_biome_includes(&flat_config_object.files, &flat_config_object.ignores);
override_pat.includes = (!includes.is_empty()).then_some(includes);
if let Some(rules) = flat_config_object.rules {
if !rules.is_empty() {
override_pat.linter = Some(biome_config::OverrideLinterConfiguration {
Expand Down Expand Up @@ -114,12 +110,9 @@ impl eslint_eslint::FlatConfigData {
}
rules.recommended = Some(false);
linter.rules = Some(rules);
if !global_config_object.ignores.is_empty() {
linter.ignore = Some(global_config_object.ignores.into_iter().collect());
}
if !global_config_object.files.is_empty() {
linter.include = Some(global_config_object.files.into_iter().collect());
}
let includes =
to_biome_includes(&global_config_object.files, &global_config_object.ignores);
linter.includes = (!includes.is_empty()).then_some(includes);
biome_config.linter = Some(linter);
(biome_config, results)
}
Expand All @@ -144,14 +137,8 @@ impl eslint_eslint::LegacyConfigData {
let mut rules = self.rules.into_biome_rules(options, &mut results);
rules.recommended = Some(false);
linter.rules = Some(rules);
if !self.ignore_patterns.is_empty() {
let ignore = self
.ignore_patterns
.into_iter()
.map(|p| p.0)
.collect::<Vec<_>>();
linter.ignore = Some(ignore);
}
let includes = to_biome_includes(&[] as &[&str], self.ignore_patterns.as_slice());
linter.includes = (!includes.is_empty()).then_some(includes);
if !self.overrides.is_empty() {
let mut overrides = biome_config::Overrides::default();
for override_elt in self.overrides {
Expand All @@ -167,13 +154,8 @@ impl eslint_eslint::LegacyConfigData {
};
override_pattern.javascript = Some(js_config)
}
if !override_elt.excluded_files.is_empty() {
override_pattern.ignore =
Some(override_elt.excluded_files.into_iter().collect());
}
if !override_elt.files.is_empty() {
override_pattern.include = Some(override_elt.files.into_iter().collect());
}
let includes = to_biome_includes(&override_elt.files, &override_elt.excluded_files);
override_pattern.includes = (!includes.is_empty()).then_some(includes);
if !override_elt.rules.is_empty() {
override_pattern.linter = Some(biome_config::OverrideLinterConfiguration {
rules: Some(override_elt.rules.into_biome_rules(options, &mut results)),
Expand Down Expand Up @@ -344,6 +326,32 @@ fn migrate_eslint_rule(
}
}

fn to_biome_includes(
files: &[impl AsRef<str>],
ignores: &[impl AsRef<str>],
) -> Vec<biome_glob::Glob> {
let mut includes = Vec::new();
if !files.is_empty() {
includes.extend(files.iter().filter_map(|glob| glob.as_ref().parse().ok()));
} else if let Ok(glob) = "**".parse() {
includes.push(glob);
}
if !ignores.is_empty() {
includes.extend(ignores.iter().filter_map(|glob| {
// ESLint supports negation: https://eslint.org/docs/latest/use/configure/ignore#unignoring-files-and-directories
if let Some(rest) = glob.as_ref().strip_prefix('!') {
rest.parse()
} else {
glob.as_ref()
.parse()
.map(|glob: biome_glob::Glob| glob.negated())
}
.ok()
}));
}
includes
}

#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -369,8 +377,10 @@ mod tests {
assert!(biome_config.formatter.is_none());
assert!(biome_config.assist.is_none());
let linter = biome_config.linter.unwrap();
assert_eq!(linter.include.unwrap(), ["*.js".into()],);
assert_eq!(linter.ignore.unwrap(), ["*.test.js".into()],);
assert_eq!(
linter.includes.unwrap(),
["*.js".parse().unwrap(), "!*.test.js".parse().unwrap()],
);
assert!(linter.rules.is_some());
}

Expand Down Expand Up @@ -416,10 +426,13 @@ mod tests {
assert!(biome_config.formatter.is_none());
assert!(biome_config.assist.is_none());
let linter = biome_config.linter.unwrap();
assert!(linter.include.is_none());
assert_eq!(
linter.ignore.unwrap(),
["*.test.js".into(), "*.spec.js".into()]
linter.includes.unwrap(),
[
"**".parse().unwrap(),
"!*.test.js".parse().unwrap(),
"!*.spec.js".parse().unwrap()
]
);
assert_eq!(
linter
Expand All @@ -436,8 +449,7 @@ mod tests {
let overrides = biome_config.overrides.unwrap();
assert_eq!(overrides.0.len(), 1);
let override0 = overrides.0.into_iter().next().unwrap();
assert_eq!(override0.include.unwrap(), ["*.ts".into()],);
assert!(override0.ignore.is_none());
assert_eq!(override0.includes.unwrap(), ["*.ts".parse().unwrap()],);
assert_eq!(
override0
.linter
Expand Down
Loading

0 comments on commit 703bff0

Please sign in to comment.