Skip to content

Commit

Permalink
fix(linter/func-names): handle ts accessibility when reporting missin…
Browse files Browse the repository at this point in the history
…g names (#4713)
  • Loading branch information
DonIsaac committed Aug 7, 2024
1 parent 475266d commit 7345bc9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
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]
1class C { public foo = function() {} }
· ────────
╰────

eslint(func-names): Unexpected unnamed method.
╭─[func_names.tsx:1:19]
1class C { [foo] = function() {} }
Expand Down

0 comments on commit 7345bc9

Please sign in to comment.