Skip to content

Commit

Permalink
chore: add examples and more overrides stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed Oct 19, 2023
1 parent 9f5ec1c commit be96785
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 17 deletions.
2 changes: 1 addition & 1 deletion crates/biome_service/src/configuration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
13 changes: 8 additions & 5 deletions crates/biome_service/src/file_handlers/javascript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand Down
18 changes: 11 additions & 7 deletions crates/biome_service/src/file_handlers/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand Down
43 changes: 40 additions & 3 deletions crates/biome_service/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<JsFormatOptions> {
pub fn as_js_format_options(&self, path: &Path) -> Option<JsFormatOptions> {
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));
Expand Down Expand Up @@ -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<JsonFormatOptions> {
pub fn as_json_format_options(&self, path: &Path) -> Option<JsonFormatOptions> {
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));
Expand Down Expand Up @@ -532,8 +534,43 @@ impl OverrideSettings {
None
}

pub fn as_js_parser_options(&self, path: &Path) -> Option<JsParserOptions> {
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<JsonParserOptions> {
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<AnalyzerRules> {
pub fn as_analyzer_rules_options(&self, path: &Path) -> Option<AnalyzerRules> {
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));
Expand Down
120 changes: 119 additions & 1 deletion website/src/content/docs/reference/configuration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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/**`:

<CodeBlockHeader filename="biome.json" />

```json
{
"formatter": {
"lineWidth": 100
},
"overrides": [
{
"include": ["generated/**"],
"formatter": {
"lineWidth": 160,
"indentStyle": "space"
}
}
]
}
```

### `overrides.<ITEM>.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:

<CodeBlockHeader filename="biome.json" />

```json
{
"linter": {
"enabled": true,
"rules": {
"recommended": true
}
},
"overrides": [
{
"include": ["lib/**"],
"linter": {
"rules": {
"suspicious": {
"noDebugger": "off"
}
}
}
},
{
"include": ["shims/**"],
"linter": {
"enabled": false
}
}
]
}
```

### `overrides.<ITEM>.organizeImports`

It will include the options of [top level organize imports](#organize_imports), minus `ignore` and `include`.
Expand All @@ -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:

<CodeBlockHeader filename="biome.json" />

```json
{
"formatter": {
"lineWidth": 120
},
"javascript": {
"formatter": {
"quoteStyle": "single"
}
},
"overrides": [
{
"include": ["lib/**"],
"javascript": {
"formatter": {
"quoteStyle": "double"
}
}
}
]
}
```


### `overrides.<ITEM>.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:

<CodeBlockHeader filename="biome.json" />

```json
{
"linter": {
"enabled": true,
"rules": {
"recommended": true
}
},
"overrides": [
{
"include": [".vscode/**"],
"json": {
"parser": {
"allowComments": true,
"allowTrailingComma": true
}
}
}
]
}
```

0 comments on commit be96785

Please sign in to comment.