eslint-config-typed 5.19.0 → 5.19.1
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/plugins/total-functions/rules/no-hidden-type-assertions.d.mts.map +1 -1
- package/dist/plugins/total-functions/rules/no-hidden-type-assertions.mjs +87 -26
- package/dist/plugins/total-functions/rules/no-hidden-type-assertions.mjs.map +1 -1
- package/dist/plugins/ts-restrictions/rules/array-type-utils.d.mts +10 -1
- package/dist/plugins/ts-restrictions/rules/array-type-utils.d.mts.map +1 -1
- package/dist/plugins/ts-restrictions/rules/array-type-utils.mjs +23 -1
- package/dist/plugins/ts-restrictions/rules/array-type-utils.mjs.map +1 -1
- package/dist/plugins/ts-restrictions/rules/no-unnecessary-array-from.d.mts.map +1 -1
- package/dist/plugins/ts-restrictions/rules/no-unnecessary-array-from.mjs +8 -1
- package/dist/plugins/ts-restrictions/rules/no-unnecessary-array-from.mjs.map +1 -1
- package/dist/plugins/ts-restrictions/rules/prefer-non-mutating-array-method.d.mts.map +1 -1
- package/dist/plugins/ts-restrictions/rules/prefer-non-mutating-array-method.mjs +8 -1
- package/dist/plugins/ts-restrictions/rules/prefer-non-mutating-array-method.mjs.map +1 -1
- package/dist/plugins/vitest-coding-style/rules/prefer-assert-is-false-over-expect-false.d.mts +4 -2
- package/dist/plugins/vitest-coding-style/rules/prefer-assert-is-false-over-expect-false.d.mts.map +1 -1
- package/dist/plugins/vitest-coding-style/rules/prefer-assert-is-false-over-expect-false.mjs +9 -8
- package/dist/plugins/vitest-coding-style/rules/prefer-assert-is-false-over-expect-false.mjs.map +1 -1
- package/dist/plugins/vitest-coding-style/rules/prefer-assert-is-true-over-expect-true.d.mts +4 -2
- package/dist/plugins/vitest-coding-style/rules/prefer-assert-is-true-over-expect-true.d.mts.map +1 -1
- package/dist/plugins/vitest-coding-style/rules/prefer-assert-is-true-over-expect-true.mjs +9 -8
- package/dist/plugins/vitest-coding-style/rules/prefer-assert-is-true-over-expect-true.mjs.map +1 -1
- package/package.json +8 -8
- package/src/plugins/total-functions/rules/no-hidden-type-assertions.mts +165 -46
- package/src/plugins/total-functions/rules/no-hidden-type-assertions.test.mts +55 -0
- package/src/plugins/ts-restrictions/rules/array-type-utils.mts +47 -1
- package/src/plugins/ts-restrictions/rules/no-unnecessary-array-from.mts +14 -1
- package/src/plugins/ts-restrictions/rules/no-unnecessary-array-from.test.mts +8 -0
- package/src/plugins/ts-restrictions/rules/prefer-non-mutating-array-method.mts +14 -1
- package/src/plugins/ts-restrictions/rules/prefer-non-mutating-array-method.test.mts +19 -0
- package/src/plugins/vitest-coding-style/rules/prefer-assert-coding-style-rules.test.mts +37 -0
- package/src/plugins/vitest-coding-style/rules/prefer-assert-is-false-over-expect-false.mts +20 -13
- package/src/plugins/vitest-coding-style/rules/prefer-assert-is-true-over-expect-true.mts +20 -13
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"no-hidden-type-assertions.d.mts","sourceRoot":"","sources":["../../../../src/plugins/total-functions/rules/no-hidden-type-assertions.mts"],"names":[],"mappings":"AAIA,OAAO,
|
|
1
|
+
{"version":3,"file":"no-hidden-type-assertions.d.mts","sourceRoot":"","sources":["../../../../src/plugins/total-functions/rules/no-hidden-type-assertions.mts"],"names":[],"mappings":"AAIA,OAAO,EAEL,WAAW,EAEZ,MAAM,0BAA0B,CAAC;AA4BlC,oDAAoD;AAEpD,eAAO,MAAM,sBAAsB;;CAuRxB,CAAC"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { isTypeNeverType, isTypeUnknownType, } from '@typescript-eslint/type-utils';
|
|
2
|
-
import { ESLintUtils } from '@typescript-eslint/utils';
|
|
2
|
+
import { AST_NODE_TYPES, ESLintUtils, } from '@typescript-eslint/utils';
|
|
3
|
+
import { typeWrapperLayers } from 'eslint-rule-forge';
|
|
3
4
|
import { Arr } from 'ts-data-forge';
|
|
4
5
|
import { isArrayTypeNode, isConditionalTypeNode, isFunctionTypeNode, isIndexedAccessTypeNode, isIntersectionTypeNode, isNamedTupleMember, isTupleTypeNode, isTypeLiteralNode, isTypeNode, isTypeOperatorNode, isTypeReferenceNode, isUnionTypeNode, SyntaxKind, } from 'typescript';
|
|
5
6
|
import { createRule } from './common.mjs';
|
|
@@ -50,34 +51,102 @@ export const noHiddenTypeAssertions = createRule({
|
|
|
50
51
|
const parametersToTypeNodes = (parameters, depth) => parameters
|
|
51
52
|
.flatMap((parameter) => parameter.type !== undefined ? [parameter.type] : [])
|
|
52
53
|
.flatMap((parameter) => explodeTypeNode(parameter, depth));
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
const typeParamsUsedInReturnTypeOf = (declaration) => {
|
|
60
|
+
const { typeParameters, type: returnType } = declaration;
|
|
61
|
+
if (returnType === undefined ||
|
|
62
|
+
typeParameters === undefined ||
|
|
63
|
+
typeParameters.length === 0) {
|
|
64
|
+
return undefined;
|
|
65
|
+
}
|
|
66
|
+
const returnTypes = explodeTypeNode(returnType, 0);
|
|
67
|
+
return typeParameters
|
|
68
|
+
.map((typeParameter, index) => ({ typeParameter, index }))
|
|
69
|
+
.filter((typeParameter) => returnTypes.some((retType) => isTypeReferenceNode(retType) &&
|
|
70
|
+
retType.typeName.kind === SyntaxKind.Identifier &&
|
|
71
|
+
retType.typeName.text === typeParameter.typeParameter.name.text));
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
const returnTypeParamsAppearInValueParams = (declaration, typeParamsUsedInReturnType) => {
|
|
79
|
+
const parameters = parametersToTypeNodes(declaration.parameters, 0);
|
|
80
|
+
return typeParamsUsedInReturnType
|
|
81
|
+
.map(({ typeParameter }) => typeParameter)
|
|
82
|
+
.every((typeParameter) => parameters.some((parameter) => isTypeReferenceNode(parameter) &&
|
|
83
|
+
parameter.typeName.kind === SyntaxKind.Identifier &&
|
|
84
|
+
typeParameter.name.text === parameter.typeName.text));
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
const hasHiddenTypeAssertionSignature = (type) => type.getCallSignatures().some((signature) => {
|
|
91
|
+
const { declaration } = signature;
|
|
92
|
+
if (declaration === undefined ||
|
|
93
|
+
declaration.kind === SyntaxKind.JSDocSignature) {
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
const typeParamsUsedInReturnType = typeParamsUsedInReturnTypeOf(declaration);
|
|
97
|
+
return (typeParamsUsedInReturnType !== undefined &&
|
|
98
|
+
!returnTypeParamsAppearInValueParams(declaration, typeParamsUsedInReturnType));
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
const isCallThroughCastOfHiddenTypeAssertion = (node, callSignature) => {
|
|
111
|
+
const layers = typeWrapperLayers(node.callee);
|
|
112
|
+
const castIndex = layers.findIndex((layer) => layer.type === AST_NODE_TYPES.TSAsExpression ||
|
|
113
|
+
layer.type === AST_NODE_TYPES.TSTypeAssertion);
|
|
114
|
+
if (castIndex === -1) {
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
const returnType = callSignature.getReturnType();
|
|
118
|
+
if (isTypeUnknownType(returnType) || isTypeNeverType(returnType)) {
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
return layers
|
|
122
|
+
.slice(castIndex + 1)
|
|
123
|
+
.some((layer) => hasHiddenTypeAssertionSignature(checker.getTypeAtLocation(parserServices.esTreeNodeToTSNodeMap.get(layer))));
|
|
124
|
+
};
|
|
53
125
|
return {
|
|
54
126
|
CallExpression: (node) => {
|
|
55
127
|
const tsExpressionNode = parserServices.esTreeNodeToTSNodeMap.get(node);
|
|
56
128
|
const callSignature = checker.getResolvedSignature(tsExpressionNode);
|
|
57
|
-
if (callSignature
|
|
129
|
+
if (callSignature === undefined) {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
if (isCallThroughCastOfHiddenTypeAssertion(node, callSignature)) {
|
|
133
|
+
context.report({
|
|
134
|
+
node,
|
|
135
|
+
messageId: 'errorStringGeneric',
|
|
136
|
+
});
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
if (callSignature.declaration === undefined) {
|
|
58
140
|
return;
|
|
59
141
|
}
|
|
60
142
|
if (callSignature.declaration.kind === SyntaxKind.JSDocSignature) {
|
|
61
143
|
|
|
62
144
|
return;
|
|
63
145
|
}
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
const returnType = callSignature.declaration.type;
|
|
67
|
-
if (returnType === undefined ||
|
|
68
|
-
typeParameters === undefined ||
|
|
69
|
-
typeParameters.length === 0) {
|
|
146
|
+
const typeParamsUsedInReturnType = typeParamsUsedInReturnTypeOf(callSignature.declaration);
|
|
147
|
+
if (typeParamsUsedInReturnType === undefined) {
|
|
70
148
|
return;
|
|
71
149
|
}
|
|
72
|
-
const returnTypes = explodeTypeNode(returnType, 0);
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
const typeParamsUsedInReturnType = typeParameters
|
|
77
|
-
.map((typeParameter, index) => ({ typeParameter, index }))
|
|
78
|
-
.filter((typeParameter) => returnTypes.some((retType) => isTypeReferenceNode(retType) &&
|
|
79
|
-
retType.typeName.kind === SyntaxKind.Identifier &&
|
|
80
|
-
retType.typeName.text === typeParameter.typeParameter.name.text));
|
|
81
150
|
|
|
82
151
|
|
|
83
152
|
|
|
@@ -96,15 +165,7 @@ export const noHiddenTypeAssertions = createRule({
|
|
|
96
165
|
if (allCorrespondingTypeArgumentsAreUnknownType) {
|
|
97
166
|
return;
|
|
98
167
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
const allTypeParamsUsedInReturnTypeAlsoAppearInValueParams = typeParamsUsedInReturnType
|
|
103
|
-
.map(({ typeParameter }) => typeParameter)
|
|
104
|
-
.every((typeParameter) => parameters.some((parameter) => isTypeReferenceNode(parameter) &&
|
|
105
|
-
parameter.typeName.kind === SyntaxKind.Identifier &&
|
|
106
|
-
typeParameter.name.text === parameter.typeName.text));
|
|
107
|
-
if (!allTypeParamsUsedInReturnTypeAlsoAppearInValueParams) {
|
|
168
|
+
if (!returnTypeParamsAppearInValueParams(callSignature.declaration, typeParamsUsedInReturnType)) {
|
|
108
169
|
context.report({
|
|
109
170
|
node,
|
|
110
171
|
messageId: 'errorStringGeneric',
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"no-hidden-type-assertions.mjs","sourceRoot":"","sources":["../../../../src/plugins/total-functions/rules/no-hidden-type-assertions.mts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,EACf,iBAAiB,GAClB,MAAM,+BAA+B,CAAC;AACvC,OAAO,
|
|
1
|
+
{"version":3,"file":"no-hidden-type-assertions.mjs","sourceRoot":"","sources":["../../../../src/plugins/total-functions/rules/no-hidden-type-assertions.mts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,EACf,iBAAiB,GAClB,MAAM,+BAA+B,CAAC;AACvC,OAAO,EACL,cAAc,EACd,WAAW,GAEZ,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AACtD,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AACpC,OAAO,EACL,eAAe,EACf,qBAAqB,EACrB,kBAAkB,EAClB,uBAAuB,EACvB,sBAAsB,EACtB,kBAAkB,EAClB,eAAe,EACf,iBAAiB,EACjB,UAAU,EACV,kBAAkB,EAClB,mBAAmB,EACnB,eAAe,EAKf,UAAU,GAKX,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE1C,oDAAoD;AAEpD,MAAM,CAAC,MAAM,sBAAsB,GAAG,UAAU,CAAC;IAC/C,IAAI,E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CAAC,OAAO,KAAK,SAAS;wBACjC,CAAC,CAAC,OAAO,CAAC,iBAAiB,CAAC,aAAa,CAAC,OAAO,CAAC;wBAClD,CAAC,CAAC,SAAS,CAAC;oBAEhB,MAAM,WAAW,GAAG,gBAAgB,IAAI,oBAAoB,CAAC;oBAE7D,OAAO,CACL,WAAW,KAAK,SAAS;wBACzB,CAAC,iBAAiB,CAAC,WAAW,CAAC,IAAI,eAAe,CAAC,WAAW,CAAC,CAAC,CACjE,CAAC;gBACJ,CAAC,CAAC,CAAC;gBAEL,IAAI,2CAA2C,EAAE,CAAC;oBAChD,OAAO;gBACT,CAAC;gBAED,IACE,CAAC,mCAAmC,CAClC,aAAa,CAAC,WAAW,EACzB,0BAA0B,CAC3B,EACD,CAAC;oBACD,OAAO,CAAC,MAAM,CAAC;wBACb,IAAI;wBACJ,SAAS,EAAE,oBAAoB;qBACvB,CAAC,CAAC;gBACd,CAAC;YACH,CAAC;SACF,CAAC;IACJ,CAAC;IACD,cAAc,EAAE,EAAE;CACV,CAAC,CAAC;AAEZ,MAAM,WAAW,GAAG,CAClB,WAAwB,EACmC,EAAE;IAC7D,IAAI,CAAC;QACH,oEAAoE;QACpE,MAAM,QAAQ,GAAI,WAAqD;aACpE,IAAI,CAAC;QAER,OAAO,QAAQ,KAAK,SAAS,IAAI,UAAU,CAAC,QAAQ,CAAC,CAAC;IACxD,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC,CAAC"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { AST_NODE_TYPES, type TSESTree } from '@typescript-eslint/utils';
|
|
1
|
+
import { AST_NODE_TYPES, type ParserServicesWithTypeInformation, type TSESTree } from '@typescript-eslint/utils';
|
|
2
|
+
import type { DeepReadonly } from 'ts-type-forge';
|
|
2
3
|
import * as ts from 'typescript';
|
|
3
4
|
/**
|
|
4
5
|
* Builds a predicate that returns `true` if every constituent of the given type
|
|
@@ -21,4 +22,12 @@ export declare const toMemberObjectText: (argText: string, argNodeType: AST_NODE
|
|
|
21
22
|
* a plain defensive copy and is not matched.
|
|
22
23
|
*/
|
|
23
24
|
export declare const matchArrayFromCall: (node: TSESTree.Expression) => TSESTree.Expression | undefined;
|
|
25
|
+
/**
|
|
26
|
+
* Whether dropping the `Array.from()` copy `inner` would leave a `satisfies`
|
|
27
|
+
* between it and `wrapped` unmet: `(Array.from(xs) satisfies number[])` with
|
|
28
|
+
* `xs: readonly number[]` is checked against the mutable copy, which `xs`
|
|
29
|
+
* itself is not, so the copy is doing the type's work and stays. `as`, `!`
|
|
30
|
+
* and `<T>` do not check their operand that way and are not asked.
|
|
31
|
+
*/
|
|
32
|
+
export declare const createIsCopyNeededBySatisfies: (checker: ts.TypeChecker, esTreeNodeToTSNodeMap: ParserServicesWithTypeInformation['esTreeNodeToTSNodeMap']) => ((wrapped: DeepReadonly<TSESTree.Expression>, inner: DeepReadonly<TSESTree.Expression>, arg: DeepReadonly<TSESTree.Expression>) => boolean);
|
|
24
33
|
//# sourceMappingURL=array-type-utils.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"array-type-utils.d.mts","sourceRoot":"","sources":["../../../../src/plugins/ts-restrictions/rules/array-type-utils.mts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"array-type-utils.d.mts","sourceRoot":"","sources":["../../../../src/plugins/ts-restrictions/rules/array-type-utils.mts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,KAAK,iCAAiC,EACtC,KAAK,QAAQ,EACd,MAAM,0BAA0B,CAAC;AAGlC,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAClD,OAAO,KAAK,EAAE,MAAM,YAAY,CAAC;AAajC;;;;;;;GAOG;AACH,eAAO,MAAM,wBAAwB,YAE1B,EAAE,CAAC,WAAW,KAEtB,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,KAAK,OAAO,CAqB7B,CAAC;AAgBF;;;GAGG;AACH,eAAO,MAAM,kBAAkB,YACpB,MAAM,eACF,cAAc,KAC1B,MAG4B,CAAC;AAEhC;;;;;GAKG;AACH,eAAO,MAAM,kBAAkB,SAEvB,QAAQ,CAAC,UAAU,KACxB,QAAQ,CAAC,UAAU,GAAG,SAyBxB,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,6BAA6B,YAE/B,EAAE,CAAC,WAAW,yBACA,iCAAiC,CAAC,uBAAuB,CAAC,KAChF,CAAC,CACF,OAAO,EAAE,YAAY,CAAC,QAAQ,CAAC,UAAU,CAAC,EAC1C,KAAK,EAAE,YAAY,CAAC,QAAQ,CAAC,UAAU,CAAC,EACxC,GAAG,EAAE,YAAY,CAAC,QAAQ,CAAC,UAAU,CAAC,KACnC,OAAO,CAmBX,CAAC"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { AST_NODE_TYPES } from '@typescript-eslint/utils';
|
|
1
|
+
import { AST_NODE_TYPES, } from '@typescript-eslint/utils';
|
|
2
|
+
import { isTypeWrapper } from 'eslint-rule-forge';
|
|
2
3
|
import { Arr } from 'ts-data-forge';
|
|
3
4
|
import * as ts from 'typescript';
|
|
4
5
|
|
|
@@ -84,4 +85,25 @@ node) => {
|
|
|
84
85
|
const arg = node.arguments[0];
|
|
85
86
|
return arg.type === AST_NODE_TYPES.SpreadElement ? undefined : arg;
|
|
86
87
|
};
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
export const createIsCopyNeededBySatisfies = (
|
|
96
|
+
|
|
97
|
+
checker, esTreeNodeToTSNodeMap) => {
|
|
98
|
+
const isCopyNeededBySatisfies = (wrapped, inner, arg) => wrapped !== inner &&
|
|
99
|
+
isTypeWrapper(wrapped) &&
|
|
100
|
+
((wrapped.type === AST_NODE_TYPES.TSSatisfiesExpression &&
|
|
101
|
+
!checker.isTypeAssignableTo(typeOf(arg), typeOf(wrapped.typeAnnotation))) ||
|
|
102
|
+
isCopyNeededBySatisfies(wrapped.expression, inner, arg));
|
|
103
|
+
const typeOf = (node) => checker.getTypeAtLocation(esTreeNodeToTSNodeMap.get(asNode(node)));
|
|
104
|
+
return isCopyNeededBySatisfies;
|
|
105
|
+
};
|
|
106
|
+
const asNode = (node) =>
|
|
107
|
+
|
|
108
|
+
node;
|
|
87
109
|
//# sourceMappingURL=array-type-utils.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"array-type-utils.mjs","sourceRoot":"","sources":["../../../../src/plugins/ts-restrictions/rules/array-type-utils.mts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"array-type-utils.mjs","sourceRoot":"","sources":["../../../../src/plugins/ts-restrictions/rules/array-type-utils.mts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,GAGf,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAClD,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AAEpC,OAAO,KAAK,EAAE,MAAM,YAAY,CAAC;AAEjC;;;;GAIG;AACH,MAAM,mBAAmB,GACvB,EAAE,CAAC,SAAS,CAAC,aAAa;IAC1B,EAAE,CAAC,SAAS,CAAC,aAAa;IAC1B,EAAE,CAAC,SAAS,CAAC,WAAW;IACxB,EAAE,CAAC,SAAS,CAAC,YAAY,CAAC;AAE5B;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,wBAAwB,GAAG;AACtC,8EAA8E;AAC9E,OAAuB,EAEO,EAAE;IAChC,8EAA8E;IAC9E,MAAM,kBAAkB,GAAG,CAAC,IAAa,EAAW,EAAE;QACpD,IAAI,CAAC,IAAI,CAAC,KAAK,GAAG,CAAC,EAAE,CAAC,SAAS,CAAC,GAAG,GAAG,EAAE,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,KAAK,CAAC,EAAE,CAAC;Y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BAAqB,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAErE,OAAO,uBAAuB,CAAC;AACjC,CAAC,CAAC;AAEF,MAAM,MAAM,GAAG,CAA0B,IAAqB,EAAK,EAAE;AACnE,oEAAoE;AACpE,IAAS,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"no-unnecessary-array-from.d.mts","sourceRoot":"","sources":["../../../../src/plugins/ts-restrictions/rules/no-unnecessary-array-from.mts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,QAAQ,EAEd,MAAM,0BAA0B,CAAC;
|
|
1
|
+
{"version":3,"file":"no-unnecessary-array-from.d.mts","sourceRoot":"","sources":["../../../../src/plugins/ts-restrictions/rules/no-unnecessary-array-from.mts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,QAAQ,EAEd,MAAM,0BAA0B,CAAC;AAUlC,KAAK,OAAO,GAAG,SAAS,EAAE,CAAC;AAE3B,KAAK,UAAU,GAAG,sBAAsB,CAAC;AA+DzC,eAAO,MAAM,sBAAsB,EAAE,QAAQ,CAAC,UAAU,CAAC,UAAU,EAAE,OAAO,CA2FhE,CAAC"}
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { AST_NODE_TYPES, ESLintUtils, } from '@typescript-eslint/utils';
|
|
2
2
|
import { createGetLayerTypes, skipTypeWrappers } from 'eslint-rule-forge';
|
|
3
|
-
import { createIsArrayOrTupleType, matchArrayFromCall, toMemberObjectText, } from './array-type-utils.mjs';
|
|
3
|
+
import { createIsArrayOrTupleType, createIsCopyNeededBySatisfies, matchArrayFromCall, toMemberObjectText, } from './array-type-utils.mjs';
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
4
7
|
|
|
5
8
|
|
|
6
9
|
|
|
@@ -74,6 +77,7 @@ export const noUnnecessaryArrayFrom = {
|
|
|
74
77
|
const parserServices = ESLintUtils.getParserServices(context);
|
|
75
78
|
const checker = parserServices.program.getTypeChecker();
|
|
76
79
|
const isArrayOrTupleType = createIsArrayOrTupleType(checker);
|
|
80
|
+
const isCopyNeededBySatisfies = createIsCopyNeededBySatisfies(checker, parserServices.esTreeNodeToTSNodeMap);
|
|
77
81
|
const getLayerTypes = createGetLayerTypes(parserServices);
|
|
78
82
|
return {
|
|
79
83
|
CallExpression: (node) => {
|
|
@@ -105,6 +109,9 @@ export const noUnnecessaryArrayFrom = {
|
|
|
105
109
|
if (!getLayerTypes(callee.object).every(isArrayOrTupleType)) {
|
|
106
110
|
return;
|
|
107
111
|
}
|
|
112
|
+
if (isCopyNeededBySatisfies(callee.object, inner, arg)) {
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
108
115
|
context.report({
|
|
109
116
|
node: inner,
|
|
110
117
|
messageId: 'unnecessaryArrayFrom',
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"no-unnecessary-array-from.mjs","sourceRoot":"","sources":["../../../../src/plugins/ts-restrictions/rules/no-unnecessary-array-from.mts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,WAAW,GAGZ,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,mBAAmB,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAE1E,OAAO,EACL,wBAAwB,EACxB,kBAAkB,EAClB,kBAAkB,GACnB,MAAM,wBAAwB,CAAC;AAMhC
|
|
1
|
+
{"version":3,"file":"no-unnecessary-array-from.mjs","sourceRoot":"","sources":["../../../../src/plugins/ts-restrictions/rules/no-unnecessary-array-from.mts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,WAAW,GAGZ,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,mBAAmB,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAE1E,OAAO,EACL,wBAAwB,EACxB,6BAA6B,EAC7B,kBAAkB,EAClB,kBAAkB,GACnB,MAAM,wBAAwB,CAAC;AAMhC;;;;;;;;;;;;;;;;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AAO,GAAG,OAAO,CAAC,UAAU,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;wBAEhD,0DAA0D;wBAC1D,mEAAmE;wBACnE,MAAM,UAAU,GAAG,kBAAkB,CAAC,OAAO,EAAE,GAAG,CAAC,IAAI,CAAC,CAAC;wBAEzD,OAAO,KAAK,CAAC,WAAW,CAAC,KAAK,EAAE,UAAU,CAAC,CAAC;oBAC9C,CAAC;iBACF,CAAC,CAAC;YACL,CAAC;SACF,CAAC;IACJ,CAAC;IACD,cAAc,EAAE,EAAE;CACV,CAAC;AAEb,MAAM,MAAM,GAAG,CAA0B,IAAqB,EAAK,EAAE;AACnE,oEAAoE;AACpE,IAAS,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prefer-non-mutating-array-method.d.mts","sourceRoot":"","sources":["../../../../src/plugins/ts-restrictions/rules/prefer-non-mutating-array-method.mts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,QAAQ,EAEd,MAAM,0BAA0B,CAAC;
|
|
1
|
+
{"version":3,"file":"prefer-non-mutating-array-method.d.mts","sourceRoot":"","sources":["../../../../src/plugins/ts-restrictions/rules/prefer-non-mutating-array-method.mts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,QAAQ,EAEd,MAAM,0BAA0B,CAAC;AAelC,KAAK,OAAO,GAAG,SAAS,EAAE,CAAC;AAE3B,KAAK,UAAU,GAAG,yBAAyB,CAAC;AAqE5C,eAAO,MAAM,4BAA4B,EAAE,QAAQ,CAAC,UAAU,CAC5D,UAAU,EACV,OAAO,CAkMC,CAAC"}
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import { AST_NODE_TYPES, ESLintUtils, } from '@typescript-eslint/utils';
|
|
2
2
|
import { createGetLayerTypes, isTypeWrapper, skipTypeWrappers, } from 'eslint-rule-forge';
|
|
3
3
|
import { Arr } from 'ts-data-forge';
|
|
4
|
-
import { createIsArrayOrTupleType, matchArrayFromCall, toMemberObjectText, } from './array-type-utils.mjs';
|
|
4
|
+
import { createIsArrayOrTupleType, createIsCopyNeededBySatisfies, matchArrayFromCall, toMemberObjectText, } from './array-type-utils.mjs';
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
5
8
|
|
|
6
9
|
|
|
7
10
|
|
|
@@ -70,6 +73,7 @@ export const preferNonMutatingArrayMethod = {
|
|
|
70
73
|
const parserServices = ESLintUtils.getParserServices(context);
|
|
71
74
|
const checker = parserServices.program.getTypeChecker();
|
|
72
75
|
const isArrayOrTupleType = createIsArrayOrTupleType(checker);
|
|
76
|
+
const isCopyNeededBySatisfies = createIsCopyNeededBySatisfies(checker, parserServices.esTreeNodeToTSNodeMap);
|
|
73
77
|
const getLayerTypes = createGetLayerTypes(parserServices);
|
|
74
78
|
return {
|
|
75
79
|
CallExpression: (node) => {
|
|
@@ -135,6 +139,9 @@ export const preferNonMutatingArrayMethod = {
|
|
|
135
139
|
if (!getLayerTypes(callee.object).every(isArrayOrTupleType)) {
|
|
136
140
|
return;
|
|
137
141
|
}
|
|
142
|
+
if (isCopyNeededBySatisfies(callee.object, inner, arg)) {
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
138
145
|
const sourceCode = context.sourceCode;
|
|
139
146
|
|
|
140
147
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prefer-non-mutating-array-method.mjs","sourceRoot":"","sources":["../../../../src/plugins/ts-restrictions/rules/prefer-non-mutating-array-method.mts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,WAAW,GAGZ,MAAM,0BAA0B,CAAC;AAClC,OAAO,EACL,mBAAmB,EACnB,aAAa,EACb,gBAAgB,GACjB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AAEpC,OAAO,EACL,wBAAwB,EACxB,kBAAkB,EAClB,kBAAkB,GACnB,MAAM,wBAAwB,CAAC;AAMhC
|
|
1
|
+
{"version":3,"file":"prefer-non-mutating-array-method.mjs","sourceRoot":"","sources":["../../../../src/plugins/ts-restrictions/rules/prefer-non-mutating-array-method.mts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,WAAW,GAGZ,MAAM,0BAA0B,CAAC;AAClC,OAAO,EACL,mBAAmB,EACnB,aAAa,EACb,gBAAgB,GACjB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AAEpC,OAAO,EACL,wBAAwB,EACxB,6BAA6B,EAC7B,kBAAkB,EAClB,kBAAkB,GACnB,MAAM,wBAAwB,CAAC;AAMhC;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,qBAAqB,GAAwB,IAAI,GAAG,CAAC;IACzD,MAAM;IACN,SAAS;IACT,MAAM;IACN,QAAQ;CACT,CAAC,CAAC;AAEH;;;;;;;GAOG;AACH,MAAM,kBAAkB,GAAG,CACzB,IAAuC,EAC9B,EAAE;IACX,iEAAiE;IACjE,IAAI,aAAa,CAAC,IAAI,CAAC,EAAE,CAAC;QACxB,OAAO,kBAAkB,CAAC,gBAAgB,CAAC,IAAI,CAAC,CAAC,CAAC;IACpD,CAAC;IAED,IACE,IAAI,CAAC,IAAI,KAAK,cAAc,CAAC,UAAU;QACvC,IAAI,CAAC,IAAI,KAAK,cAAc,CAAC,cAAc;QAC3C,IAAI,CAAC,IAAI,KAAK,cAAc,CAAC,OAAO,EACpC,CAAC;QACD,OAAO,IAAI,CAAC;IACd,CAAC;IAED,IAAI,IAAI,CAAC,IAAI,KAAK,cAAc,CAAC,eAAe,EAAE,CAAC;QACjD,OAAO,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;IACvC,CAAC;IAED,IAAI,IAAI,CAAC,IAAI,KAAK,cAAc,CAAC,eAAe,EAAE,CAAC;QACjD,OAAO,CACL,CAAC,IAAI,CAAC,QAAQ,KAAK,GAAG,IAAI,IAAI,CAAC,QAAQ,KAAK,GAAG,CAAC;YA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ED,OAAO,aAAa,QAAQ,GAAG,CAAC;wBAClC,CAAC;oBACH,CAAC;gBACH,CAAC,CAAC,EAAE,CAAC;gBAEL,IAAI,WAAW,KAAK,SAAS,EAAE,CAAC;oBAC9B,OAAO;gBACT,CAAC;gBAED,OAAO,CAAC,MAAM,CAAC;oBACb,IAAI;oBACJ,SAAS,EAAE,yBAAyB;oBACpC,IAAI,EAAE,EAAE,cAAc,EAAE,UAAU,EAAE,WAAW,EAAE;oBACjD,8DAA8D;oBAC9D,gEAAgE;oBAChE,8BAA8B;oBAC9B,GAAG,EAAE,CAAC,KAAK,EAAE,EAAE;wBACb,MAAM,UAAU,GAAG,kBAAkB,CACnC,UAAU,CAAC,OAAO,CAAC,GAAG,CAAC,EACvB,GAAG,CAAC,IAAI,CACT,CAAC;wBAEF,OAAO;4BACL,KAAK,CAAC,WAAW,CAAC,KAAK,EAAE,UAAU,CAAC;4BACpC,KAAK,CAAC,gBAAgB,CACpB,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,EACzC,WAAW,CACZ;yBACF,CAAC;oBACJ,CAAC;iBACF,CAAC,CAAC;YACL,CAAC;SACF,CAAC;IACJ,CAAC;IACD,cAAc,EAAE,EAAE;CACV,CAAC;AAEX,MAAM,MAAM,GAAG,CAA0B,IAAqB,EAAK,EAAE;AACnE,oEAAoE;AACpE,IAAS,CAAC"}
|
package/dist/plugins/vitest-coding-style/rules/prefer-assert-is-false-over-expect-false.d.mts
CHANGED
|
@@ -4,8 +4,10 @@ type Options = readonly [];
|
|
|
4
4
|
/**
|
|
5
5
|
* `expect(X).toBe(false)` becomes `assert.isFalse(X)` when `X` is a boolean. The
|
|
6
6
|
* calls, the `expect` function and the `false` are read through `as`,
|
|
7
|
-
* `satisfies`, `!` and `<T>`; `X` keeps its wrappers, and
|
|
8
|
-
*
|
|
7
|
+
* `satisfies`, `!` and `<T>`; `X` keeps its wrappers, and is a boolean only
|
|
8
|
+
* if the type at every layer of them is one: a widening `as` is the author
|
|
9
|
+
* saying it may not be, and a narrowing one (`v as boolean` on an `unknown`,
|
|
10
|
+
* `flag!`) does not make the value a boolean.
|
|
9
11
|
*/
|
|
10
12
|
export declare const preferAssertIsFalseOverExpectFalseRule: TSESLint.RuleModule<MessageIds, Options>;
|
|
11
13
|
export {};
|
package/dist/plugins/vitest-coding-style/rules/prefer-assert-is-false-over-expect-false.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prefer-assert-is-false-over-expect-false.d.mts","sourceRoot":"","sources":["../../../../src/plugins/vitest-coding-style/rules/prefer-assert-is-false-over-expect-false.mts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,QAAQ,EACd,MAAM,0BAA0B,CAAC;
|
|
1
|
+
{"version":3,"file":"prefer-assert-is-false-over-expect-false.d.mts","sourceRoot":"","sources":["../../../../src/plugins/vitest-coding-style/rules/prefer-assert-is-false-over-expect-false.mts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,QAAQ,EACd,MAAM,0BAA0B,CAAC;AAUlC,KAAK,UAAU,GAAG,oCAAoC,CAAC;AAEvD,KAAK,OAAO,GAAG,SAAS,EAAE,CAAC;AAE3B;;;;;;;GAOG;AACH,eAAO,MAAM,sCAAsC,EAAE,QAAQ,CAAC,UAAU,CACtE,UAAU,EACV,OAAO,CA6EC,CAAC"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AST_NODE_TYPES, ESLintUtils, } from '@typescript-eslint/utils';
|
|
2
|
-
import { getArgumentText, skipTypeWrappers } from 'eslint-rule-forge';
|
|
2
|
+
import { getArgumentText, skipTypeWrappers, typeWrapperLayers, } from 'eslint-rule-forge';
|
|
3
3
|
import { Arr } from 'ts-data-forge';
|
|
4
4
|
import * as ts from 'typescript';
|
|
5
5
|
import { getVitestReceiver } from './vitest-binding.mjs';
|
|
@@ -9,6 +9,8 @@ import { getVitestReceiver } from './vitest-binding.mjs';
|
|
|
9
9
|
|
|
10
10
|
|
|
11
11
|
|
|
12
|
+
|
|
13
|
+
|
|
12
14
|
export const preferAssertIsFalseOverExpectFalseRule = {
|
|
13
15
|
meta: {
|
|
14
16
|
type: 'suggestion',
|
|
@@ -46,13 +48,12 @@ export const preferAssertIsFalseOverExpectFalseRule = {
|
|
|
46
48
|
expected?.type === AST_NODE_TYPES.Literal &&
|
|
47
49
|
expected.value === false) {
|
|
48
50
|
const arg = expectCall.arguments[0];
|
|
49
|
-
if (arg !== undefined
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
}
|
|
51
|
+
if (arg !== undefined &&
|
|
52
|
+
!typeWrapperLayers(arg).every((layer) => {
|
|
53
|
+
const type = checker.getTypeAtLocation(parserServices.esTreeNodeToTSNodeMap.get(layer));
|
|
54
|
+
return (type.flags & ts.TypeFlags.Boolean) !== 0;
|
|
55
|
+
})) {
|
|
56
|
+
return;
|
|
56
57
|
}
|
|
57
58
|
const argText = arg === undefined ? '' : getArgumentText(context.sourceCode, arg);
|
|
58
59
|
context.report({
|
package/dist/plugins/vitest-coding-style/rules/prefer-assert-is-false-over-expect-false.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prefer-assert-is-false-over-expect-false.mjs","sourceRoot":"","sources":["../../../../src/plugins/vitest-coding-style/rules/prefer-assert-is-false-over-expect-false.mts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,WAAW,GAEZ,MAAM,0BAA0B,CAAC;AAClC,OAAO,
|
|
1
|
+
{"version":3,"file":"prefer-assert-is-false-over-expect-false.mjs","sourceRoot":"","sources":["../../../../src/plugins/vitest-coding-style/rules/prefer-assert-is-false-over-expect-false.mts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,WAAW,GAEZ,MAAM,0BAA0B,CAAC;AAClC,OAAO,EACL,eAAe,EACf,gBA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10 @@ type Options = readonly [];
|
|
|
4
4
|
/**
|
|
5
5
|
* `expect(X).toBe(true)` becomes `assert.isTrue(X)` when `X` is a boolean. The
|
|
6
6
|
* calls, the `expect` function and the `true` are read through `as`,
|
|
7
|
-
* `satisfies`, `!` and `<T>`; `X` keeps its wrappers, and
|
|
8
|
-
*
|
|
7
|
+
* `satisfies`, `!` and `<T>`; `X` keeps its wrappers, and is a boolean only
|
|
8
|
+
* if the type at every layer of them is one: a widening `as` is the author
|
|
9
|
+
* saying it may not be, and a narrowing one (`v as boolean` on an `unknown`,
|
|
10
|
+
* `flag!`) does not make the value a boolean.
|
|
9
11
|
*/
|
|
10
12
|
export declare const preferAssertIsTrueOverExpectTrueRule: TSESLint.RuleModule<MessageIds, Options>;
|
|
11
13
|
export {};
|
package/dist/plugins/vitest-coding-style/rules/prefer-assert-is-true-over-expect-true.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prefer-assert-is-true-over-expect-true.d.mts","sourceRoot":"","sources":["../../../../src/plugins/vitest-coding-style/rules/prefer-assert-is-true-over-expect-true.mts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,QAAQ,EACd,MAAM,0BAA0B,CAAC;
|
|
1
|
+
{"version":3,"file":"prefer-assert-is-true-over-expect-true.d.mts","sourceRoot":"","sources":["../../../../src/plugins/vitest-coding-style/rules/prefer-assert-is-true-over-expect-true.mts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,QAAQ,EACd,MAAM,0BAA0B,CAAC;AAUlC,KAAK,UAAU,GAAG,kCAAkC,CAAC;AAErD,KAAK,OAAO,GAAG,SAAS,EAAE,CAAC;AAE3B;;;;;;;GAOG;AACH,eAAO,MAAM,oCAAoC,EAAE,QAAQ,CAAC,UAAU,CACpE,UAAU,EACV,OAAO,CA6EC,CAAC"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AST_NODE_TYPES, ESLintUtils, } from '@typescript-eslint/utils';
|
|
2
|
-
import { getArgumentText, skipTypeWrappers } from 'eslint-rule-forge';
|
|
2
|
+
import { getArgumentText, skipTypeWrappers, typeWrapperLayers, } from 'eslint-rule-forge';
|
|
3
3
|
import { Arr } from 'ts-data-forge';
|
|
4
4
|
import * as ts from 'typescript';
|
|
5
5
|
import { getVitestReceiver } from './vitest-binding.mjs';
|
|
@@ -9,6 +9,8 @@ import { getVitestReceiver } from './vitest-binding.mjs';
|
|
|
9
9
|
|
|
10
10
|
|
|
11
11
|
|
|
12
|
+
|
|
13
|
+
|
|
12
14
|
export const preferAssertIsTrueOverExpectTrueRule = {
|
|
13
15
|
meta: {
|
|
14
16
|
type: 'suggestion',
|
|
@@ -46,13 +48,12 @@ export const preferAssertIsTrueOverExpectTrueRule = {
|
|
|
46
48
|
expected?.type === AST_NODE_TYPES.Literal &&
|
|
47
49
|
expected.value === true) {
|
|
48
50
|
const arg = expectCall.arguments[0];
|
|
49
|
-
if (arg !== undefined
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
}
|
|
51
|
+
if (arg !== undefined &&
|
|
52
|
+
!typeWrapperLayers(arg).every((layer) => {
|
|
53
|
+
const type = checker.getTypeAtLocation(parserServices.esTreeNodeToTSNodeMap.get(layer));
|
|
54
|
+
return (type.flags & ts.TypeFlags.Boolean) !== 0;
|
|
55
|
+
})) {
|
|
56
|
+
return;
|
|
56
57
|
}
|
|
57
58
|
const argText = arg === undefined ? '' : getArgumentText(context.sourceCode, arg);
|
|
58
59
|
context.report({
|
package/dist/plugins/vitest-coding-style/rules/prefer-assert-is-true-over-expect-true.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prefer-assert-is-true-over-expect-true.mjs","sourceRoot":"","sources":["../../../../src/plugins/vitest-coding-style/rules/prefer-assert-is-true-over-expect-true.mts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,WAAW,GAEZ,MAAM,0BAA0B,CAAC;AAClC,OAAO,
|
|
1
|
+
{"version":3,"file":"prefer-assert-is-true-over-expect-true.mjs","sourceRoot":"","sources":["../../../../src/plugins/vitest-coding-style/rules/prefer-assert-is-true-over-expect-true.mts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,WAAW,GAEZ,MAAM,0BAA0B,CAAC;AAClC,OAAO,EACL,eAAe,EACf,gBAAgB,EAChB,iBAAiB,GAClB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AACpC,OAAO,KAAK,EAAE,MAAM,YAAY,CAAC;AACjC,OAAO,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AAMzD;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,oCAAoC,GAG7C;IACF,IAAI,EAAE;QACJ,IAAI,EAAE,YAAY;QAClB,IAAI,EAAE;YACJ,WAAW,EACT,0EAA0E;SAC7E;QACD,OAAO,EAAE,MAAM;QACf,MAAM,EAAE,EAAE;QACV,QAAQ,EAAE;YACR,gCAAgC,EAC9B,sDAAsD;SACzD;KACF;IACD,cAAc,EAAE,EAAE;IAClB,MAAM,EAAE,CAAC,OAAO,EAAE,EAAE;QAClB,MAAM,cAAc,GAAG,WAAW,CAAC,iBAAiB,CAAC,OAAO,CAAC,CAAC;QAE9D,MAAM,OAAO,GAAG,cAAc,CAAC,OAAO,CAAC,cAAc,EAAE,CAAC;QAExD,OAAO;YACL,cAAc,EAAE,CAAC,IAAI,EAAE,EAAE;gBACvB,MAAM,MAAM,GAAG,gBAAgB,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;gBAE7C,IAAI,MAAM,CAAC,IAAI,KAAK,cAAc,CAAC,gBAAgB,EAAE,CAAC;oBACpD,OAAO;gBACT,CAAC;gBAED,MAAM,UAAU,GAAG,gBAAgB,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;gBAEnD,MAAM,CAAC,WAAW,CAAC,GAAG,IAAI,CAAC,SAAS,C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IAAI;wBACJ,SAAS,EAAE,kCAAkC;wBAC7C,GAAG,EAAE,CAAC,KAAK,EAAE,EAAE,CACb,KAAK,CAAC,WAAW,CAAC,IAAI,EAAE,iBAAiB,OAAO,GAAG,CAAC;qBACvD,CAAC,CAAC;gBACL,CAAC;YACH,CAAC;SACF,CAAC;IACJ,CAAC;CACO,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-config-typed",
|
|
3
|
-
"version": "5.19.
|
|
3
|
+
"version": "5.19.1",
|
|
4
4
|
"private": false,
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript"
|
|
@@ -70,12 +70,12 @@
|
|
|
70
70
|
"eslint-plugin-sort-destructure-keys": "^3.0.0",
|
|
71
71
|
"eslint-plugin-testing-library": "^7.16.2",
|
|
72
72
|
"eslint-plugin-unicorn": "^77.0.0",
|
|
73
|
-
"eslint-rule-forge": "^0.
|
|
73
|
+
"eslint-rule-forge": "^0.3.0",
|
|
74
74
|
"globals": "^17.13.0",
|
|
75
75
|
"is-glob": "^4.0.3",
|
|
76
76
|
"is-immutable-type": "^5.0.4",
|
|
77
77
|
"micromatch": "^4.0.8",
|
|
78
|
-
"ts-data-forge": "^14.
|
|
78
|
+
"ts-data-forge": "^14.9.0",
|
|
79
79
|
"ts-type-forge": "^9.2.2",
|
|
80
80
|
"tsutils": "^3.21.0"
|
|
81
81
|
},
|
|
@@ -90,10 +90,10 @@
|
|
|
90
90
|
"@vitest/ui": "5.0.3",
|
|
91
91
|
"cspell": "10.3.6",
|
|
92
92
|
"dedent": "1.7.2",
|
|
93
|
-
"eslint-config-typed": "5.19.
|
|
94
|
-
"eslint-plugin-ts-data-forge": "0.
|
|
95
|
-
"eslint-plugin-ts-fortress": "1.3.
|
|
96
|
-
"eslint-plugin-ts-type-forge": "0.8.
|
|
93
|
+
"eslint-config-typed": "5.19.1",
|
|
94
|
+
"eslint-plugin-ts-data-forge": "0.10.1",
|
|
95
|
+
"eslint-plugin-ts-fortress": "1.3.6",
|
|
96
|
+
"eslint-plugin-ts-type-forge": "0.8.11",
|
|
97
97
|
"immer": "11.1.21",
|
|
98
98
|
"jiti": "2.7.0",
|
|
99
99
|
"json-schema": "0.4.0",
|
|
@@ -105,7 +105,7 @@
|
|
|
105
105
|
"prettier-plugin-organize-imports": "4.3.0",
|
|
106
106
|
"prettier-plugin-packagejson": "3.0.2",
|
|
107
107
|
"react": "19.3.0",
|
|
108
|
-
"ts-codemod-lib": "3.
|
|
108
|
+
"ts-codemod-lib": "3.5.0",
|
|
109
109
|
"ts-repo-utils": "10.7.1",
|
|
110
110
|
"tsx": "4.23.15",
|
|
111
111
|
"typescript": "6.0.3",
|
|
@@ -2,7 +2,12 @@ import {
|
|
|
2
2
|
isTypeNeverType,
|
|
3
3
|
isTypeUnknownType,
|
|
4
4
|
} from '@typescript-eslint/type-utils';
|
|
5
|
-
import {
|
|
5
|
+
import {
|
|
6
|
+
AST_NODE_TYPES,
|
|
7
|
+
ESLintUtils,
|
|
8
|
+
type TSESTree,
|
|
9
|
+
} from '@typescript-eslint/utils';
|
|
10
|
+
import { typeWrapperLayers } from 'eslint-rule-forge';
|
|
6
11
|
import { Arr } from 'ts-data-forge';
|
|
7
12
|
import {
|
|
8
13
|
isArrayTypeNode,
|
|
@@ -19,9 +24,13 @@ import {
|
|
|
19
24
|
isUnionTypeNode,
|
|
20
25
|
type NodeArray,
|
|
21
26
|
type ParameterDeclaration,
|
|
27
|
+
type Signature,
|
|
28
|
+
type SignatureDeclaration,
|
|
22
29
|
SyntaxKind,
|
|
30
|
+
type Type,
|
|
23
31
|
type TypeElement,
|
|
24
32
|
type TypeNode,
|
|
33
|
+
type TypeParameterDeclaration,
|
|
25
34
|
} from 'typescript';
|
|
26
35
|
import { createRule } from './common.mjs';
|
|
27
36
|
|
|
@@ -94,13 +103,158 @@ export const noHiddenTypeAssertions = createRule({
|
|
|
94
103
|
)
|
|
95
104
|
.flatMap((parameter) => explodeTypeNode(parameter, depth));
|
|
96
105
|
|
|
106
|
+
/**
|
|
107
|
+
* The type parameters of `declaration` that its return type uses, with
|
|
108
|
+
* their positional index (to look up the corresponding type arguments), or
|
|
109
|
+
* `undefined` when it declares no type parameters or no return type.
|
|
110
|
+
*/
|
|
111
|
+
const typeParamsUsedInReturnTypeOf = (
|
|
112
|
+
declaration: SignatureDeclaration,
|
|
113
|
+
):
|
|
114
|
+
| readonly Readonly<{
|
|
115
|
+
typeParameter: TypeParameterDeclaration;
|
|
116
|
+
index: number;
|
|
117
|
+
}>[]
|
|
118
|
+
| undefined => {
|
|
119
|
+
const { typeParameters, type: returnType } = declaration;
|
|
120
|
+
|
|
121
|
+
if (
|
|
122
|
+
returnType === undefined ||
|
|
123
|
+
typeParameters === undefined ||
|
|
124
|
+
typeParameters.length === 0
|
|
125
|
+
) {
|
|
126
|
+
return undefined;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const returnTypes = explodeTypeNode(returnType, 0);
|
|
130
|
+
|
|
131
|
+
return typeParameters
|
|
132
|
+
.map((typeParameter, index) => ({ typeParameter, index }))
|
|
133
|
+
.filter((typeParameter) =>
|
|
134
|
+
returnTypes.some(
|
|
135
|
+
(retType) =>
|
|
136
|
+
isTypeReferenceNode(retType) &&
|
|
137
|
+
retType.typeName.kind === SyntaxKind.Identifier &&
|
|
138
|
+
retType.typeName.text === typeParameter.typeParameter.name.text,
|
|
139
|
+
),
|
|
140
|
+
);
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Whether every type parameter of `declaration` that its return type uses
|
|
145
|
+
* also appears in at least one (value) parameter. If so, the function
|
|
146
|
+
* probably isn't a hidden type assertion.
|
|
147
|
+
*/
|
|
148
|
+
const returnTypeParamsAppearInValueParams = (
|
|
149
|
+
declaration: SignatureDeclaration,
|
|
150
|
+
typeParamsUsedInReturnType: readonly Readonly<{
|
|
151
|
+
typeParameter: TypeParameterDeclaration;
|
|
152
|
+
}>[],
|
|
153
|
+
): boolean => {
|
|
154
|
+
const parameters = parametersToTypeNodes(declaration.parameters, 0);
|
|
155
|
+
|
|
156
|
+
return typeParamsUsedInReturnType
|
|
157
|
+
.map(({ typeParameter }) => typeParameter)
|
|
158
|
+
.every((typeParameter) =>
|
|
159
|
+
parameters.some(
|
|
160
|
+
(parameter) =>
|
|
161
|
+
isTypeReferenceNode(parameter) &&
|
|
162
|
+
parameter.typeName.kind === SyntaxKind.Identifier &&
|
|
163
|
+
typeParameter.name.text === parameter.typeName.text,
|
|
164
|
+
),
|
|
165
|
+
);
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Whether a call signature of `type` is a hidden type assertion: generic
|
|
170
|
+
* in a type parameter that only its return type uses.
|
|
171
|
+
*/
|
|
172
|
+
const hasHiddenTypeAssertionSignature = (type: Type): boolean =>
|
|
173
|
+
type.getCallSignatures().some((signature) => {
|
|
174
|
+
const { declaration } = signature;
|
|
175
|
+
|
|
176
|
+
if (
|
|
177
|
+
declaration === undefined ||
|
|
178
|
+
declaration.kind === SyntaxKind.JSDocSignature
|
|
179
|
+
) {
|
|
180
|
+
return false;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const typeParamsUsedInReturnType =
|
|
184
|
+
typeParamsUsedInReturnTypeOf(declaration);
|
|
185
|
+
|
|
186
|
+
return (
|
|
187
|
+
typeParamsUsedInReturnType !== undefined &&
|
|
188
|
+
!returnTypeParamsAppearInValueParams(
|
|
189
|
+
declaration,
|
|
190
|
+
typeParamsUsedInReturnType,
|
|
191
|
+
)
|
|
192
|
+
);
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Whether the callee is a cast (`as` or `<T>`) of a function whose
|
|
197
|
+
* signature is a hidden type assertion: `(parse as () => Foo)()` picks
|
|
198
|
+
* `Foo` for `parse`'s type parameter as surely as `parse<Foo>()` does, and
|
|
199
|
+
* the cast is not reported by `no-unsafe-type-assertion`, since the
|
|
200
|
+
* generic function is assignable to every instantiation of it. Every layer
|
|
201
|
+
* under the outermost cast is asked, so that `parse as unknown as () =>
|
|
202
|
+
* Foo` is seen too. A call whose result is `unknown` or `never` asserts
|
|
203
|
+
* nothing.
|
|
204
|
+
*/
|
|
205
|
+
const isCallThroughCastOfHiddenTypeAssertion = (
|
|
206
|
+
node: TSESTree.CallExpression,
|
|
207
|
+
callSignature: Signature,
|
|
208
|
+
): boolean => {
|
|
209
|
+
const layers = typeWrapperLayers(node.callee);
|
|
210
|
+
|
|
211
|
+
const castIndex = layers.findIndex(
|
|
212
|
+
(layer) =>
|
|
213
|
+
layer.type === AST_NODE_TYPES.TSAsExpression ||
|
|
214
|
+
layer.type === AST_NODE_TYPES.TSTypeAssertion,
|
|
215
|
+
);
|
|
216
|
+
|
|
217
|
+
if (castIndex === -1) {
|
|
218
|
+
return false;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const returnType = callSignature.getReturnType();
|
|
222
|
+
|
|
223
|
+
if (isTypeUnknownType(returnType) || isTypeNeverType(returnType)) {
|
|
224
|
+
return false;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
return layers
|
|
228
|
+
.slice(castIndex + 1)
|
|
229
|
+
.some((layer) =>
|
|
230
|
+
hasHiddenTypeAssertionSignature(
|
|
231
|
+
checker.getTypeAtLocation(
|
|
232
|
+
parserServices.esTreeNodeToTSNodeMap.get(layer),
|
|
233
|
+
),
|
|
234
|
+
),
|
|
235
|
+
);
|
|
236
|
+
};
|
|
237
|
+
|
|
97
238
|
return {
|
|
98
239
|
CallExpression: (node) => {
|
|
99
240
|
const tsExpressionNode = parserServices.esTreeNodeToTSNodeMap.get(node);
|
|
100
241
|
|
|
101
242
|
const callSignature = checker.getResolvedSignature(tsExpressionNode);
|
|
102
243
|
|
|
103
|
-
if (callSignature
|
|
244
|
+
if (callSignature === undefined) {
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
if (isCallThroughCastOfHiddenTypeAssertion(node, callSignature)) {
|
|
249
|
+
context.report({
|
|
250
|
+
node,
|
|
251
|
+
messageId: 'errorStringGeneric',
|
|
252
|
+
} as const);
|
|
253
|
+
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
if (callSignature.declaration === undefined) {
|
|
104
258
|
return;
|
|
105
259
|
}
|
|
106
260
|
|
|
@@ -109,39 +263,14 @@ export const noHiddenTypeAssertions = createRule({
|
|
|
109
263
|
return;
|
|
110
264
|
}
|
|
111
265
|
|
|
112
|
-
const
|
|
113
|
-
|
|
114
|
-
const parameters = parametersToTypeNodes(
|
|
115
|
-
callSignature.declaration.parameters,
|
|
116
|
-
0,
|
|
266
|
+
const typeParamsUsedInReturnType = typeParamsUsedInReturnTypeOf(
|
|
267
|
+
callSignature.declaration,
|
|
117
268
|
);
|
|
118
269
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
if (
|
|
122
|
-
returnType === undefined ||
|
|
123
|
-
typeParameters === undefined ||
|
|
124
|
-
typeParameters.length === 0
|
|
125
|
-
) {
|
|
270
|
+
if (typeParamsUsedInReturnType === undefined) {
|
|
126
271
|
return;
|
|
127
272
|
}
|
|
128
273
|
|
|
129
|
-
const returnTypes = explodeTypeNode(returnType, 0);
|
|
130
|
-
|
|
131
|
-
// Of all the type parameters, these are the ones that are used in the
|
|
132
|
-
// return type (alongside their positional index, which we use next to
|
|
133
|
-
// look up corresponding type arguments)
|
|
134
|
-
const typeParamsUsedInReturnType = typeParameters
|
|
135
|
-
.map((typeParameter, index) => ({ typeParameter, index }))
|
|
136
|
-
.filter((typeParameter) =>
|
|
137
|
-
returnTypes.some(
|
|
138
|
-
(retType) =>
|
|
139
|
-
isTypeReferenceNode(retType) &&
|
|
140
|
-
retType.typeName.kind === SyntaxKind.Identifier &&
|
|
141
|
-
retType.typeName.text === typeParameter.typeParameter.name.text,
|
|
142
|
-
),
|
|
143
|
-
);
|
|
144
|
-
|
|
145
274
|
// Of all the type parameters that appear in the return type,
|
|
146
275
|
// are they all set to `unknown` type arguments in this specific call?
|
|
147
276
|
// If so, this is safe even if the function being called is a hidden type assertion.
|
|
@@ -171,22 +300,12 @@ export const noHiddenTypeAssertions = createRule({
|
|
|
171
300
|
return;
|
|
172
301
|
}
|
|
173
302
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
.every((typeParameter) =>
|
|
181
|
-
parameters.some(
|
|
182
|
-
(parameter) =>
|
|
183
|
-
isTypeReferenceNode(parameter) &&
|
|
184
|
-
parameter.typeName.kind === SyntaxKind.Identifier &&
|
|
185
|
-
typeParameter.name.text === parameter.typeName.text,
|
|
186
|
-
),
|
|
187
|
-
);
|
|
188
|
-
|
|
189
|
-
if (!allTypeParamsUsedInReturnTypeAlsoAppearInValueParams) {
|
|
303
|
+
if (
|
|
304
|
+
!returnTypeParamsAppearInValueParams(
|
|
305
|
+
callSignature.declaration,
|
|
306
|
+
typeParamsUsedInReturnType,
|
|
307
|
+
)
|
|
308
|
+
) {
|
|
190
309
|
context.report({
|
|
191
310
|
node,
|
|
192
311
|
messageId: 'errorStringGeneric',
|
|
@@ -16,6 +16,30 @@ const ruleTester = new RuleTester({
|
|
|
16
16
|
|
|
17
17
|
ruleTester.run('no-hidden-type-assertions', noHiddenTypeAssertions, {
|
|
18
18
|
valid: [
|
|
19
|
+
// A cast to a function type that returns `unknown` asserts nothing
|
|
20
|
+
{
|
|
21
|
+
filename: 'file.ts',
|
|
22
|
+
code: dedent`
|
|
23
|
+
declare const parse: <T>(text: string) => T;
|
|
24
|
+
export const result = (parse as (text: string) => unknown)('{}');
|
|
25
|
+
`,
|
|
26
|
+
},
|
|
27
|
+
// A cast of a generic function whose type parameter appears in a parameter
|
|
28
|
+
{
|
|
29
|
+
filename: 'file.ts',
|
|
30
|
+
code: dedent`
|
|
31
|
+
declare const id: <T>(x: T) => T;
|
|
32
|
+
export const result = (id as (x: string) => string)('a');
|
|
33
|
+
`,
|
|
34
|
+
},
|
|
35
|
+
// A cast of a function that is not generic
|
|
36
|
+
{
|
|
37
|
+
filename: 'file.ts',
|
|
38
|
+
code: dedent`
|
|
39
|
+
declare const f: (text: string) => number;
|
|
40
|
+
export const result = (f as (text: string) => number)('a');
|
|
41
|
+
`,
|
|
42
|
+
},
|
|
19
43
|
// No call signature
|
|
20
44
|
{
|
|
21
45
|
filename: 'file.ts',
|
|
@@ -284,5 +308,36 @@ ruleTester.run('no-hidden-type-assertions', noHiddenTypeAssertions, {
|
|
|
284
308
|
},
|
|
285
309
|
],
|
|
286
310
|
},
|
|
311
|
+
// A hidden type assertion made by casting the generic function to a
|
|
312
|
+
// function type, which instantiates it without a type argument
|
|
313
|
+
{
|
|
314
|
+
filename: 'file.ts',
|
|
315
|
+
code: dedent`
|
|
316
|
+
declare const parse: <T>(text: string) => T;
|
|
317
|
+
type Foo = Readonly<{ a: number }>;
|
|
318
|
+
export const a = (parse as (text: string) => Foo)('{}');
|
|
319
|
+
export const b = (<(text: string) => Foo>parse)('{}');
|
|
320
|
+
export const c = (parse as unknown as (text: string) => Foo)('{}');
|
|
321
|
+
export const d = (parse! as (text: string) => Foo)('{}');
|
|
322
|
+
`,
|
|
323
|
+
errors: [
|
|
324
|
+
{
|
|
325
|
+
messageId: 'errorStringGeneric',
|
|
326
|
+
type: AST_NODE_TYPES.CallExpression,
|
|
327
|
+
},
|
|
328
|
+
{
|
|
329
|
+
messageId: 'errorStringGeneric',
|
|
330
|
+
type: AST_NODE_TYPES.CallExpression,
|
|
331
|
+
},
|
|
332
|
+
{
|
|
333
|
+
messageId: 'errorStringGeneric',
|
|
334
|
+
type: AST_NODE_TYPES.CallExpression,
|
|
335
|
+
},
|
|
336
|
+
{
|
|
337
|
+
messageId: 'errorStringGeneric',
|
|
338
|
+
type: AST_NODE_TYPES.CallExpression,
|
|
339
|
+
},
|
|
340
|
+
],
|
|
341
|
+
},
|
|
287
342
|
],
|
|
288
343
|
} as const);
|
|
@@ -1,5 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
AST_NODE_TYPES,
|
|
3
|
+
type ParserServicesWithTypeInformation,
|
|
4
|
+
type TSESTree,
|
|
5
|
+
} from '@typescript-eslint/utils';
|
|
6
|
+
import { isTypeWrapper } from 'eslint-rule-forge';
|
|
2
7
|
import { Arr } from 'ts-data-forge';
|
|
8
|
+
import type { DeepReadonly } from 'ts-type-forge';
|
|
3
9
|
import * as ts from 'typescript';
|
|
4
10
|
|
|
5
11
|
/**
|
|
@@ -109,3 +115,43 @@ export const matchArrayFromCall = (
|
|
|
109
115
|
|
|
110
116
|
return arg.type === AST_NODE_TYPES.SpreadElement ? undefined : arg;
|
|
111
117
|
};
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Whether dropping the `Array.from()` copy `inner` would leave a `satisfies`
|
|
121
|
+
* between it and `wrapped` unmet: `(Array.from(xs) satisfies number[])` with
|
|
122
|
+
* `xs: readonly number[]` is checked against the mutable copy, which `xs`
|
|
123
|
+
* itself is not, so the copy is doing the type's work and stays. `as`, `!`
|
|
124
|
+
* and `<T>` do not check their operand that way and are not asked.
|
|
125
|
+
*/
|
|
126
|
+
export const createIsCopyNeededBySatisfies = (
|
|
127
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
128
|
+
checker: ts.TypeChecker,
|
|
129
|
+
esTreeNodeToTSNodeMap: ParserServicesWithTypeInformation['esTreeNodeToTSNodeMap'],
|
|
130
|
+
): ((
|
|
131
|
+
wrapped: DeepReadonly<TSESTree.Expression>,
|
|
132
|
+
inner: DeepReadonly<TSESTree.Expression>,
|
|
133
|
+
arg: DeepReadonly<TSESTree.Expression>,
|
|
134
|
+
) => boolean) => {
|
|
135
|
+
const isCopyNeededBySatisfies = (
|
|
136
|
+
wrapped: DeepReadonly<TSESTree.Expression>,
|
|
137
|
+
inner: DeepReadonly<TSESTree.Expression>,
|
|
138
|
+
arg: DeepReadonly<TSESTree.Expression>,
|
|
139
|
+
): boolean =>
|
|
140
|
+
wrapped !== inner &&
|
|
141
|
+
isTypeWrapper(wrapped) &&
|
|
142
|
+
((wrapped.type === AST_NODE_TYPES.TSSatisfiesExpression &&
|
|
143
|
+
!checker.isTypeAssignableTo(
|
|
144
|
+
typeOf(arg),
|
|
145
|
+
typeOf(wrapped.typeAnnotation),
|
|
146
|
+
)) ||
|
|
147
|
+
isCopyNeededBySatisfies(wrapped.expression, inner, arg));
|
|
148
|
+
|
|
149
|
+
const typeOf = (node: DeepReadonly<TSESTree.Node>): ts.Type =>
|
|
150
|
+
checker.getTypeAtLocation(esTreeNodeToTSNodeMap.get(asNode(node)));
|
|
151
|
+
|
|
152
|
+
return isCopyNeededBySatisfies;
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
const asNode = <T extends TSESTree.Node>(node: DeepReadonly<T>): T =>
|
|
156
|
+
// eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
157
|
+
node as T;
|
|
@@ -8,6 +8,7 @@ import { createGetLayerTypes, skipTypeWrappers } from 'eslint-rule-forge';
|
|
|
8
8
|
import type { DeepReadonly } from 'ts-type-forge';
|
|
9
9
|
import {
|
|
10
10
|
createIsArrayOrTupleType,
|
|
11
|
+
createIsCopyNeededBySatisfies,
|
|
11
12
|
matchArrayFromCall,
|
|
12
13
|
toMemberObjectText,
|
|
13
14
|
} from './array-type-utils.mjs';
|
|
@@ -29,7 +30,10 @@ type MessageIds = 'unnecessaryArrayFrom';
|
|
|
29
30
|
*
|
|
30
31
|
* The `Array.from()` call is found through type wrappers, which the fix keeps
|
|
31
32
|
* (`(Array.from(x) satisfies T).map(f)` → `(x satisfies T).map(f)`), and the
|
|
32
|
-
* argument's type is that of its value, not of a cast around it.
|
|
33
|
+
* argument's type is that of its value, not of a cast around it. A
|
|
34
|
+
* `satisfies` around the call that the argument itself does not meet keeps
|
|
35
|
+
* the copy: `(Array.from(xs) satisfies number[])` with `xs: readonly number[]`
|
|
36
|
+
* would not type-check as `xs satisfies number[]`.
|
|
33
37
|
*/
|
|
34
38
|
const NON_MUTATING_ARRAY_METHODS: ReadonlySet<string> = new Set([
|
|
35
39
|
'at',
|
|
@@ -97,6 +101,11 @@ export const noUnnecessaryArrayFrom: TSESLint.RuleModule<MessageIds, Options> =
|
|
|
97
101
|
|
|
98
102
|
const isArrayOrTupleType = createIsArrayOrTupleType(checker);
|
|
99
103
|
|
|
104
|
+
const isCopyNeededBySatisfies = createIsCopyNeededBySatisfies(
|
|
105
|
+
checker,
|
|
106
|
+
parserServices.esTreeNodeToTSNodeMap,
|
|
107
|
+
);
|
|
108
|
+
|
|
100
109
|
const getLayerTypes = createGetLayerTypes(parserServices);
|
|
101
110
|
|
|
102
111
|
return {
|
|
@@ -138,6 +147,10 @@ export const noUnnecessaryArrayFrom: TSESLint.RuleModule<MessageIds, Options> =
|
|
|
138
147
|
return;
|
|
139
148
|
}
|
|
140
149
|
|
|
150
|
+
if (isCopyNeededBySatisfies(callee.object, inner, arg)) {
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
|
|
141
154
|
context.report({
|
|
142
155
|
node: inner,
|
|
143
156
|
messageId: 'unnecessaryArrayFrom',
|
|
@@ -309,6 +309,14 @@ describe('no-unnecessary-array-from through type wrappers', () => {
|
|
|
309
309
|
const y = Array.from(xs as Iterable<number>).map((x) => x * 2);
|
|
310
310
|
`,
|
|
311
311
|
},
|
|
312
|
+
{
|
|
313
|
+
name: 'a `satisfies` the original array does not meet keeps the copy',
|
|
314
|
+
code: dedent`
|
|
315
|
+
declare const xs: readonly number[];
|
|
316
|
+
const y = (Array.from(xs) satisfies number[]).map((x) => x * 2);
|
|
317
|
+
const z = (Array.from(xs)! satisfies number[]).toSorted();
|
|
318
|
+
`,
|
|
319
|
+
},
|
|
312
320
|
],
|
|
313
321
|
invalid: [
|
|
314
322
|
{
|
|
@@ -13,6 +13,7 @@ import { Arr } from 'ts-data-forge';
|
|
|
13
13
|
import type { DeepReadonly } from 'ts-type-forge';
|
|
14
14
|
import {
|
|
15
15
|
createIsArrayOrTupleType,
|
|
16
|
+
createIsCopyNeededBySatisfies,
|
|
16
17
|
matchArrayFromCall,
|
|
17
18
|
toMemberObjectText,
|
|
18
19
|
} from './array-type-utils.mjs';
|
|
@@ -38,7 +39,10 @@ type MessageIds = 'preferNonMutatingMethod';
|
|
|
38
39
|
*
|
|
39
40
|
* The `Array.from()` call is found through type wrappers, which the fix keeps
|
|
40
41
|
* (`(Array.from(xs) as T[]).sort()` → `(xs as T[]).toSorted()`), and the
|
|
41
|
-
* argument's type is that of its value, not of a cast around it.
|
|
42
|
+
* argument's type is that of its value, not of a cast around it. A
|
|
43
|
+
* `satisfies` around the call that the argument itself does not meet keeps
|
|
44
|
+
* the copy: `(Array.from(xs) satisfies number[])` with `xs: readonly number[]`
|
|
45
|
+
* would not type-check as `xs satisfies number[]`.
|
|
42
46
|
*/
|
|
43
47
|
const MUTATING_METHOD_NAMES: ReadonlySet<string> = new Set([
|
|
44
48
|
'fill',
|
|
@@ -110,6 +114,11 @@ export const preferNonMutatingArrayMethod: TSESLint.RuleModule<
|
|
|
110
114
|
|
|
111
115
|
const isArrayOrTupleType = createIsArrayOrTupleType(checker);
|
|
112
116
|
|
|
117
|
+
const isCopyNeededBySatisfies = createIsCopyNeededBySatisfies(
|
|
118
|
+
checker,
|
|
119
|
+
parserServices.esTreeNodeToTSNodeMap,
|
|
120
|
+
);
|
|
121
|
+
|
|
113
122
|
const getLayerTypes = createGetLayerTypes(parserServices);
|
|
114
123
|
|
|
115
124
|
return {
|
|
@@ -200,6 +209,10 @@ export const preferNonMutatingArrayMethod: TSESLint.RuleModule<
|
|
|
200
209
|
return;
|
|
201
210
|
}
|
|
202
211
|
|
|
212
|
+
if (isCopyNeededBySatisfies(callee.object, inner, arg)) {
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
|
|
203
216
|
const sourceCode = context.sourceCode;
|
|
204
217
|
|
|
205
218
|
// `getText` leaves out the parentheses around a comma expression,
|
|
@@ -248,6 +248,13 @@ describe('prefer-non-mutating-array-method through type wrappers', () => {
|
|
|
248
248
|
const y = Array.from(xs as Iterable<number>).sort();
|
|
249
249
|
`,
|
|
250
250
|
},
|
|
251
|
+
{
|
|
252
|
+
name: 'a `satisfies` the original array does not meet keeps the copy',
|
|
253
|
+
code: dedent`
|
|
254
|
+
declare const xs: readonly number[];
|
|
255
|
+
const y = (Array.from(xs) satisfies number[]).sort();
|
|
256
|
+
`,
|
|
257
|
+
},
|
|
251
258
|
],
|
|
252
259
|
invalid: [
|
|
253
260
|
{
|
|
@@ -279,6 +286,18 @@ describe('prefer-non-mutating-array-method through type wrappers', () => {
|
|
|
279
286
|
{ messageId: 'preferNonMutatingMethod' },
|
|
280
287
|
],
|
|
281
288
|
},
|
|
289
|
+
{
|
|
290
|
+
name: 'a `satisfies` the original array meets too',
|
|
291
|
+
code: dedent`
|
|
292
|
+
declare const xs: number[];
|
|
293
|
+
const y = (Array.from(xs) satisfies readonly number[]).sort();
|
|
294
|
+
`,
|
|
295
|
+
output: dedent`
|
|
296
|
+
declare const xs: number[];
|
|
297
|
+
const y = (xs satisfies readonly number[]).toSorted();
|
|
298
|
+
`,
|
|
299
|
+
errors: [{ messageId: 'preferNonMutatingMethod' }],
|
|
300
|
+
},
|
|
282
301
|
{
|
|
283
302
|
name: 'a fill value wrapped in `satisfies`',
|
|
284
303
|
code: dedent`
|
|
@@ -602,6 +602,22 @@ describe('prefer-assert-is-true-over-expect-true through type wrappers', () => {
|
|
|
602
602
|
expect(flag).toBe(false as boolean);
|
|
603
603
|
`,
|
|
604
604
|
},
|
|
605
|
+
{
|
|
606
|
+
name: 'a narrowing assertion does not make the value a boolean',
|
|
607
|
+
code: dedent`
|
|
608
|
+
declare const v: unknown;
|
|
609
|
+
declare const maybe: boolean | undefined;
|
|
610
|
+
expect(v as boolean).toBe(true);
|
|
611
|
+
expect(maybe!).toBe(true);
|
|
612
|
+
`,
|
|
613
|
+
},
|
|
614
|
+
{
|
|
615
|
+
name: 'a widening cast is the author saying it may not be a boolean',
|
|
616
|
+
code: dedent`
|
|
617
|
+
declare const flag: boolean;
|
|
618
|
+
expect(flag as boolean | undefined).toBe(true);
|
|
619
|
+
`,
|
|
620
|
+
},
|
|
605
621
|
],
|
|
606
622
|
invalid: [
|
|
607
623
|
{
|
|
@@ -650,6 +666,18 @@ describe('prefer-assert-is-true-over-expect-true through type wrappers', () => {
|
|
|
650
666
|
`,
|
|
651
667
|
errors: [{ messageId: 'preferAssertIsTrueOverExpectTrue' }],
|
|
652
668
|
},
|
|
669
|
+
{
|
|
670
|
+
name: 'an argument that is a boolean at every layer',
|
|
671
|
+
code: dedent`
|
|
672
|
+
declare const flag: boolean;
|
|
673
|
+
expect(flag as boolean).toBe(true);
|
|
674
|
+
`,
|
|
675
|
+
output: dedent`
|
|
676
|
+
declare const flag: boolean;
|
|
677
|
+
assert.isTrue(flag as boolean);
|
|
678
|
+
`,
|
|
679
|
+
errors: [{ messageId: 'preferAssertIsTrueOverExpectTrue' }],
|
|
680
|
+
},
|
|
653
681
|
],
|
|
654
682
|
},
|
|
655
683
|
);
|
|
@@ -694,6 +722,15 @@ describe('prefer-assert-is-false-over-expect-false through type wrappers', () =>
|
|
|
694
722
|
expect(flag).toBe(true as boolean);
|
|
695
723
|
`,
|
|
696
724
|
},
|
|
725
|
+
{
|
|
726
|
+
name: 'a narrowing assertion does not make the value a boolean',
|
|
727
|
+
code: dedent`
|
|
728
|
+
declare const v: unknown;
|
|
729
|
+
declare const maybe: boolean | undefined;
|
|
730
|
+
expect(v as boolean).toBe(false);
|
|
731
|
+
expect(maybe!).toBe(false);
|
|
732
|
+
`,
|
|
733
|
+
},
|
|
697
734
|
],
|
|
698
735
|
invalid: [
|
|
699
736
|
{
|
|
@@ -3,7 +3,11 @@ import {
|
|
|
3
3
|
ESLintUtils,
|
|
4
4
|
type TSESLint,
|
|
5
5
|
} from '@typescript-eslint/utils';
|
|
6
|
-
import {
|
|
6
|
+
import {
|
|
7
|
+
getArgumentText,
|
|
8
|
+
skipTypeWrappers,
|
|
9
|
+
typeWrapperLayers,
|
|
10
|
+
} from 'eslint-rule-forge';
|
|
7
11
|
import { Arr } from 'ts-data-forge';
|
|
8
12
|
import * as ts from 'typescript';
|
|
9
13
|
import { getVitestReceiver } from './vitest-binding.mjs';
|
|
@@ -15,8 +19,10 @@ type Options = readonly [];
|
|
|
15
19
|
/**
|
|
16
20
|
* `expect(X).toBe(false)` becomes `assert.isFalse(X)` when `X` is a boolean. The
|
|
17
21
|
* calls, the `expect` function and the `false` are read through `as`,
|
|
18
|
-
* `satisfies`, `!` and `<T>`; `X` keeps its wrappers, and
|
|
19
|
-
*
|
|
22
|
+
* `satisfies`, `!` and `<T>`; `X` keeps its wrappers, and is a boolean only
|
|
23
|
+
* if the type at every layer of them is one: a widening `as` is the author
|
|
24
|
+
* saying it may not be, and a narrowing one (`v as boolean` on an `unknown`,
|
|
25
|
+
* `flag!`) does not make the value a boolean.
|
|
20
26
|
*/
|
|
21
27
|
export const preferAssertIsFalseOverExpectFalseRule: TSESLint.RuleModule<
|
|
22
28
|
MessageIds,
|
|
@@ -71,16 +77,17 @@ export const preferAssertIsFalseOverExpectFalseRule: TSESLint.RuleModule<
|
|
|
71
77
|
) {
|
|
72
78
|
const arg = expectCall.arguments[0];
|
|
73
79
|
|
|
74
|
-
if (
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
80
|
+
if (
|
|
81
|
+
arg !== undefined &&
|
|
82
|
+
!typeWrapperLayers(arg).every((layer) => {
|
|
83
|
+
const type = checker.getTypeAtLocation(
|
|
84
|
+
parserServices.esTreeNodeToTSNodeMap.get(layer),
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
return (type.flags & ts.TypeFlags.Boolean) !== 0;
|
|
88
|
+
})
|
|
89
|
+
) {
|
|
90
|
+
return;
|
|
84
91
|
}
|
|
85
92
|
|
|
86
93
|
const argText =
|
|
@@ -3,7 +3,11 @@ import {
|
|
|
3
3
|
ESLintUtils,
|
|
4
4
|
type TSESLint,
|
|
5
5
|
} from '@typescript-eslint/utils';
|
|
6
|
-
import {
|
|
6
|
+
import {
|
|
7
|
+
getArgumentText,
|
|
8
|
+
skipTypeWrappers,
|
|
9
|
+
typeWrapperLayers,
|
|
10
|
+
} from 'eslint-rule-forge';
|
|
7
11
|
import { Arr } from 'ts-data-forge';
|
|
8
12
|
import * as ts from 'typescript';
|
|
9
13
|
import { getVitestReceiver } from './vitest-binding.mjs';
|
|
@@ -15,8 +19,10 @@ type Options = readonly [];
|
|
|
15
19
|
/**
|
|
16
20
|
* `expect(X).toBe(true)` becomes `assert.isTrue(X)` when `X` is a boolean. The
|
|
17
21
|
* calls, the `expect` function and the `true` are read through `as`,
|
|
18
|
-
* `satisfies`, `!` and `<T>`; `X` keeps its wrappers, and
|
|
19
|
-
*
|
|
22
|
+
* `satisfies`, `!` and `<T>`; `X` keeps its wrappers, and is a boolean only
|
|
23
|
+
* if the type at every layer of them is one: a widening `as` is the author
|
|
24
|
+
* saying it may not be, and a narrowing one (`v as boolean` on an `unknown`,
|
|
25
|
+
* `flag!`) does not make the value a boolean.
|
|
20
26
|
*/
|
|
21
27
|
export const preferAssertIsTrueOverExpectTrueRule: TSESLint.RuleModule<
|
|
22
28
|
MessageIds,
|
|
@@ -71,16 +77,17 @@ export const preferAssertIsTrueOverExpectTrueRule: TSESLint.RuleModule<
|
|
|
71
77
|
) {
|
|
72
78
|
const arg = expectCall.arguments[0];
|
|
73
79
|
|
|
74
|
-
if (
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
80
|
+
if (
|
|
81
|
+
arg !== undefined &&
|
|
82
|
+
!typeWrapperLayers(arg).every((layer) => {
|
|
83
|
+
const type = checker.getTypeAtLocation(
|
|
84
|
+
parserServices.esTreeNodeToTSNodeMap.get(layer),
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
return (type.flags & ts.TypeFlags.Boolean) !== 0;
|
|
88
|
+
})
|
|
89
|
+
) {
|
|
90
|
+
return;
|
|
84
91
|
}
|
|
85
92
|
|
|
86
93
|
const argText =
|