From 833eb1fa483ac631685ec83e5fbcd0d8d082163a Mon Sep 17 00:00:00 2001 From: Victorien Elvinger Date: Sun, 18 Aug 2024 15:18:55 +0200 Subject: [PATCH] fix(configuration): merge inherited overrides (#3671) --- CHANGELOG.md | 60 +++++++++++- .../biome_cli/tests/cases/config_extends.rs | 46 ++++++++++ .../tests/commands/migrate_eslint.rs | 38 ++++++++ .../extends_config_merge_overrides.snap | 70 ++++++++++++++ .../migrate_merge_with_overrides.snap | 62 +++++++++++++ crates/biome_deserialize/src/merge.rs | 92 ++++++++++++++++--- .../src/merge_derive.rs | 2 +- 7 files changed, 356 insertions(+), 14 deletions(-) create mode 100644 crates/biome_cli/tests/snapshots/main_cases_config_extends/extends_config_merge_overrides.snap create mode 100644 crates/biome_cli/tests/snapshots/main_commands_migrate_eslint/migrate_merge_with_overrides.snap diff --git a/CHANGELOG.md b/CHANGELOG.md index 1cd671096ff3..3f929c3e2363 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -86,13 +86,19 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b #### Bug fixes - `biome lint --write` now takes `--only` and `--skip` into account ([#3470](https://github.com/biomejs/biome/issues/3470)). Contributed by @Conaclos + - Fix [#3368](https://github.com/biomejs/biome/issues/3368), now the reporter `github` tracks the diagnostics that belong to formatting and organize imports. Contributed by @ematipico + - Fix [#3545](https://github.com/biomejs/biome/issues/3545), display a warning, 'Avoid using unnecessary Fragment,' when a Fragment contains only one child element that is placed on a new line. Contributed by @satojin219 +- Migrating from Prettier or ESLint no longer overwrite the `overrides` field from the configuration ([#3544](https://github.com/biomejs/biome/issues/3544)). Contributed by @Conaclos + ### Configuration -- Add support for loading configuration from `.editorconfig` files ([#1724](https://github.com/biomejs/biome/issues/1724)). Contributed by @dyc3 +- Add support for loading configuration from `.editorconfig` files ([#1724](https://github.com/biomejs/biome/issues/1724)). + Configuration supplied in `.editorconfig` will be overridden by the configuration in `biome.json`. Support is disabled by default and can be enabled by adding the following to your formatter configuration in `biome.json`: + ```json { "formatter": { @@ -101,6 +107,58 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b } ``` + Contributed by @dyc3 + +- `overrides` from an extended configuration is now merged with the `overrides` of the extension. + + Given the following shared configuration `biome.shared.json`: + + ```json5 + { + "overrides": [ + { + "include": ["**/*.json"], + // ... + } + ] + } + ``` + + and the following configuration: + + ```json5 + { + "extends": ["./biome.shared.json"], + "overrides": [ + { + "include": ["**/*.ts"], + // ... + } + ] + } + ``` + + Previously, the `overrides` from `biome.shared.json` was overwritten. + It is now merged and results in the following configuration: + + ```json5 + { + "extends": ["./biome.shared.json"], + "overrides": [ + { + "include": ["**/*.json"], + // ... + }, + { + "include": ["**/*.ts"], + // ... + } + ] + } + ``` + + Contributed by @Conaclos + ### Editors #### Bug fixes diff --git a/crates/biome_cli/tests/cases/config_extends.rs b/crates/biome_cli/tests/cases/config_extends.rs index 0859b8c84cce..e784e5177b20 100644 --- a/crates/biome_cli/tests/cases/config_extends.rs +++ b/crates/biome_cli/tests/cases/config_extends.rs @@ -358,3 +358,49 @@ fn allows_reverting_fields_in_extended_config_to_default() { result, )); } + +#[test] +fn extends_config_merge_overrides() { + let mut fs = MemoryFileSystem::default(); + let mut console = BufferConsole::default(); + + let shared = Path::new("shared.json"); + fs.insert( + shared.into(), + r#"{ + "overrides": [{ + "include": ["**/*.js"], + "linter": { "rules": { "suspicious": { "noDebugger": "off" } } } + }] + }"#, + ); + + let biome_json = Path::new("biome.json"); + fs.insert( + biome_json.into(), + r#"{ + "extends": ["shared.json"], + "overrides": [{ + "include": ["**/*.js"], + "linter": { "rules": { "correctness": { "noUnusedVariables": "error" } } } + }] + }"#, + ); + + let test_file = Path::new("test.js"); + fs.insert(test_file.into(), "debugger; const a = 0;"); + + let result = run_cli( + DynRef::Borrowed(&mut fs), + &mut console, + Args::from(["lint", test_file.as_os_str().to_str().unwrap()].as_slice()), + ); + + assert_cli_snapshot(SnapshotPayload::new( + module_path!(), + "extends_config_merge_overrides", + fs, + console, + result, + )); +} diff --git a/crates/biome_cli/tests/commands/migrate_eslint.rs b/crates/biome_cli/tests/commands/migrate_eslint.rs index 1127e06c8928..0554cdf4f4e6 100644 --- a/crates/biome_cli/tests/commands/migrate_eslint.rs +++ b/crates/biome_cli/tests/commands/migrate_eslint.rs @@ -665,3 +665,41 @@ fn migrate_eslintrcjson_extended_rules() { result, )); } + +#[test] +fn migrate_merge_with_overrides() { + let biomejson = r#"{ + "overrides": [{ + "include": ["*.js"], + "linter": { "enabled": false } + }] + }"#; + let eslintrc = r#"{ + "overrides": [{ + "files": ["bin/*.js", "lib/*.js"], + "excludedFiles": "*.test.js", + "rules": { + "eqeqeq": ["off"] + } + }] + }"#; + + let mut fs = MemoryFileSystem::default(); + fs.insert(Path::new("biome.json").into(), biomejson.as_bytes()); + fs.insert(Path::new(".eslintrc.json").into(), eslintrc.as_bytes()); + + let mut console = BufferConsole::default(); + let result = run_cli( + DynRef::Borrowed(&mut fs), + &mut console, + Args::from(["migrate", "eslint"].as_slice()), + ); + + assert_cli_snapshot(SnapshotPayload::new( + module_path!(), + "migrate_merge_with_overrides", + fs, + console, + result, + )); +} diff --git a/crates/biome_cli/tests/snapshots/main_cases_config_extends/extends_config_merge_overrides.snap b/crates/biome_cli/tests/snapshots/main_cases_config_extends/extends_config_merge_overrides.snap new file mode 100644 index 000000000000..e99dfea4254d --- /dev/null +++ b/crates/biome_cli/tests/snapshots/main_cases_config_extends/extends_config_merge_overrides.snap @@ -0,0 +1,70 @@ +--- +source: crates/biome_cli/tests/snap_test.rs +expression: content +--- +## `biome.json` + +```json +{ + "extends": ["shared.json"], + "overrides": [ + { + "include": ["**/*.js"], + "linter": { "rules": { "correctness": { "noUnusedVariables": "error" } } } + } + ] +} +``` + +## `shared.json` + +```json +{ + "overrides": [{ + "include": ["**/*.js"], + "linter": { "rules": { "suspicious": { "noDebugger": "off" } } } + }] + } +``` + +## `test.js` + +```js +debugger; const a = 0; +``` + +# Termination Message + +```block +lint ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + × Some errors were emitted while running checks. + + + +``` + +# Emitted Messages + +```block +test.js:1:17 lint/correctness/noUnusedVariables FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + × This variable is unused. + + > 1 │ debugger; const a = 0; + │ ^ + + i Unused variables usually are result of incomplete refactoring, typos and other source of bugs. + + i Unsafe fix: If this is intentional, prepend a with an underscore. + + - debugger;·const·a·=·0; + + debugger;·const·_a·=·0; + + +``` + +```block +Checked 1 file in