Skip to content

Commit 8a4f4fc

Browse files
authored
chore: expose preferred indentation (#7540)
1 parent 3f06e19 commit 8a4f4fc

File tree

14 files changed

+97
-40
lines changed

14 files changed

+97
-40
lines changed

crates/biome_analyze/src/context.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::options::{JsxRuntime, PreferredQuote};
1+
use crate::options::{JsxRuntime, PreferredIndentation, PreferredQuote};
22
use crate::{FromServices, Queryable, Rule, RuleKey, ServiceBag, registry::RuleRoot};
33
use crate::{GroupCategory, RuleCategory, RuleGroup, RuleMetadata};
44
use biome_diagnostics::{Error, Result};
@@ -16,8 +16,9 @@ pub struct RuleContext<'a, R: Rule> {
1616
globals: &'a [&'a str],
1717
file_path: &'a Utf8Path,
1818
options: &'a R::Options,
19-
preferred_quote: &'a PreferredQuote,
20-
preferred_jsx_quote: &'a PreferredQuote,
19+
preferred_quote: PreferredQuote,
20+
preferred_jsx_quote: PreferredQuote,
21+
preferred_indentation: PreferredIndentation,
2122
jsx_runtime: Option<JsxRuntime>,
2223
css_modules: bool,
2324
}
@@ -34,8 +35,9 @@ where
3435
globals: &'a [&'a str],
3536
file_path: &'a Utf8Path,
3637
options: &'a R::Options,
37-
preferred_quote: &'a PreferredQuote,
38-
preferred_jsx_quote: &'a PreferredQuote,
38+
preferred_quote: PreferredQuote,
39+
preferred_jsx_quote: PreferredQuote,
40+
preferred_indentation: PreferredIndentation,
3941
jsx_runtime: Option<JsxRuntime>,
4042
css_modules: bool,
4143
) -> Result<Self, Error> {
@@ -50,6 +52,7 @@ where
5052
options,
5153
preferred_quote,
5254
preferred_jsx_quote,
55+
preferred_indentation,
5356
jsx_runtime,
5457
css_modules,
5558
})
@@ -170,15 +173,20 @@ where
170173
}
171174

