Skip to content

Commit

Permalink
[autofix.ci] apply automated fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
autofix-ci[bot] authored Feb 8, 2025
1 parent bbaaae5 commit f3c7ed2
Showing 1 changed file with 59 additions and 50 deletions.
109 changes: 59 additions & 50 deletions crates/oxc_linter/src/rules/eslint/func_style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@ use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_semantic::{AstNode, NodeId};
use oxc_span::Span;
use serde_json::Value;
use rustc_hash::FxHashSet;
use serde_json::Value;

use crate::{
ast_util::nth_outermost_paren_parent,
context::LintContext,
rule::Rule,
};
use crate::{ast_util::nth_outermost_paren_parent, context::LintContext, rule::Rule};

fn func_style_diagnostic(span: Span, style: Style) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("Expected a function {}", style.as_str()))
Expand Down Expand Up @@ -70,20 +66,20 @@ declare_oxc_lint!(
/// Enforce the consistent use of either function declarations or expressions assigned to variables
///
/// ### Why is this bad?
/// This rule enforces a particular type of function style, either function declarations or expressions assigned to variables.
/// This rule enforces a particular type of function style, either function declarations or expressions assigned to variables.
/// You can specify which you prefer in the configuration.
///
///
/// ### Examples
/// // function declaration
/// function doSomething() {
/// // ...
/// }
///
///
/// // arrow function expression assigned to a variable
/// const doSomethingElse = () => {
/// // ...
/// };
///
///
/// // function expression assigned to a variable
/// const doSomethingAgain = function() {
/// // ...
Expand All @@ -92,37 +88,37 @@ declare_oxc_lint!(
/// Examples of incorrect code for this rule with the default "expression" option:
/// ```js
/// /*eslint func-style: ["error", "expression"]*/
///
///
/// function foo() {
/// // ...
/// }
/// ```
///
///
/// Examples of incorrect code for this rule with the "declaration" option:
/// ```js
/// /*eslint func-style: ["error", "declaration"]*/
/// var foo = function() {
/// // ...
/// };
///
///
/// var foo = () => {};
/// ```
///
///
/// Examples of incorrect code for this rule with the "declaration" and {"overrides": { "namedExports": "expression" }} option:
/// ```js
/// /*eslint func-style: ["error", "declaration", { "overrides": { "namedExports": "expression" } }]*/
/// export function foo() {
/// // ...
/// }
/// ```
///
///
/// Examples of incorrect code for this rule with the "expression" and {"overrides": { "namedExports": "declaration" }} option:
/// ```js
/// /*eslint func-style: ["error", "expression", { "overrides": { "namedExports": "declaration" } }]*/
/// export var foo = function() {
/// // ...
/// };
///
///
/// export var bar = () => {};
/// ```
///
Expand All @@ -132,7 +128,7 @@ declare_oxc_lint!(
/// var foo = function() {
/// // ...
/// };
///
///
/// Examples of correct code for this rule with the "declaration" option:
/// ```js
/// /*eslint func-style: ["error", "declaration"]*/
Expand All @@ -144,13 +140,13 @@ declare_oxc_lint!(
/// // ...
/// };
/// ```
///
///
/// Examples of additional correct code for this rule with the "declaration", { "allowArrowFunctions": true } options:
/// ```js
/// /*eslint func-style: ["error", "declaration", { "allowArrowFunctions": true }]*/
/// var foo = () => {};
/// ```
///
///
/// Examples of correct code for this rule with the "declaration" and {"overrides": { "namedExports": "expression" }} option:
/// ```js
/// /*eslint func-style: ["error", "declaration", { "overrides": { "namedExports": "expression" } }]*/
Expand All @@ -159,22 +155,22 @@ declare_oxc_lint!(
/// };
/// export var bar = () => {};
/// ```
///
///
/// Examples of correct code for this rule with the "expression" and {"overrides": { "namedExports": "declaration" }} option:
/// ```js
/// /*eslint func-style: ["error", "expression", { "overrides": { "namedExports": "declaration" } }]*/
/// export function foo() {
/// // ...
/// }
/// ```
///
///
/// Examples of correct code for this rule with the {"overrides": { "namedExports": "ignore" }} option:
/// ```js
/// /*eslint func-style: ["error", "expression", { "overrides": { "namedExports": "ignore" } }]*/
/// export var foo = function() {
/// // ...
/// };
///
///
/// export var bar = () => {};
/// export function baz() {
/// // ...
Expand All @@ -188,7 +184,7 @@ declare_oxc_lint!(
fn is_export_name_decl<'a>(node: &AstNode<'a>, ctx: &LintContext<'a>) -> bool {
if let Some(export_decl_ancestor) = nth_outermost_paren_parent(node, ctx, 2) {
if let AstKind::ExportNamedDeclaration(_) = export_decl_ancestor.kind() {
return true
return true;
}
}
false
Expand All @@ -200,10 +196,7 @@ impl Rule for FuncStyle {
let obj2 = value.get(1);

Self {
style: obj1
.and_then(Value::as_str)
.map(Style::from)
.unwrap_or_default(),
style: obj1.and_then(Value::as_str).map(Style::from).unwrap_or_default(),
allow_arrow_functions: obj2
.and_then(|v| v.get("allowArrowFunctions"))
.and_then(Value::as_bool)
Expand All @@ -220,11 +213,11 @@ impl Rule for FuncStyle {
let is_decl_style = self.style == Style::Declaration;

// step 1
// We can iterate over ctx.nodes() and process FunctionDeclaration and FunctionExpression,
// We can iterate over ctx.nodes() and process FunctionDeclaration and FunctionExpression,
// whereas for ArrowFunctionExpression we need to record this and super inside it

let mut arrow_func_nodes = Vec::new();
let mut arrow_func_ancestor_records = FxHashSet::<NodeId>::default();
let mut arrow_func_ancestor_records = FxHashSet::<NodeId>::default();

for node in semantic.nodes() {
match node.kind() {
Expand All @@ -236,59 +229,73 @@ impl Rule for FuncStyle {
match func.r#type {
FunctionType::FunctionDeclaration => {
// There are two situations to diagnostic
// 1) if style not equal to "declaration"
// 1) if style not equal to "declaration"
// we need to consider whether the parent node is ExportDefaultDeclaration or ExportNamedDeclaration
// "function foo() {}" should diagnostic
// "export function foo() {}" with option ["expression"] should diagnostic
//
// 2) For cases where the parent node is ExportNamedDeclaration,
// 2) For cases where the parent node is ExportNamedDeclaration,
// we just need to check if the self.named_exports value is expression
//
//
if !is_decl_style {
let temp_jus = match parent.kind() {

Check warning on line 241 in crates/oxc_linter/src/rules/eslint/func_style.rs

View workflow job for this annotation

GitHub Actions / Spell Check

"jus" should be "just".
AstKind::ExportDefaultDeclaration(_) => false,
AstKind::ExportNamedDeclaration(_) => self.named_exports.is_none(),
AstKind::ExportNamedDeclaration(_) => {
self.named_exports.is_none()
}
_ => true,
};
if temp_jus {

Check warning on line 248 in crates/oxc_linter/src/rules/eslint/func_style.rs

View workflow job for this annotation

GitHub Actions / Spell Check

"jus" should be "just".
ctx.diagnostic(func_style_diagnostic(func.span, self.style));
}
}

if let Some(name_export_style) = self.named_exports {
if matches!(name_export_style, NamedExports::Override(Style::Expression))
&& matches!(parent.kind(), AstKind::ExportNamedDeclaration(_)) {
ctx.diagnostic(func_style_diagnostic(func.span, self.style));
if matches!(
name_export_style,
NamedExports::Override(Style::Expression)
) && matches!(parent.kind(), AstKind::ExportNamedDeclaration(_))
{
ctx.diagnostic(func_style_diagnostic(func.span, self.style));
}
}
},
}
FunctionType::FunctionExpression => {
if let AstKind::VariableDeclarator(decl) = parent.kind() {
if is_decl_style && (self.named_exports.is_none() || !is_export) {
ctx.diagnostic(func_style_diagnostic(decl.span, self.style));
}

if let Some(name_export_style) = self.named_exports {
if NamedExports::Override(Style::Declaration) == name_export_style && is_export {
ctx.diagnostic(func_style_diagnostic(decl.span, self.style));
if NamedExports::Override(Style::Declaration)
== name_export_style
&& is_export
{
ctx.diagnostic(func_style_diagnostic(
decl.span, self.style,
));
}
}
}
},
_ => {},
}
_ => {}
}
},
}
AstKind::ThisExpression(_) | AstKind::Super(_) if !self.allow_arrow_functions => {
// We need to determine if the recent FunctionBody is an arrow function
let arrow_func_ancestor =
semantic.nodes().ancestors(node.id()).skip(1).find(|v| matches!(v.kind(), AstKind::FunctionBody(_))).map(|el| semantic.nodes().parent_node(el.id()).unwrap());
let arrow_func_ancestor = semantic
.nodes()
.ancestors(node.id())
.skip(1)
.find(|v| matches!(v.kind(), AstKind::FunctionBody(_)))
.map(|el| semantic.nodes().parent_node(el.id()).unwrap());
if let Some(ret) = arrow_func_ancestor {
arrow_func_ancestor_records.insert(ret.id());
}
},
}
AstKind::ArrowFunctionExpression(_) if !self.allow_arrow_functions => {
arrow_func_nodes.push(node);
},
}
_ => continue,
}
}
Expand All @@ -304,9 +311,11 @@ impl Rule for FuncStyle {
if is_decl_style && (self.named_exports.is_none() || !is_export) {
ctx.diagnostic(func_style_diagnostic(decl.span, self.style));
}

if let Some(name_export_style) = self.named_exports {
if NamedExports::Override(Style::Declaration) == name_export_style && is_export {
if NamedExports::Override(Style::Declaration) == name_export_style
&& is_export
{
ctx.diagnostic(func_style_diagnostic(decl.span, self.style));
}
}
Expand Down

0 comments on commit f3c7ed2

Please sign in to comment.