@dfinity/eslint-config-oisy-wallet 0.0.1 → 0.0.2

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/README.md CHANGED
@@ -46,6 +46,15 @@ module.exports = {
46
46
  };
47
47
  ```
48
48
 
49
+ Finally, create an `eslint-local-rules.cjs` file at the root of your project containing the following:
50
+
51
+ ```javascript
52
+ module.exports = require("@dfinity/eslint-config-oisy-wallet/eslint-local-rules");
53
+ ```
54
+
55
+ > [!NOTE]
56
+ > This is necessary because the `eslint-plugin-local-rules` plugin we use for custom rules requires a file located at the root and does not offer any customizable location option.
57
+
49
58
  ## 🛠️ TypeScript Support
50
59
 
51
60
  If your project uses TypeScript, make sure you have a `tsconfig.json` file in your project root.
@@ -92,55 +92,94 @@ module.exports = {
92
92
  };
93
93
  },
94
94
  },
95
- "use-nullish-checks": {
95
+ "prefer-object-params": {
96
96
  meta: {
97
97
  type: "suggestion",
98
98
  docs: {
99
99
  description:
100
- "Enforce the use of isNullish functions for nullish checks",
100
+ "Enforce passing parameters as an object when a function has more than one parameter",
101
101
  category: "Best Practices",
102
102
  recommended: true,
103
103
  },
104
- fixable: "code",
105
104
  schema: [],
106
105
  },
107
106
  create(context) {
108
- const isNullishMessage =
109
- "Use isNullish() instead of direct variable check for nullish checks.";
110
- const nonNullishMessage =
111
- "Use nonNullish() instead of direct variable check for nullish checks.";
107
+ const checkForMoreThanOneParameter = (node) => {
108
+ const parent = node.parent;
112
109
 
113
- const binaryCheck = (node) => {
114
- if (node.type === "BinaryExpression") {
115
- return (
116
- (node.operator === "===" || node.operator === "!==") &&
117
- ((node.right.type === "Identifier" &&
118
- node.right.name === "undefined") ||
119
- // eslint-disable-next-line local-rules/use-nullish-checks -- This is the rule that is being implemented
120
- (node.right.type === "Literal" && node.right.value === null))
121
- );
110
+ const nonNullish = (element) =>
111
+ element !== undefined && element !== null;
112
+
113
+ // Check if it is a callback for looping methods
114
+ if (
115
+ nonNullish(parent) &&
116
+ parent.type === "CallExpression" &&
117
+ parent.callee.type === "MemberExpression" &&
118
+ ["map", "reduce", "forEach", "filter", "sort", "replace"].includes(
119
+ parent.callee.property.name,
120
+ )
121
+ ) {
122
+ return;
122
123
  }
123
- };
124
124
 
125
- const binaryReportCheck = (node) => {
126
- context.report({
127
- node,
128
- message:
129
- node.operator === "===" ? isNullishMessage : nonNullishMessage,
130
- fix(fixer) {
131
- return fixer.replaceText(
132
- node,
133
- `${node.operator === "===" ? "isNullish" : "nonNullish"}(${context.getSourceCode().getText(node.left)})`,
134
- );
135
- },
136
- });
125
+ // Check if it is a callback for Array.from
126
+ if (
127
+ nonNullish(parent) &&
128
+ parent.type === "CallExpression" &&
129
+ parent.callee.type === "MemberExpression" &&
130
+ parent.callee.object.name === "Array" &&
131
+ parent.callee.property.name === "from"
132
+ ) {
133
+ return;
134
+ }
135
+
136
+ // Check if it is a callback in a Promise constructor
137
+ if (
138
+ nonNullish(parent) &&
139
+ parent.type === "NewExpression" &&
140
+ parent.callee.name === "Promise"
141
+ ) {
142
+ return;
143
+ }
144
+
145
+ // Check if it is a callback in JSON.stringify
146
+ if (
147
+ nonNullish(parent) &&
148
+ parent.type === "CallExpression" &&
149
+ parent.callee.type === "MemberExpression" &&
150
+ parent.callee.object.name === "JSON" &&
151
+ parent.callee.property.name === "stringify"
152
+ ) {
153
+ return;
154
+ }
155
+
156
+ // Check if it is inside a class constructor
157
+ if (
158
+ nonNullish(parent) &&
159
+ parent.type === "MethodDefinition" &&
160
+ parent.kind === "constructor"
161
+ ) {
162
+ return;
163
+ }
164
+
165
+ if (node.params.length > 1) {
166
+ context.report({
167
+ node,
168
+ message:
169
+ "Functions with more than one parameter should accept an object and use destructuring.",
170
+ });
171
+ }
137
172
  };
138
173
 
139
174
  return {
140
- BinaryExpression(node) {
141
- if (binaryCheck(node)) {
142
- binaryReportCheck(node);
143
- }
175
+ FunctionDeclaration(node) {
176
+ checkForMoreThanOneParameter(node);
177
+ },
178
+ FunctionExpression(node) {
179
+ checkForMoreThanOneParameter(node);
180
+ },
181
+ ArrowFunctionExpression(node) {
182
+ checkForMoreThanOneParameter(node);
144
183
  },
145
184
  };
146
185
  },
package/index.cjs CHANGED
@@ -33,8 +33,8 @@ module.exports = {
33
33
  "@typescript-eslint/prefer-reduce-type-parameter": "error",
34
34
  "arrow-body-style": ["warn", "as-needed"],
35
35
  curly: "error",
36
+ "local-rules/prefer-object-params": "warn",
36
37
  "local-rules/use-option-type-wrapper": "warn",
37
- "local-rules/use-nullish-checks": "warn",
38
38
  "import/no-duplicates": ["error", { "prefer-inline": true }],
39
39
  "no-console": ["error", { allow: ["error", "warn"] }],
40
40
  "no-continue": "warn",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dfinity/eslint-config-oisy-wallet",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "license": "Apache-2.0",
5
5
  "description": "Shared ESLint configurations from the Oisy Wallet team",
6
6
  "repository": {
@@ -26,6 +26,10 @@
26
26
  "./svelte": {
27
27
  "import": "./svelte.cjs",
28
28
  "require": "./svelte.cjs"
29
+ },
30
+ "./eslint-local-rules": {
31
+ "import": "./eslint-local-rules.cjs",
32
+ "require": "./eslint-local-rules.cjs"
29
33
  }
30
34
  },
31
35
  "scripts": {