@dfinity/eslint-config-oisy-wallet 0.0.4 → 0.0.6

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.
@@ -1,249 +1,7 @@
1
1
  module.exports = {
2
- "no-svelte-store-in-api": {
3
- meta: {
4
- docs: {
5
- description:
6
- "Svelte stores should not be used in APIs since they are also utilized by workers.",
7
- category: "Possible Errors",
8
- recommended: false,
9
- },
10
- schema: [],
11
- },
12
- create(context) {
13
- return {
14
- ImportDeclaration(node) {
15
- const filePath = context.getFilename();
16
-
17
- const {
18
- source: { value },
19
- } = node;
20
-
21
- if (filePath.includes("/api/") && value === "svelte/store") {
22
- context.report({
23
- node,
24
- message:
25
- "Importing 'svelte/store' is not allowed in API modules.",
26
- });
27
- }
28
- },
29
- };
30
- },
31
- },
32
- "use-option-type-wrapper": {
33
- meta: {
34
- type: "suggestion",
35
- docs: {
36
- description: "Enforce use of Option<T> instead of T | null | undefined",
37
- category: "Best Practices",
38
- recommended: true,
39
- },
40
- messages: {
41
- useOption:
42
- "Use Option<{{ type }}> instead of {{ type }} | null | undefined.",
43
- },
44
- fixable: "code",
45
- schema: [],
46
- },
47
- create: function (context) {
48
- const checkForOptionType = (node) => {
49
- if (
50
- node.typeAnnotation.type === "TSUnionType" &&
51
- node.typeAnnotation.types.length === 3 &&
52
- node.typeAnnotation.types.some((t) => t.type === "TSNullKeyword") &&
53
- node.typeAnnotation.types.some((t) => t.type === "TSUndefinedKeyword")
54
- ) {
55
- const type = node.typeAnnotation.types.find(
56
- (t) =>
57
- t.type !== "TSNullKeyword" && t.type !== "TSUndefinedKeyword",
58
- );
59
-
60
- const typeText =
61
- type.type === "TSTypeReference" &&
62
- type.typeName &&
63
- type.typeName.name
64
- ? type.typeName.name
65
- : context.getSourceCode().getText(type);
66
-
67
- if (type) {
68
- try {
69
- context.report({
70
- node,
71
- messageId: "useOption",
72
- data: {
73
- type: typeText,
74
- },
75
- fix(fixer) {
76
- return fixer.replaceText(
77
- node.typeAnnotation,
78
- `Option<${typeText}>`,
79
- );
80
- },
81
- });
82
- } catch (e) {
83
- // eslint-disable-next-line no-console
84
- console.error(e);
85
- // eslint-disable-next-line no-console
86
- console.log(type);
87
- }
88
- }
89
- }
90
- };
91
-
92
- return {
93
- TSTypeAnnotation(node) {
94
- checkForOptionType(node);
95
- },
96
- TSTypeAliasDeclaration(node) {
97
- checkForOptionType(node);
98
- },
99
- };
100
- },
101
- },
102
- "prefer-object-params": {
103
- meta: {
104
- type: "suggestion",
105
- docs: {
106
- description:
107
- "Enforce passing parameters as an object when a function has more than one parameter",
108
- category: "Best Practices",
109
- recommended: true,
110
- },
111
- schema: [],
112
- },
113
- create(context) {
114
- const checkForMoreThanOneParameter = (node) => {
115
- const parent = node.parent;
116
-
117
- const nonNullish = (element) =>
118
- element !== undefined && element !== null;
119
-
120
- // Check if it is a callback for looping methods
121
- if (
122
- nonNullish(parent) &&
123
- parent.type === "CallExpression" &&
124
- parent.callee.type === "MemberExpression" &&
125
- [
126
- "map",
127
- "reduce",
128
- "forEach",
129
- "filter",
130
- "sort",
131
- "replace",
132
- "concat",
133
- "copyWithin",
134
- "every",
135
- "fill",
136
- "find",
137
- "findIndex",
138
- "findLast",
139
- "findLastIndex",
140
- "flatMap",
141
- "includes",
142
- "indexOf",
143
- "lastIndexOf",
144
- "push",
145
- "reduce",
146
- "reduceRight",
147
- "slice",
148
- "splice",
149
- "toLocaleString",
150
- "toSpliced",
151
- "unshift",
152
- "with",
153
- ].includes(parent.callee.property.name)
154
- ) {
155
- return;
156
- }
157
-
158
- // Check if it is a callback for Array.from
159
- if (
160
- nonNullish(parent) &&
161
- parent.type === "CallExpression" &&
162
- parent.callee.type === "MemberExpression" &&
163
- parent.callee.object.name === "Array" &&
164
- parent.callee.property.name === "from"
165
- ) {
166
- return;
167
- }
168
-
169
- // Check if it is a callback in a Promise constructor
170
- if (
171
- nonNullish(parent) &&
172
- parent.type === "NewExpression" &&
173
- parent.callee.name === "Promise"
174
- ) {
175
- return;
176
- }
177
-
178
- // Check if it is a callback in JSON.stringify
179
- if (
180
- nonNullish(parent) &&
181
- parent.type === "CallExpression" &&
182
- parent.callee.type === "MemberExpression" &&
183
- parent.callee.object.name === "JSON" &&
184
- parent.callee.property.name === "stringify"
185
- ) {
186
- return;
187
- }
188
-
189
- // Check if it is inside a class constructor
190
- if (
191
- nonNullish(parent) &&
192
- parent.type === "MethodDefinition" &&
193
- parent.kind === "constructor"
194
- ) {
195
- return;
196
- }
197
-
198
- if (node.params.length > 1) {
199
- context.report({
200
- node,
201
- message:
202
- "Functions with more than one parameter should accept an object and use destructuring.",
203
- });
204
- }
205
- };
206
-
207
- return {
208
- FunctionDeclaration(node) {
209
- checkForMoreThanOneParameter(node);
210
- },
211
- FunctionExpression(node) {
212
- checkForMoreThanOneParameter(node);
213
- },
214
- ArrowFunctionExpression(node) {
215
- checkForMoreThanOneParameter(node);
216
- },
217
- };
218
- },
219
- },
220
- "no-relative-imports": {
221
- meta: {
222
- type: "suggestion",
223
- docs: {
224
- description: "Disallow all relative imports",
225
- category: "Best Practices",
226
- recommended: false,
227
- },
228
- messages: {
229
- noRelativeImports:
230
- "Relative imports are not allowed. Use an alias or absolute imports.",
231
- },
232
- schema: [],
233
- },
234
- create(context) {
235
- return {
236
- ImportDeclaration(node) {
237
- const path = node.source.value;
238
-
239
- if (path.startsWith("./")) {
240
- context.report({
241
- node,
242
- messageId: "noRelativeImports",
243
- });
244
- }
245
- },
246
- };
247
- },
248
- },
2
+ "no-svelte-store-in-api": require("./rules/no-svelte-store-in-api.cjs"),
3
+ "use-option-type-wrapper": require("./rules/use-option-type-wrapper.cjs"),
4
+ "prefer-object-params": require("./rules/prefer-object-params.cjs"),
5
+ "no-relative-imports": require("./rules/no-relative-imports.cjs"),
6
+ "explicit-non-void-return-type": require("./rules/explicit-non-void-return-type.cjs"),
249
7
  };
