eslint-config-typed 5.0.0 → 5.1.0
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/ts-data-forge/rules/import-utils.d.mts +24 -0
- package/dist/plugins/ts-data-forge/rules/import-utils.d.mts.map +1 -1
- package/dist/plugins/ts-data-forge/rules/import-utils.mjs +59 -1
- package/dist/plugins/ts-data-forge/rules/import-utils.mjs.map +1 -1
- package/dist/plugins/ts-data-forge/rules/no-unnecessary-type-guard.d.mts +10 -0
- package/dist/plugins/ts-data-forge/rules/no-unnecessary-type-guard.d.mts.map +1 -0
- package/dist/plugins/ts-data-forge/rules/no-unnecessary-type-guard.mjs +335 -0
- package/dist/plugins/ts-data-forge/rules/no-unnecessary-type-guard.mjs.map +1 -0
- package/dist/plugins/ts-data-forge/rules/prefer-comparison-over-nullish-guard.d.mts +6 -0
- package/dist/plugins/ts-data-forge/rules/prefer-comparison-over-nullish-guard.d.mts.map +1 -0
- package/dist/plugins/ts-data-forge/rules/prefer-comparison-over-nullish-guard.mjs +101 -0
- package/dist/plugins/ts-data-forge/rules/prefer-comparison-over-nullish-guard.mjs.map +1 -0
- package/dist/plugins/ts-data-forge/rules/rules.d.mts +4 -0
- package/dist/plugins/ts-data-forge/rules/rules.d.mts.map +1 -1
- package/dist/plugins/ts-data-forge/rules/rules.mjs +4 -0
- package/dist/plugins/ts-data-forge/rules/rules.mjs.map +1 -1
- package/dist/rules/eslint-ts-data-forge-rules.d.mts +2 -0
- package/dist/rules/eslint-ts-data-forge-rules.d.mts.map +1 -1
- package/dist/rules/eslint-ts-data-forge-rules.mjs +4 -0
- package/dist/rules/eslint-ts-data-forge-rules.mjs.map +1 -1
- package/dist/types/rules/eslint-ts-data-forge-rules.d.mts +61 -0
- package/dist/types/rules/eslint-ts-data-forge-rules.d.mts.map +1 -1
- package/package.json +7 -1
- package/src/plugins/ts-data-forge/rules/import-utils.mts +94 -0
- package/src/plugins/ts-data-forge/rules/no-unnecessary-type-guard.mts +497 -0
- package/src/plugins/ts-data-forge/rules/no-unnecessary-type-guard.test.mts +377 -0
- package/src/plugins/ts-data-forge/rules/prefer-comparison-over-nullish-guard.mts +141 -0
- package/src/plugins/ts-data-forge/rules/prefer-comparison-over-nullish-guard.test.mts +171 -0
- package/src/plugins/ts-data-forge/rules/rules.mts +4 -0
- package/src/rules/eslint-ts-data-forge-rules.mts +6 -1
- package/src/types/rules/eslint-ts-data-forge-rules.mts +73 -0
|
@@ -2,5 +2,29 @@ import { type TSESLint, type TSESTree } from '@typescript-eslint/utils';
|
|
|
2
2
|
import { type DeepReadonly } from 'ts-type-forge';
|
|
3
3
|
export declare const getTsDataForgeImport: (program: TSESTree.Program) => TSESTree.ImportDeclaration | undefined;
|
|
4
4
|
export declare const getNamedImports: (node: DeepReadonly<TSESTree.ImportDeclaration> | undefined) => readonly string[];
|
|
5
|
+
/**
|
|
6
|
+
* Returns the in-scope local name a given canonical export is imported as
|
|
7
|
+
* (handling aliases such as `import { isX as y }` → `'y'`), or `undefined` when
|
|
8
|
+
* it is not imported. Used so autofixes reference the binding that actually
|
|
9
|
+
* exists rather than the canonical name.
|
|
10
|
+
*/
|
|
11
|
+
export declare const getImportedLocalName: (node: DeepReadonly<TSESTree.ImportDeclaration> | undefined, importedName: string) => string | undefined;
|
|
12
|
+
type ResolvedCallee = Readonly<{
|
|
13
|
+
/** The canonical (imported) ts-data-forge name, regardless of local alias. */
|
|
14
|
+
canonicalName: string;
|
|
15
|
+
/** The identifier node to rewrite when replacing the callee name. */
|
|
16
|
+
propertyNode: TSESTree.Node;
|
|
17
|
+
/** Whether the call was made through a namespace import (`tf.isX(...)`). */
|
|
18
|
+
isNamespace: boolean;
|
|
19
|
+
}>;
|
|
20
|
+
/**
|
|
21
|
+
* Builds a resolver that maps a call expression's callee to the canonical
|
|
22
|
+
* ts-data-forge function it refers to. Handles named imports (including aliases
|
|
23
|
+
* such as `import { isX as y }`) and namespace imports (`import * as tf` →
|
|
24
|
+
* `tf.isX(...)`). Returns `undefined` for any callee that does not reference the
|
|
25
|
+
* given ts-data-forge import.
|
|
26
|
+
*/
|
|
27
|
+
export declare const buildCalleeResolver: (importDecl: TSESTree.ImportDeclaration | undefined) => ((callee: DeepReadonly<TSESTree.Expression>) => ResolvedCallee | undefined);
|
|
5
28
|
export declare const buildImportFixes: (fixer: TSESLint.RuleFixer, program: TSESTree.Program, _tsDataForgeImport: TSESTree.ImportDeclaration | undefined, requiredNames: readonly string[]) => readonly TSESLint.RuleFix[];
|
|
29
|
+
export {};
|
|
6
30
|
//# sourceMappingURL=import-utils.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"import-utils.d.mts","sourceRoot":"","sources":["../../../../src/plugins/ts-data-forge/rules/import-utils.mts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,QAAQ,EACb,KAAK,QAAQ,EACd,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,KAAK,YAAY,EAAE,MAAM,eAAe,CAAC;AAKlD,eAAO,MAAM,oBAAoB,GAC/B,SAAS,QAAQ,CAAC,OAAO,KACxB,QAAQ,CAAC,iBAAiB,GAAG,SAK7B,CAAC;AAEJ,eAAO,MAAM,eAAe,GAC1B,MAAM,YAAY,CAAC,QAAQ,CAAC,iBAAiB,CAAC,GAAG,SAAS,KACzD,SAAS,MAAM,EAejB,CAAC;AAEF,eAAO,MAAM,gBAAgB,GAC3B,OAAO,QAAQ,CAAC,SAAS,EACzB,SAAS,QAAQ,CAAC,OAAO,EACzB,oBAAoB,QAAQ,CAAC,iBAAiB,GAAG,SAAS,EAC1D,eAAe,SAAS,MAAM,EAAE,KAC/B,SAAS,QAAQ,CAAC,OAAO,EAa3B,CAAC"}
|
|
1
|
+
{"version":3,"file":"import-utils.d.mts","sourceRoot":"","sources":["../../../../src/plugins/ts-data-forge/rules/import-utils.mts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,QAAQ,EACb,KAAK,QAAQ,E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21
|
+
* (handling aliases such as `import { isX as y }` → `'y'`), or `undefined` when
|
|
22
|
+
* it is not imported. Used so autofixes reference the binding that actually
|
|
23
|
+
* exists rather than the canonical name.
|
|
24
|
+
*/
|
|
25
|
+
const getImportedLocalName = (node, importedName) => node?.specifiers.find((specifier) => specifier.type === AST_NODE_TYPES.ImportSpecifier &&
|
|
26
|
+
(specifier.imported.type === AST_NODE_TYPES.Identifier
|
|
27
|
+
? specifier.imported.name
|
|
28
|
+
: specifier.imported.value) === importedName)?.local.name;
|
|
29
|
+
/**
|
|
30
|
+
* Builds a resolver that maps a call expression's callee to the canonical
|
|
31
|
+
* ts-data-forge function it refers to. Handles named imports (including aliases
|
|
32
|
+
* such as `import { isX as y }`) and namespace imports (`import * as tf` →
|
|
33
|
+
* `tf.isX(...)`). Returns `undefined` for any callee that does not reference the
|
|
34
|
+
* given ts-data-forge import.
|
|
35
|
+
*/
|
|
36
|
+
const buildCalleeResolver = (importDecl) => {
|
|
37
|
+
const specifiers = importDecl?.specifiers ?? [];
|
|
38
|
+
const namespaceName = specifiers.find((specifier) => specifier.type === AST_NODE_TYPES.ImportNamespaceSpecifier)?.local.name;
|
|
39
|
+
const localToCanonical = new Map(specifiers.flatMap((specifier) => {
|
|
40
|
+
if (specifier.type !== AST_NODE_TYPES.ImportSpecifier)
|
|
41
|
+
return [];
|
|
42
|
+
const importedName = specifier.imported.type === AST_NODE_TYPES.Identifier
|
|
43
|
+
? specifier.imported.name
|
|
44
|
+
: specifier.imported.value;
|
|
45
|
+
return typeof importedName === 'string'
|
|
46
|
+
? [[specifier.local.name, importedName]]
|
|
47
|
+
: [];
|
|
48
|
+
}));
|
|
49
|
+
return (callee) => {
|
|
50
|
+
if (callee.type === AST_NODE_TYPES.Identifier) {
|
|
51
|
+
const canonicalName = localToCanonical.get(callee.name);
|
|
52
|
+
return canonicalName === undefined
|
|
53
|
+
? undefined
|
|
54
|
+
: {
|
|
55
|
+
canonicalName,
|
|
56
|
+
// eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
57
|
+
propertyNode: callee,
|
|
58
|
+
isNamespace: false,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
if (callee.type === AST_NODE_TYPES.MemberExpression &&
|
|
62
|
+
!callee.computed &&
|
|
63
|
+
callee.object.type === AST_NODE_TYPES.Identifier &&
|
|
64
|
+
namespaceName !== undefined &&
|
|
65
|
+
callee.object.name === namespaceName &&
|
|
66
|
+
callee.property.type === AST_NODE_TYPES.Identifier) {
|
|
67
|
+
return {
|
|
68
|
+
canonicalName: callee.property.name,
|
|
69
|
+
// eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
70
|
+
propertyNode: callee.property,
|
|
71
|
+
isNamespace: true,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
return undefined;
|
|
75
|
+
};
|
|
76
|
+
};
|
|
19
77
|
const buildImportFixes = (fixer, program, _tsDataForgeImport, requiredNames) => {
|
|
20
78
|
const specifierText = requiredNames.join(', ');
|
|
21
79
|
const importStatement = `import { ${specifierText} } from '${TS_DATA_FORGE_MODULE}';`;
|
|
@@ -27,5 +85,5 @@ const buildImportFixes = (fixer, program, _tsDataForgeImport, requiredNames) =>
|
|
|
27
85
|
};
|
|
28
86
|
/* eslint-enable @typescript-eslint/prefer-readonly-parameter-types */
|
|
29
87
|
|
|
30
|
-
export { buildImportFixes, getNamedImports, getTsDataForgeImport };
|
|
88
|
+
export { buildCalleeResolver, buildImportFixes, getImportedLocalName, getNamedImports, getTsDataForgeImport };
|
|
31
89
|
//# sourceMappingURL=import-utils.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"import-utils.mjs","sources":["../../../../src/plugins/ts-data-forge/rules/import-utils.mts"],"sourcesContent":[null],"names":[],"mappings":";;;AAQA;AAEO,MAAM,oBAAoB,GAAG,CAClC,OAAyB,KAEzB,OAAO,CAAC,IAAI,CAAC,IAAI,CACf,CAAC,IAAI,KACH,IAAI,CAAC,IAAI,KAA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EA,QAAA,OAAO,SAAS;AAClB,IAAA,CAAC;AACH;AAEO,MAAM,gBAAgB,GAAG,CAC9B,KAAyB,EACzB,OAAyB,EACzB,kBAA0D,EAC1D,aAAgC,KACD;IAC/B,MAAM,aAAa,GAAG,aAAa,CAAC,IAAI,CAAC,IAAI,CAAC;AAE9C,IAAA,MAAM,eAAe,GACnB,CAAA,SAAA,EAAY,aAAa,CAAA,SAAA,EAAY,oBAAoB,IAAa;IAExE,MAAM,OAAO,GAAG,IAAI;AAEpB,IAAA,MAAM,aAAa,GAAG,CAAA,EAAG,eAAe,CAAA,EAAG,OAAO,EAAW;;;IAI7D,OAAO,CAAC,KAAK,CAAC,gBAAgB,CAAC,OAAO,EAAE,aAAa,CAAC,CAAC;AACzD;AACA;;;;"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type TSESLint } from '@typescript-eslint/utils';
|
|
2
|
+
type Options = readonly [
|
|
3
|
+
Readonly<{
|
|
4
|
+
ignore?: readonly string[];
|
|
5
|
+
}>?
|
|
6
|
+
];
|
|
7
|
+
type MessageIds = 'alwaysTrue' | 'alwaysFalse' | 'replaceTypeGuard';
|
|
8
|
+
export declare const noUnnecessaryTypeGuard: TSESLint.RuleModule<MessageIds, Options>;
|
|
9
|
+
export {};
|
|
10
|
+
//# sourceMappingURL=no-unnecessary-type-guard.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"no-unnecessary-type-guard.d.mts","sourceRoot":"","sources":["../../../../src/plugins/ts-data-forge/rules/no-unnecessary-type-guard.mts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,QAAQ,EAEd,MAAM,0BAA0B,CAAC;AAUlC,KAAK,OAAO,GAAG,SAAS;IACtB,QAAQ,CAAC;QACP,MAAM,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;KAC5B,CAAC,CAAC;CACJ,CAAC;AAEF,KAAK,UAAU,GAAG,YAAY,GAAG,aAAa,GAAG,kBAAkB,CAAC;AAyFpE,eAAO,MAAM,sBAAsB,EAAE,QAAQ,CAAC,UAAU,CAAC,UAAU,EAAE,OAAO,CAgRhE,CAAC"}
|
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
import { ESLintUtils, AST_NODE_TYPES } from '@typescript-eslint/utils';
|
|
2
|
+
import * as ts from 'typescript';
|
|
3
|
+
import { getTsDataForgeImport, buildCalleeResolver, getImportedLocalName, buildImportFixes } from './import-utils.mjs';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Internal marker property attached to every `ts-type-forge` branded type. Its
|
|
7
|
+
* presence (together with the brand key) lets us recognize a `NonEmptyString`
|
|
8
|
+
* without holding a reference to the type itself.
|
|
9
|
+
*/
|
|
10
|
+
const BRAND_MARKER_PROPERTY = 'TSTypeForgeInternals--edd2f9ce-7ca5-45b0-9d1a-bd61b9b5d9c3';
|
|
11
|
+
const NON_EMPTY_STRING_BRAND_KEY = 'NonEmptyString';
|
|
12
|
+
const NULLISH_REPLACEMENTS_EXCLUDE = {
|
|
13
|
+
undefined: 'isNotUndefined',
|
|
14
|
+
null: 'isNotNull',
|
|
15
|
+
};
|
|
16
|
+
const NULLISH_REPLACEMENTS_NARROW = {
|
|
17
|
+
undefined: 'isUndefined',
|
|
18
|
+
null: 'isNull',
|
|
19
|
+
};
|
|
20
|
+
const GUARD_SPECS = {
|
|
21
|
+
isUndefined: { kind: 'narrowTo', atoms: ['undefined'] },
|
|
22
|
+
isNotUndefined: { kind: 'excludeFrom', atoms: ['undefined'] },
|
|
23
|
+
isNull: { kind: 'narrowTo', atoms: ['null'] },
|
|
24
|
+
isNotNull: { kind: 'excludeFrom', atoms: ['null'] },
|
|
25
|
+
isBoolean: { kind: 'narrowTo', atoms: ['boolean'] },
|
|
26
|
+
isNotBoolean: { kind: 'excludeFrom', atoms: ['boolean'] },
|
|
27
|
+
isNumber: { kind: 'narrowTo', atoms: ['number'] },
|
|
28
|
+
isNotNumber: { kind: 'excludeFrom', atoms: ['number'] },
|
|
29
|
+
isString: { kind: 'narrowTo', atoms: ['string'] },
|
|
30
|
+
isNotString: { kind: 'excludeFrom', atoms: ['string'] },
|
|
31
|
+
isBigint: { kind: 'narrowTo', atoms: ['bigint'] },
|
|
32
|
+
isNotBigint: { kind: 'excludeFrom', atoms: ['bigint'] },
|
|
33
|
+
isSymbol: { kind: 'narrowTo', atoms: ['symbol'] },
|
|
34
|
+
isNotSymbol: { kind: 'excludeFrom', atoms: ['symbol'] },
|
|
35
|
+
isNullish: {
|
|
36
|
+
kind: 'narrowTo',
|
|
37
|
+
atoms: ['null', 'undefined'],
|
|
38
|
+
replacements: NULLISH_REPLACEMENTS_NARROW,
|
|
39
|
+
},
|
|
40
|
+
isNonNullish: {
|
|
41
|
+
kind: 'excludeFrom',
|
|
42
|
+
atoms: ['null', 'undefined'],
|
|
43
|
+
replacements: NULLISH_REPLACEMENTS_EXCLUDE,
|
|
44
|
+
},
|
|
45
|
+
isNonEmptyString: { kind: 'nonEmptyString' },
|
|
46
|
+
};
|
|
47
|
+
const DEFERRED_OR_OPAQUE_FLAGS = ts.TypeFlags.Any |
|
|
48
|
+
ts.TypeFlags.Unknown |
|
|
49
|
+
ts.TypeFlags.Never |
|
|
50
|
+
ts.TypeFlags.TypeParameter |
|
|
51
|
+
ts.TypeFlags.IndexedAccess |
|
|
52
|
+
ts.TypeFlags.Conditional |
|
|
53
|
+
ts.TypeFlags.Substitution;
|
|
54
|
+
const noUnnecessaryTypeGuard = {
|
|
55
|
+
meta: {
|
|
56
|
+
type: 'suggestion',
|
|
57
|
+
docs: {
|
|
58
|
+
description: 'Detect ts-data-forge type guard calls that perform no narrowing (the argument type already satisfies, or can never satisfy, the guard).',
|
|
59
|
+
},
|
|
60
|
+
fixable: 'code',
|
|
61
|
+
schema: [
|
|
62
|
+
{
|
|
63
|
+
type: 'object',
|
|
64
|
+
properties: {
|
|
65
|
+
ignore: {
|
|
66
|
+
type: 'array',
|
|
67
|
+
items: { type: 'string' },
|
|
68
|
+
description: 'Names of ts-data-forge guard functions to skip checking.',
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
additionalProperties: false,
|
|
72
|
+
},
|
|
73
|
+
],
|
|
74
|
+
messages: {
|
|
75
|
+
alwaysTrue: 'Unnecessary `{{name}}` call: the argument type guarantees it always returns `true`. Replace the call with `true`.',
|
|
76
|
+
alwaysFalse: 'Unnecessary `{{name}}` call: the argument type guarantees it always returns `false`. Replace the call with `false`.',
|
|
77
|
+
replaceTypeGuard: 'Unnecessary `{{from}}` call: the argument can only be narrowed by `{{to}}`. Use `{{to}}` instead.',
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
create: (context) => {
|
|
81
|
+
const options = context.options[0];
|
|
82
|
+
const parserServices = ESLintUtils.getParserServices(context);
|
|
83
|
+
const compilerOptions = parserServices.program.getCompilerOptions();
|
|
84
|
+
// Without `strictNullChecks`, `null` / `undefined` are erased from the type
|
|
85
|
+
// system, so reasoning about nullish narrowing is unsound. Disable entirely.
|
|
86
|
+
const strictNullChecks = compilerOptions.strictNullChecks ?? compilerOptions.strict ?? false;
|
|
87
|
+
if (!strictNullChecks)
|
|
88
|
+
return {};
|
|
89
|
+
const checker = parserServices.program.getTypeChecker();
|
|
90
|
+
const sourceCode = context.sourceCode;
|
|
91
|
+
const program = sourceCode.ast;
|
|
92
|
+
const ignored = new Set(options?.ignore);
|
|
93
|
+
const tsDataForgeImport = getTsDataForgeImport(program);
|
|
94
|
+
const resolveGuard = buildCalleeResolver(tsDataForgeImport);
|
|
95
|
+
const buildReplaceFix = (isNamespace,
|
|
96
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
97
|
+
propertyNode, replacement) => (fixer) => {
|
|
98
|
+
// A namespace member access (`tf.isX`) exposes every export, so the
|
|
99
|
+
// canonical name is always in scope.
|
|
100
|
+
if (isNamespace) {
|
|
101
|
+
return [fixer.replaceText(propertyNode, replacement)];
|
|
102
|
+
}
|
|
103
|
+
// If the replacement guard is already imported (possibly under an
|
|
104
|
+
// alias), rename to its in-scope local name. Otherwise add a new
|
|
105
|
+
// import and rename to the canonical name.
|
|
106
|
+
const localName = getImportedLocalName(tsDataForgeImport, replacement);
|
|
107
|
+
if (localName !== undefined) {
|
|
108
|
+
return [fixer.replaceText(propertyNode, localName)];
|
|
109
|
+
}
|
|
110
|
+
return [
|
|
111
|
+
...buildImportFixes(fixer, program, tsDataForgeImport, [
|
|
112
|
+
replacement,
|
|
113
|
+
]),
|
|
114
|
+
fixer.replaceText(propertyNode, replacement),
|
|
115
|
+
];
|
|
116
|
+
};
|
|
117
|
+
const reportConstant = (node, name, value, argument) => {
|
|
118
|
+
// Replacing the call with a literal drops the argument expression, so only
|
|
119
|
+
// autofix when evaluating it cannot have observable side effects.
|
|
120
|
+
const canFix = isSideEffectFreeArg(argument);
|
|
121
|
+
context.report({
|
|
122
|
+
// eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
123
|
+
node: node,
|
|
124
|
+
messageId: value ? 'alwaysTrue' : 'alwaysFalse',
|
|
125
|
+
data: { name },
|
|
126
|
+
fix: canFix
|
|
127
|
+
? (fixer) => fixer.replaceText(
|
|
128
|
+
// eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
129
|
+
node, value ? 'true' : 'false')
|
|
130
|
+
: undefined,
|
|
131
|
+
});
|
|
132
|
+
};
|
|
133
|
+
const reportReplace = (node, from, to, isNamespace,
|
|
134
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
135
|
+
propertyNode) => {
|
|
136
|
+
context.report({
|
|
137
|
+
// eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
138
|
+
node: node,
|
|
139
|
+
messageId: 'replaceTypeGuard',
|
|
140
|
+
data: { from, to },
|
|
141
|
+
fix: buildReplaceFix(isNamespace, propertyNode, to),
|
|
142
|
+
});
|
|
143
|
+
};
|
|
144
|
+
return {
|
|
145
|
+
CallExpression: (node) => {
|
|
146
|
+
if (node.arguments.length !== 1)
|
|
147
|
+
return;
|
|
148
|
+
const argument = node.arguments[0];
|
|
149
|
+
if (argument === undefined ||
|
|
150
|
+
argument.type === AST_NODE_TYPES.SpreadElement) {
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
const resolved = resolveGuard(node.callee);
|
|
154
|
+
if (resolved === undefined)
|
|
155
|
+
return;
|
|
156
|
+
const { canonicalName, propertyNode, isNamespace } = resolved;
|
|
157
|
+
if (ignored.has(canonicalName))
|
|
158
|
+
return;
|
|
159
|
+
const spec = GUARD_SPECS[canonicalName];
|
|
160
|
+
if (spec === undefined)
|
|
161
|
+
return;
|
|
162
|
+
const argTsNode = parserServices.esTreeNodeToTSNodeMap.get(argument);
|
|
163
|
+
const argType = checker.getTypeAtLocation(argTsNode);
|
|
164
|
+
const parts = collectUnionParts(argType);
|
|
165
|
+
if (parts === undefined)
|
|
166
|
+
return; // opaque / generic → bail conservatively
|
|
167
|
+
const inputAtoms = new Set(parts.map(classifyAtom));
|
|
168
|
+
const hasMemberOutsideTarget = (atoms) => {
|
|
169
|
+
const targetSet = new Set(atoms);
|
|
170
|
+
return parts.some((p) => !targetSet.has(classifyAtom(p)));
|
|
171
|
+
};
|
|
172
|
+
switch (spec.kind) {
|
|
173
|
+
case 'excludeFrom': {
|
|
174
|
+
const removable = spec.atoms.filter((a) => inputAtoms.has(a));
|
|
175
|
+
if (removable.length === 0) {
|
|
176
|
+
// Nothing to exclude → the guard always holds.
|
|
177
|
+
reportConstant(node, canonicalName, true, argument);
|
|
178
|
+
}
|
|
179
|
+
else if (!hasMemberOutsideTarget(spec.atoms)) {
|
|
180
|
+
// Every member is excluded → the guard never holds.
|
|
181
|
+
reportConstant(node, canonicalName, false, argument);
|
|
182
|
+
}
|
|
183
|
+
else if (removable.length < spec.atoms.length) {
|
|
184
|
+
const replacement = spec.replacements?.[joinAtoms(removable)];
|
|
185
|
+
if (replacement !== undefined) {
|
|
186
|
+
reportReplace(node, canonicalName, replacement, isNamespace, propertyNode);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
break;
|
|
190
|
+
}
|
|
191
|
+
case 'narrowTo': {
|
|
192
|
+
const present = spec.atoms.filter((a) => inputAtoms.has(a));
|
|
193
|
+
if (present.length === 0) {
|
|
194
|
+
// No member is in the target set → the guard never holds.
|
|
195
|
+
reportConstant(node, canonicalName, false, argument);
|
|
196
|
+
}
|
|
197
|
+
else if (!hasMemberOutsideTarget(spec.atoms)) {
|
|
198
|
+
// Input ⊆ target → the guard always holds and narrows nothing.
|
|
199
|
+
reportConstant(node, canonicalName, true, argument);
|
|
200
|
+
}
|
|
201
|
+
else if (present.length < spec.atoms.length) {
|
|
202
|
+
const replacement = spec.replacements?.[joinAtoms(present)];
|
|
203
|
+
if (replacement !== undefined) {
|
|
204
|
+
reportReplace(node, canonicalName, replacement, isNamespace, propertyNode);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
break;
|
|
208
|
+
}
|
|
209
|
+
case 'nonEmptyString': {
|
|
210
|
+
const hasNullish = inputAtoms.has('undefined') || inputAtoms.has('null');
|
|
211
|
+
const nonNullishParts = parts.filter((p) => {
|
|
212
|
+
const atom = classifyAtom(p);
|
|
213
|
+
return atom !== 'undefined' && atom !== 'null';
|
|
214
|
+
});
|
|
215
|
+
if (!nonNullishParts.some(couldBeNonEmptyString)) {
|
|
216
|
+
// No member can be a non-empty string → the guard never holds.
|
|
217
|
+
reportConstant(node, canonicalName, false, argument);
|
|
218
|
+
break;
|
|
219
|
+
}
|
|
220
|
+
if (!nonNullishParts.every(isGuaranteedNonEmptyString)) {
|
|
221
|
+
break; // still does real string/empty work
|
|
222
|
+
}
|
|
223
|
+
if (hasNullish) {
|
|
224
|
+
reportReplace(node, canonicalName, 'isNonNullish', isNamespace, propertyNode);
|
|
225
|
+
}
|
|
226
|
+
else {
|
|
227
|
+
reportConstant(node, canonicalName, true, argument);
|
|
228
|
+
}
|
|
229
|
+
break;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
},
|
|
233
|
+
};
|
|
234
|
+
},
|
|
235
|
+
defaultOptions: [{}],
|
|
236
|
+
};
|
|
237
|
+
/**
|
|
238
|
+
* Decomposes a type into its union members. Returns `undefined` when any member
|
|
239
|
+
* is opaque (`any` / `unknown` / `never`) or deferred (type parameter, indexed
|
|
240
|
+
* access, …), signalling that the rule should not draw conclusions.
|
|
241
|
+
*/
|
|
242
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
243
|
+
const collectUnionParts = (type) => {
|
|
244
|
+
const parts = type.isUnion() ? type.types : [type];
|
|
245
|
+
for (const part of parts) {
|
|
246
|
+
if ((part.flags & DEFERRED_OR_OPAQUE_FLAGS) !== 0)
|
|
247
|
+
return undefined;
|
|
248
|
+
}
|
|
249
|
+
return parts;
|
|
250
|
+
};
|
|
251
|
+
/** Classifies a single (non-union) type into a primitive {@link Atom}. */
|
|
252
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
253
|
+
const classifyAtom = (type) => {
|
|
254
|
+
const { flags } = type;
|
|
255
|
+
if ((flags & ts.TypeFlags.Undefined) !== 0)
|
|
256
|
+
return 'undefined';
|
|
257
|
+
if ((flags & ts.TypeFlags.Null) !== 0)
|
|
258
|
+
return 'null';
|
|
259
|
+
if ((flags & ts.TypeFlags.BooleanLike) !== 0)
|
|
260
|
+
return 'boolean';
|
|
261
|
+
if ((flags & ts.TypeFlags.NumberLike) !== 0)
|
|
262
|
+
return 'number';
|
|
263
|
+
if ((flags & ts.TypeFlags.StringLike) !== 0)
|
|
264
|
+
return 'string';
|
|
265
|
+
if ((flags & ts.TypeFlags.BigIntLike) !== 0)
|
|
266
|
+
return 'bigint';
|
|
267
|
+
if ((flags & ts.TypeFlags.ESSymbolLike) !== 0)
|
|
268
|
+
return 'symbol';
|
|
269
|
+
// Branded primitives (e.g. `NonEmptyString = string & {...}`) are
|
|
270
|
+
// intersections; classify them by their underlying primitive constituent.
|
|
271
|
+
if (type.isIntersection()) {
|
|
272
|
+
for (const constituent of type.types) {
|
|
273
|
+
const atom = classifyAtom(constituent);
|
|
274
|
+
if (atom !== 'other')
|
|
275
|
+
return atom;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
return 'other';
|
|
279
|
+
};
|
|
280
|
+
const ATOM_ORDER = [
|
|
281
|
+
'undefined',
|
|
282
|
+
'null',
|
|
283
|
+
'boolean',
|
|
284
|
+
'number',
|
|
285
|
+
'string',
|
|
286
|
+
'bigint',
|
|
287
|
+
'symbol',
|
|
288
|
+
];
|
|
289
|
+
/** A stable key for a set of atoms, used to look up replacement guards. */
|
|
290
|
+
const joinAtoms = (atoms) => ATOM_ORDER.filter((atom) => atoms.includes(atom)).join('|');
|
|
291
|
+
/**
|
|
292
|
+
* Returns `true` when the type is guaranteed to be a non-empty string: either a
|
|
293
|
+
* non-empty string literal or a value branded as `NonEmptyString`.
|
|
294
|
+
*/
|
|
295
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
296
|
+
const isGuaranteedNonEmptyString = (type) => {
|
|
297
|
+
if (type.isStringLiteral())
|
|
298
|
+
return type.value !== '';
|
|
299
|
+
return (type.getProperty(NON_EMPTY_STRING_BRAND_KEY) !== undefined &&
|
|
300
|
+
type.getProperty(BRAND_MARKER_PROPERTY) !== undefined);
|
|
301
|
+
};
|
|
302
|
+
/**
|
|
303
|
+
* Returns `true` when the type can possibly be a non-empty string (a general
|
|
304
|
+
* `string`, a branded string, or a non-empty string literal). The empty string
|
|
305
|
+
* literal and non-string types cannot.
|
|
306
|
+
*/
|
|
307
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
308
|
+
const couldBeNonEmptyString = (type) => {
|
|
309
|
+
if (type.isStringLiteral())
|
|
310
|
+
return type.value !== '';
|
|
311
|
+
return classifyAtom(type) === 'string';
|
|
312
|
+
};
|
|
313
|
+
/**
|
|
314
|
+
* Returns `true` when evaluating the expression cannot have observable side
|
|
315
|
+
* effects, so replacing the surrounding call with a literal is safe. Limited to
|
|
316
|
+
* simple references (identifiers, `this`, literals, and member-access chains).
|
|
317
|
+
*/
|
|
318
|
+
const isSideEffectFreeArg = (node) => {
|
|
319
|
+
// `node.type` is one of 130+ AST kinds; only a handful are relevant here.
|
|
320
|
+
// eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check
|
|
321
|
+
switch (node.type) {
|
|
322
|
+
case AST_NODE_TYPES.Identifier:
|
|
323
|
+
case AST_NODE_TYPES.ThisExpression:
|
|
324
|
+
case AST_NODE_TYPES.Literal:
|
|
325
|
+
return true;
|
|
326
|
+
case AST_NODE_TYPES.MemberExpression:
|
|
327
|
+
return (isSideEffectFreeArg(node.object) &&
|
|
328
|
+
(!node.computed || isSideEffectFreeArg(node.property)));
|
|
329
|
+
default:
|
|
330
|
+
return false;
|
|
331
|
+
}
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
export { noUnnecessaryTypeGuard };
|
|
335
|
+
//# sourceMappingURL=no-unnecessary-type-guard.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"no-unnecessary-type-guard.mjs","sources":["../../../../src/plugins/ts-data-forge/rules/no-unnecessary-type-guard.mts"],"sourcesContent":[null],"names":[],"mappings":";;;;AAqDA;;;;AAIG;AACH,MAAM,qBAAqB,GACzB,4DAA4D;AAE9D,MAAM,0BAA0B,GAAG,gBAAgB;AAEnD,MAAM,4BAA4B,GAAqC;AACrE,IAAA,SAAS,EAAE,gBAAgB;AAC3B,IAAA,IAAI,EAAE,WAAW;CACT;AAEV,MAAM,2BAA2B,GAAqC;AACpE,IAAA,SAAS,EAAE,aAAa;AACxB,IAAA,IAAI,EAAE,QAAQ;CACN;AAEV,MAAM,WAAW,GAA4C;IAC3D,WAAW,EAAE,EAAE,IAAI,EAAE,UAAU,EAAE,KAAK,EAAE,CAAC,WAAW,CAAC,EAAE;IACvD,cAAc,EAAE,EAAE,IAAI,EAAE,aAAa,EAAE,KAAK,EA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E;IAEpD,QACE,IAAI,CAAC,WAAW,CAAC,0BAA0B,CAAC,KAAK,SAAS;QAC1D,IAAI,CAAC,WAAW,CAAC,qBAAqB,CAAC,KAAK,SAAS;AAEzD,CAAC;AAED;;;;AAIG;AACH;AACA,MAAM,qBAAqB,GAAG,CAAC,IAAa,KAAa;IACvD,IAAI,IAAI,CAAC,eAAe,EAAE;AAAE,QAAA,OAAO,IAAI,CAAC,KAAK,KAAK,EAAE;AAEpD,IAAA,OAAO,YAAY,CAAC,IAAI,CAAC,KAAK,QAAQ;AACxC,CAAC;AAED;;;;AAIG;AAEH,MAAM,mBAAmB,GAAG,CAAC,IAAiC,KAAa;;;AAGzE,IAAA,QAAQ,IAAI,CAAC,IAAI;QACf,KAAK,cAAc,CAAC,UAAU;QAC9B,KAAK,cAAc,CAAC,cAAc;QAClC,KAAK,cAAc,CAAC,OAAO;AACzB,YAAA,OAAO,IAAI;QAEb,KAAK,cAAc,CAAC,gBAAgB;AAClC,YAAA,QACE,mBAAmB,CAAC,IAAI,CAAC,MAAM,CAAC;AAChC,iBAAC,CAAC,IAAI,CAAC,QAAQ,IAAI,mBAAmB,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;AAG1D,QAAA;AACE,YAAA,OAAO,KAAK;;AAElB,CAAC;;;;"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { type TSESLint } from '@typescript-eslint/utils';
|
|
2
|
+
type Options = readonly [];
|
|
3
|
+
type MessageIds = 'preferComparison';
|
|
4
|
+
export declare const preferComparisonOverNullishGuard: TSESLint.RuleModule<MessageIds, Options>;
|
|
5
|
+
export {};
|
|
6
|
+
//# sourceMappingURL=prefer-comparison-over-nullish-guard.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prefer-comparison-over-nullish-guard.d.mts","sourceRoot":"","sources":["../../../../src/plugins/ts-data-forge/rules/prefer-comparison-over-nullish-guard.mts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,QAAQ,EAEd,MAAM,0BAA0B,CAAC;AAIlC,KAAK,OAAO,GAAG,SAAS,EAAE,CAAC;AAE3B,KAAK,UAAU,GAAG,kBAAkB,CAAC;AA8DrC,eAAO,MAAM,gCAAgC,EAAE,QAAQ,CAAC,UAAU,CAChE,UAAU,EACV,OAAO,CAkEC,CAAC"}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { AST_NODE_TYPES } from '@typescript-eslint/utils';
|
|
2
|
+
import { buildCalleeResolver, getTsDataForgeImport } from './import-utils.mjs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The nullish guards that are meant for point-free use (e.g.
|
|
6
|
+
* `xs.filter(isNotUndefined)`). When called with an explicit argument a direct
|
|
7
|
+
* comparison is clearer, so map each to its equivalent operator and literal.
|
|
8
|
+
*/
|
|
9
|
+
const COMPARISON_GUARDS = {
|
|
10
|
+
isUndefined: { op: '===', literal: 'undefined' },
|
|
11
|
+
isNotUndefined: { op: '!==', literal: 'undefined' },
|
|
12
|
+
isNull: { op: '===', literal: 'null' },
|
|
13
|
+
isNotNull: { op: '!==', literal: 'null' },
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Argument expression kinds whose precedence is lower than equality, so they
|
|
17
|
+
* must be parenthesized when placed on the left of `===` / `!==`.
|
|
18
|
+
*/
|
|
19
|
+
const ARG_NEEDS_PARENS = new Set([
|
|
20
|
+
AST_NODE_TYPES.LogicalExpression,
|
|
21
|
+
AST_NODE_TYPES.ConditionalExpression,
|
|
22
|
+
AST_NODE_TYPES.AssignmentExpression,
|
|
23
|
+
AST_NODE_TYPES.SequenceExpression,
|
|
24
|
+
AST_NODE_TYPES.ArrowFunctionExpression,
|
|
25
|
+
AST_NODE_TYPES.YieldExpression,
|
|
26
|
+
]);
|
|
27
|
+
/**
|
|
28
|
+
* Returns `true` when the comparison must be wrapped in parentheses to preserve
|
|
29
|
+
* the original grouping given the call expression's parent context.
|
|
30
|
+
*/
|
|
31
|
+
const needsWrappingInParent = (node) => {
|
|
32
|
+
const { parent } = node;
|
|
33
|
+
// `parent.type` is one of 130+ AST kinds; only a handful need wrapping.
|
|
34
|
+
// eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check
|
|
35
|
+
switch (parent.type) {
|
|
36
|
+
case AST_NODE_TYPES.UnaryExpression:
|
|
37
|
+
case AST_NODE_TYPES.AwaitExpression:
|
|
38
|
+
case AST_NODE_TYPES.BinaryExpression:
|
|
39
|
+
case AST_NODE_TYPES.TSNonNullExpression:
|
|
40
|
+
return true;
|
|
41
|
+
case AST_NODE_TYPES.MemberExpression:
|
|
42
|
+
return parent.object === node;
|
|
43
|
+
case AST_NODE_TYPES.CallExpression:
|
|
44
|
+
case AST_NODE_TYPES.NewExpression:
|
|
45
|
+
return parent.callee === node;
|
|
46
|
+
default:
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
const preferComparisonOverNullishGuard = {
|
|
51
|
+
meta: {
|
|
52
|
+
type: 'suggestion',
|
|
53
|
+
docs: {
|
|
54
|
+
description: 'Prefer a direct `=== null` / `!== undefined` comparison over calling `isNull`, `isNotNull`, `isUndefined`, or `isNotUndefined` with an explicit argument (those guards are intended for point-free use such as `xs.filter(isNotUndefined)`).',
|
|
55
|
+
},
|
|
56
|
+
fixable: 'code',
|
|
57
|
+
schema: [],
|
|
58
|
+
messages: {
|
|
59
|
+
preferComparison: 'Prefer `{{comparison}}` over calling `{{name}}` with an explicit argument.',
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
create: (context) => {
|
|
63
|
+
const sourceCode = context.sourceCode;
|
|
64
|
+
const resolveGuard = buildCalleeResolver(getTsDataForgeImport(sourceCode.ast));
|
|
65
|
+
return {
|
|
66
|
+
CallExpression: (node) => {
|
|
67
|
+
if (node.arguments.length !== 1)
|
|
68
|
+
return;
|
|
69
|
+
const argument = node.arguments[0];
|
|
70
|
+
if (argument === undefined ||
|
|
71
|
+
argument.type === AST_NODE_TYPES.SpreadElement) {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
const resolved = resolveGuard(node.callee);
|
|
75
|
+
if (resolved === undefined)
|
|
76
|
+
return;
|
|
77
|
+
const spec = COMPARISON_GUARDS[resolved.canonicalName];
|
|
78
|
+
if (spec === undefined)
|
|
79
|
+
return;
|
|
80
|
+
const argRaw = sourceCode.getText(argument);
|
|
81
|
+
const argText = ARG_NEEDS_PARENS.has(argument.type)
|
|
82
|
+
? `(${argRaw})`
|
|
83
|
+
: argRaw;
|
|
84
|
+
const comparison = `${argText} ${spec.op} ${spec.literal}`;
|
|
85
|
+
const replacement = needsWrappingInParent(node)
|
|
86
|
+
? `(${comparison})`
|
|
87
|
+
: comparison;
|
|
88
|
+
context.report({
|
|
89
|
+
node,
|
|
90
|
+
messageId: 'preferComparison',
|
|
91
|
+
data: { comparison, name: resolved.canonicalName },
|
|
92
|
+
fix: (fixer) => fixer.replaceText(node, replacement),
|
|
93
|
+
});
|
|
94
|
+
},
|
|
95
|
+
};
|
|
96
|
+
},
|
|
97
|
+
defaultOptions: [],
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
export { preferComparisonOverNullishGuard };
|
|
101
|
+
//# sourceMappingURL=prefer-comparison-over-nullish-guard.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prefer-comparison-over-nullish-guard.mjs","sources":["../../../../src/plugins/ts-data-forge/rules/prefer-comparison-over-nullish-guard.mts"],"sourcesContent":[null],"names":[],"mappings":";;;AAiBA;;;;AAIG;AACH,MAAM,iBAAiB,GAAiD;IACtE,WAAW,EAAE,EAAE,EAAE,EAAE,KAAK,EAAE,OAAO,EAAE,WAAW,EAAE;IAChD,cAAc,EAAE,EAAE,EAAE,EAAE,KAAK,EAAE,OAAO,EAAE,WAAW,EAAE;IACnD,MAAM,EAAE,EAAE,EAAE,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE;IACtC,SAAS,EAAE,EAAE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,CAAC,IAAI,EAAE,WAAW,CAAC;AACrD,iBAAA,CAAC;YACJ,CAAC;SACF;IACH,CAAC;AACD,IAAA,cAAc,EAAE,EAAE;;;;;"}
|
|
@@ -8,5 +8,9 @@ export declare const tsDataForgeRules: {
|
|
|
8
8
|
readonly 'prefer-is-non-null-object': import("@typescript-eslint/utils/ts-eslint").RuleModule<"useIsNonNullObject", readonly [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener>;
|
|
9
9
|
readonly 'prefer-range-for-loop': import("@typescript-eslint/utils/ts-eslint").RuleModule<"useRangeForLoop", readonly [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener>;
|
|
10
10
|
readonly 'prefer-is-record-and-has-key': import("@typescript-eslint/utils/ts-eslint").RuleModule<"useIsRecordAndHasKey", readonly [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener>;
|
|
11
|
+
readonly 'no-unnecessary-type-guard': import("@typescript-eslint/utils/ts-eslint").RuleModule<"alwaysTrue" | "alwaysFalse" | "replaceTypeGuard", readonly [(Readonly<{
|
|
12
|
+
ignore?: readonly string[];
|
|
13
|
+
}> | undefined)?], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener>;
|
|
14
|
+
readonly 'prefer-comparison-over-nullish-guard': import("@typescript-eslint/utils/ts-eslint").RuleModule<"preferComparison", readonly [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener>;
|
|
11
15
|
};
|
|
12
16
|
//# sourceMappingURL=rules.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rules.d.mts","sourceRoot":"","sources":["../../../../src/plugins/ts-data-forge/rules/rules.mts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"rules.d.mts","sourceRoot":"","sources":["../../../../src/plugins/ts-data-forge/rules/rules.mts"],"names":[],"mappings":"AAaA,eAAO,MAAM,gBAAgB;;;;;;;;;;;;;;CAYa,CAAC"}
|
|
@@ -1,9 +1,11 @@
|
|
|
1
|
+
import { noUnnecessaryTypeGuard } from './no-unnecessary-type-guard.mjs';
|
|
1
2
|
import { preferArrIsArrayAtLeastLength } from './prefer-arr-is-array-at-least-length.mjs';
|
|
2
3
|
import { preferArrIsArrayOfLength } from './prefer-arr-is-array-of-length.mjs';
|
|
3
4
|
import { preferArrIsArray } from './prefer-arr-is-array.mjs';
|
|
4
5
|
import { preferArrIsNonEmpty } from './prefer-arr-is-non-empty.mjs';
|
|
5
6
|
import { preferArrSum } from './prefer-arr-sum.mjs';
|
|
6
7
|
import { preferAsInt } from './prefer-as-int.mjs';
|
|
8
|
+
import { preferComparisonOverNullishGuard } from './prefer-comparison-over-nullish-guard.mjs';
|
|
7
9
|
import { preferIsNonNullObject } from './prefer-is-non-null-object.mjs';
|
|
8
10
|
import { preferIsRecordAndHasKey } from './prefer-is-record-and-has-key.mjs';
|
|
9
11
|
import { preferRangeForLoop } from './prefer-range-for-loop.mjs';
|
|
@@ -18,6 +20,8 @@ const tsDataForgeRules = {
|
|
|
18
20
|
'prefer-is-non-null-object': preferIsNonNullObject,
|
|
19
21
|
'prefer-range-for-loop': preferRangeForLoop,
|
|
20
22
|
'prefer-is-record-and-has-key': preferIsRecordAndHasKey,
|
|
23
|
+
'no-unnecessary-type-guard': noUnnecessaryTypeGuard,
|
|
24
|
+
'prefer-comparison-over-nullish-guard': preferComparisonOverNullishGuard,
|
|
21
25
|
};
|
|
22
26
|
|
|
23
27
|
export { tsDataForgeRules };
|