@prowise/eslint-config-linting-rules 2.3.3 → 2.3.5

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/package.json CHANGED
@@ -1 +1 @@
1
- {"name":"@prowise/eslint-config-linting-rules","version":"2.3.3","description":"Default Prowise linting rules for npm packages.","main":"index.js","type":"module","exports":{"./typescript":"./typescript.js","./backend":"./backend.js","./front-react":"./front-react.js","./front-solid":"./front-solid.js","./next":"./next.js"},"scripts":{"lint":"eslint","test":"node tests/jsx-pascal-case-rule.test.js","inspect":"eslint --inspect-config"},"repository":{"type":"git","url":"git+ssh://git@bitbucket.org/prowise/npm-linting-rules.git"},"author":"Prowise B.V.","license":"UNLICENSED","homepage":"https://bitbucket.org/prowise/npm-linting-rules#readme","dependencies":{"@next/eslint-plugin-next":"^15.3.5","@stylistic/eslint-plugin":"^5.1.0","@vitest/eslint-plugin":"^1.3.4","confusing-browser-globals":"^1.0.11","eslint":"^9.30.1","eslint-import-resolver-typescript":"^4.4.4","eslint-plugin-file-progress":"^3.0.2","eslint-plugin-import-x":"^4.16.1","eslint-plugin-jsdoc":"^51.3.4","eslint-plugin-jsx-a11y":"^6.10.2","eslint-plugin-n":"^17.21.0","eslint-plugin-prefer-arrow-functions":"^3.6.2","eslint-plugin-react":"^7.37.5","eslint-plugin-react-hooks":"^5.2.0","eslint-plugin-solid":"^0.14.5","globals":"^16.3.0","typescript-eslint":"^8.36.0"},"devDependencies":{"@eslint/config-inspector":"^1.1.0"}}
1
+ {"name":"@prowise/eslint-config-linting-rules","version":"2.3.5","description":"Default Prowise linting rules for npm packages.","main":"index.js","type":"module","exports":{"./typescript":"./typescript.js","./backend":"./backend.js","./front-react":"./front-react.js","./front-solid":"./front-solid.js","./next":"./next.js"},"scripts":{"lint":"eslint","test":"node tests/jsx-pascal-case-rule.test.js","inspect":"eslint --inspect-config"},"repository":{"type":"git","url":"git+ssh://git@bitbucket.org/prowise/npm-linting-rules.git"},"author":"Prowise B.V.","license":"UNLICENSED","homepage":"https://bitbucket.org/prowise/npm-linting-rules#readme","dependencies":{"@next/eslint-plugin-next":"^15.3.5","@stylistic/eslint-plugin":"^5.1.0","@vitest/eslint-plugin":"^1.3.4","confusing-browser-globals":"^1.0.11","eslint":"^9.31.0","eslint-import-resolver-typescript":"^4.4.4","eslint-plugin-file-progress":"^3.0.2","eslint-plugin-import-x":"^4.16.1","eslint-plugin-jsdoc":"^51.3.4","eslint-plugin-jsx-a11y":"^6.10.2","eslint-plugin-n":"^17.21.0","eslint-plugin-prefer-arrow-functions":"^3.6.2","eslint-plugin-react":"^7.37.5","eslint-plugin-react-hooks":"^5.2.0","eslint-plugin-solid":"^0.14.5","globals":"^16.3.0","typescript-eslint":"^8.36.0"},"devDependencies":{"@eslint/config-inspector":"^1.1.0"}}
@@ -7,26 +7,26 @@ export const jsxElementRule = {
7
7
  },
8
8
 
