@checkdigit/eslint-plugin 7.13.1-PR.120-6b16 → 7.14.0-PR.57-0f87
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/dist-mjs/index.mjs +9 -2
- package/dist-mjs/no-side-effects.mjs +31 -87
- package/dist-mjs/no-util.mjs +47 -0
- package/dist-types/no-util.d.ts +5 -0
- package/package.json +1 -1
- package/src/file-path-comment.ts +1 -1
- package/src/index.ts +8 -1
- package/src/no-side-effects.ts +70 -161
- package/src/no-util.ts +53 -0
package/dist-mjs/index.mjs
CHANGED
|
@@ -23,6 +23,7 @@ import noEnum from "./no-enum.mjs";
|
|
|
23
23
|
import noSideEffects from "./no-side-effects.mjs";
|
|
24
24
|
import noRandomV4UUID from "./no-random-v4-uuid.mjs";
|
|
25
25
|
import noTestImport from "./no-test-import.mjs";
|
|
26
|
+
import noUtil from "./no-util.mjs";
|
|
26
27
|
import noUuid from "./no-uuid.mjs";
|
|
27
28
|
import noWallabyComment from "./no-wallaby-comment.mjs";
|
|
28
29
|
import objectLiteralResponse from "./object-literal-response.mjs";
|
|
@@ -37,6 +38,7 @@ var rules = {
|
|
|
37
38
|
"no-enum": noEnum,
|
|
38
39
|
"no-random-v4-uuid": noRandomV4UUID,
|
|
39
40
|
"no-status-code-assert": noStatusCodeAssert,
|
|
41
|
+
"no-util": noUtil,
|
|
40
42
|
"no-uuid": noUuid,
|
|
41
43
|
"require-assert-message": requireAssertMessage,
|
|
42
44
|
"require-strict-assert": requireStrictAssert,
|
|
@@ -73,12 +75,16 @@ var configs = {
|
|
|
73
75
|
"@checkdigit/file-path-comment": "error",
|
|
74
76
|
"@checkdigit/no-random-v4-uuid": "error",
|
|
75
77
|
"@checkdigit/no-status-code-assert": "error",
|
|
78
|
+
"@checkdigit/no-util": "error",
|
|
76
79
|
"@checkdigit/no-uuid": "error",
|
|
77
80
|
"@checkdigit/require-assert-message": "error",
|
|
78
81
|
"@checkdigit/require-strict-assert": "error",
|
|
79
82
|
"@checkdigit/require-ts-extension-imports-exports": "error",
|
|
80
83
|
"@checkdigit/no-wallaby-comment": "error",
|
|
81
|
-
"@checkdigit/no-side-effects":
|
|
84
|
+
"@checkdigit/no-side-effects": [
|
|
85
|
+
"error",
|
|
86
|
+
{ excludedIdentifiers: ["assert", "debug", "log", "promisify", "Symbol.for"] }
|
|
87
|
+
],
|
|
82
88
|
"@checkdigit/regular-expression-comment": "error",
|
|
83
89
|
"@checkdigit/require-assert-predicate-rejects-throws": "error",
|
|
84
90
|
"@checkdigit/object-literal-response": "error",
|
|
@@ -107,6 +113,7 @@ var configs = {
|
|
|
107
113
|
"@checkdigit/file-path-comment": "off",
|
|
108
114
|
"@checkdigit/no-random-v4-uuid": "error",
|
|
109
115
|
"@checkdigit/no-status-code-assert": "error",
|
|
116
|
+
"@checkdigit/no-util": "error",
|
|
110
117
|
"@checkdigit/no-uuid": "error",
|
|
111
118
|
"@checkdigit/require-assert-message": "error",
|
|
112
119
|
"@checkdigit/require-strict-assert": "error",
|
|
@@ -138,4 +145,4 @@ var src_default = defaultToExport;
|
|
|
138
145
|
export {
|
|
139
146
|
src_default as default
|
|
140
147
|
};
|
|
141
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
148
|
+
//# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsiLi4vc3JjL2luZGV4LnRzIl0sCiAgIm1hcHBpbmdzIjogIjtBQVVBLE9BQU8sd0JBQXdCLFVBQVUsa0NBQWtDO0FBQzNFLE9BQU8sdUJBQXVCLFVBQVUsaUNBQWlDO0FBQ3pFLE9BQU8seUJBQXlCLFVBQVUsbUNBQW1DO0FBQzdFLE9BQU8sMkJBQTJCLFVBQVUscUNBQXFDO0FBQ2pGLE9BQU8sd0JBQXdCO0FBQy9CLE9BQU87QUFBQSxFQUNMLFVBQVU7QUFBQSxPQUNMO0FBQ1AsT0FBTztBQUFBLEVBQ0wsVUFBVTtBQUFBL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UFFLEdBQUc7QUFBQSxRQUNoRCxDQUFDLGVBQWUsZ0NBQWdDLEVBQUUsR0FBRztBQUFBLFFBQ3JELENBQUMsZUFBZSx5QkFBeUIsRUFBRSxHQUFHO0FBQUEsUUFDOUMsQ0FBQyxlQUFlLGdDQUFnQyxFQUFFLEdBQUc7QUFBQSxRQUNyRCxDQUFDLGVBQWUscUNBQXFDLEVBQUUsR0FBRztBQUFBLFFBQzFELENBQUMsZUFBZSxvQkFBb0IsRUFBRSxHQUFHO0FBQUEsUUFDekMsQ0FBQyxlQUFlLDJDQUEyQyxFQUFFLEdBQUc7QUFBQSxNQUNsRTtBQUFBLElBQ0Y7QUFBQSxFQUNGO0FBQ0Y7QUFFQSxJQUFNLGtCQUVGO0FBQUEsRUFDRixHQUFHO0FBQUEsRUFDSDtBQUNGO0FBQ0EsSUFBTyxjQUFROyIsCiAgIm5hbWVzIjogW10KfQo=
|
|
@@ -1,29 +1,30 @@
|
|
|
1
1
|
// src/no-side-effects.ts
|
|
2
2
|
import { ESLintUtils } from "@typescript-eslint/utils";
|
|
3
3
|
import { TSESTree } from "@typescript-eslint/typescript-estree";
|
|
4
|
-
import getDocumentationUrl from "./get-documentation-url.mjs";
|
|
5
4
|
var ruleId = "no-side-effects";
|
|
6
5
|
var NO_SIDE_EFFECTS = "NO_SIDE_EFFECTS";
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
TSESTree.AST_NODE_TYPES.
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
TSESTree.AST_NODE_TYPES.
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
)
|
|
26
|
-
|
|
6
|
+
function isExpressionStatement(node) {
|
|
7
|
+
return node.type === TSESTree.AST_NODE_TYPES.ExpressionStatement;
|
|
8
|
+
}
|
|
9
|
+
function isAwaitExpression(statement) {
|
|
10
|
+
return isExpressionStatement(statement) && statement.expression.type === TSESTree.AST_NODE_TYPES.AwaitExpression;
|
|
11
|
+
}
|
|
12
|
+
function isCallExpressionCalleeMemberExpression(statement, excludedIdentifiers) {
|
|
13
|
+
return isExpressionStatement(statement) && statement.expression.type === TSESTree.AST_NODE_TYPES.CallExpression && statement.expression.callee.type === TSESTree.AST_NODE_TYPES.MemberExpression && statement.expression.callee.object.type === TSESTree.AST_NODE_TYPES.Identifier && !excludedIdentifiers.includes(statement.expression.callee.object.name);
|
|
14
|
+
}
|
|
15
|
+
function isVariableDeclarationAwaitExpression(node) {
|
|
16
|
+
return node.type === TSESTree.AST_NODE_TYPES.VariableDeclaration && node.declarations.length > 0 && node.declarations[0]?.init?.type === TSESTree.AST_NODE_TYPES.AwaitExpression;
|
|
17
|
+
}
|
|
18
|
+
function isIdentifierCallee(node, excludedIdentifiers) {
|
|
19
|
+
return node.callee.type === TSESTree.AST_NODE_TYPES.Identifier && !excludedIdentifiers.includes(node.callee.name);
|
|
20
|
+
}
|
|
21
|
+
function isMemberExpressionCallee(node, excludedIdentifiers) {
|
|
22
|
+
return node.callee.type === TSESTree.AST_NODE_TYPES.MemberExpression && node.callee.object.type === TSESTree.AST_NODE_TYPES.Identifier && node.callee.property.type === TSESTree.AST_NODE_TYPES.Identifier && !excludedIdentifiers.includes(`${node.callee.object.name}.${node.callee.property.name}`);
|
|
23
|
+
}
|
|
24
|
+
function isNonIdentifierObjectMemberExpressionCallee(node) {
|
|
25
|
+
return node.callee.type === TSESTree.AST_NODE_TYPES.MemberExpression && node.callee.object.type !== TSESTree.AST_NODE_TYPES.Identifier;
|
|
26
|
+
}
|
|
27
|
+
function isVariableDeclarationCallExpression(node, excludedIdentifiers) {
|
|
27
28
|
if (node.type !== TSESTree.AST_NODE_TYPES.VariableDeclaration || node.declarations.length === 0) {
|
|
28
29
|
return false;
|
|
29
30
|
}
|
|
@@ -31,71 +32,18 @@ var isVariableDeclarationCallExpression = (node, excludedIdentifiers) => {
|
|
|
31
32
|
if (init?.type !== TSESTree.AST_NODE_TYPES.CallExpression) {
|
|
32
33
|
return false;
|
|
33
34
|
}
|
|
34
|
-
const callee = init.callee;
|
|
35
|
-
if (callee.type === TSESTree.AST_NODE_TYPES.ArrowFunctionExpression || callee.type === TSESTree.AST_NODE_TYPES.FunctionExpression) {
|
|
36
|
-
return true;
|
|
37
|
-
}
|
|
38
35
|
return isIdentifierCallee(init, excludedIdentifiers) || isMemberExpressionCallee(init, excludedIdentifiers) || isNonIdentifierObjectMemberExpressionCallee(init);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
));
|
|
44
|
-
var isVariableDeclarationNewExpression = (node) => {
|
|
45
|
-
if (node.type !== TSESTree.AST_NODE_TYPES.VariableDeclaration || node.declarations.length === 0) {
|
|
46
|
-
return false;
|
|
47
|
-
}
|
|
48
|
-
const init = node.declarations[0]?.init;
|
|
49
|
-
return init?.type === TSESTree.AST_NODE_TYPES.NewExpression;
|
|
50
|
-
};
|
|
51
|
-
var isFunctionDeclarationAwaitExpression = (node) => node.type === TSESTree.AST_NODE_TYPES.FunctionDeclaration && node.body.body.some(
|
|
52
|
-
(statement) => statement.type === TSESTree.AST_NODE_TYPES.ExpressionStatement && statement.expression.type === TSESTree.AST_NODE_TYPES.AwaitExpression
|
|
53
|
-
);
|
|
54
|
-
var hasSideEffects = (statement, excludedIdentifiers) => {
|
|
55
|
-
if (isAwaitExpression(statement)) {
|
|
56
|
-
return TSESTree.AST_NODE_TYPES.AwaitExpression;
|
|
57
|
-
}
|
|
58
|
-
if (isCallExpressionCalleeMemberExpression(statement, excludedIdentifiers)) {
|
|
59
|
-
return "CallExpressionCalleeMemberExpression";
|
|
60
|
-
}
|
|
61
|
-
if (isVariableDeclarationAwaitExpression(statement)) {
|
|
62
|
-
return "VariableDeclarationAwaitExpression";
|
|
63
|
-
}
|
|
64
|
-
if (isVariableDeclarationCallExpression(statement, excludedIdentifiers)) {
|
|
65
|
-
return "VariableDeclarationCallExpression";
|
|
66
|
-
}
|
|
67
|
-
if (isVariableDeclarationNewExpression(statement)) {
|
|
68
|
-
return "VariableDeclarationNewExpression";
|
|
69
|
-
}
|
|
70
|
-
if (isExportNamedDeclarationWithSideEffects(statement, excludedIdentifiers)) {
|
|
71
|
-
return "ExportNamedDeclarationWithSideEffects";
|
|
72
|
-
}
|
|
73
|
-
if (isExpressionStatementWithSideEffects(statement, excludedIdentifiers)) {
|
|
74
|
-
return "ExpressionStatementWithSideEffects";
|
|
75
|
-
}
|
|
76
|
-
if (isControlFlowStatement(statement)) {
|
|
77
|
-
return "ControlFlowStatement";
|
|
78
|
-
}
|
|
79
|
-
if (isNotValidVariableDeclaration(statement)) {
|
|
80
|
-
return "NotValidVariableDeclaration";
|
|
81
|
-
}
|
|
82
|
-
if (isAssignmentExpression(statement)) {
|
|
83
|
-
return TSESTree.AST_NODE_TYPES.AssignmentExpression;
|
|
84
|
-
}
|
|
85
|
-
if (isFunctionDeclarationAwaitExpression(statement)) {
|
|
86
|
-
return "FunctionDeclarationAwaitExpression";
|
|
87
|
-
}
|
|
88
|
-
return null;
|
|
89
|
-
};
|
|
90
|
-
var createRule = ESLintUtils.RuleCreator(
|
|
91
|
-
(name) => getDocumentationUrl(name)
|
|
92
|
-
);
|
|
36
|
+
}
|
|
37
|
+
function hasSideEffects(statement, excludedIdentifiers) {
|
|
38
|
+
return isAwaitExpression(statement) || isCallExpressionCalleeMemberExpression(statement, excludedIdentifiers) || isVariableDeclarationAwaitExpression(statement) || isVariableDeclarationCallExpression(statement, excludedIdentifiers) || statement.type === TSESTree.AST_NODE_TYPES.ExportNamedDeclaration && statement.declaration !== null && (isVariableDeclarationAwaitExpression(statement.declaration) || isVariableDeclarationCallExpression(statement.declaration, excludedIdentifiers));
|
|
39
|
+
}
|
|
40
|
+
var createRule = ESLintUtils.RuleCreator((name) => name);
|
|
93
41
|
var rule = createRule({
|
|
94
42
|
name: ruleId,
|
|
95
43
|
meta: {
|
|
96
44
|
type: "problem",
|
|
97
45
|
docs: {
|
|
98
|
-
description: "Ensure no side effects can occur at the module-level
|
|
46
|
+
description: "Ensure no side effects can occur at the module-level"
|
|
99
47
|
},
|
|
100
48
|
schema: [
|
|
101
49
|
{
|
|
@@ -126,14 +74,10 @@ var rule = createRule({
|
|
|
126
74
|
return;
|
|
127
75
|
}
|
|
128
76
|
node.body.forEach((statement) => {
|
|
129
|
-
|
|
130
|
-
if (sideEffectType !== null) {
|
|
77
|
+
if (hasSideEffects(statement, excludedIdentifiers)) {
|
|
131
78
|
context.report({
|
|
132
79
|
node: statement,
|
|
133
|
-
messageId: NO_SIDE_EFFECTS
|
|
134
|
-
data: {
|
|
135
|
-
sideEffectType
|
|
136
|
-
}
|
|
80
|
+
messageId: NO_SIDE_EFFECTS
|
|
137
81
|
});
|
|
138
82
|
}
|
|
139
83
|
});
|
|
@@ -146,4 +90,4 @@ export {
|
|
|
146
90
|
no_side_effects_default as default,
|
|
147
91
|
ruleId
|
|
148
92
|
};
|
|
149
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
93
|
+
//# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsiLi4vc3JjL25vLXNpZGUtZWZmZWN0cy50cyJdLAogICJtYXBwaW5ncyI6ICI7QUFRQSxTQUFTLG1CQUFtQjtBQUM1QixTQUFTLGdCQUFnQjtBQU1sQixJQUFNLFNBQVM7QUFDdEIsSUFBTSxrQkFBa0I7QUFHeEIsU0FBUyxzQkFBc0IsTUFBMkQ7QUFDeEYsU0FBTyxLQUFLLFNBQVMsU0FBUyxlQUFlO0FBQy9DO0FBR0EsU0FBUyxrQkFBa0IsV0FBbUM7QUFDNUQsU0FBTyxzQkFBc0IsU0FBUyxLQUFLLFVBQVUsV0FBVyxTQUFTLFNBQVMsZUFBZTtBQUNuRztBQUdBLFNBQVMsdUNBQXVDLFdBQTBCLHFCQUF3QztBQUNoSCxTQUNF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
var createRule = ESLintUtils.RuleCreator((name) => name);
|
|
6
|
+
var rule = createRule({
|
|
7
|
+
name: ruleId,
|
|
8
|
+
meta: {
|
|
9
|
+
type: "problem",
|
|
10
|
+
docs: {
|
|
11
|
+
description: "Detects if file name is util"
|
|
12
|
+
},
|
|
13
|
+
schema: [],
|
|
14
|
+
messages: {
|
|
15
|
+
[NO_UTIL]: "File name '{{filename}}' contains banned 'util' pattern."
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
defaultOptions: [],
|
|
19
|
+
create(context) {
|
|
20
|
+
return {
|
|
21
|
+
Program() {
|
|
22
|
+
const filename = context.filename;
|
|
23
|
+
const utilRegex = /(?:^|[-_/])util(?=[-_./]|$)/iu;
|
|
24
|
+
if (utilRegex.test(filename)) {
|
|
25
|
+
const sourceCode = context.sourceCode;
|
|
26
|
+
const tokens = sourceCode.tokensAndComments;
|
|
27
|
+
const firstNonCommentToken = tokens.find(
|
|
28
|
+
(token) => token.type !== AST_TOKEN_TYPES.Block && token.type !== AST_TOKEN_TYPES.Line
|
|
29
|
+
);
|
|
30
|
+
if (firstNonCommentToken !== void 0) {
|
|
31
|
+
context.report({
|
|
32
|
+
messageId: NO_UTIL,
|
|
33
|
+
data: { filename },
|
|
34
|
+
node: firstNonCommentToken
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
var no_util_default = rule;
|
|
43
|
+
export {
|
|
44
|
+
no_util_default as default,
|
|
45
|
+
ruleId
|
|
46
|
+
};
|
|
47
|
+
//# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsiLi4vc3JjL25vLXV0aWwudHMiXSwKICAibWFwcGluZ3MiOiAiO0FBUUEsU0FBUyxpQkFBaUIsbUJBQW1CO0FBRXRDLElBQU0sU0FBUztBQUN0QixJQUFNLFVBQVU7QUFFaEIsSUFBTSx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57-0f87","description":"Check Digit eslint plugins","keywords":["eslint","eslintplugin"],"homepage":"https://github.com/checkdigit/eslint-plugin#readme","bugs":{"url":"https://github.com/checkdigit/eslint-plugin/issues"},"repository":{"type":"git","url":"https://github.com/checkdigit/eslint-plugin"},"license":"MIT","author":"Check Digit, LLC","sideEffects":false,"type":"module","exports":{".":{"types":"./dist-types/index.d.ts","import":"./dist-mjs/index.mjs","default":"./dist-mjs/index.mjs"}},"files":["src","dist-types","dist-mjs","!src/**/test/**","!src/**/*.test.ts","!src/**/*.spec.ts","!dist-types/**/test/**","!dist-types/**/*.test.d.ts","!dist-types/**/*.spec.d.ts","!dist-mjs/**/test/**","!dist-mjs/**/*.test.mjs","!dist-mjs/**/*.spec.mjs","SECURITY.md"],"scripts":{"build:dist-mjs":"rimraf dist-mjs && npx builder --type=module --sourceMap --outDir=dist-mjs && node dist-mjs/index.mjs","build:dist-types":"rimraf dist-types && npx builder --type=types --outDir=dist-types","ci:compile":"tsc --noEmit","ci:coverage":"NODE_OPTIONS=\"--disable-warning ExperimentalWarning --experimental-vm-modules\" jest --coverage=true","ci:lint":"npm run lint","ci:style":"npm run prettier","ci:test":"NODE_OPTIONS=\"--disable-warning ExperimentalWarning --experimental-vm-modules\" jest --coverage=false","lint":"eslint --max-warnings 0 .","lint:fix":"eslint --max-warnings 0 --fix .","prepare":"","prepublishOnly":"npm run build:dist-types && npm run build:dist-mjs","prettier":"prettier --ignore-path .gitignore --list-different .","prettier:fix":"prettier --ignore-path .gitignore --write .","test":"npm run ci:compile && npm run ci:test && npm run ci:lint && npm run ci:style"},"prettier":"@checkdigit/prettier-config","jest":{"preset":"@checkdigit/jest-config"},"dependencies":{"@typescript-eslint/type-utils":"^8.23.0","@typescript-eslint/utils":"^8.23.0","http-status-codes":"^2.3.0","ts-api-utils":"^2.0.1"},"devDependencies":{"@checkdigit/jest-config":"^6.0.2","@checkdigit/prettier-config":"^6.1.0","@checkdigit/typescript-config":"^9.0.0","@eslint/js":"^9.19.0","@types/eslint":"^9.6.1","@types/eslint-config-prettier":"^6.11.3","@typescript-eslint/parser":"^8.23.0","@typescript-eslint/rule-tester":"^8.23.0","eslint":"^9.19.0","eslint-config-prettier":"^10.0.1","eslint-import-resolver-typescript":"^3.7.0","eslint-plugin-eslint-plugin":"^6.4.0","eslint-plugin-import":"^2.31.0","eslint-plugin-no-only-tests":"^3.3.0","eslint-plugin-no-secrets":"^2.2.1","eslint-plugin-node":"^11.1.0","eslint-plugin-sonarjs":"1.0.4","rimraf":"^6.0.1","typescript-eslint":"^8.23.0"},"peerDependencies":{"eslint":">=9 <10"},"engines":{"node":">=22.14"}}
|
package/src/file-path-comment.ts
CHANGED
package/src/index.ts
CHANGED
|
@@ -32,6 +32,7 @@ import noEnum from './no-enum.ts';
|
|
|
32
32
|
import noSideEffects from './no-side-effects.ts';
|
|
33
33
|
import noRandomV4UUID from './no-random-v4-uuid.ts';
|
|
34
34
|
import noTestImport from './no-test-import.ts';
|
|
35
|
+
import noUtil from './no-util.ts';
|
|
35
36
|
import noUuid from './no-uuid.ts';
|
|
36
37
|
import noWallabyComment from './no-wallaby-comment.ts';
|
|
37
38
|
import objectLiteralResponse from './object-literal-response.ts';
|
|
@@ -47,6 +48,7 @@ const rules: Record<string, TSESLint.LooseRuleDefinition> = {
|
|
|
47
48
|
'no-enum': noEnum,
|
|
48
49
|
'no-random-v4-uuid': noRandomV4UUID,
|
|
49
50
|
'no-status-code-assert': noStatusCodeAssert,
|
|
51
|
+
'no-util': noUtil,
|
|
50
52
|
'no-uuid': noUuid,
|
|
51
53
|
'require-assert-message': requireAssertMessage,
|
|
52
54
|
'require-strict-assert': requireStrictAssert,
|
|
@@ -85,12 +87,16 @@ const configs: Record<string, TSESLint.FlatConfig.Config[]> = {
|
|
|
85
87
|
'@checkdigit/file-path-comment': 'error',
|
|
86
88
|
'@checkdigit/no-random-v4-uuid': 'error',
|
|
87
89
|
'@checkdigit/no-status-code-assert': 'error',
|
|
90
|
+
'@checkdigit/no-util': 'error',
|
|
88
91
|
'@checkdigit/no-uuid': 'error',
|
|
89
92
|
'@checkdigit/require-assert-message': 'error',
|
|
90
93
|
'@checkdigit/require-strict-assert': 'error',
|
|
91
94
|
'@checkdigit/require-ts-extension-imports-exports': 'error',
|
|
92
95
|
'@checkdigit/no-wallaby-comment': 'error',
|
|
93
|
-
'@checkdigit/no-side-effects':
|
|
96
|
+
'@checkdigit/no-side-effects': [
|
|
97
|
+
'error',
|
|
98
|
+
{ excludedIdentifiers: ['assert', 'debug', 'log', 'promisify', 'Symbol.for'] },
|
|
99
|
+
],
|
|
94
100
|
'@checkdigit/regular-expression-comment': 'error',
|
|
95
101
|
'@checkdigit/require-assert-predicate-rejects-throws': 'error',
|
|
96
102
|
'@checkdigit/object-literal-response': 'error',
|
|
@@ -119,6 +125,7 @@ const configs: Record<string, TSESLint.FlatConfig.Config[]> = {
|
|
|
119
125
|
'@checkdigit/file-path-comment': 'off',
|
|
120
126
|
'@checkdigit/no-random-v4-uuid': 'error',
|
|
121
127
|
'@checkdigit/no-status-code-assert': 'error',
|
|
128
|
+
'@checkdigit/no-util': 'error',
|
|
122
129
|
'@checkdigit/no-uuid': 'error',
|
|
123
130
|
'@checkdigit/require-assert-message': 'error',
|
|
124
131
|
'@checkdigit/require-strict-assert': 'error',
|
package/src/no-side-effects.ts
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
// no-side-effects.ts
|
|
2
2
|
|
|
3
3
|
/*
|
|
4
|
-
* Copyright (c) 2022-
|
|
4
|
+
* Copyright (c) 2022-2024 Check Digit, LLC
|
|
5
5
|
*
|
|
6
6
|
* This code is licensed under the MIT license (see LICENSE.txt for details).
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
import { ESLintUtils } from '@typescript-eslint/utils';
|
|
10
10
|
import { TSESTree } from '@typescript-eslint/typescript-estree';
|
|
11
|
-
import getDocumentationUrl from './get-documentation-url.ts';
|
|
12
11
|
|
|
13
12
|
interface RuleOptions {
|
|
14
13
|
excludedIdentifiers: string[];
|
|
@@ -17,74 +16,61 @@ interface RuleOptions {
|
|
|
17
16
|
export const ruleId = 'no-side-effects';
|
|
18
17
|
const NO_SIDE_EFFECTS = 'NO_SIDE_EFFECTS';
|
|
19
18
|
|
|
20
|
-
// Type
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
//
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
node.type === TSESTree.AST_NODE_TYPES.VariableDeclaration && node.kind !== 'const' && node.kind !== 'using';
|
|
39
|
-
|
|
40
|
-
// Checks if a node is a control flow statement
|
|
41
|
-
const isControlFlowStatement = (node: TSESTree.Node): boolean =>
|
|
42
|
-
[
|
|
43
|
-
TSESTree.AST_NODE_TYPES.TryStatement,
|
|
44
|
-
TSESTree.AST_NODE_TYPES.IfStatement,
|
|
45
|
-
TSESTree.AST_NODE_TYPES.SwitchStatement,
|
|
46
|
-
TSESTree.AST_NODE_TYPES.ForStatement,
|
|
47
|
-
TSESTree.AST_NODE_TYPES.WhileStatement,
|
|
48
|
-
TSESTree.AST_NODE_TYPES.DoWhileStatement,
|
|
49
|
-
].includes(node.type);
|
|
50
|
-
|
|
51
|
-
// Checks if a node is an AssignmentExpression
|
|
52
|
-
const isAssignmentExpression = (node: TSESTree.Node): boolean =>
|
|
53
|
-
node.type === TSESTree.AST_NODE_TYPES.ExpressionStatement &&
|
|
54
|
-
node.expression.type === TSESTree.AST_NODE_TYPES.AssignmentExpression;
|
|
55
|
-
|
|
56
|
-
// Helper functions
|
|
57
|
-
|
|
58
|
-
// Checks if the callee is an identifier and not excluded
|
|
59
|
-
const isIdentifierCallee = (node: TSESTree.CallExpression, excludedIdentifiers: string[]): boolean =>
|
|
60
|
-
node.callee.type === TSESTree.AST_NODE_TYPES.Identifier && !excludedIdentifiers.includes(node.callee.name);
|
|
61
|
-
|
|
62
|
-
// Checks if the callee is a member expression and not excluded
|
|
63
|
-
const isMemberExpressionCallee = (node: TSESTree.CallExpression, excludedIdentifiers: string[]): boolean =>
|
|
64
|
-
node.callee.type === TSESTree.AST_NODE_TYPES.MemberExpression &&
|
|
65
|
-
node.callee.object.type === TSESTree.AST_NODE_TYPES.Identifier &&
|
|
66
|
-
node.callee.property.type === TSESTree.AST_NODE_TYPES.Identifier &&
|
|
67
|
-
!excludedIdentifiers.includes(`${node.callee.object.name}.${node.callee.property.name}`);
|
|
68
|
-
|
|
69
|
-
// Checks if the callee is a member expression with a non-identifier object
|
|
70
|
-
const isNonIdentifierObjectMemberExpressionCallee = (node: TSESTree.CallExpression): boolean =>
|
|
71
|
-
node.callee.type === TSESTree.AST_NODE_TYPES.MemberExpression &&
|
|
72
|
-
node.callee.object.type !== TSESTree.AST_NODE_TYPES.Identifier;
|
|
73
|
-
|
|
74
|
-
// Checks if a statement is a CallExpression with a member expression callee
|
|
75
|
-
const isCallExpressionCalleeMemberExpression = (statement: TSESTree.Node, excludedIdentifiers: string[]): boolean =>
|
|
76
|
-
isExpressionStatement(statement) &&
|
|
77
|
-
statement.expression.type === TSESTree.AST_NODE_TYPES.CallExpression &&
|
|
78
|
-
statement.expression.callee.type === TSESTree.AST_NODE_TYPES.MemberExpression &&
|
|
79
|
-
statement.expression.callee.object.type === TSESTree.AST_NODE_TYPES.Identifier &&
|
|
80
|
-
statement.expression.callee.property.type === TSESTree.AST_NODE_TYPES.Identifier &&
|
|
81
|
-
!excludedIdentifiers.includes(statement.expression.callee.object.name) &&
|
|
82
|
-
!excludedIdentifiers.includes(
|
|
83
|
-
`${statement.expression.callee.object.name}.${statement.expression.callee.property.name}`,
|
|
19
|
+
// Type guard to check if a node is an ExpressionStatement
|
|
20
|
+
function isExpressionStatement(node: TSESTree.Node): node is TSESTree.ExpressionStatement {
|
|
21
|
+
return node.type === TSESTree.AST_NODE_TYPES.ExpressionStatement;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// Type guard to check if a node is an AwaitExpression
|
|
25
|
+
function isAwaitExpression(statement: TSESTree.Node): boolean {
|
|
26
|
+
return isExpressionStatement(statement) && statement.expression.type === TSESTree.AST_NODE_TYPES.AwaitExpression;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// To check if it is a call expression with a member expression i.e. module.method()
|
|
30
|
+
function isCallExpressionCalleeMemberExpression(statement: TSESTree.Node, excludedIdentifiers: string[]): boolean {
|
|
31
|
+
return (
|
|
32
|
+
isExpressionStatement(statement) &&
|
|
33
|
+
statement.expression.type === TSESTree.AST_NODE_TYPES.CallExpression &&
|
|
34
|
+
statement.expression.callee.type === TSESTree.AST_NODE_TYPES.MemberExpression &&
|
|
35
|
+
statement.expression.callee.object.type === TSESTree.AST_NODE_TYPES.Identifier &&
|
|
36
|
+
!excludedIdentifiers.includes(statement.expression.callee.object.name)
|
|
84
37
|
);
|
|
38
|
+
}
|
|
85
39
|
|
|
86
|
-
//
|
|
87
|
-
|
|
40
|
+
// To check if it is a variable declaration with an await expression i.e. const configuration = await someFunction();
|
|
41
|
+
function isVariableDeclarationAwaitExpression(node: TSESTree.Node): boolean {
|
|
42
|
+
return (
|
|
43
|
+
node.type === TSESTree.AST_NODE_TYPES.VariableDeclaration &&
|
|
44
|
+
node.declarations.length > 0 &&
|
|
45
|
+
node.declarations[0]?.init?.type === TSESTree.AST_NODE_TYPES.AwaitExpression
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Helper function to check if the callee is an identifier and not excluded
|
|
50
|
+
function isIdentifierCallee(node: TSESTree.CallExpression, excludedIdentifiers: string[]): boolean {
|
|
51
|
+
return node.callee.type === TSESTree.AST_NODE_TYPES.Identifier && !excludedIdentifiers.includes(node.callee.name);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Helper function to check if the callee is a member expression and not excluded
|
|
55
|
+
function isMemberExpressionCallee(node: TSESTree.CallExpression, excludedIdentifiers: string[]): boolean {
|
|
56
|
+
return (
|
|
57
|
+
node.callee.type === TSESTree.AST_NODE_TYPES.MemberExpression &&
|
|
58
|
+
node.callee.object.type === TSESTree.AST_NODE_TYPES.Identifier &&
|
|
59
|
+
node.callee.property.type === TSESTree.AST_NODE_TYPES.Identifier &&
|
|
60
|
+
!excludedIdentifiers.includes(`${node.callee.object.name}.${node.callee.property.name}`)
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Helper function to check if the callee is a member expression with a non-identifier object
|
|
65
|
+
function isNonIdentifierObjectMemberExpressionCallee(node: TSESTree.CallExpression): boolean {
|
|
66
|
+
return (
|
|
67
|
+
node.callee.type === TSESTree.AST_NODE_TYPES.MemberExpression &&
|
|
68
|
+
node.callee.object.type !== TSESTree.AST_NODE_TYPES.Identifier
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// To check if it is a variable declaration with a call expression i.e const configuration = new Class(); or Member Expressions i.e const server = http.createServer();
|
|
73
|
+
function isVariableDeclarationCallExpression(node: TSESTree.Node, excludedIdentifiers: string[]): boolean {
|
|
88
74
|
if (node.type !== TSESTree.AST_NODE_TYPES.VariableDeclaration || node.declarations.length === 0) {
|
|
89
75
|
return false;
|
|
90
76
|
}
|
|
@@ -94,108 +80,35 @@ const isVariableDeclarationCallExpression = (node: TSESTree.Node, excludedIdenti
|
|
|
94
80
|
return false;
|
|
95
81
|
}
|
|
96
82
|
|
|
97
|
-
const callee = init.callee;
|
|
98
|
-
if (
|
|
99
|
-
callee.type === TSESTree.AST_NODE_TYPES.ArrowFunctionExpression ||
|
|
100
|
-
callee.type === TSESTree.AST_NODE_TYPES.FunctionExpression
|
|
101
|
-
) {
|
|
102
|
-
return true;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
83
|
return (
|
|
106
84
|
isIdentifierCallee(init, excludedIdentifiers) ||
|
|
107
85
|
isMemberExpressionCallee(init, excludedIdentifiers) ||
|
|
108
86
|
isNonIdentifierObjectMemberExpressionCallee(init)
|
|
109
87
|
);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
// Checks if an ExportNamedDeclaration has side effects
|
|
113
|
-
const isExportNamedDeclarationWithSideEffects = (statement: TSESTree.Node, excludedIdentifiers: string[]): boolean =>
|
|
114
|
-
statement.type === TSESTree.AST_NODE_TYPES.ExportNamedDeclaration &&
|
|
115
|
-
statement.declaration !== null &&
|
|
116
|
-
(isVariableDeclarationAwaitExpression(statement.declaration) ||
|
|
117
|
-
isVariableDeclarationCallExpression(statement.declaration, excludedIdentifiers));
|
|
118
|
-
|
|
119
|
-
// Checks if an ExpressionStatement has side effects
|
|
120
|
-
const isExpressionStatementWithSideEffects = (statement: TSESTree.Node, excludedIdentifiers: string[]): boolean =>
|
|
121
|
-
statement.type === TSESTree.AST_NODE_TYPES.ExpressionStatement &&
|
|
122
|
-
statement.expression.type === TSESTree.AST_NODE_TYPES.CallExpression &&
|
|
123
|
-
((statement.expression.callee.type === TSESTree.AST_NODE_TYPES.Identifier &&
|
|
124
|
-
!excludedIdentifiers.includes(statement.expression.callee.name)) ||
|
|
125
|
-
(statement.expression.callee.type === TSESTree.AST_NODE_TYPES.MemberExpression &&
|
|
126
|
-
statement.expression.callee.object.type === TSESTree.AST_NODE_TYPES.Identifier &&
|
|
127
|
-
statement.expression.callee.property.type === TSESTree.AST_NODE_TYPES.Identifier &&
|
|
128
|
-
!excludedIdentifiers.includes(
|
|
129
|
-
`${statement.expression.callee.object.name}.${statement.expression.callee.property.name}`,
|
|
130
|
-
)));
|
|
131
|
-
|
|
132
|
-
// Checks if a node is a VariableDeclaration with a NewExpression
|
|
133
|
-
const isVariableDeclarationNewExpression = (node: TSESTree.Node): boolean => {
|
|
134
|
-
if (node.type !== TSESTree.AST_NODE_TYPES.VariableDeclaration || node.declarations.length === 0) {
|
|
135
|
-
return false;
|
|
136
|
-
}
|
|
88
|
+
}
|
|
137
89
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
statement.
|
|
148
|
-
|
|
90
|
+
// Helper function to check if a given AST node (statement) has any side effects that should be reported
|
|
91
|
+
function hasSideEffects(statement: TSESTree.Node, excludedIdentifiers: string[]): boolean {
|
|
92
|
+
return (
|
|
93
|
+
isAwaitExpression(statement) ||
|
|
94
|
+
isCallExpressionCalleeMemberExpression(statement, excludedIdentifiers) ||
|
|
95
|
+
isVariableDeclarationAwaitExpression(statement) ||
|
|
96
|
+
isVariableDeclarationCallExpression(statement, excludedIdentifiers) ||
|
|
97
|
+
(statement.type === TSESTree.AST_NODE_TYPES.ExportNamedDeclaration &&
|
|
98
|
+
statement.declaration !== null &&
|
|
99
|
+
(isVariableDeclarationAwaitExpression(statement.declaration) ||
|
|
100
|
+
isVariableDeclarationCallExpression(statement.declaration, excludedIdentifiers)))
|
|
149
101
|
);
|
|
102
|
+
}
|
|
150
103
|
|
|
151
|
-
|
|
152
|
-
const hasSideEffects = (statement: TSESTree.Node, excludedIdentifiers: string[]): string | null => {
|
|
153
|
-
if (isAwaitExpression(statement)) {
|
|
154
|
-
return TSESTree.AST_NODE_TYPES.AwaitExpression;
|
|
155
|
-
}
|
|
156
|
-
if (isCallExpressionCalleeMemberExpression(statement, excludedIdentifiers)) {
|
|
157
|
-
return 'CallExpressionCalleeMemberExpression';
|
|
158
|
-
}
|
|
159
|
-
if (isVariableDeclarationAwaitExpression(statement)) {
|
|
160
|
-
return 'VariableDeclarationAwaitExpression';
|
|
161
|
-
}
|
|
162
|
-
if (isVariableDeclarationCallExpression(statement, excludedIdentifiers)) {
|
|
163
|
-
return 'VariableDeclarationCallExpression';
|
|
164
|
-
}
|
|
165
|
-
if (isVariableDeclarationNewExpression(statement)) {
|
|
166
|
-
return 'VariableDeclarationNewExpression';
|
|
167
|
-
}
|
|
168
|
-
if (isExportNamedDeclarationWithSideEffects(statement, excludedIdentifiers)) {
|
|
169
|
-
return 'ExportNamedDeclarationWithSideEffects';
|
|
170
|
-
}
|
|
171
|
-
if (isExpressionStatementWithSideEffects(statement, excludedIdentifiers)) {
|
|
172
|
-
return 'ExpressionStatementWithSideEffects';
|
|
173
|
-
}
|
|
174
|
-
if (isControlFlowStatement(statement)) {
|
|
175
|
-
return 'ControlFlowStatement';
|
|
176
|
-
}
|
|
177
|
-
if (isNotValidVariableDeclaration(statement)) {
|
|
178
|
-
return 'NotValidVariableDeclaration';
|
|
179
|
-
}
|
|
180
|
-
if (isAssignmentExpression(statement)) {
|
|
181
|
-
return TSESTree.AST_NODE_TYPES.AssignmentExpression;
|
|
182
|
-
}
|
|
183
|
-
if (isFunctionDeclarationAwaitExpression(statement)) {
|
|
184
|
-
return 'FunctionDeclarationAwaitExpression';
|
|
185
|
-
}
|
|
186
|
-
return null;
|
|
187
|
-
};
|
|
188
|
-
|
|
189
|
-
const createRule: ReturnType<typeof ESLintUtils.RuleCreator> = ESLintUtils.RuleCreator((name) =>
|
|
190
|
-
getDocumentationUrl(name),
|
|
191
|
-
);
|
|
104
|
+
const createRule: ReturnType<typeof ESLintUtils.RuleCreator> = ESLintUtils.RuleCreator((name) => name);
|
|
192
105
|
|
|
193
106
|
const rule: ReturnType<typeof createRule> = createRule({
|
|
194
107
|
name: ruleId,
|
|
195
108
|
meta: {
|
|
196
109
|
type: 'problem',
|
|
197
110
|
docs: {
|
|
198
|
-
description: 'Ensure no side effects can occur at the module-level
|
|
111
|
+
description: 'Ensure no side effects can occur at the module-level',
|
|
199
112
|
},
|
|
200
113
|
schema: [
|
|
201
114
|
{
|
|
@@ -231,14 +144,10 @@ const rule: ReturnType<typeof createRule> = createRule({
|
|
|
231
144
|
}
|
|
232
145
|
|
|
233
146
|
node.body.forEach((statement: TSESTree.Node) => {
|
|
234
|
-
|
|
235
|
-
if (sideEffectType !== null) {
|
|
147
|
+
if (hasSideEffects(statement, excludedIdentifiers)) {
|
|
236
148
|
context.report({
|
|
237
149
|
node: statement,
|
|
238
150
|
messageId: NO_SIDE_EFFECTS,
|
|
239
|
-
data: {
|
|
240
|
-
sideEffectType,
|
|
241
|
-
},
|
|
242
151
|
});
|
|
243
152
|
}
|
|
244
153
|
});
|
package/src/no-util.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// no-util.ts
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* Copyright (c) 2021-2025 Check Digit, LLC
|
|
5
|
+
*
|
|
6
|
+
* This code is licensed under the MIT license (see LICENSE.txt for details).
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { AST_TOKEN_TYPES, ESLintUtils } from '@typescript-eslint/utils';
|
|
10
|
+
|
|
11
|
+
export const ruleId = 'no-util';
|
|
12
|
+
const NO_UTIL = 'NO_UTIL';
|
|
13
|
+
|
|
14
|
+
const createRule = ESLintUtils.RuleCreator((name) => name);
|
|
15
|
+
|
|
16
|
+
const rule: ESLintUtils.RuleModule<typeof NO_UTIL> = createRule({
|
|
17
|
+
name: ruleId,
|
|
18
|
+
meta: {
|
|
19
|
+
type: 'problem',
|
|
20
|
+
docs: {
|
|
21
|
+
description: 'Detects if file name is util',
|
|
22
|
+
},
|
|
23
|
+
schema: [],
|
|
24
|
+
messages: {
|
|
25
|
+
[NO_UTIL]: "File name '{{filename}}' contains banned 'util' pattern.",
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
defaultOptions: [],
|
|
29
|
+
create(context) {
|
|
30
|
+
return {
|
|
31
|
+
Program() {
|
|
32
|
+
const filename = context.filename;
|
|
33
|
+
const utilRegex = /(?:^|[-_/])util(?=[-_./]|$)/iu;
|
|
34
|
+
if (utilRegex.test(filename)) {
|
|
35
|
+
const sourceCode = context.sourceCode;
|
|
36
|
+
const tokens = sourceCode.tokensAndComments;
|
|
37
|
+
const firstNonCommentToken = tokens.find(
|
|
38
|
+
(token) => token.type !== AST_TOKEN_TYPES.Block && token.type !== AST_TOKEN_TYPES.Line,
|
|
39
|
+
);
|
|
40
|
+
if (firstNonCommentToken !== undefined) {
|
|
41
|
+
context.report({
|
|
42
|
+
messageId: NO_UTIL,
|
|
43
|
+
data: { filename },
|
|
44
|
+
node: firstNonCommentToken,
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
},
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
export default rule;
|