diff --git a/CHANGELOG.md b/CHANGELOG.md index 923a691bdd0f..02c4f29cc4d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,11 +40,23 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b - Add [nursery/noMissingVarFunction](https://biomejs.dev/linter/rules/no-missing-var-function). Contributed by @michellocana +#### Bug fixes + +- [useFilenamingConvention](https://biomejs.dev/linter/rules/use-filenaming-convention) no longer suggests names with a disallowed case ([#3952](https://github.com/biomejs/biome/issues/3952)). Contributed by @Conaclos + +- [useFilenamingConvention](https://biomejs.dev/linter/rules/use-filenaming-convention) now recognizes file names starting with ASCII digits as lowercase ([#3952](https://github.com/biomejs/biome/issues/3952)). + + Thus, `2024-09-17-filename`, `2024_09_17_filename` and `20240917FileName` are in `kebab-case`, `snake_case`, and `camelCase` respectively. + + Contributed by @Conaclos + +- [useFilenamingConvention](https://biomejs.dev/linter/rules/use-filenaming-convention) now applies the configured formats to the file extensions ([#3650](https://github.com/biomejs/biome/discussions/3650)). Contributed by @Conaclos + ### Parser #### Bug fixes -- [useStrictMode](https://biomejs.dev/linter/rules/use-strict-mode/) now reports Script files with dome diretcives, but without the `use strict` directive. Contributed by @Conaclos +- [useStrictMode](https://biomejs.dev/linter/rules/use-strict-mode/) now reports Script files with some directives, but without the `use strict` directive. Contributed by @Conaclos - The CSS parser now accepts the characters U+FFDCF and U+FFFD in identifiers. Contributed by @Conaclos @@ -70,14 +82,6 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b #### Bug fixes -- [useFilenamingConvention](https://biomejs.dev/linter/rules/use-filenaming-convention) no longer suggests names with a disallowed case ([#3952](https://github.com/biomejs/biome/issues/3952)). Contributed by @Conaclos - -- [useFilenamingConvention](https://biomejs.dev/linter/rules/use-filenaming-convention) now recognizes file names starting with ASCII digits as lowercase ([#3952](https://github.com/biomejs/biome/issues/3952)). - - Thus, `2024-09-17-filename`, `2024_09_17_filename` and `20240917FileName` are in `kebab-case`, `snake_case`, and `camelCase` respectively. - - Contributed by @Conaclos - - [useSemanticElements](https://biomejs.dev/linter/rules/use-semantic-elements/) now ignores `alert` and `alertdialog` roles ([3858](https://github.com/biomejs/biome/issues/3858)). Contributed by @Conaclos - [noUndeclaredDependencies](https://biomejs.dev/linter/rules/no-undeclared-dependencies/) now ignores `@/` imports and recognizes type imports from Definitely Typed and `bun` imports. Contributed by @Conaclos diff --git a/crates/biome_js_analyze/src/lint/style/use_filenaming_convention.rs b/crates/biome_js_analyze/src/lint/style/use_filenaming_convention.rs index e2713215651f..3f30f3af0e60 100644 --- a/crates/biome_js_analyze/src/lint/style/use_filenaming_convention.rs +++ b/crates/biome_js_analyze/src/lint/style/use_filenaming_convention.rs @@ -34,6 +34,7 @@ declare_lint_rule! { /// /// By default, the rule ensures that the filename is either in [`camelCase`], [`kebab-case`], [`snake_case`], /// or equal to the name of one export in the file. + /// By default, the rule ensures that the extensions are either in [`camelCase`], [`kebab-case`], or [`snake_case`]. /// /// ## Ignoring some files /// @@ -104,6 +105,9 @@ declare_lint_rule! { /// You can enforce a stricter convention by setting `filenameCases` option. /// `filenameCases` accepts an array of cases among the following cases: [`camelCase`], [`kebab-case`], [`PascalCase`], [`snake_case`], and `export`. /// + /// This option also applies to the file extensions. + /// Extensions in lowercase are always allowed regardless of how `filenameCases` is set. + /// /// [case]: https://en.wikipedia.org/wiki/Naming_convention_(programming)#Examples_of_multiple-word_identifier_formats /// [`camelCase`]: https://en.wikipedia.org/wiki/Camel_case /// [`kebab-case`]: https://en.wikipedia.org/wiki/Letter_case#Kebab_case @@ -185,15 +189,18 @@ impl Rule for UseFilenamingConvention { }; (name, split) }; + let allowed_cases = options.filename_cases.cases(); + let allowed_extension_cases = allowed_cases | Case::Lower; // Check extension case - if extensions.any(|extension| Case::identify(extension, true) != Case::Lower) { + if extensions.any(|extension| { + !allowed_extension_cases.contains(Case::identify(extension, options.strict_case)) + }) { return Some(FileNamingConventionState::Extension); } if name.is_empty() { return None; } // Check filename case - let allowed_cases = options.filename_cases.cases(); if !allowed_cases.is_empty() { let trimmed_name = name.trim_matches('_'); let case = Case::identify(trimmed_name, options.strict_case); @@ -241,7 +248,7 @@ impl Rule for UseFilenamingConvention { .join(" or ") } else { allowed_case_names - .collect::>() + .collect::>() .join(" or ") }; let mut split = file_name.split('.'); @@ -306,11 +313,14 @@ impl Rule for UseFilenamingConvention { })) }, FileNamingConventionState::Extension => { + let allowed_cases = options.filename_cases.cases() | Case::Lower; + let allowed_case_names = allowed_cases.into_iter().map(|case| case.to_string()); + let allowed_case_names = allowed_case_names.collect::>().join(" or "); Some(RuleDiagnostic::new( rule_category!(), None as Option, markup! { - "The file extension should be in lowercase without any special characters." + "The file extension should be in "{allowed_case_names}"." }, )) }, diff --git a/crates/biome_js_analyze/tests/specs/style/useFilenamingConvention/filename.invalid-extension.js b/crates/biome_js_analyze/tests/specs/style/useFilenamingConvention/filename.INVALID-extension.js similarity index 100% rename from crates/biome_js_analyze/tests/specs/style/useFilenamingConvention/filename.invalid-extension.js rename to crates/biome_js_analyze/tests/specs/style/useFilenamingConvention/filename.INVALID-extension.js diff --git a/crates/biome_js_analyze/tests/specs/style/useFilenamingConvention/filename.invalid-extension.js.snap b/crates/biome_js_analyze/tests/specs/style/useFilenamingConvention/filename.INVALID-extension.js.snap similarity index 53% rename from crates/biome_js_analyze/tests/specs/style/useFilenamingConvention/filename.invalid-extension.js.snap rename to crates/biome_js_analyze/tests/specs/style/useFilenamingConvention/filename.INVALID-extension.js.snap index df8bcb0ead5f..96a17c797509 100644 --- a/crates/biome_js_analyze/tests/specs/style/useFilenamingConvention/filename.invalid-extension.js.snap +++ b/crates/biome_js_analyze/tests/specs/style/useFilenamingConvention/filename.INVALID-extension.js.snap @@ -1,6 +1,6 @@ --- source: crates/biome_js_analyze/tests/spec_tests.rs -expression: filename.invalid-extension.js +expression: filename.INVALID-extension.js --- # Input ```jsx @@ -9,11 +9,9 @@ expression: filename.invalid-extension.js # Diagnostics ``` -filename.invalid-extension.js lint/style/useFilenamingConvention ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ +filename.INVALID-extension.js lint/style/useFilenamingConvention ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - ! The file extension should be in lowercase without any special characters. + ! The file extension should be in camelCase or kebab-case or snake_case. ``` - - diff --git a/crates/biome_js_analyze/tests/specs/style/useFilenamingConvention/filename.test.invalid-extension.ts b/crates/biome_js_analyze/tests/specs/style/useFilenamingConvention/filename.INVALID.js similarity index 100% rename from crates/biome_js_analyze/tests/specs/style/useFilenamingConvention/filename.test.invalid-extension.ts rename to crates/biome_js_analyze/tests/specs/style/useFilenamingConvention/filename.INVALID.js diff --git a/crates/biome_js_analyze/tests/specs/style/useFilenamingConvention/filename.INVALID.js.snap b/crates/biome_js_analyze/tests/specs/style/useFilenamingConvention/filename.INVALID.js.snap new file mode 100644 index 000000000000..ca4fe4f32709 --- /dev/null +++ b/crates/biome_js_analyze/tests/specs/style/useFilenamingConvention/filename.INVALID.js.snap @@ -0,0 +1,17 @@ +--- +source: crates/biome_js_analyze/tests/spec_tests.rs +expression: filename.INVALID.js +--- +# Input +```jsx + +``` + +# Diagnostics +``` +filename.INVALID.js lint/style/useFilenamingConvention ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + ! The file extension should be in lowercase. + + +``` diff --git a/crates/biome_js_analyze/tests/specs/style/useFilenamingConvention/filename.INVALID.options.json b/crates/biome_js_analyze/tests/specs/style/useFilenamingConvention/filename.INVALID.options.json new file mode 100644 index 000000000000..b7cbd3d4e629 --- /dev/null +++ b/crates/biome_js_analyze/tests/specs/style/useFilenamingConvention/filename.INVALID.options.json @@ -0,0 +1,15 @@ +{ + "$schema": "../../../../../../packages/@biomejs/biome/configuration_schema.json", + "linter": { + "rules": { + "style": { + "useFilenamingConvention": { + "level": "error", + "options": { + "filenameCases": ["export"] + } + } + } + } + } +} diff --git a/crates/biome_js_analyze/tests/specs/style/useFilenamingConvention/filename.test.invalid-extension.ts.snap b/crates/biome_js_analyze/tests/specs/style/useFilenamingConvention/filename.test.invalid-extension.ts.snap deleted file mode 100644 index 5253d095a4ba..000000000000 --- a/crates/biome_js_analyze/tests/specs/style/useFilenamingConvention/filename.test.invalid-extension.ts.snap +++ /dev/null @@ -1,19 +0,0 @@ ---- -source: crates/biome_js_analyze/tests/spec_tests.rs -expression: filename.test.invalid-extension.ts ---- -# Input -```ts - -``` - -# Diagnostics -``` -filename.test.invalid-extension.ts lint/style/useFilenamingConvention ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - - ! The file extension should be in lowercase without any special characters. - - -``` - - diff --git a/crates/biome_js_analyze/tests/specs/style/useFilenamingConvention/filename.valid-kebab-extension.js b/crates/biome_js_analyze/tests/specs/style/useFilenamingConvention/filename.valid-kebab-extension.js new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/crates/biome_js_analyze/tests/specs/style/useFilenamingConvention/filename.valid-kebab-extension.js.snap b/crates/biome_js_analyze/tests/specs/style/useFilenamingConvention/filename.valid-kebab-extension.js.snap new file mode 100644 index 000000000000..c059ca2206c8 --- /dev/null +++ b/crates/biome_js_analyze/tests/specs/style/useFilenamingConvention/filename.valid-kebab-extension.js.snap @@ -0,0 +1,8 @@ +--- +source: crates/biome_js_analyze/tests/spec_tests.rs +expression: filename.valid-kebab-extension.js +--- +# Input +```jsx + +``` diff --git a/crates/biome_js_analyze/tests/specs/style/useFilenamingConvention/filename.validCamelExt.js b/crates/biome_js_analyze/tests/specs/style/useFilenamingConvention/filename.validCamelExt.js new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/crates/biome_js_analyze/tests/specs/style/useFilenamingConvention/filename.validCamelExt.js.snap b/crates/biome_js_analyze/tests/specs/style/useFilenamingConvention/filename.validCamelExt.js.snap new file mode 100644 index 000000000000..cb3ff980c057 --- /dev/null +++ b/crates/biome_js_analyze/tests/specs/style/useFilenamingConvention/filename.validCamelExt.js.snap @@ -0,0 +1,8 @@ +--- +source: crates/biome_js_analyze/tests/spec_tests.rs +expression: filename.validCamelExt.js +--- +# Input +```jsx + +``` diff --git a/crates/biome_js_analyze/tests/specs/style/useFilenamingConvention/filename.valid_snake_ext.js b/crates/biome_js_analyze/tests/specs/style/useFilenamingConvention/filename.valid_snake_ext.js new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/crates/biome_js_analyze/tests/specs/style/useFilenamingConvention/filename.valid_snake_ext.js.snap b/crates/biome_js_analyze/tests/specs/style/useFilenamingConvention/filename.valid_snake_ext.js.snap new file mode 100644 index 000000000000..2d8ca830cbe4 --- /dev/null +++ b/crates/biome_js_analyze/tests/specs/style/useFilenamingConvention/filename.valid_snake_ext.js.snap @@ -0,0 +1,8 @@ +--- +source: crates/biome_js_analyze/tests/spec_tests.rs +expression: filename.valid_snake_ext.js +--- +# Input +```jsx + +```