Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

padding-line-between-statements: support arrow node type #395

Open
3 of 4 tasks
Jelmerro opened this issue May 13, 2024 · 0 comments
Open
3 of 4 tasks

padding-line-between-statements: support arrow node type #395

Jelmerro opened this issue May 13, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@Jelmerro
Copy link

Clear and concise description of the problem

I would like the arrow node type to be added to the list of node types for the padding-line-between-statements rule. Currently const funcname = () => {...} is interpreted as const, which is technically true, but is also a very common way to define functions without the function keyword. As such, it would help for controlling padding lines to be able to select lines with arrow functions in them.

Suggested solution

diff --git a/packages/eslint-plugin-js/rules/padding-line-between-statements/padding-line-between-statements.ts b/packages/eslint-plugin-js/rules/padding-line-between-statements/padding-line-between-statements.ts
index 449a9c4..74c441a 100644
--- a/packages/eslint-plugin-js/rules/padding-line-between-statements/padding-line-between-statements.ts
+++ b/packages/eslint-plugin-js/rules/padding-line-between-statements/padding-line-between-statements.ts
@@ -91,6 +91,24 @@ function isIIFEStatement(node: ASTNode): boolean {
   return false
 }
 
+/**
+ * Checks whether the given node is an arrow function.
+ * @param node The node to check.
+ * @returns `true` if the node is a arrow-function statement.
+ * @private
+ */
+function isArrowFuntion(node: ASTNode): boolean {
+  if (
+      node !== null &&
+      node.type === "VariableDeclaration" &&
+      node.declarations[0].init !== null &&
+      node.declarations[0].init.type === "ArrowFunctionExpression"
+  ) {
+      return true;
+  }
+  return false;
+}
+
 /**
  * Checks whether the given node is a block-like statement.
  * This checks the last token of the node is the closing brace of a block.
@@ -293,6 +311,9 @@ const PaddingTypes = {
  */
 const StatementTypes = {
   '*': { test: () => true },
+  'arrow': {
+    test: (node) => isArrowFuntion(node),
+  },
   'block-like': {
     test: (node, sourceCode) => isBlockLikeStatement(sourceCode, node),
   },

Alternative

No response

Additional context

The feature is taken from here: https://github.com/eslint/eslint/pull/16970/files and was rejected by eslint as being too stylistic, hence I would like to have it added to stylistic.

Validations

Contributes

  • If this feature request is accepted, I am willing to submit a PR to fix this issue
@Jelmerro Jelmerro added the enhancement New feature or request label May 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant