feat(rome_js_analyze): useCamelCase accepts function when they are used in new or exported
MODIFY
crates/rome_js_analyze/src/semantic_analyzers/nursery/use_camel_case.rs
crates/rome_js_analyze/src/semantic_analyzers/nursery/use_camel_case.rs
@@ -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,
@@ -60,9 +61,13 @@ 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::*;
+ 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
@@ -73,6 +78,23 @@ fn is_non_camel_ok(binding: &JsIdentifierBinding) -> Option<bool> {
};
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 {
const CATEGORY: RuleCategory = RuleCategory::Lint;
@@ -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();

MODIFY
crates/rome_js_analyze/tests/specs/nursery/useCamelCase.js
crates/rome_js_analyze/tests/specs/nursery/useCamelCase.js
@@ -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());

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