9
9
  create: (context) => {
10
- function getReturnType(node) {
11
- if (node.type === "LogicalExpression") return node.right.type;
12
- if (node.type === "ConditionalExpression") return node.consequent.type;
10
+ // get the return type of the function body
11
+ function getReturnType(body) {
12
+ if (body.type === "Literal" && body.value === null) return undefined;
13
+ if (body.type === "LogicalExpression") return body.right.type;
14
+ if (body.type === "ConditionalExpression") return body.consequent.type;
13
15
 
14
- if (node.type === "BlockStatement") {
15
- const returnStatement = node.body.find((stmt) => stmt.type === "ReturnStatement");
16
- if (returnStatement) return returnStatement.argument.type === "LogicalExpression" ? returnStatement.argument.right.type : returnStatement.argument.type;
16
+ if (body.type === "BlockStatement") {
17
+ const returnStatement = body.body.find((stmt) => stmt.type === "ReturnStatement");
18
+ if (returnStatement) return getReturnType(returnStatement.argument);
17
19
 
18
- const switchStatement = node.body.find((stmt) => stmt.type === "SwitchStatement");
20
+ const switchStatement = body.body.find((stmt) => stmt.type === "SwitchStatement");
19
21
  if (switchStatement) return switchStatement.cases[0].consequent.find((stmt) => stmt.type === "ReturnStatement")?.argument.type;
20
22
 
21
23
  return undefined;
22
24
  }
23
25
 
24
- return node.type;
26
+ return body.type;
25
27
  }
26
28
 
27
- function isJSXNode(node) {
28
- if (!node) return false;
29
- const type = getReturnType(node);
29
+ function isJSXNode(type) {
30
30
  return type === "JSXElement" || type === "JSXFragment";
31
31
  }
32
32
 
@@ -36,38 +36,33 @@ export const jsxElementRule = {
36
36
  !["GET", "HEAD", "POST", "PUT", "DELETE", "CONNECT", "OPTIONS", "TRACE", "PATCH"].includes(name);
37
37
  }
38
38
 
39
- function checkFunction(node) {
40
- if (!node.id || !node.id.name) return;
41
- const functionName = node.id.name;
42
- const returnStatement = node.body.body.find((stmt) => stmt.type === "ReturnStatement");
39
+ function validate(node, functionName, returnType) {
40
+ // no type or function name, then skip the rule!
41
+ if (!functionName || !returnType) return;
43
42
 
44
- // skip any null returns which are likely used for hooks
45
- if (returnStatement?.argument.type === "Literal" && returnStatement?.argument.value === null) return;
46
-
47
- if (!isJSXNode(returnStatement?.argument) && isPascalCase(functionName)) {
43
+ if (!isJSXNode(returnType) && isPascalCase(functionName)) {
48
44
  context.report({ node, message: `Function-name "${functionName}" must be camelCase` });
49
45
  }
50
46
 
51
- if (isJSXNode(returnStatement?.argument) && !isPascalCase(functionName)) {
47
+ if (isJSXNode(returnType) && !isPascalCase(functionName)) {
52
48
  context.report({ node, message: `JSX element-name "${functionName}" must be PascalCase` });
53
49
  }
54
50
  }
55
51
 
56
52
  return {
57
- FunctionDeclaration: checkFunction,
53
+ FunctionDeclaration: (node) => {
54
+ const functionName = node?.id?.name;
55
+ const returnType = getReturnType(node.body);
56
+
57
+ validate(node, functionName, returnType);
58
+ },
59
+
58
60
  ArrowFunctionExpression: (node) => {
59
61
  if (node.parent.type === "VariableDeclarator") {
60
- const functionName = node.parent.id.name;
61
-
62
- // skip any null returns which are likely used for hooks
63
- if (node.body.type === "Literal" && node.body.value === null) return;
62
+ const functionName = node?.parent?.id?.name;
63
+ const returnType = getReturnType(node.body);
64
64
 
65
- if (!isJSXNode(node.body) && isPascalCase(functionName)) {
66
- context.report({ node, message: `Function-name "${functionName}" must be camelCase` });
67
- }
68
- if (isJSXNode(node.body) && !isPascalCase(functionName)) {
69
- context.report({ node, message: `JSX element-name "${functionName}" must be PascalCase` });
70
- }
65
+ validate(node, functionName, returnType);
71
66
  }
72
67
  },
73
68
  };