From 7345bc944c9d7953bd2628347b7df48253c7f8eb Mon Sep 17 00:00:00 2001 From: DonIsaac <22823424+DonIsaac@users.noreply.github.com> Date: Wed, 7 Aug 2024 03:53:40 +0000 Subject: [PATCH] fix(linter/func-names): handle ts accessibility when reporting missing names (#4713) --- .../oxc_linter/src/rules/eslint/func_names.rs | 42 ++++++++++--------- .../oxc_linter/src/snapshots/func_names.snap | 6 +++ 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/crates/oxc_linter/src/rules/eslint/func_names.rs b/crates/oxc_linter/src/rules/eslint/func_names.rs index 44b3b6badca73..80cb760cd0d0b 100644 --- a/crates/oxc_linter/src/rules/eslint/func_names.rs +++ b/crates/oxc_linter/src/rules/eslint/func_names.rs @@ -269,43 +269,47 @@ fn get_function_name_with_kind<'a>(func: &Function<'a>, parent_node: &AstNode<'a match parent_node.kind() { AstKind::MethodDefinition(definition) => { - if definition.r#static { - tokens.push("static".into()); + if !definition.computed && definition.key.is_private_identifier() { + tokens.push(Cow::Borrowed("private")); + } else if let Some(accessibility) = definition.accessibility { + tokens.push(Cow::Borrowed(accessibility.as_str())); } - if !definition.computed && definition.key.is_private_identifier() { - tokens.push("private".into()); + if definition.r#static { + tokens.push(Cow::Borrowed("static")); } } AstKind::PropertyDefinition(definition) => { - if definition.r#static { - tokens.push("static".into()); + if !definition.computed && definition.key.is_private_identifier() { + tokens.push(Cow::Borrowed("private")); + } else if let Some(accessibility) = definition.accessibility { + tokens.push(Cow::Borrowed(accessibility.as_str())); } - if !definition.computed && definition.key.is_private_identifier() { - tokens.push("private".into()); + if definition.r#static { + tokens.push(Cow::Borrowed("static")); } } _ => {} } if func.r#async { - tokens.push("async".into()); + tokens.push(Cow::Borrowed("async")); } if func.generator { - tokens.push("generator".into()); + tokens.push(Cow::Borrowed("generator")); } match parent_node.kind() { AstKind::MethodDefinition(method_definition) => match method_definition.kind { - MethodDefinitionKind::Constructor => tokens.push("constructor".into()), - MethodDefinitionKind::Get => tokens.push("getter".into()), - MethodDefinitionKind::Set => tokens.push("setter".into()), - MethodDefinitionKind::Method => tokens.push("method".into()), + MethodDefinitionKind::Constructor => tokens.push(Cow::Borrowed("constructor")), + MethodDefinitionKind::Get => tokens.push(Cow::Borrowed("getter")), + MethodDefinitionKind::Set => tokens.push(Cow::Borrowed("setter")), + MethodDefinitionKind::Method => tokens.push(Cow::Borrowed("method")), }, - AstKind::PropertyDefinition(_) => tokens.push("method".into()), - _ => tokens.push("function".into()), + AstKind::PropertyDefinition(_) => tokens.push(Cow::Borrowed("method")), + _ => tokens.push(Cow::Borrowed("function")), } match parent_node.kind() { @@ -342,10 +346,7 @@ fn get_function_name_with_kind<'a>(func: &Function<'a>, parent_node: &AstNode<'a impl Rule for FuncNames { fn from_configuration(value: serde_json::Value) -> Self { let Some(default_value) = value.get(0) else { - return Self { - default_config: FuncNamesConfig::default(), - generators_config: FuncNamesConfig::default(), - }; + return Self::default(); }; let default_config = FuncNamesConfig::try_from(default_value).unwrap(); @@ -639,6 +640,7 @@ fn test() { Some(serde_json::json!(["as-needed", { "generators": "never" }])), ), // { "ecmaVersion": 6 }, ("class C { foo = function() {} }", Some(serde_json::json!(["always"]))), // { "ecmaVersion": 2022 }, + ("class C { public foo = function() {} }", Some(serde_json::json!(["always"]))), // { "ecmaVersion": 2022 }, ("class C { [foo] = function() {} }", Some(serde_json::json!(["always"]))), // { "ecmaVersion": 2022 }, ("class C { #foo = function() {} }", Some(serde_json::json!(["always"]))), // { "ecmaVersion": 2022 }, ("class C { foo = bar(function() {}) }", Some(serde_json::json!(["as-needed"]))), // { "ecmaVersion": 2022 }, diff --git a/crates/oxc_linter/src/snapshots/func_names.snap b/crates/oxc_linter/src/snapshots/func_names.snap index 485e02082d16d..c1c9e3bdd4c06 100644 --- a/crates/oxc_linter/src/snapshots/func_names.snap +++ b/crates/oxc_linter/src/snapshots/func_names.snap @@ -283,6 +283,12 @@ source: crates/oxc_linter/src/tester.rs · ──────── ╰──── + ⚠ eslint(func-names): Unexpected unnamed public method foo. + ╭─[func_names.tsx:1:24] + 1 │ class C { public foo = function() {} } + · ──────── + ╰──── + ⚠ eslint(func-names): Unexpected unnamed method. ╭─[func_names.tsx:1:19] 1 │ class C { [foo] = function() {} }