@checkdigit/eslint-plugin 7.13.1-PR.120-6b16 → 7.14.0-PR.117-1980
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-type-assertion-as.mjs +45 -0
- package/dist-types/no-type-assertion-as.d.ts +5 -0
- package/package.json +1 -1
- package/src/index.ts +8 -1
- package/src/no-side-effects.ts +70 -161
- package/src/no-type-assertion-as.ts +60 -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 noTypeAssertionAs from "./no-type-assertion-as.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";
|
|
@@ -44,6 +45,7 @@ var rules = {
|
|
|
44
45
|
"no-test-import": noTestImport,
|
|
45
46
|
"no-wallaby-comment": noWallabyComment,
|
|
46
47
|
"no-side-effects": noSideEffects,
|
|
48
|
+
"no-type-assertion-as": noTypeAssertionAs,
|
|
47
49
|
"regular-expression-comment": regexComment,
|
|
48
50
|
"require-assert-predicate-rejects-throws": requireAssertPredicateRejectsThrows,
|
|
49
51
|
"object-literal-response": objectLiteralResponse,
|
|
@@ -78,7 +80,11 @@ var configs = {
|
|
|
78
80
|
"@checkdigit/require-strict-assert": "error",
|
|
79
81
|
"@checkdigit/require-ts-extension-imports-exports": "error",
|
|
80
82
|
"@checkdigit/no-wallaby-comment": "error",
|
|
81
|
-
"@checkdigit/no-side-effects":
|
|
83
|
+
"@checkdigit/no-side-effects": [
|
|
84
|
+
"error",
|
|
85
|
+
{ excludedIdentifiers: ["assert", "debug", "log", "promisify", "Symbol.for"] }
|
|
86
|
+
],
|
|
87
|
+
"@checkdigit/no-type-assertion-as": "error",
|
|
82
88
|
"@checkdigit/regular-expression-comment": "error",
|
|
83
89
|
"@checkdigit/require-assert-predicate-rejects-throws": "error",
|
|
84
90
|
"@checkdigit/object-literal-response": "error",
|
|
@@ -113,6 +119,7 @@ var configs = {
|
|
|
113
119
|
"@checkdigit/require-ts-extension-imports-exports": "error",
|
|
114
120
|
"@checkdigit/no-wallaby-comment": "off",
|
|
115
121
|
"@checkdigit/no-side-effects": "error",
|
|
122
|
+
"@checkdigit/no-type-assertion-as": "error",
|
|
116
123
|
"@checkdigit/regular-expression-comment": "error",
|
|
117
124
|
"@checkdigit/require-assert-predicate-rejects-throws": "error",
|
|
118
125
|
"@checkdigit/object-literal-response": "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,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsiLi4vc3JjL2luZGV4LnRzIl0sCiAgIm1hcHBpbmdzIjogIjtBQVVBLE9BQU8sd0JBQXdCLFVBQVUsa0NBQWtDO0FBQzNFLE9BQU8sdUJBQXVCLFVBQVUsaUNBQWlDO0FBQ3pFLE9BQU8seUJBQXlCLFVBQVUsbUNBQW1DO0FBQzdFLE9BQU8sMkJBQTJCLFVBQVUscUNBQXFDO0FBQ2pGLE9BQU8sd0JBQXdCO0FBQy9CLE9BQU87QUFBQSxFQUNMLFVBQVU7QUFBQSxPQUNMO0FBQ1AsT0FBTztBQUFBLEVBQ0wsVUFBVTtBQUFBLE9BQ0w7QUFDUCxPQUFPO0FBQUEsRUFDTCxVQUFVO0FBQUEsT0FDTDtBQUNQLE9BQU8sa0JBQWtCLFVBQVUsNEJBQ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QyxlQUFlLHlCQUF5QixFQUFFLEdBQUc7QUFBQSxRQUM5QyxDQUFDLGVBQWUsZ0NBQWdDLEVBQUUsR0FBRztBQUFBLFFBQ3JELENBQUMsZUFBZSxxQ0FBcUMsRUFBRSxHQUFHO0FBQUEsUUFDMUQsQ0FBQyxlQUFlLG9CQUFvQixFQUFFLEdBQUc7QUFBQSxRQUN6QyxDQUFDLGVBQWUsMkNBQTJDLEVBQUUsR0FBRztBQUFBLE1BQ2xFO0FBQUEsSUFDRjtBQUFBLEVBQ0Y7QUFDRjtBQUVBLElBQU0sa0JBRUY7QUFBQSxFQUNGLEdBQUc7QUFBQSxFQUNIO0FBQ0Y7QUFDQSxJQUFPLGNBQVE7IiwKICAibmFtZXMiOiBbXQp9Cg==
|
|
@@ -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,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsiLi4vc3JjL25vLXNpZGUtZWZmZWN0cy50cyJdLAogICJtYXBwaW5ncyI6ICI7QUFRQSxTQUFTLG1CQUFtQjtBQUM1QixTQUFTLGdCQUFnQjtBQU1sQixJQUFNLFNBQVM7QUFDdEIsSUFBTSxrQkFBa0I7QUFHeEIsU0FBUyxzQkFBc0IsTUFBMkQ7QUFDeEYsU0FBTyxLQUFLLFNBQVMsU0FBUyxlQUFlO0FBQy9DO0FBR0EsU0FBUyxrQkFBa0IsV0FBbUM7QUFDNUQsU0FBTyxzQkFBc0IsU0FBUyxLQUFLLFVBQVUsV0FBVyxTQUFTLFNBQVMsZUFBZTtBQUNuRztBQUdBLFNBQVMsdUNBQXVDLFdBQTBCLHFCQUF3QztBQUNoSCxTQUNFLHNCQUFzQixTQUFTLEtBQy9CLFVBQVUsV0FBVyxTQUFTLFNBQVMsZUFBZSxrQkFDdEQsVUFBVSxX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SUFDckI7QUFBQSxFQUNGO0FBQUEsRUFDQSxnQkFBZ0IsQ0FBQyxFQUFFLHFCQUFxQixDQUFDLEVBQUUsRUFBRSxDQUFDO0FBQUEsRUFDOUMsT0FBTyxTQUFTO0FBQ2QsVUFBTSxVQUF1QixRQUFRLFFBQVEsQ0FBQztBQUM5QyxVQUFNLHNCQUFzQixRQUFRLG9CQUFvQixTQUFTLElBQUksUUFBUSxzQkFBc0IsQ0FBQztBQUNwRyxXQUFPO0FBQUEsTUFDTCxRQUFRLE1BQXdCO0FBQzlCLGNBQU0sWUFBWSxLQUFLLEtBQUs7QUFBQSxVQUMxQixDQUFDLGNBQ0MsVUFBVSxTQUFTLFNBQVMsZUFBZSwwQkFDM0MsVUFBVSxTQUFTLFNBQVMsZUFBZSw0QkFDM0MsVUFBVSxTQUFTLFNBQVMsZUFBZTtBQUFBLFFBQy9DO0FBRUEsWUFBSSxDQUFDLFdBQVc7QUFDZDtBQUFBLFFBQ0Y7QUFFQSxhQUFLLEtBQUssUUFBUSxDQUFDLGNBQTZCO0FBQzlDLGNBQUksZUFBZSxXQUFXLG1CQUFtQixHQUFHO0FBQ2xELG9CQUFRLE9BQU87QUFBQSxjQUNiLE1BQU07QUFBQSxjQUNOLFdBQVc7QUFBQSxZQUNiLENBQUM7QUFBQSxVQUNIO0FBQUEsUUFDRixDQUFDO0FBQUEsTUFDSDtBQUFBLElBQ0Y7QUFBQSxFQUNGO0FBQ0YsQ0FBQztBQUVELElBQU8sMEJBQVE7IiwKICAibmFtZXMiOiBbXQp9Cg==
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// src/no-type-assertion-as.ts
|
|
2
|
+
import { AST_NODE_TYPES, ESLintUtils } from "@typescript-eslint/utils";
|
|
3
|
+
var ruleId = "no-as-type-assertion";
|
|
4
|
+
var NO_AS_TYPE_ASSERTION = "NO_AS_TYPE_ASSERTION";
|
|
5
|
+
var createRule = ESLintUtils.RuleCreator((name) => name);
|
|
6
|
+
var rule = createRule({
|
|
7
|
+
name: ruleId,
|
|
8
|
+
meta: {
|
|
9
|
+
type: "problem",
|
|
10
|
+
docs: {
|
|
11
|
+
description: "Disallow the use of `as` type assertions and suggest using `satisfies` instead"
|
|
12
|
+
},
|
|
13
|
+
schema: [],
|
|
14
|
+
messages: {
|
|
15
|
+
[NO_AS_TYPE_ASSERTION]: "Avoid using `as` type assertions. Use `satisfies` instead."
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
defaultOptions: [],
|
|
19
|
+
create(context) {
|
|
20
|
+
return {
|
|
21
|
+
TSAsExpression(node) {
|
|
22
|
+
const isUnknownUnionOrIntersection = (node.typeAnnotation.type === AST_NODE_TYPES.TSUnionType || node.typeAnnotation.type === AST_NODE_TYPES.TSIntersectionType) && node.typeAnnotation.types.some((type) => type.type === AST_NODE_TYPES.TSUnknownKeyword);
|
|
23
|
+
if (node.typeAnnotation.type === AST_NODE_TYPES.TSUnknownKeyword || node.expression.type === AST_NODE_TYPES.TSAsExpression && node.expression.typeAnnotation.type === AST_NODE_TYPES.TSUnknownKeyword || isUnknownUnionOrIntersection) {
|
|
24
|
+
if (node.parent.type !== AST_NODE_TYPES.TSAsExpression) {
|
|
25
|
+
context.report({
|
|
26
|
+
node,
|
|
27
|
+
messageId: NO_AS_TYPE_ASSERTION
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
} else {
|
|
31
|
+
context.report({
|
|
32
|
+
node,
|
|
33
|
+
messageId: NO_AS_TYPE_ASSERTION
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
var no_type_assertion_as_default = rule;
|
|
41
|
+
export {
|
|
42
|
+
no_type_assertion_as_default as default,
|
|
43
|
+
ruleId
|
|
44
|
+
};
|
|
45
|
+
//# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsiLi4vc3JjL25vLXR5cGUtYXNzZ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XO0FBQUEsVUFDYixDQUFDO0FBQUEsUUFDSDtBQUFBLE1BQ0Y7QUFBQSxJQUNGO0FBQUEsRUFDRjtBQUNGLENBQUM7QUFFRCxJQUFPLCtCQUFROyIsCiAgIm5hbWVzIjogW10KfQo=
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { ESLintUtils } from '@typescript-eslint/utils';
|
|
2
|
+
export declare const ruleId = "no-as-type-assertion";
|
|
3
|
+
declare const NO_AS_TYPE_ASSERTION = "NO_AS_TYPE_ASSERTION";
|
|
4
|
+
declare const rule: ESLintUtils.RuleModule<typeof NO_AS_TYPE_ASSERTION>;
|
|
5
|
+
export default rule;
|
package/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"name":"@checkdigit/eslint-plugin","version":"7.
|
|
1
|
+
{"name":"@checkdigit/eslint-plugin","version":"7.14.0-PR.117-1980","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":">=20.17"}}
|
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 noTypeAssertionAs from './no-type-assertion-as.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';
|
|
@@ -54,6 +55,7 @@ const rules: Record<string, TSESLint.LooseRuleDefinition> = {
|
|
|
54
55
|
'no-test-import': noTestImport,
|
|
55
56
|
'no-wallaby-comment': noWallabyComment,
|
|
56
57
|
'no-side-effects': noSideEffects,
|
|
58
|
+
'no-type-assertion-as': noTypeAssertionAs,
|
|
57
59
|
'regular-expression-comment': regexComment,
|
|
58
60
|
'require-assert-predicate-rejects-throws': requireAssertPredicateRejectsThrows,
|
|
59
61
|
'object-literal-response': objectLiteralResponse,
|
|
@@ -90,7 +92,11 @@ const configs: Record<string, TSESLint.FlatConfig.Config[]> = {
|
|
|
90
92
|
'@checkdigit/require-strict-assert': 'error',
|
|
91
93
|
'@checkdigit/require-ts-extension-imports-exports': 'error',
|
|
92
94
|
'@checkdigit/no-wallaby-comment': 'error',
|
|
93
|
-
'@checkdigit/no-side-effects':
|
|
95
|
+
'@checkdigit/no-side-effects': [
|
|
96
|
+
'error',
|
|
97
|
+
{ excludedIdentifiers: ['assert', 'debug', 'log', 'promisify', 'Symbol.for'] },
|
|
98
|
+
],
|
|
99
|
+
'@checkdigit/no-type-assertion-as': 'error',
|
|
94
100
|
'@checkdigit/regular-expression-comment': 'error',
|
|
95
101
|
'@checkdigit/require-assert-predicate-rejects-throws': 'error',
|
|
96
102
|
'@checkdigit/object-literal-response': 'error',
|
|
@@ -125,6 +131,7 @@ const configs: Record<string, TSESLint.FlatConfig.Config[]> = {
|
|
|
125
131
|
'@checkdigit/require-ts-extension-imports-exports': 'error',
|
|
126
132
|
'@checkdigit/no-wallaby-comment': 'off',
|
|
127
133
|
'@checkdigit/no-side-effects': 'error',
|
|
134
|
+
'@checkdigit/no-type-assertion-as': 'error',
|
|
128
135
|
'@checkdigit/regular-expression-comment': 'error',
|
|
129
136
|
'@checkdigit/require-assert-predicate-rejects-throws': 'error',
|
|
130
137
|
'@checkdigit/object-literal-response': '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
|
});
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// no-type-assertion-as.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_NODE_TYPES, ESLintUtils, TSESTree } from '@typescript-eslint/utils';
|
|
10
|
+
|
|
11
|
+
export const ruleId = 'no-as-type-assertion';
|
|
12
|
+
const NO_AS_TYPE_ASSERTION = 'NO_AS_TYPE_ASSERTION';
|
|
13
|
+
|
|
14
|
+
const createRule = ESLintUtils.RuleCreator((name) => name);
|
|
15
|
+
|
|
16
|
+
const rule: ESLintUtils.RuleModule<typeof NO_AS_TYPE_ASSERTION> = createRule({
|
|
17
|
+
name: ruleId,
|
|
18
|
+
meta: {
|
|
19
|
+
type: 'problem',
|
|
20
|
+
docs: {
|
|
21
|
+
description: 'Disallow the use of `as` type assertions and suggest using `satisfies` instead',
|
|
22
|
+
},
|
|
23
|
+
schema: [],
|
|
24
|
+
messages: {
|
|
25
|
+
[NO_AS_TYPE_ASSERTION]: 'Avoid using `as` type assertions. Use `satisfies` instead.',
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
defaultOptions: [],
|
|
29
|
+
create(context) {
|
|
30
|
+
return {
|
|
31
|
+
TSAsExpression(node: TSESTree.TSAsExpression) {
|
|
32
|
+
const isUnknownUnionOrIntersection =
|
|
33
|
+
(node.typeAnnotation.type === AST_NODE_TYPES.TSUnionType ||
|
|
34
|
+
node.typeAnnotation.type === AST_NODE_TYPES.TSIntersectionType) &&
|
|
35
|
+
node.typeAnnotation.types.some((type) => type.type === AST_NODE_TYPES.TSUnknownKeyword);
|
|
36
|
+
|
|
37
|
+
if (
|
|
38
|
+
node.typeAnnotation.type === AST_NODE_TYPES.TSUnknownKeyword ||
|
|
39
|
+
(node.expression.type === AST_NODE_TYPES.TSAsExpression &&
|
|
40
|
+
node.expression.typeAnnotation.type === AST_NODE_TYPES.TSUnknownKeyword) ||
|
|
41
|
+
isUnknownUnionOrIntersection
|
|
42
|
+
) {
|
|
43
|
+
if (node.parent.type !== AST_NODE_TYPES.TSAsExpression) {
|
|
44
|
+
context.report({
|
|
45
|
+
node,
|
|
46
|
+
messageId: NO_AS_TYPE_ASSERTION,
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
} else {
|
|
50
|
+
context.report({
|
|
51
|
+
node,
|
|
52
|
+
messageId: NO_AS_TYPE_ASSERTION,
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
export default rule;
|