@checkdigit/eslint-plugin 7.12.1-PR.120-20b8 → 7.13.0-PR.117-c94d
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 +27 -36
- package/dist-mjs/no-type-assertion-as.mjs +52 -0
- package/dist-types/no-type-assertion-as.d.ts +6 -0
- package/package.json +1 -1
- package/src/index.ts +8 -1
- package/src/no-side-effects.ts +62 -108
- package/src/no-type-assertion-as.ts +68 -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,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsiLi4vc3JjL2luZGV4LnRzIl0sCiAgIm1hcHBpbmdzIjogIjtBQVVBLE9BQU8sd0JBQXdCLFVBQVUsa0NBQWtDO0FBQzNFLE9BQU8sdUJBQXVCLFVBQVUsaUNBQWlDO0FBQ3pFLE9BQU8seUJBQXlCLFVBQVUsbUNBQW1DO0FBQzdFLE9BQU8sMkJBQTJCLFVBQVUscUNBQXFDO0FBQ2pGLE9BQU8sd0JBQXdCO0FBQy9CLE9BQU87QUFBQSxFQUNMLFVBQVU7QUFBQSxPQUNMO0FBQ1AsT0FBTztBQUFBLEVBQ0wsVUFBVTtBQUFBLE9BQ0w7QUFDUCxPQUFPO0FBQUEsRUFDTCxVQUFVO0FBQUEsT0FDTDtBQUNQLE9BQU8sa0JBQWtCLFVBQVUsNEJB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BQyxlQUFlLHlCQUF5QixFQUFFLEdBQUc7QUFBQSxRQUM5QyxDQUFDLGVBQWUsZ0NBQWdDLEVBQUUsR0FBRztBQUFBLFFBQ3JELENBQUMsZUFBZSxxQ0FBcUMsRUFBRSxHQUFHO0FBQUEsUUFDMUQsQ0FBQyxlQUFlLG9CQUFvQixFQUFFLEdBQUc7QUFBQSxRQUN6QyxDQUFDLGVBQWUsMkNBQTJDLEVBQUUsR0FBRztBQUFBLE1BQ2xFO0FBQUEsSUFDRjtBQUFBLEVBQ0Y7QUFDRjtBQUVBLElBQU0sa0JBRUY7QUFBQSxFQUNGLEdBQUc7QUFBQSxFQUNIO0FBQ0Y7QUFDQSxJQUFPLGNBQVE7IiwKICAibmFtZXMiOiBbXQp9Cg==
|
|
@@ -3,49 +3,40 @@ import { ESLintUtils } from "@typescript-eslint/utils";
|
|
|
3
3
|
import { TSESTree } from "@typescript-eslint/typescript-estree";
|
|
4
4
|
var ruleId = "no-side-effects";
|
|
5
5
|
var NO_SIDE_EFFECTS = "NO_SIDE_EFFECTS";
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
TSESTree.AST_NODE_TYPES.
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
TSESTree.AST_NODE_TYPES.
|
|
17
|
-
|
|
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
|
}
|
|
30
|
-
if (node.kind === "const" || node.kind === "using") {
|
|
31
|
-
return false;
|
|
32
|
-
}
|
|
33
31
|
const init = node.declarations[0]?.init;
|
|
34
32
|
if (init?.type !== TSESTree.AST_NODE_TYPES.CallExpression) {
|
|
35
33
|
return false;
|
|
36
34
|
}
|
|
37
35
|
return isIdentifierCallee(init, excludedIdentifiers) || isMemberExpressionCallee(init, excludedIdentifiers) || isNonIdentifierObjectMemberExpressionCallee(init);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
));
|
|
43
|
-
var hasSideEffects = (statement, excludedIdentifiers) => {
|
|
44
|
-
if (isConstOrUsingVariableDeclaration(statement)) {
|
|
45
|
-
return false;
|
|
46
|
-
}
|
|
47
|
-
return isAwaitExpression(statement) || isCallExpressionCalleeMemberExpression(statement, excludedIdentifiers) || isVariableDeclarationAwaitExpression(statement) || isVariableDeclarationCallExpression(statement, excludedIdentifiers) || isExportNamedDeclarationWithSideEffects(statement, excludedIdentifiers) || isExpressionStatementWithSideEffects(statement, excludedIdentifiers) || isControlFlowStatement(statement) || isNotValidVariableDeclaration(statement) || isAssignmentExpression(statement);
|
|
48
|
-
};
|
|
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
|
+
}
|
|
49
40
|
var createRule = ESLintUtils.RuleCreator((name) => name);
|
|
50
41
|
var rule = createRule({
|
|
51
42
|
name: ruleId,
|
|
@@ -99,4 +90,4 @@ export {
|
|
|
99
90
|
no_side_effects_default as default,
|
|
100
91
|
ruleId
|
|
101
92
|
};
|
|
102
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
93
|
+
//# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsiLi4vc3JjL25vLXNpZGUtZWZmZWN0cy50cyJdLAogICJtYXBwaW5ncyI6ICI7QUFRQSxTQUFT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BQSxjQUNOLFdBQVc7QUFBQSxZQUNiLENBQUM7QUFBQSxVQUNIO0FBQUEsUUFDRixDQUFDO0FBQUEsTUFDSDtBQUFBLElBQ0Y7QUFBQSxFQUNGO0FBQ0YsQ0FBQztBQUVELElBQU8sMEJBQVE7IiwKICAibmFtZXMiOiBbXQp9Cg==
|
|
@@ -0,0 +1,52 @@
|
|
|
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 NO_UNKNOWN_TYPE_ASSERTION = "NO_UNKNOWN_TYPE_ASSERTION";
|
|
6
|
+
var createRule = ESLintUtils.RuleCreator((name) => name);
|
|
7
|
+
var rule = createRule({
|
|
8
|
+
name: ruleId,
|
|
9
|
+
meta: {
|
|
10
|
+
type: "suggestion",
|
|
11
|
+
docs: {
|
|
12
|
+
description: "Disallow the use of `as` type assertions and suggest using `satisfies` instead"
|
|
13
|
+
},
|
|
14
|
+
schema: [],
|
|
15
|
+
messages: {
|
|
16
|
+
[NO_AS_TYPE_ASSERTION]: "Avoid using `as` type assertions. Use `satisfies` instead.",
|
|
17
|
+
[NO_UNKNOWN_TYPE_ASSERTION]: "Avoid using `unknown` type assertions."
|
|
18
|
+
},
|
|
19
|
+
fixable: "code"
|
|
20
|
+
},
|
|
21
|
+
defaultOptions: [],
|
|
22
|
+
create(context) {
|
|
23
|
+
return {
|
|
24
|
+
TSAsExpression(node) {
|
|
25
|
+
const sourceCode = context.sourceCode;
|
|
26
|
+
const typeAnnotation = sourceCode.getText(node.typeAnnotation);
|
|
27
|
+
const expression = sourceCode.getText(node.expression);
|
|
28
|
+
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);
|
|
29
|
+
if (node.typeAnnotation.type === AST_NODE_TYPES.TSUnknownKeyword || node.expression.type === AST_NODE_TYPES.TSAsExpression && node.expression.typeAnnotation.type === AST_NODE_TYPES.TSUnknownKeyword || isUnknownUnionOrIntersection) {
|
|
30
|
+
if (node.parent.type !== AST_NODE_TYPES.TSAsExpression) {
|
|
31
|
+
context.report({
|
|
32
|
+
node,
|
|
33
|
+
messageId: NO_UNKNOWN_TYPE_ASSERTION
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
} else {
|
|
37
|
+
context.report({
|
|
38
|
+
node,
|
|
39
|
+
messageId: NO_AS_TYPE_ASSERTION,
|
|
40
|
+
fix: (fixer) => fixer.replaceText(node, `${expression} satisfies ${typeAnnotation}`)
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
var no_type_assertion_as_default = rule;
|
|
48
|
+
export {
|
|
49
|
+
no_type_assertion_as_default as default,
|
|
50
|
+
ruleId
|
|
51
|
+
};
|
|
52
|
+
//# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsiLi4vc3JjL25vLXR5cGUtYXNzZXJ0aW9uLWFzLnRzIl0sCiAgIm1hcHBpbmdzIjogIjtBQVFBLFNBQVMsZ0JBQWdCLG1CQUE2QjtBQUUvQyxJQUFNLFNBQVM7QUFDdEIsSUFBTSx1QkFBdUI7QUFDN0IsSUFBTSw0QkFBNEI7QUFFbEMsSUFBTSxhQUFhLFlBQVksWUFBWSxDQUFDLFNBQVMsSUFBSTtBQUV6RCxJQUFNLE9BQStGLFdBQVc7QUFBQSxFQUM5RyxNQUFNO0FBQUEsRUFDTixNQUFNO0FBQUEsSUFDSixNQUFNO0FBQUEsSUFDTixNQUFNO0FBQUEsTUFDSixhQUFhO0FBQUEsSUFDZjtBQUFBLEl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QSxJQUNGO0FBQUEsRUFDRjtBQUNGLENBQUM7QUFFRCxJQUFPLCtCQUFROyIsCiAgIm5hbWVzIjogW10KfQo=
|
|
@@ -0,0 +1,6 @@
|
|
|
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 NO_UNKNOWN_TYPE_ASSERTION = "NO_UNKNOWN_TYPE_ASSERTION";
|
|
5
|
+
declare const rule: ESLintUtils.RuleModule<typeof NO_AS_TYPE_ASSERTION | typeof NO_UNKNOWN_TYPE_ASSERTION>;
|
|
6
|
+
export default rule;
|
package/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"name":"@checkdigit/eslint-plugin","version":"7.
|
|
1
|
+
{"name":"@checkdigit/eslint-plugin","version":"7.13.0-PR.117-c94d","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,7 +1,7 @@
|
|
|
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
|
*/
|
|
@@ -16,83 +16,62 @@ interface RuleOptions {
|
|
|
16
16
|
export const ruleId = 'no-side-effects';
|
|
17
17
|
const NO_SIDE_EFFECTS = 'NO_SIDE_EFFECTS';
|
|
18
18
|
|
|
19
|
-
// Type
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
//
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
node.type === TSESTree.AST_NODE_TYPES.VariableDeclaration && node.kind !== 'const' && node.kind !== 'using';
|
|
38
|
-
|
|
39
|
-
// Checks if a node is a VariableDeclaration that is const or using
|
|
40
|
-
const isConstOrUsingVariableDeclaration = (node: TSESTree.Node): boolean =>
|
|
41
|
-
node.type === TSESTree.AST_NODE_TYPES.VariableDeclaration && (node.kind === 'const' || node.kind === 'using');
|
|
42
|
-
|
|
43
|
-
// Checks if a node is a control flow statement
|
|
44
|
-
const isControlFlowStatement = (node: TSESTree.Node): boolean =>
|
|
45
|
-
[
|
|
46
|
-
TSESTree.AST_NODE_TYPES.TryStatement,
|
|
47
|
-
TSESTree.AST_NODE_TYPES.IfStatement,
|
|
48
|
-
TSESTree.AST_NODE_TYPES.SwitchStatement,
|
|
49
|
-
TSESTree.AST_NODE_TYPES.ForStatement,
|
|
50
|
-
TSESTree.AST_NODE_TYPES.WhileStatement,
|
|
51
|
-
TSESTree.AST_NODE_TYPES.DoWhileStatement,
|
|
52
|
-
].includes(node.type);
|
|
53
|
-
|
|
54
|
-
// Checks if a node is an AssignmentExpression
|
|
55
|
-
const isAssignmentExpression = (node: TSESTree.Node): boolean =>
|
|
56
|
-
node.type === TSESTree.AST_NODE_TYPES.ExpressionStatement &&
|
|
57
|
-
node.expression.type === TSESTree.AST_NODE_TYPES.AssignmentExpression;
|
|
58
|
-
|
|
59
|
-
// Helper functions
|
|
60
|
-
|
|
61
|
-
// Checks if the callee is an identifier and not excluded
|
|
62
|
-
const isIdentifierCallee = (node: TSESTree.CallExpression, excludedIdentifiers: string[]): boolean =>
|
|
63
|
-
node.callee.type === TSESTree.AST_NODE_TYPES.Identifier && !excludedIdentifiers.includes(node.callee.name);
|
|
64
|
-
|
|
65
|
-
// Checks if the callee is a member expression and not excluded
|
|
66
|
-
const isMemberExpressionCallee = (node: TSESTree.CallExpression, excludedIdentifiers: string[]): boolean =>
|
|
67
|
-
node.callee.type === TSESTree.AST_NODE_TYPES.MemberExpression &&
|
|
68
|
-
node.callee.object.type === TSESTree.AST_NODE_TYPES.Identifier &&
|
|
69
|
-
node.callee.property.type === TSESTree.AST_NODE_TYPES.Identifier &&
|
|
70
|
-
!excludedIdentifiers.includes(`${node.callee.object.name}.${node.callee.property.name}`);
|
|
71
|
-
|
|
72
|
-
// Checks if the callee is a member expression with a non-identifier object
|
|
73
|
-
const isNonIdentifierObjectMemberExpressionCallee = (node: TSESTree.CallExpression): boolean =>
|
|
74
|
-
node.callee.type === TSESTree.AST_NODE_TYPES.MemberExpression &&
|
|
75
|
-
node.callee.object.type !== TSESTree.AST_NODE_TYPES.Identifier;
|
|
76
|
-
|
|
77
|
-
// Checks if a statement is a CallExpression with a member expression callee
|
|
78
|
-
const isCallExpressionCalleeMemberExpression = (statement: TSESTree.Node, excludedIdentifiers: string[]): boolean =>
|
|
79
|
-
isExpressionStatement(statement) &&
|
|
80
|
-
statement.expression.type === TSESTree.AST_NODE_TYPES.CallExpression &&
|
|
81
|
-
statement.expression.callee.type === TSESTree.AST_NODE_TYPES.MemberExpression &&
|
|
82
|
-
statement.expression.callee.object.type === TSESTree.AST_NODE_TYPES.Identifier &&
|
|
83
|
-
statement.expression.callee.property.type === TSESTree.AST_NODE_TYPES.Identifier &&
|
|
84
|
-
!excludedIdentifiers.includes(statement.expression.callee.object.name) &&
|
|
85
|
-
!excludedIdentifiers.includes(
|
|
86
|
-
`${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)
|
|
87
37
|
);
|
|
38
|
+
}
|
|
88
39
|
|
|
89
|
-
//
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
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
|
+
}
|
|
94
53
|
|
|
95
|
-
|
|
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 {
|
|
74
|
+
if (node.type !== TSESTree.AST_NODE_TYPES.VariableDeclaration || node.declarations.length === 0) {
|
|
96
75
|
return false;
|
|
97
76
|
}
|
|
98
77
|
|
|
@@ -106,46 +85,21 @@ const isVariableDeclarationCallExpression = (node: TSESTree.Node, excludedIdenti
|
|
|
106
85
|
isMemberExpressionCallee(init, excludedIdentifiers) ||
|
|
107
86
|
isNonIdentifierObjectMemberExpressionCallee(init)
|
|
108
87
|
);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
// Checks if an ExportNamedDeclaration has side effects
|
|
112
|
-
const isExportNamedDeclarationWithSideEffects = (statement: TSESTree.Node, excludedIdentifiers: string[]): boolean =>
|
|
113
|
-
statement.type === TSESTree.AST_NODE_TYPES.ExportNamedDeclaration &&
|
|
114
|
-
statement.declaration !== null &&
|
|
115
|
-
(isVariableDeclarationAwaitExpression(statement.declaration) ||
|
|
116
|
-
isVariableDeclarationCallExpression(statement.declaration, excludedIdentifiers));
|
|
117
|
-
|
|
118
|
-
// Checks if an ExpressionStatement has side effects
|
|
119
|
-
const isExpressionStatementWithSideEffects = (statement: TSESTree.Node, excludedIdentifiers: string[]): boolean =>
|
|
120
|
-
statement.type === TSESTree.AST_NODE_TYPES.ExpressionStatement &&
|
|
121
|
-
statement.expression.type === TSESTree.AST_NODE_TYPES.CallExpression &&
|
|
122
|
-
((statement.expression.callee.type === TSESTree.AST_NODE_TYPES.Identifier &&
|
|
123
|
-
!excludedIdentifiers.includes(statement.expression.callee.name)) ||
|
|
124
|
-
(statement.expression.callee.type === TSESTree.AST_NODE_TYPES.MemberExpression &&
|
|
125
|
-
statement.expression.callee.object.type === TSESTree.AST_NODE_TYPES.Identifier &&
|
|
126
|
-
statement.expression.callee.property.type === TSESTree.AST_NODE_TYPES.Identifier &&
|
|
127
|
-
!excludedIdentifiers.includes(
|
|
128
|
-
`${statement.expression.callee.object.name}.${statement.expression.callee.property.name}`,
|
|
129
|
-
)));
|
|
130
|
-
|
|
131
|
-
// Checks if a statement has side effects
|
|
132
|
-
const hasSideEffects = (statement: TSESTree.Node, excludedIdentifiers: string[]): boolean => {
|
|
133
|
-
if (isConstOrUsingVariableDeclaration(statement)) {
|
|
134
|
-
return false;
|
|
135
|
-
}
|
|
88
|
+
}
|
|
136
89
|
|
|
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 {
|
|
137
92
|
return (
|
|
138
93
|
isAwaitExpression(statement) ||
|
|
139
94
|
isCallExpressionCalleeMemberExpression(statement, excludedIdentifiers) ||
|
|
140
95
|
isVariableDeclarationAwaitExpression(statement) ||
|
|
141
96
|
isVariableDeclarationCallExpression(statement, excludedIdentifiers) ||
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
isAssignmentExpression(statement)
|
|
97
|
+
(statement.type === TSESTree.AST_NODE_TYPES.ExportNamedDeclaration &&
|
|
98
|
+
statement.declaration !== null &&
|
|
99
|
+
(isVariableDeclarationAwaitExpression(statement.declaration) ||
|
|
100
|
+
isVariableDeclarationCallExpression(statement.declaration, excludedIdentifiers)))
|
|
147
101
|
);
|
|
148
|
-
}
|
|
102
|
+
}
|
|
149
103
|
|
|
150
104
|
const createRule: ReturnType<typeof ESLintUtils.RuleCreator> = ESLintUtils.RuleCreator((name) => name);
|
|
151
105
|
|
|
@@ -0,0 +1,68 @@
|
|
|
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
|
+
const NO_UNKNOWN_TYPE_ASSERTION = 'NO_UNKNOWN_TYPE_ASSERTION';
|
|
14
|
+
|
|
15
|
+
const createRule = ESLintUtils.RuleCreator((name) => name);
|
|
16
|
+
|
|
17
|
+
const rule: ESLintUtils.RuleModule<typeof NO_AS_TYPE_ASSERTION | typeof NO_UNKNOWN_TYPE_ASSERTION> = createRule({
|
|
18
|
+
name: ruleId,
|
|
19
|
+
meta: {
|
|
20
|
+
type: 'suggestion',
|
|
21
|
+
docs: {
|
|
22
|
+
description: 'Disallow the use of `as` type assertions and suggest using `satisfies` instead',
|
|
23
|
+
},
|
|
24
|
+
schema: [],
|
|
25
|
+
messages: {
|
|
26
|
+
[NO_AS_TYPE_ASSERTION]: 'Avoid using `as` type assertions. Use `satisfies` instead.',
|
|
27
|
+
[NO_UNKNOWN_TYPE_ASSERTION]: 'Avoid using `unknown` type assertions.',
|
|
28
|
+
},
|
|
29
|
+
fixable: 'code',
|
|
30
|
+
},
|
|
31
|
+
defaultOptions: [],
|
|
32
|
+
create(context) {
|
|
33
|
+
return {
|
|
34
|
+
TSAsExpression(node: TSESTree.TSAsExpression) {
|
|
35
|
+
const sourceCode = context.sourceCode;
|
|
36
|
+
const typeAnnotation = sourceCode.getText(node.typeAnnotation);
|
|
37
|
+
const expression = sourceCode.getText(node.expression);
|
|
38
|
+
|
|
39
|
+
const isUnknownUnionOrIntersection =
|
|
40
|
+
(node.typeAnnotation.type === AST_NODE_TYPES.TSUnionType ||
|
|
41
|
+
node.typeAnnotation.type === AST_NODE_TYPES.TSIntersectionType) &&
|
|
42
|
+
node.typeAnnotation.types.some((type) => type.type === AST_NODE_TYPES.TSUnknownKeyword);
|
|
43
|
+
|
|
44
|
+
if (
|
|
45
|
+
node.typeAnnotation.type === AST_NODE_TYPES.TSUnknownKeyword ||
|
|
46
|
+
(node.expression.type === AST_NODE_TYPES.TSAsExpression &&
|
|
47
|
+
node.expression.typeAnnotation.type === AST_NODE_TYPES.TSUnknownKeyword) ||
|
|
48
|
+
isUnknownUnionOrIntersection
|
|
49
|
+
) {
|
|
50
|
+
if (node.parent.type !== AST_NODE_TYPES.TSAsExpression) {
|
|
51
|
+
context.report({
|
|
52
|
+
node,
|
|
53
|
+
messageId: NO_UNKNOWN_TYPE_ASSERTION,
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
} else {
|
|
57
|
+
context.report({
|
|
58
|
+
node,
|
|
59
|
+
messageId: NO_AS_TYPE_ASSERTION,
|
|
60
|
+
fix: (fixer) => fixer.replaceText(node, `${expression} satisfies ${typeAnnotation}`),
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
export default rule;
|