@blumintinc/eslint-plugin-blumint 0.1.16 → 0.1.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.js CHANGED
@@ -17,7 +17,7 @@ const require_memo_1 = require("./rules/require-memo");
17
17
  module.exports = {
18
18
  meta: {
19
19
  name: '@blumintinc/eslint-plugin-blumint',
20
- version: '0.1.16',
20
+ version: '0.1.18',
21
21
  },
22
22
  parseOptions: {
23
23
  ecmaVersion: 2020,
@@ -1,31 +1,25 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.requireMemo = void 0;
4
+ const utils_1 = require("@typescript-eslint/utils");
4
5
  const ASTHelpers_1 = require("../utils/ASTHelpers");
5
6
  const isComponentExplicitlyUnmemoized = (componentName) => componentName.toLowerCase().includes('unmemoized');
6
- function isMemoCallExpression(node) {
7
- if (node.type !== 'CallExpression')
8
- return false;
9
- if (node.callee.type === 'MemberExpression') {
10
- const { callee: { object, property }, } = node;
11
- if (object.type === 'Identifier' &&
12
- property.type === 'Identifier' &&
13
- object.name === 'React' &&
14
- property.name === 'memo') {
15
- return true;
16
- }
17
- }
18
- else if (node.callee.type === 'Identifier' && node.callee.name === 'memo') {
19
- return true;
20
- }
21
- return false;
7
+ function isFunction(node) {
8
+ return (node.type === utils_1.AST_NODE_TYPES.ArrowFunctionExpression ||
9
+ node.type === utils_1.AST_NODE_TYPES.FunctionExpression);
22
10
  }
23
11
  function isHigherOrderFunctionReturningJSX(node) {
24
- if (node.type === 'ArrowFunctionExpression' ||
25
- node.type === 'FunctionExpression') {
26
- if (node.body.type === 'ArrowFunctionExpression' ||
27
- node.body.type === 'FunctionExpression') {
28
- return ASTHelpers_1.ASTHelpers.returnsJSX(node.body.body);
12
+ if (isFunction(node)) {
13
+ // Check if function takes another function as an argument
14
+ const hasFunctionParam = 'params' in node && node.params.some(isFunction);
15
+ if (node.body && node.body.type === 'BlockStatement') {
16
+ for (const statement of node.body.body) {
17
+ if (statement.type === 'ReturnStatement' && statement.argument) {
18
+ const returnsJSX = ASTHelpers_1.ASTHelpers.returnsJSX(statement.argument);
19
+ const returnsFunction = isFunction(statement.argument);
20
+ return (hasFunctionParam || returnsFunction) && returnsJSX;
21
+ }
22
+ }
29
23
  }
30
24
  }
31
25
  return false;
@@ -36,15 +30,19 @@ function checkFunction(context, node) {
36
30
  return;
37
31
  }
38
32
  if (isHigherOrderFunctionReturningJSX(node)) {
33
+ console.log('Found HOF');
39
34
  return;
40
35
  }
41
- let currentNode = node.parent;
42
- while (currentNode.type === 'CallExpression') {
43
- if (isMemoCallExpression(currentNode)) {
44
- return;
45
- }
46
- currentNode = currentNode.parent;
36
+ const currentNode = node.parent;
37
+ if (node.parent.type === 'CallExpression') {
38
+ return;
47
39
  }
40
+ // while (currentNode.type === 'CallExpression') {
41
+ // if (isMemoCallExpression(currentNode) || true) {
42
+ // return;
43
+ // }
44
+ // currentNode = currentNode.parent;
45
+ // }
48
46
  if (ASTHelpers_1.ASTHelpers.returnsJSX(node.body) && ASTHelpers_1.ASTHelpers.hasParameters(node)) {
49
47
  if (currentNode.type === 'VariableDeclarator' &&
50
48
  currentNode.id.type === 'Identifier' &&
@@ -143,17 +143,30 @@ class ASTHelpers {
143
143
  return false;
144
144
  }
145
145
  static returnsJSX(node) {
146
- if (node.type === 'JSXElement') {
146
+ if (node.type === 'JSXElement' || node.type === 'JSXFragment') {
147
147
  return true;
148
148
  }
149
149
  if (node.type === 'BlockStatement') {
150
150
  for (const statement of node.body) {
151
151
  if (statement.type === 'ReturnStatement' &&
152
- statement.argument?.type === 'JSXElement') {
152
+ (statement.argument?.type === 'JSXElement' ||
153
+ statement.argument?.type === 'JSXFragment')) {
153
154
  return true;
154
155
  }
156
+ // Handle conditional returns
157
+ if (statement.type === 'ReturnStatement' &&
158
+ statement.argument?.type === 'ConditionalExpression') {
159
+ const conditionalExpr = statement.argument;
160
+ if (ASTHelpers.returnsJSX(conditionalExpr.consequent) ||
161
+ ASTHelpers.returnsJSX(conditionalExpr.alternate)) {
162
+ return true;
163
+ }
164
+ }
155
165
  }
156
166
  }
167
+ if (node.type === 'ConditionalExpression') {
168
+ return (this.returnsJSX(node.consequent) || this.returnsJSX(node.alternate));
169
+ }
157
170
  return false;
158
171
  }
159
172
  static hasParameters(node) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blumintinc/eslint-plugin-blumint",
3
- "version": "0.1.16",
3
+ "version": "0.1.18",
4
4
  "description": "Custom eslint rules for use at BluMint",
5
5
  "keywords": [
6
6
  "eslint",