Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(useFilenamingConvention): apply formats to file extensions #3958

Merged
merged 1 commit into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
///
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -241,7 +248,7 @@ impl Rule for UseFilenamingConvention {
.join(" or ")
} else {
allowed_case_names
.collect::<SmallVec<[_; 3]>>()
.collect::<SmallVec<[_; 4]>>()
.join(" or ")
};
let mut split = file_name.split('.');
Expand Down Expand Up @@ -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::<SmallVec<[_; 4]>>().join(" or ");
Some(RuleDiagnostic::new(
rule_category!(),
None as Option<TextRange>,
markup! {
"The file extension should be in lowercase without any special characters."
"The file extension should be in "<Emphasis>{allowed_case_names}</Emphasis>"."
},
))
},
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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.


```


Original file line number Diff line number Diff line change
@@ -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.


```
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$schema": "../../../../../../packages/@biomejs/biome/configuration_schema.json",
"linter": {
"rules": {
"style": {
"useFilenamingConvention": {
"level": "error",
"options": {
"filenameCases": ["export"]
}
}
}
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
source: crates/biome_js_analyze/tests/spec_tests.rs
expression: filename.valid-kebab-extension.js
---
# Input
```jsx

```
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
source: crates/biome_js_analyze/tests/spec_tests.rs
expression: filename.validCamelExt.js
---
# Input
```jsx

```
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
source: crates/biome_js_analyze/tests/spec_tests.rs
expression: filename.valid_snake_ext.js
---
# Input
```jsx

```
Loading