package/index.cjs CHANGED
@@ -34,11 +34,11 @@ module.exports = {
34
34
  },
35
35
  overrides: [
36
36
  {
37
- files: ['*.ts'],
37
+ files: ["*.ts"],
38
38
  rules: {
39
- 'prefer-const': 'error'
40
- }
41
- }
39
+ "prefer-const": "error",
40
+ },
41
+ },
42
42
  ],
43
43
  rules: {
44
44
  "@typescript-eslint/consistent-type-definitions": "error",
@@ -66,6 +66,7 @@ module.exports = {
66
66
  "no-else-return": ["warn", { allowElseIf: false }],
67
67
  "no-unused-vars": "off",
68
68
  "prefer-template": "error",
69
+ "require-await": "error",
69
70
  },
70
71
  globals: {
71
72
  NodeJS: true,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dfinity/eslint-config-oisy-wallet",
3
- "version": "0.0.4",
3
+ "version": "0.0.6",
4
4
  "license": "Apache-2.0",
5
5
  "description": "Shared ESLint configurations from the Oisy Wallet team",
6
6
  "repository": {
@@ -12,10 +12,12 @@
12
12
  },
13
13
  "main": "index.cjs",
14
14
  "files": [
15
+ "README.md",
15
16
  "LICENSE",
16
17
  "index.cjs",
17
18
  "svelte.cjs",
18
- "eslint-local-rules.cjs"
19
+ "eslint-local-rules.cjs",
20
+ "rules/**/*.cjs"
19
21
  ],
20
22
  "exports": {
21
23
  ".": {
@@ -56,8 +58,8 @@
56
58
  "peerDependencies": {
57
59
  "@typescript-eslint/eslint-plugin": "^6.20.0",
58
60
  "eslint": "^8.57.0",
61
+ "eslint-config-love": "^84.1.0",
59
62
  "eslint-config-prettier": "^9.1.0",
60
- "eslint-config-standard-with-typescript": "^43.0.1",
61
63
  "eslint-plugin-import": "^2.29.1",
62
64
  "eslint-plugin-local-rules": "^3.0.2",
63
65
  "eslint-plugin-n": "^16.6.2",
@@ -0,0 +1,84 @@
1
+ const inferReturnType = (node) => {
2
+ if (node.async) {
3
+ if (
4
+ node.body.type === "BlockStatement" &&
5
+ !node.body.body.some((statement) => statement.type === "ReturnStatement")
6
+ ) {
7
+ return "Promise<void>";
8
+ }
9
+ return "Promise<unknown>";
10
+ }
11
+
12
+ if (
13
+ node.body.type === "BlockStatement" &&
14
+ !node.body.body.some((statement) => statement.type === "ReturnStatement")
15
+ ) {
16
+ return "void";
17
+ }
18
+
19
+ const returnStatements = node.body.body.filter(
20
+ (statement) => statement.type === "ReturnStatement",
21
+ );
22
+
23
+ if (returnStatements.length > 0) {
24
+ const returnArgument = returnStatements[0].argument;
25
+
26
+ if (returnArgument) {
27
+ if (returnArgument.type === "Literal") {
28
+ return typeof returnArgument.value;
29
+ }
30
+
31
+ if (returnArgument.type === "ObjectExpression") {
32
+ return "{}";
33
+ }
34
+ }
35
+ }
36
+
37
+ return "unknown";
38
+ };
39
+
40
+ const checkReturnType = ({ node, context }) => {
41
+ const returnType = node.returnType;
42
+
43
+ if (!returnType) {
44
+ const inferredType = inferReturnType(node);
45
+
46
+ if (inferredType !== "void" && inferredType !== "Promise<void>") {
47
+ context.report({
48
+ node,
49
+ messageId: "missingReturnType",
50
+ });
51
+ }
52
+ }
53
+ };
54
+
55
+ module.exports = {
56
+ meta: {
57
+ type: "suggestion",
58
+ docs: {
59
+ description:
60
+ "Require explicit return types for functions, except when returning void",
61
+ category: "Best Practices",
62
+ recommended: false,
63
+ },
64
+ messages: {
65
+ missingReturnType:
66
+ "Function return type must be explicitly defined unless it returns void.",
67
+ },
68
+ schema: [],
69
+ },
70
+
71
+ create(context) {
72
+ return {
73
+ FunctionDeclaration(node) {
74
+ checkReturnType({ node, context });
75
+ },
76
+ FunctionExpression(node) {
77
+ checkReturnType({ node, context });
78
+ },
79
+ ArrowFunctionExpression(node) {
80
+ checkReturnType({ node, context });
81
+ },
82
+ };
83
+ },
84
+ };
@@ -0,0 +1,29 @@
1
+ module.exports = {
2
+ meta: {
3
+ type: "suggestion",
4
+ docs: {
5
+ description: "Disallow all relative imports",
6
+ category: "Best Practices",
7
+ recommended: false,
8
+ },
9
+ messages: {
10
+ noRelativeImports:
11
+ "Relative imports are not allowed. Use an alias or absolute imports.",
12
+ },
13
+ schema: [],
14
+ },
15
+ create(context) {
16
+ return {
17
+ ImportDeclaration(node) {
18
+ const path = node.source.value;
19
+
20
+ if (path.startsWith("./")) {
21
+ context.report({
22
+ node,
23
+ messageId: "noRelativeImports",
24
+ });
25
+ }
26
+ },
27
+ };
28
+ },
29
+ };
@@ -0,0 +1,29 @@
1
+ module.exports = {
2
+ meta: {
3
+ docs: {
4
+ description:
5
+ "Svelte stores should not be used in APIs since they are also utilized by workers.",
6
+ category: "Possible Errors",
7
+ recommended: false,
8
+ },
9
+ schema: [],
10
+ },
11
+ create(context) {
12
+ return {
13
+ ImportDeclaration(node) {
14
+ const filePath = context.getFilename();
15
+
16
+ const {
17
+ source: { value },
18
+ } = node;
19
+
20
+ if (filePath.includes("/api/") && value === "svelte/store") {
21
+ context.report({
22
+ node,
23
+ message: "Importing 'svelte/store' is not allowed in API modules.",
24
+ });
25
+ }
26
+ },
27
+ };
28
+ },
29
+ };
@@ -0,0 +1,117 @@
1
+ module.exports = {
2
+ meta: {
3
+ type: "suggestion",
4
+ docs: {
5
+ description:
6
+ "Enforce passing parameters as an object when a function has more than one parameter",
7
+ category: "Best Practices",
8
+ recommended: true,
9
+ },
10
+ schema: [],
11
+ },
12
+ create(context) {
13
+ const checkForMoreThanOneParameter = (node) => {
14
+ const parent = node.parent;
15
+
16
+ const nonNullish = (element) => element !== undefined && element !== null;
17
+
18
+ // Check if it is a callback for looping methods
19
+ if (
20
+ nonNullish(parent) &&
21
+ parent.type === "CallExpression" &&
22
+ parent.callee.type === "MemberExpression" &&
23
+ [
24
+ "map",
25
+ "reduce",
26
+ "forEach",
27
+ "filter",
28
+ "sort",
29
+ "replace",
30
+ "concat",
31
+ "copyWithin",
32
+ "every",
33
+ "fill",
34
+ "find",
35
+ "findIndex",
36
+ "findLast",
37
+ "findLastIndex",
38
+ "flatMap",
39
+ "includes",
40
+ "indexOf",
41
+ "lastIndexOf",
42
+ "push",
43
+ "reduce",
44
+ "reduceRight",
45
+ "slice",
46
+ "splice",
47
+ "toLocaleString",
48
+ "toSpliced",
49
+ "unshift",
50
+ "with",
51
+ ].includes(parent.callee.property.name)
52
+ ) {
53
+ return;
54
+ }
55
+
56
+ // Check if it is a callback for Array.from
57
+ if (
58
+ nonNullish(parent) &&
59
+ parent.type === "CallExpression" &&
60
+ parent.callee.type === "MemberExpression" &&
61
+ parent.callee.object.name === "Array" &&
62
+ parent.callee.property.name === "from"
63
+ ) {
64
+ return;
65
+ }
66
+
67
+ // Check if it is a callback in a Promise constructor
68
+ if (
69
+ nonNullish(parent) &&
70
+ parent.type === "NewExpression" &&
71
+ parent.callee.name === "Promise"
72
+ ) {
73
+ return;
74
+ }
75
+
76
+ // Check if it is a callback in JSON.stringify
77
+ if (
78
+ nonNullish(parent) &&
79
+ parent.type === "CallExpression" &&
80
+ parent.callee.type === "MemberExpression" &&
81
+ parent.callee.object.name === "JSON" &&
82
+ parent.callee.property.name === "stringify"
83
+ ) {
84
+ return;
85
+ }
86
+
87
+ // Check if it is inside a class constructor
88
+ if (
89
+ nonNullish(parent) &&
90
+ parent.type === "MethodDefinition" &&
91
+ parent.kind === "constructor"
92
+ ) {
93
+ return;
94
+ }
95
+
96
+ if (node.params.length > 1) {
97
+ context.report({
98
+ node,
99
+ message:
100
+ "Functions with more than one parameter should accept an object and use destructuring.",
101
+ });
102
+ }
103
+ };
104
+
105
+ return {
106
+ FunctionDeclaration(node) {
107
+ checkForMoreThanOneParameter(node);
108
+ },
109
+ FunctionExpression(node) {
110
+ checkForMoreThanOneParameter(node);
111
+ },
112
+ ArrowFunctionExpression(node) {
113
+ checkForMoreThanOneParameter(node);
114
+ },
115
+ };
116
+ },
117
+ };
@@ -0,0 +1,67 @@
1
+ module.exports = {
2
+ meta: {
3
+ type: "suggestion",
4
+ docs: {
5
+ description: "Enforce use of Option<T> instead of T | null | undefined",
6
+ category: "Best Practices",
7
+ recommended: true,
8
+ },
9
+ messages: {
10
+ useOption:
11
+ "Use Option<{{ type }}> instead of {{ type }} | null | undefined.",
12
+ },
13
+ fixable: "code",
14
+ schema: [],
15
+ },
16
+ create: function (context) {
17
+ const checkForOptionType = (node) => {
18
+ if (
19
+ node.typeAnnotation.type === "TSUnionType" &&
20
+ node.typeAnnotation.types.length === 3 &&
21
+ node.typeAnnotation.types.some((t) => t.type === "TSNullKeyword") &&
22
+ node.typeAnnotation.types.some((t) => t.type === "TSUndefinedKeyword")
23
+ ) {
24
+ const type = node.typeAnnotation.types.find(
25
+ (t) => t.type !== "TSNullKeyword" && t.type !== "TSUndefinedKeyword",
26
+ );
27
+
28
+ const typeText =
29
+ type.type === "TSTypeReference" && type.typeName && type.typeName.name
30
+ ? type.typeName.name
31
+ : context.getSourceCode().getText(type);
32
+
33
+ if (type) {
34
+ try {
35
+ context.report({
36
+ node,
37
+ messageId: "useOption",
38
+ data: {
39
+ type: typeText,
40
+ },
41
+ fix(fixer) {
42
+ return fixer.replaceText(
43
+ node.typeAnnotation,
44
+ `Option<${typeText}>`,
45
+ );
46
+ },
47
+ });
48
+ } catch (e) {
49
+ // eslint-disable-next-line no-console
50
+ console.error(e);
51
+ // eslint-disable-next-line no-console
52
+ console.log(type);
53
+ }
54
+ }
55
+ }
56
+ };
57
+
58
+ return {
59
+ TSTypeAnnotation(node) {
60
+ checkForOptionType(node);
61
+ },
62
+ TSTypeAliasDeclaration(node) {
63
+ checkForOptionType(node);
64
+ },
65
+ };
66
+ },
67
+ };