diff --git a/crates/biome_service/src/configuration/mod.rs b/crates/biome_service/src/configuration/mod.rs index a388fbe0aa6e..4d0c22713568 100644 --- a/crates/biome_service/src/configuration/mod.rs +++ b/crates/biome_service/src/configuration/mod.rs @@ -584,7 +584,7 @@ pub fn to_analyzer_rules(settings: &WorkspaceSettings, path: &Path) -> AnalyzerR let overrides = &settings.override_settings; overrides - .to_analyzer_rules_options(path) + .as_analyzer_rules_options(path) .unwrap_or_else(|| { let mut analyzer_rules = AnalyzerRules::default(); if let Some(rules) = linter_settings.rules.as_ref() { diff --git a/crates/biome_service/src/file_handlers/javascript.rs b/crates/biome_service/src/file_handlers/javascript.rs index b5d2d21d1e32..0d5f0479acec 100644 --- a/crates/biome_service/src/file_handlers/javascript.rs +++ b/crates/biome_service/src/file_handlers/javascript.rs @@ -99,7 +99,7 @@ impl Language for JsLanguage { language: &JsFormatterSettings, path: &RomePath, ) -> JsFormatOptions { - overrides.js_format_options(path).unwrap_or_else(|| { + overrides.as_js_format_options(path).unwrap_or_else(|| { let indent_style = if let Some(indent_style) = language.indent_style { indent_style } else { @@ -195,10 +195,13 @@ fn parse( LanguageId::TypeScriptReact => JsFileSource::tsx(), _ => JsFileSource::js_module(), }); - let settings = &settings.as_ref().languages.javascript.parser; - let options = JsParserOptions { - parse_class_parameter_decorators: settings.parse_class_parameter_decorators, - }; + let parser_settings = &settings.as_ref().languages.javascript.parser; + let overrides = &settings.as_ref().override_settings; + let options = overrides + .as_js_parser_options(&rome_path) + .unwrap_or(JsParserOptions { + parse_class_parameter_decorators: parser_settings.parse_class_parameter_decorators, + }); let parse = biome_js_parser::parse_js_with_cache(text, source_type, options, cache); let root = parse.syntax(); let diagnostics = parse.into_diagnostics(); diff --git a/crates/biome_service/src/file_handlers/json.rs b/crates/biome_service/src/file_handlers/json.rs index 27db404ff069..a7fcb342fd4d 100644 --- a/crates/biome_service/src/file_handlers/json.rs +++ b/crates/biome_service/src/file_handlers/json.rs @@ -54,7 +54,7 @@ impl Language for JsonLanguage { language: &Self::FormatterSettings, path: &RomePath, ) -> Self::FormatOptions { - overrides.json_format_options(path).unwrap_or_else(|| { + overrides.as_json_format_options(path).unwrap_or_else(|| { let indent_style = if let Some(indent_style) = language.indent_style { indent_style } else { @@ -134,18 +134,22 @@ fn parse( cache: &mut NodeCache, ) -> AnyParse { let parser = &settings.as_ref().languages.json.parser; + let overrides = &settings.as_ref().override_settings; let source_type = JsonFileSource::try_from(rome_path.as_path()).unwrap_or_else(|_| match language_hint { LanguageId::Json => JsonFileSource::json(), LanguageId::Jsonc => JsonFileSource::jsonc(), _ => JsonFileSource::json(), }); - let options: JsonParserOptions = JsonParserOptions { - allow_comments: parser.allow_comments - || source_type.is_jsonc() - || is_file_allowed(rome_path), - allow_trailing_commas: parser.allow_trailing_commas || is_file_allowed(rome_path), - }; + let options: JsonParserOptions = + overrides + .as_json_parser_options(&rome_path) + .unwrap_or(JsonParserOptions { + allow_comments: parser.allow_comments + || source_type.is_jsonc() + || is_file_allowed(rome_path), + allow_trailing_commas: parser.allow_trailing_commas || is_file_allowed(rome_path), + }); let parse = biome_json_parser::parse_json_with_cache(text, cache, options); let root = parse.syntax(); let diagnostics = parse.into_diagnostics(); diff --git a/crates/biome_service/src/settings.rs b/crates/biome_service/src/settings.rs index 5d13c9e5eb58..d3533ff29c6b 100644 --- a/crates/biome_service/src/settings.rs +++ b/crates/biome_service/src/settings.rs @@ -11,8 +11,10 @@ use biome_formatter::{IndentStyle, IndentWidth, LineWidth}; use biome_fs::RomePath; use biome_js_analyze::metadata; use biome_js_formatter::context::JsFormatOptions; +use biome_js_parser::JsParserOptions; use biome_js_syntax::JsLanguage; use biome_json_formatter::context::JsonFormatOptions; +use biome_json_parser::JsonParserOptions; use biome_json_syntax::JsonLanguage; use indexmap::IndexSet; use std::path::Path; @@ -458,7 +460,7 @@ impl OverrideSettings { } /// It scans the current override rules and return the formatting options that of the first override is matched - pub fn js_format_options(&self, path: &Path) -> Option { + pub fn as_js_format_options(&self, path: &Path) -> Option { for pattern in &self.patterns { let included = pattern.include.as_ref().map(|p| p.matches_path(path)); let excluded = pattern.exclude.as_ref().map(|p| p.matches_path(path)); @@ -499,7 +501,7 @@ impl OverrideSettings { } /// It scans the current override rules and return the formatting options that of the first override is matched - pub fn json_format_options(&self, path: &Path) -> Option { + pub fn as_json_format_options(&self, path: &Path) -> Option { for pattern in &self.patterns { let included = pattern.include.as_ref().map(|p| p.matches_path(path)); let excluded = pattern.exclude.as_ref().map(|p| p.matches_path(path)); @@ -532,8 +534,43 @@ impl OverrideSettings { None } + pub fn as_js_parser_options(&self, path: &Path) -> Option { + for pattern in &self.patterns { + let included = pattern.include.as_ref().map(|p| p.matches_path(path)); + let excluded = pattern.exclude.as_ref().map(|p| p.matches_path(path)); + + if included == Some(true) || excluded == Some(false) { + let js_parser = &pattern.languages.javascript.parser; + + return Some(JsParserOptions { + parse_class_parameter_decorators: js_parser.parse_class_parameter_decorators, + }); + } + } + + None + } + + pub fn as_json_parser_options(&self, path: &Path) -> Option { + for pattern in &self.patterns { + let included = pattern.include.as_ref().map(|p| p.matches_path(path)); + let excluded = pattern.exclude.as_ref().map(|p| p.matches_path(path)); + + if included == Some(true) || excluded == Some(false) { + let json_parser = &pattern.languages.json.parser; + + return Some(JsonParserOptions { + allow_comments: json_parser.allow_comments, + allow_trailing_commas: json_parser.allow_trailing_commas, + }); + } + } + + None + } + /// Retrieves the options of lint rules that have been overridden - pub fn to_analyzer_rules_options(&self, path: &Path) -> Option { + pub fn as_analyzer_rules_options(&self, path: &Path) -> Option { for pattern in &self.patterns { let included = pattern.include.as_ref().map(|p| p.matches_path(path)); let excluded = pattern.exclude.as_ref().map(|p| p.matches_path(path)); diff --git a/website/src/content/docs/reference/configuration.mdx b/website/src/content/docs/reference/configuration.mdx index 6e6fe755f524..fee7f538ee4e 100644 --- a/website/src/content/docs/reference/configuration.mdx +++ b/website/src/content/docs/reference/configuration.mdx @@ -785,10 +785,69 @@ A list of Unix shell style patterns. Biome handles only the files and folders th It will include the options of [top level formatter](#formatter) configuration, minus `ignore` and `include`. +#### Examples + +For example, it's possible to modify the formatter `lineWidth`, `indentStyle` for certain files that are included in the glob path `generated/**`: + + + +```json +{ + "formatter": { + "lineWidth": 100 + }, + "overrides": [ + { + "include": ["generated/**"], + "formatter": { + "lineWidth": 160, + "indentStyle": "space" + } + } + ] +} +``` + ### `overrides..linter` It will include the options of [top level linter](#linter) configuration, minus `ignore` and `include`. + +#### Examples + +You can disable certain rules for certain glob paths, and disable the linter for other glob paths: + + + +```json +{ + "linter": { + "enabled": true, + "rules": { + "recommended": true + } + }, + "overrides": [ + { + "include": ["lib/**"], + "linter": { + "rules": { + "suspicious": { + "noDebugger": "off" + } + } + } + }, + { + "include": ["shims/**"], + "linter": { + "enabled": false + } + } + ] +} +``` + ### `overrides..organizeImports` It will include the options of [top level organize imports](#organize_imports), minus `ignore` and `include`. @@ -797,6 +856,65 @@ It will include the options of [top level organize imports](#organize_imports), It will include the options of [top level javascript](#javascript) configuration. +#### Examples + +You can change the formatting behaviour of JavaScript files in certain folders: + + + +```json +{ + "formatter": { + "lineWidth": 120 + }, + "javascript": { + "formatter": { + "quoteStyle": "single" + } + }, + "overrides": [ + { + "include": ["lib/**"], + "javascript": { + "formatter": { + "quoteStyle": "double" + } + } + } + ] +} +``` + + ### `overrides..json` -It will include the options of [top level formatter](#json) configuration. +It will include the options of [top level json](#json) configuration. + + +#### Examples + +You can enable parsing features for certain JSON files: + + + +```json +{ + "linter": { + "enabled": true, + "rules": { + "recommended": true + } + }, + "overrides": [ + { + "include": [".vscode/**"], + "json": { + "parser": { + "allowComments": true, + "allowTrailingComma": true + } + } + } + ] +} +```