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(lint/useNamingConvention): accept unicase characters #1806

Merged
merged 1 commit into from
Feb 13, 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
21 changes: 21 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,27 @@ Read our [guidelines for writing a good changelog entry](https://github.com/biom

Contributed by @Conaclos

- [useNamingConvention](https://biomejs.dev/linter/rules/use-naming-convention) now supports [unicase](https://en.wikipedia.org/wiki/Unicase) letters ([#1786](https://github.com/biomejs/biome/issues/1786)).

[unicase](https://en.wikipedia.org/wiki/Unicase) letters have a single case: they are neither uppercase nor lowercase.
Previously, Biome reported names in unicase as invalid.
It now accepts a name in unicase everywhere.

The following code is now accepted:

```js
const 안녕하세요 = { 안녕하세요: 0 };
```

We still reject a name that mixes unicase characters with lowercase or uppercase characters:
The following names are rejected:

```js
const A안녕하세요 = { a안녕하세요: 0 };
```

Contributed by @Conaclos

#### Bug fixes

- Fix [#1651](https://github.com/biomejs/biome/issues/1651). [noVar](https://biomejs.dev/linter/rules/no-var/) now ignores TsGlobalDeclaration. Contributed by @vasucp1207
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,8 @@ impl Rule for UseNamingConvention {
}
let trimmed_name = trim_underscore_dollar(name);
let actual_case = Case::identify(trimmed_name, options.strict_case);
if trimmed_name.is_empty()
if actual_case == Case::Uni
|| trimmed_name.is_empty()
|| allowed_cases
.iter()
.any(|&expected_style| actual_case.is_compatible_with(expected_style))
Expand All @@ -314,7 +315,8 @@ impl Rule for UseNamingConvention {
}
let preferred_case = element.allowed_cases(ctx.options())[0];
let new_trimmed_name = preferred_case.convert(trimmed_name);
let suggested_name = name.replace(trimmed_name, &new_trimmed_name);
let suggested_name = (trimmed_name != new_trimmed_name)
.then(|| name.replacen(trimmed_name, &new_trimmed_name, 1));
Some(State {
element,
suggested_name,
Expand Down Expand Up @@ -357,26 +359,33 @@ impl Rule for UseNamingConvention {
}));
}
}

Some(RuleDiagnostic::new(
let diagnostic = RuleDiagnostic::new(
rule_category!(),
ctx.query().syntax().text_trimmed_range(),
markup! {
"This "<Emphasis>{element.to_string()}</Emphasis>" name"{trimmed_info}" should be in "<Emphasis>{allowed_case_names}</Emphasis>"."
},
).note(markup! {
"The name could be renamed to `"{suggested_name}"`."
}))
);
Some(if let Some(suggested_name) = suggested_name {
diagnostic.note(markup! {
"The name could be renamed to `"{suggested_name}"`."
})
} else {
diagnostic
})
}

fn action(ctx: &RuleContext<Self>, state: &Self::State) -> Option<JsRuleAction> {
let node = ctx.query();
let model = ctx.model();
let mut mutation = ctx.root().begin();
let State {
element,
suggested_name,
} = state;
let Some(suggested_name) = suggested_name else {
return None;
};
let node = ctx.query();
let model = ctx.model();
let mut mutation = ctx.root().begin();
let renamable = match node {
AnyIdentifierBindingLike::JsIdentifierBinding(binding) => {
if binding.is_exported(model) {
Expand Down Expand Up @@ -452,7 +461,7 @@ impl AnyIdentifierBindingLike {
#[derive(Debug)]
pub(crate) struct State {
element: Named,
suggested_name: String,
suggested_name: Option<String>,
}

/// Rule's options.
Expand Down
Loading
Loading