Skip to content
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
42 changes: 22 additions & 20 deletions crates/oxc_linter/src/rules/eslint/func_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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 },
Expand Down
6 changes: 6 additions & 0 deletions crates/oxc_linter/src/snapshots/func_names.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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() {} }
Expand Down