Skip to content

Commit

Permalink
fix: false negative
Browse files Browse the repository at this point in the history
  • Loading branch information
kecrily committed May 15, 2024
1 parent 84a88d8 commit 9f57298
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/src/rules/func-style.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ This rule has a string option:
* `"expression"` (default) requires the use of function expressions instead of function declarations
* `"declaration"` requires the use of function declarations instead of function expressions

This rule has an object option for two exception:
This rule has an object option for two exceptions:

* `"allowArrowFunctions"`: `true` (default `false`) allows the use of arrow functions. This option applies only when the string option is set to `"declaration"` (arrow functions are always allowed when the string option is set to `"expression"`, regardless of this option)
* `"overrides"`:
Expand Down
9 changes: 6 additions & 3 deletions lib/rules/func-style.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ module.exports = {
if (
!enforceDeclarations &&
node.parent.type !== "ExportDefaultDeclaration" &&
typeof exportFunctionStyle === "undefined"
(typeof exportFunctionStyle === "undefined" || node.parent.type !== "ExportNamedDeclaration")
) {
context.report({ node, messageId: "expression" });
}
Expand All @@ -84,7 +84,7 @@ module.exports = {
if (
enforceDeclarations &&
node.parent.type === "VariableDeclarator" &&
typeof exportFunctionStyle === "undefined"
(typeof exportFunctionStyle === "undefined" || node.parent.parent.parent.type !== "ExportNamedDeclaration")
) {
context.report({ node: node.parent, messageId: "declaration" });
}
Expand Down Expand Up @@ -116,7 +116,10 @@ module.exports = {
const hasThisExpr = stack.pop();

if (!hasThisExpr && node.parent.type === "VariableDeclarator") {
if (enforceDeclarations && typeof exportFunctionStyle === "undefined") {
if (
enforceDeclarations &&
(typeof exportFunctionStyle === "undefined" || node.parent.parent.parent.type !== "ExportNamedDeclaration")
) {
context.report({ node: node.parent, messageId: "declaration" });
}

Expand Down
33 changes: 33 additions & 0 deletions tests/lib/rules/func-style.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,39 @@ ruleTester.run("func-style", rule, {
type: "VariableDeclarator"
}
]
},
{
code: "function foo() {};",
options: ["expression", { overrides: { namedExports: "declaration" } }],
languageOptions: { ecmaVersion: 6 },
errors: [
{
messageId: "expression",
type: "FunctionDeclaration"
}
]
},
{
code: "var foo = function() {};",
options: ["declaration", { overrides: { namedExports: "expression" } }],
languageOptions: { ecmaVersion: 6 },
errors: [
{
messageId: "declaration",
type: "VariableDeclarator"
}
]
},
{
code: "var foo = () => {};",
options: ["declaration", { overrides: { namedExports: "expression" } }],
languageOptions: { ecmaVersion: 6 },
errors: [
{
messageId: "declaration",
type: "VariableDeclarator"
}
]
}
]
});

0 comments on commit 9f57298

Please sign in to comment.