Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

feat(rome_js_analyze): useCamelCase accepts function when they are used in new or exported #3210

Merged
merged 2 commits into from
Sep 14, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use rome_analyze::{
};
use rome_console::markup;
use rome_diagnostics::Applicability;
use rome_js_semantic::{AllReferencesExtensions, IsExportedCanBeQueried, SemanticModel};
use rome_js_syntax::{
JsFormalParameter, JsFunctionDeclaration, JsFunctionExportDefaultDeclaration,
JsGetterClassMember, JsIdentifierBinding, JsLiteralMemberName, JsMethodClassMember,
Expand Down Expand Up @@ -60,18 +61,39 @@ fn check_is_camel(name: &str) -> Option<State> {
}

// It is OK to be non camel case when:
// 1. it's a const variable (eg: const THIS_IS_OK)
fn is_non_camel_ok(binding: &JsIdentifierBinding) -> Option<bool> {
// 1. it's a const variable (eg: const THIS_IS_OK);
// 2. it's a function used in a new expression (eg: new PascalCase());
// 3. it's a exported function.
fn is_non_camel_ok(binding: &JsIdentifierBinding, model: &SemanticModel) -> Option<bool> {
use JsSyntaxKind::*;
let declarator = binding.parent::<JsVariableDeclarator>()?;
let is_ok = match declarator.syntax().parent().map(|parent| parent.kind()) {
Some(JS_VARIABLE_DECLARATOR_LIST) => declarator
.parent::<JsVariableDeclaratorList>()?
.parent::<JsVariableDeclaration>()?
.is_const(),
_ => false,
};
Some(is_ok)
match binding.syntax().parent()?.kind() {
JS_VARIABLE_DECLARATOR => {
let declarator = binding.parent::<JsVariableDeclarator>()?;
let is_ok = match declarator.syntax().parent().map(|parent| parent.kind()) {
Some(JS_VARIABLE_DECLARATOR_LIST) => declarator
.parent::<JsVariableDeclaratorList>()?
.parent::<JsVariableDeclaration>()?
.is_const(),
_ => false,
};
Some(is_ok)
}
JS_FUNCTION_DECLARATION => {
if binding.is_exported(model) {
return Some(true);
}

for reference in binding.all_reads(model) {
let greatparent = reference.node().grand_parent()?;
if let JS_NEW_EXPRESSION = greatparent.kind() {
return Some(true);
}
}

Some(false)
}
_ => Some(false),
}
}

impl Rule for UseCamelCase {
Expand All @@ -83,10 +105,11 @@ impl Rule for UseCamelCase {

fn run(ctx: &RuleContext<Self>) -> Option<Self::State> {
let name = ctx.query();
let model = ctx.model();

match name {
JsAnyCamelCaseName::JsIdentifierBinding(binding) => {
let is_non_camel_ok = is_non_camel_ok(binding);
let is_non_camel_ok = is_non_camel_ok(binding, model);
match is_non_camel_ok {
Some(false) | None => {
let is_variable = binding.parent::<JsVariableDeclarator>().is_some();
Expand Down
9 changes: 9 additions & 0 deletions crates/rome_js_analyze/tests/specs/nursery/useCamelCase.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,12 @@ let [ UPPER_CASE ] = env;
const THIS_IS_OK = 1;
const { THIS_IS_OK } = env;
const [ THIS_IS_OK ] = env;

function PascalCaseOkBecauseNew() { }
console.log(new PascalCaseOkBecauseNew());

function PascalCaseOkBecauseExport() { }
export default PascalCaseOkBecauseExport;

function PascalCaseNOk() { }
console.log(PascalCaseNOk());
29 changes: 29 additions & 0 deletions crates/rome_js_analyze/tests/specs/nursery/useCamelCase.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ const THIS_IS_OK = 1;
const { THIS_IS_OK } = env;
const [ THIS_IS_OK ] = env;

function PascalCaseOkBecauseNew() { }
console.log(new PascalCaseOkBecauseNew());

function PascalCaseOkBecauseExport() { }
export default PascalCaseOkBecauseExport;

function PascalCaseNOk() { }
console.log(PascalCaseNOk());

```

# Diagnostics
Expand Down Expand Up @@ -199,4 +208,24 @@ Safe fix: Rename this symbol to camel case

```

```
warning[nursery/useCamelCase]: Prefer functions names in camel case.
┌─ useCamelCase.js:45:10
45 │ function PascalCaseNOk() { }
│ -------------

Safe fix: Rename this symbol to camel case
| @@ -42,5 +42,5 @@
41 41 | function PascalCaseOkBecauseExport() { }
42 42 | export default PascalCaseOkBecauseExport;
43 43 |
44 | - function PascalCaseNOk() { }
45 | - console.log(PascalCaseNOk());
44 | + function pascalCaseNOk() { }
45 | + console.log(pascalCaseNOk());


```