Skip to content

Commit 7345bc9

Browse files
committed
fix(linter/func-names): handle ts accessibility when reporting missing names (#4713)
1 parent 475266d commit 7345bc9

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed

crates/oxc_linter/src/rules/eslint/func_names.rs

+22-20
Original file line numberDiff line numberDiff line change
@@ -269,43 +269,47 @@ fn get_function_name_with_kind<'a>(func: &Function<'a>, parent_node: &AstNode<'a
269269

270270
match parent_node.kind() {
271271
AstKind::MethodDefinition(definition) => {
272-
if definition.r#static {
273-
tokens.push("static".into());
272+
if !definition.computed && definition.key.is_private_identifier() {
273+
tokens.push(Cow::Borrowed("private"));
274+
} else if let Some(accessibility) = definition.accessibility {
275+
tokens.push(Cow::Borrowed(accessibility.as_str()));
274276
}
275277

276-
if !definition.computed && definition.key.is_private_identifier() {
277-
tokens.push("private".into());
278+
if definition.r#static {
279+
tokens.push(Cow::Borrowed("static"));
278280
}
279281
}
280282
AstKind::PropertyDefinition(definition) => {
281-
if definition.r#static {
282-
tokens.push("static".into());
283+
if !definition.computed && definition.key.is_private_identifier() {
284+
tokens.push(Cow::Borrowed("private"));
285+
} else if let Some(accessibility) = definition.accessibility {
286+
tokens.push(Cow::Borrowed(accessibility.as_str()));
283287
}
284288

285-
if !definition.computed && definition.key.is_private_identifier() {
286-
tokens.push("private".into());
289+
if definition.r#static {
290+
tokens.push(Cow::Borrowed("static"));
287291
}
288292
}
289293
_ => {}
290294
}
291295

292296
if func.r#async {
293-
tokens.push("async".into());
297+
tokens.push(Cow::Borrowed("async"));
294298
}
295299

296300
if func.generator {
297-
tokens.push("generator".into());
301+
tokens.push(Cow::Borrowed("generator"));
298302
}
299303

300304
match parent_node.kind() {
301305
AstKind::MethodDefinition(method_definition) => match method_definition.kind {
302-
MethodDefinitionKind::Constructor => tokens.push("constructor".into()),
303-
MethodDefinitionKind::Get => tokens.push("getter".into()),
304-
MethodDefinitionKind::Set => tokens.push("setter".into()),
305-
MethodDefinitionKind::Method => tokens.push("method".into()),
306+
MethodDefinitionKind::Constructor => tokens.push(Cow::Borrowed("constructor")),
307+
MethodDefinitionKind::Get => tokens.push(Cow::Borrowed("getter")),
308+
MethodDefinitionKind::Set => tokens.push(Cow::Borrowed("setter")),
309+
MethodDefinitionKind::Method => tokens.push(Cow::Borrowed("method")),
306310
},
307-
AstKind::PropertyDefinition(_) => tokens.push("method".into()),
308-
_ => tokens.push("function".into()),
311+
AstKind::PropertyDefinition(_) => tokens.push(Cow::Borrowed("method")),
312+
_ => tokens.push(Cow::Borrowed("function")),
309313
}
310314

311315
match parent_node.kind() {
@@ -342,10 +346,7 @@ fn get_function_name_with_kind<'a>(func: &Function<'a>, parent_node: &AstNode<'a
342346
impl Rule for FuncNames {
343347
fn from_configuration(value: serde_json::Value) -> Self {
344348
let Some(default_value) = value.get(0) else {
345-
return Self {
346-
default_config: FuncNamesConfig::default(),
347-
generators_config: FuncNamesConfig::default(),
348-
};
349+
return Self::default();
349350
};
350351

351352
let default_config = FuncNamesConfig::try_from(default_value).unwrap();
@@ -639,6 +640,7 @@ fn test() {
639640
Some(serde_json::json!(["as-needed", { "generators": "never" }])),
640641
), // { "ecmaVersion": 6 },
641642
("class C { foo = function() {} }", Some(serde_json::json!(["always"]))), // { "ecmaVersion": 2022 },
643+
("class C { public foo = function() {} }", Some(serde_json::json!(["always"]))), // { "ecmaVersion": 2022 },
642644
("class C { [foo] = function() {} }", Some(serde_json::json!(["always"]))), // { "ecmaVersion": 2022 },
643645
("class C { #foo = function() {} }", Some(serde_json::json!(["always"]))), // { "ecmaVersion": 2022 },
644646
("class C { foo = bar(function() {}) }", Some(serde_json::json!(["as-needed"]))), // { "ecmaVersion": 2022 },

crates/oxc_linter/src/snapshots/func_names.snap

+6
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,12 @@ source: crates/oxc_linter/src/tester.rs
283283
· ────────
284284
╰────
285285

286+
eslint(func-names): Unexpected unnamed public method foo.
287+
╭─[func_names.tsx:1:24]
288+
1class C { public foo = function() {} }
289+
· ────────
290+
╰────
291+
286292
eslint(func-names): Unexpected unnamed method.
287293
╭─[func_names.tsx:1:19]
288294
1class C { [foo] = function() {} }

0 commit comments

Comments
 (0)