@dfinity/eslint-config-oisy-wallet 0.0.5 → 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.
- package/eslint-local-rules.cjs +5 -5
- package/package.json +4 -2
- package/rules/explicit-non-void-return-type.cjs +84 -0
- package/rules/no-relative-imports.cjs +29 -0
- package/rules/no-svelte-store-in-api.cjs +29 -0
- package/rules/prefer-object-params.cjs +117 -0
- package/rules/use-option-type-wrapper.cjs +67 -0
package/eslint-local-rules.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
module.exports = {
|
|
2
|
-
"no-svelte-store-in-api": require("./rules/no-svelte-store-in-api"),
|
|
3
|
-
"use-option-type-wrapper": require("./rules/use-option-type-wrapper"),
|
|
4
|
-
"prefer-object-params": require("./rules/prefer-object-params"),
|
|
5
|
-
"no-relative-imports": require("./rules/no-relative-imports"),
|
|
6
|
-
"explicit-non-void-return-type": require("./rules/explicit-non-void-return-type"),
|
|
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"),
|
|
7
7
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dfinity/eslint-config-oisy-wallet",
|
|
3
|
-
"version": "0.0.
|
|
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
|
".": {
|
|
@@ -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
|
+
};
|