172175
/// Returns the preferred quote that should be used when providing code actions
173-
pub fn as_preferred_quote(&self) -> &PreferredQuote {
176+
pub fn preferred_quote(&self) -> PreferredQuote {
174177
self.preferred_quote
175178
}
176179

177180
/// Returns the preferred JSX quote that should be used when providing code actions
178-
pub fn as_preferred_jsx_quote(&self) -> &PreferredQuote {
181+
pub fn preferred_jsx_quote(&self) -> PreferredQuote {
179182
self.preferred_jsx_quote
180183
}
181184

185+
/// Returns the preferred indentation style that should be when providing code actions.
186+
pub fn preferred_indentation(&self) -> PreferredIndentation {
187+
self.preferred_indentation
188+
}
189+
182190
pub fn is_css_modules(&self) -> bool {
183191
self.css_modules
184192
}

crates/biome_analyze/src/options.rs

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use rustc_hash::FxHashMap;
33

44
use crate::{FixKind, Rule, RuleKey};
55
use std::any::{Any, TypeId};
6+
use std::borrow::Cow;
67
use std::sync::Arc;
78

89
/// A convenient new type data structure to store the options that belong to a rule
@@ -68,6 +69,9 @@ pub struct AnalyzerConfiguration {
6869
/// Allows choosing a different JSX quote when applying fixes inside the lint rules
6970
pub preferred_jsx_quote: PreferredQuote,
7071

72+
/// Allows applying the right indentation in fix suggestions.
73+
preferred_indentation: PreferredIndentation,
74+
7175
/// Indicates the type of runtime or transformation used for interpreting JSX.
7276
jsx_runtime: Option<JsxRuntime>,
7377

@@ -101,6 +105,14 @@ impl AnalyzerConfiguration {
101105
self
102106
}
103107

108+
pub fn with_preferred_indentation(
109+
mut self,
110+
preferred_indentation: PreferredIndentation,
111+
) -> Self {
112+
self.preferred_indentation = preferred_indentation;
113+
self
114+
}
115+
104116
pub fn with_css_modules(mut self, css_modules: bool) -> Self {
105117
self.css_modules = css_modules;
106118
self
@@ -171,20 +183,45 @@ impl AnalyzerOptions {
171183
.get_rule_fix_kind(&RuleKey::rule::<R>())
172184
}
173185

174-
pub fn preferred_quote(&self) -> &PreferredQuote {
175-
&self.configuration.preferred_quote
186+
pub fn preferred_quote(&self) -> PreferredQuote {
187+
self.configuration.preferred_quote
176188
}
177189

178-
pub fn preferred_jsx_quote(&self) -> &PreferredQuote {
179-
&self.configuration.preferred_jsx_quote
190+
pub fn preferred_jsx_quote(&self) -> PreferredQuote {
191+
self.configuration.preferred_jsx_quote
192+
}
193+
194+
pub fn preferred_indentation(&self) -> PreferredIndentation {
195+
self.configuration.preferred_indentation
180196
}
181197

182198
pub fn css_modules(&self) -> bool {
183199
self.configuration.css_modules
184200
}
185201
}
186202

187-
#[derive(Debug, Default)]
203+
#[derive(Clone, Copy, Debug, Default)]
204+
pub enum PreferredIndentation {
205+
/// Use tabs for indentation.
206+
#[default]
207+
Tab,
208+
/// Use the given amount of spaces for indentation.
209+
Spaces(u8),
210+
}
211+
212+
impl PreferredIndentation {
213+
/// Returns the indentation in its string form.
214+
pub fn to_string(self) -> Cow<'static, str> {
215+
match self {
216+
Self::Tab => Cow::Borrowed("\t"),
217+
Self::Spaces(tab_width) => {
218+
Cow::Owned(std::iter::repeat_n(' ', tab_width as usize).collect())
219+
}
220+
}
221+
}
222+
}
223+
224+
#[derive(Clone, Copy, Debug, Default)]
188225
pub enum PreferredQuote {
189226
/// Double quotes
190227
#[default]

crates/biome_analyze/src/registry.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,7 @@ impl<L: Language + Default> RegistryRule<L> {
404404
let globals = params.options.globals();
405405
let preferred_quote = params.options.preferred_quote();
406406
let preferred_jsx_quote = params.options.preferred_jsx_quote();
407+
let preferred_indentation = params.options.preferred_indentation();
407408
let jsx_runtime = params.options.jsx_runtime();
408409
let css_modules = params.options.css_modules();
409410
let options = params.options.rule_options::<R>().unwrap_or_default();
@@ -416,6 +417,7 @@ impl<L: Language + Default> RegistryRule<L> {
416417
&options,
417418
preferred_quote,
418419
preferred_jsx_quote,
420+
preferred_indentation,
419421
jsx_runtime,
420422
css_modules,
421423
)?;

crates/biome_analyze/src/signals.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ where
357357
let globals = self.options.globals();
358358
let preferred_quote = self.options.preferred_quote();
359359
let preferred_jsx_quote = self.options.preferred_jsx_quote();
360+
let preferred_indentation = self.options.preferred_indentation();
360361
let options = self.options.rule_options::<R>().unwrap_or_default();
361362
let ctx = RuleContext::new(
362363
&self.query_result,
@@ -367,6 +368,7 @@ where
367368
&options,
368369
preferred_quote,
369370
preferred_jsx_quote,
371+
preferred_indentation,
370372
self.options.jsx_runtime(),
371373
self.options.css_modules(),
372374
)
@@ -403,6 +405,7 @@ where
403405
&options,
404406
self.options.preferred_quote(),
405407
self.options.preferred_jsx_quote(),
408+
self.options.preferred_indentation(),
406409
self.options.jsx_runtime(),
407410
self.options.css_modules(),
408411
)
@@ -467,6 +470,7 @@ where
467470
&options,
468471
self.options.preferred_quote(),
469472
self.options.preferred_jsx_quote(),
473+
self.options.preferred_indentation(),
470474
self.options.jsx_runtime(),
471475
self.options.css_modules(),
472476
)

crates/biome_configuration/src/analyzer/linter/mod.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,3 @@ impl LinterConfiguration {
9292
self.rules.clone().unwrap_or_default()
9393
}
9494
}
95-
96-
impl Rules {
97-
pub fn get_rule(&self) {}
98-
}

crates/biome_js_analyze/src/lint/complexity/use_literal_keys.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ impl Rule for UseLiteralKeys {
174174
token.clone()
175175
} else {
176176
let identifier = inner_string_text(token);
177-
if ctx.as_preferred_quote().is_double() {
177+
if ctx.preferred_quote().is_double() {
178178
make::js_string_literal(&identifier)
179179
} else {
180180
make::js_string_literal_single_quotes(&identifier)

crates/biome_js_analyze/src/lint/correctness/no_string_case_mismatch.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ impl Rule for NoStringCaseMismatch {
102102
state.literal.clone(),
103103
AnyJsExpression::AnyJsLiteralExpression(
104104
AnyJsLiteralExpression::JsStringLiteralExpression(
105-
make::js_string_literal_expression(if ctx.as_preferred_quote().is_double() {
105+
make::js_string_literal_expression(if ctx.preferred_quote().is_double() {
106106
make::js_string_literal(&expected_value)
107107
} else {
108108
make::js_string_literal_single_quotes(&expected_value)

crates/biome_js_analyze/src/lint/correctness/use_import_extensions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ impl Rule for UseImportExtensions {
169169
let mut mutation = ctx.root().begin();
170170

171171
let (suggested_path, extension) = state.suggestion.clone()?;
172-
let new_module_name = if ctx.as_preferred_quote().is_double() {
172+
let new_module_name = if ctx.preferred_quote().is_double() {
173173
make::js_string_literal(&suggested_path)
174174
} else {
175175
make::js_string_literal_single_quotes(&suggested_path)

crates/biome_js_analyze/src/lint/correctness/use_valid_typeof.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ impl Rule for UseValidTypeof {
191191
mutation.replace_node(
192192
other.clone(),
193193
AnyJsExpression::AnyJsLiteralExpression(AnyJsLiteralExpression::from(
194-
make::js_string_literal_expression(if ctx.as_preferred_quote().is_double() {
194+
make::js_string_literal_expression(if ctx.preferred_quote().is_double() {
195195
make::js_string_literal(suggestion)
196196
} else {
197197
make::js_string_literal_single_quotes(suggestion)

crates/biome_js_analyze/src/lint/nursery/use_sorted_classes.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ impl Rule for UseSortedClasses {
214214
let is_double_quote = string_literal
215215
.value_token()
216216
.map(|token| token.text_trimmed().starts_with('"'))
217-
.unwrap_or(ctx.as_preferred_quote().is_double());
217+
.unwrap_or(ctx.preferred_quote().is_double());
218218
let replacement = js_string_literal_expression(if is_double_quote {
219219
js_string_literal(state)
220220
} else {
@@ -223,7 +223,7 @@ impl Rule for UseSortedClasses {
223223
mutation.replace_node(string_literal.clone(), replacement);
224224
}
225225
AnyClassStringLike::JsLiteralMemberName(string_literal) => {
226-
let replacement = js_literal_member_name(if ctx.as_preferred_quote().is_double() {
226+
let replacement = js_literal_member_name(if ctx.preferred_quote().is_double() {
227227
js_string_literal(state)
228228
} else {
229229
js_string_literal_single_quotes(state)
@@ -234,7 +234,7 @@ impl Rule for UseSortedClasses {
234234
let is_double_quote = jsx_string_node
235235
.value_token()
236236
.map(|token| token.text_trimmed().starts_with('"'))
237-
.unwrap_or(ctx.as_preferred_jsx_quote().is_double());
237+
.unwrap_or(ctx.preferred_jsx_quote().is_double());
238238
let replacement = jsx_string(if is_double_quote {
239239
js_string_literal(state)
240240
} else {

0 commit comments

Comments
 (0)