@kikiutils/eslint-config 11.4.0 → 12.0.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/README.md +41 -23
- package/dist/_internals/configs/base-rules.d.ts.map +1 -1
- package/dist/_internals/configs/base-rules.js +39 -40
- package/dist/_internals/configs/base-rules.js.map +1 -1
- package/dist/_internals/configs/jsdoc.d.ts +0 -39
- package/dist/_internals/configs/jsdoc.d.ts.map +1 -1
- package/dist/_internals/configs/jsdoc.js +1 -45
- package/dist/_internals/configs/jsdoc.js.map +1 -1
- package/dist/_internals/configs/type-aware.d.ts.map +1 -1
- package/dist/_internals/configs/type-aware.js +26 -1
- package/dist/_internals/configs/type-aware.js.map +1 -1
- package/dist/_internals/configs/typescript.d.ts +42 -0
- package/dist/_internals/configs/typescript.d.ts.map +1 -0
- package/dist/_internals/configs/typescript.js +49 -0
- package/dist/_internals/configs/typescript.js.map +1 -0
- package/dist/_internals/eslint-plugin-promise.d.ts +7 -0
- package/dist/_internals/eslint-plugin-promise.d.ts.map +1 -0
- package/dist/_internals/rules/consistent-condition-layout.d.ts.map +1 -1
- package/dist/_internals/rules/consistent-condition-layout.js +15 -14
- package/dist/_internals/rules/consistent-condition-layout.js.map +1 -1
- package/dist/_internals/rules/consistent-parameter-layout.d.ts.map +1 -1
- package/dist/_internals/rules/consistent-parameter-layout.js +17 -11
- package/dist/_internals/rules/consistent-parameter-layout.js.map +1 -1
- package/dist/_internals/rules/index.d.ts +7 -1
- package/dist/_internals/rules/index.d.ts.map +1 -1
- package/dist/_internals/rules/index.js +11 -2
- package/dist/_internals/rules/index.js.map +1 -1
- package/dist/_internals/rules/no-composable-in-head-getter.d.ts.map +1 -1
- package/dist/_internals/rules/no-composable-in-head-getter.js +97 -19
- package/dist/_internals/rules/no-composable-in-head-getter.js.map +1 -1
- package/dist/_internals/rules/no-reactive-composable-return.d.ts +3 -0
- package/dist/_internals/rules/no-reactive-composable-return.d.ts.map +1 -0
- package/dist/_internals/rules/no-reactive-composable-return.js +43 -0
- package/dist/_internals/rules/no-reactive-composable-return.js.map +1 -0
- package/dist/_internals/rules/no-reactive-destructure.d.ts +3 -0
- package/dist/_internals/rules/no-reactive-destructure.d.ts.map +1 -0
- package/dist/_internals/rules/no-reactive-destructure.js +44 -0
- package/dist/_internals/rules/no-reactive-destructure.js.map +1 -0
- package/dist/_internals/rules/no-silent-catch.d.ts.map +1 -1
- package/dist/_internals/rules/no-silent-catch.js +3 -2
- package/dist/_internals/rules/no-silent-catch.js.map +1 -1
- package/dist/_internals/rules/no-thenable-zod-refinement.d.ts.map +1 -1
- package/dist/_internals/rules/no-thenable-zod-refinement.js +9 -3
- package/dist/_internals/rules/no-thenable-zod-refinement.js.map +1 -1
- package/dist/_internals/rules/prefer-enhanced-date.d.ts +3 -0
- package/dist/_internals/rules/prefer-enhanced-date.d.ts.map +1 -0
- package/dist/_internals/rules/prefer-enhanced-date.js +307 -0
- package/dist/_internals/rules/prefer-enhanced-date.js.map +1 -0
- package/dist/_internals/rules/promise-function-async.d.ts.map +1 -1
- package/dist/_internals/rules/promise-function-async.js.map +1 -1
- package/dist/_internals/rules/statement-padding.d.ts.map +1 -1
- package/dist/_internals/rules/statement-padding.js +19 -15
- package/dist/_internals/rules/statement-padding.js.map +1 -1
- package/dist/_internals/rules/utils.d.ts +23 -0
- package/dist/_internals/rules/utils.d.ts.map +1 -0
- package/dist/_internals/rules/utils.js +17 -0
- package/dist/_internals/rules/utils.js.map +1 -0
- package/dist/_internals/rules/vue-reactivity.d.ts +3 -0
- package/dist/_internals/rules/vue-reactivity.d.ts.map +1 -0
- package/dist/_internals/rules/vue-reactivity.js +30 -0
- package/dist/_internals/rules/vue-reactivity.js.map +1 -0
- package/dist/base.d.ts.map +1 -1
- package/dist/base.js +51 -29
- package/dist/base.js.map +1 -1
- package/dist/nuxt.d.ts +3 -1
- package/dist/nuxt.d.ts.map +1 -1
- package/dist/nuxt.js +53 -3
- package/dist/nuxt.js.map +1 -1
- package/dist/style.d.ts.map +1 -1
- package/dist/style.js +1 -0
- package/dist/style.js.map +1 -1
- package/dist/vue.d.ts +1 -1
- package/dist/vue.d.ts.map +1 -1
- package/dist/vue.js +17 -19
- package/dist/vue.js.map +1 -1
- package/package.json +13 -12
- package/src/_internals/configs/base-rules.ts +33 -52
- package/src/_internals/configs/jsdoc.ts +0 -57
- package/src/_internals/configs/type-aware.ts +29 -1
- package/src/_internals/configs/typescript.ts +62 -0
- package/src/_internals/eslint-plugin-promise.d.ts +6 -0
- package/src/_internals/rules/consistent-condition-layout.ts +20 -23
- package/src/_internals/rules/consistent-parameter-layout.ts +30 -34
- package/src/_internals/rules/index.ts +10 -1
- package/src/_internals/rules/no-composable-in-head-getter.ts +132 -26
- package/src/_internals/rules/no-reactive-composable-return.ts +55 -0
- package/src/_internals/rules/no-reactive-destructure.ts +64 -0
- package/src/_internals/rules/no-silent-catch.ts +4 -10
- package/src/_internals/rules/no-thenable-zod-refinement.ts +14 -9
- package/src/_internals/rules/prefer-enhanced-date.ts +448 -0
- package/src/_internals/rules/promise-function-async.ts +2 -6
- package/src/_internals/rules/statement-padding.ts +23 -24
- package/src/_internals/{utils.ts → rules/utils.ts} +31 -0
- package/src/_internals/rules/vue-reactivity.ts +42 -0
- package/src/base.ts +51 -47
- package/src/nuxt.ts +90 -5
- package/src/style.ts +1 -0
- package/src/vue.ts +24 -26
- package/dist/_internals/configs/restricted-syntax.d.ts +0 -5
- package/dist/_internals/configs/restricted-syntax.d.ts.map +0 -1
- package/dist/_internals/configs/restricted-syntax.js +0 -12
- package/dist/_internals/configs/restricted-syntax.js.map +0 -1
- package/dist/_internals/configs/typefest.d.ts +0 -3
- package/dist/_internals/configs/typefest.d.ts.map +0 -1
- package/dist/_internals/configs/typefest.js +0 -21
- package/dist/_internals/configs/typefest.js.map +0 -1
- package/dist/_internals/utils.d.ts +0 -4
- package/dist/_internals/utils.d.ts.map +0 -1
- package/dist/_internals/utils.js +0 -10
- package/dist/_internals/utils.js.map +0 -1
- package/src/_internals/configs/restricted-syntax.ts +0 -8
- package/src/_internals/configs/typefest.ts +0 -19
|
@@ -1,4 +1,11 @@
|
|
|
1
1
|
import type { Rule } from 'eslint';
|
|
2
|
+
import ts from 'typescript';
|
|
3
|
+
|
|
4
|
+
import type { ParserServices } from './utils';
|
|
5
|
+
import { composablePattern } from './utils';
|
|
6
|
+
|
|
7
|
+
type CallNode = Extract<Rule.Node, { type: 'CallExpression' }>;
|
|
8
|
+
type FunctionNode = Extract<Rule.Node, { type: 'ArrowFunctionExpression' | 'FunctionExpression' }>;
|
|
2
9
|
|
|
3
10
|
const headComposables = new Set([
|
|
4
11
|
'useHead',
|
|
@@ -9,43 +16,142 @@ const headComposables = new Set([
|
|
|
9
16
|
'useServerSeoMeta',
|
|
10
17
|
]);
|
|
11
18
|
|
|
12
|
-
|
|
19
|
+
// Functions nested in the body are skipped: calling the outer function does not run them.
|
|
20
|
+
function findCalledComposable(bodies: readonly ts.Node[]): string | undefined {
|
|
21
|
+
let composable: string | undefined;
|
|
22
|
+
const visit = (node: ts.Node): void => {
|
|
23
|
+
if (composable || ts.isFunctionLike(node)) return;
|
|
24
|
+
if (ts.isCallExpression(node) && ts.isIdentifier(node.expression) && composablePattern.test(node.expression.text)) {
|
|
25
|
+
composable = node.expression.text;
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
ts.forEachChild(node, visit);
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
for (const body of bodies) {
|
|
33
|
+
visit(body);
|
|
34
|
+
if (composable) return composable;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function getBody(node: ts.Node | undefined): ts.Node | undefined {
|
|
39
|
+
if (node && ts.isFunctionLike(node) && 'body' in node) return node.body;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Uses the callee's own declaration, so an annotated value is judged by what it holds, not by its type.
|
|
43
|
+
function getImplementationBodies(checker: ts.TypeChecker, call: ts.CallExpression): ts.Node[] {
|
|
44
|
+
const callee = ts.isPropertyAccessExpression(call.expression) ? call.expression.name : call.expression;
|
|
45
|
+
let symbol = checker.getSymbolAtLocation(callee);
|
|
46
|
+
if (symbol && symbol.flags & ts.SymbolFlags.Alias) symbol = checker.getAliasedSymbol(symbol);
|
|
47
|
+
const declarations = symbol?.declarations ?? [];
|
|
48
|
+
const values = declarations.map((declaration) => {
|
|
49
|
+
if (ts.isVariableDeclaration(declaration) || ts.isPropertyAssignment(declaration)) return declaration.initializer;
|
|
50
|
+
return getBody(declaration) && declaration;
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
if (values.some(Boolean)) {
|
|
54
|
+
return values
|
|
55
|
+
.map((value) => getBody(value))
|
|
56
|
+
.filter((body) => body !== undefined);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Nuxt auto-import globals are ambient `typeof import('...')['fn']` constants; only their type leads to the source.
|
|
60
|
+
const declaration = checker.getResolvedSignature(call)?.getDeclaration();
|
|
61
|
+
// An overload signature has no body; the implementation shares its symbol.
|
|
62
|
+
const name = declaration && ts.isFunctionDeclaration(declaration) ? declaration.name : undefined;
|
|
63
|
+
const implementations = name ? checker.getSymbolAtLocation(name)?.declarations ?? [] : [declaration];
|
|
64
|
+
return implementations
|
|
65
|
+
.map((implementation) => getBody(implementation))
|
|
66
|
+
.filter((body) => body !== undefined);
|
|
67
|
+
}
|
|
13
68
|
|
|
14
69
|
export const noComposableInHeadGetter: Rule.RuleModule = {
|
|
15
70
|
create(context) {
|
|
71
|
+
// Type information is optional: without it, only composables called directly in a getter are reported.
|
|
72
|
+
const services = context.sourceCode.parserServices as Partial<ParserServices> | undefined;
|
|
73
|
+
const checker = services?.program?.getTypeChecker();
|
|
74
|
+
const nodeMap = services?.esTreeNodeToTSNodeMap;
|
|
75
|
+
const headCalls: Array<{ head: string; node: CallNode }> = [];
|
|
76
|
+
const getters: Array<{ head: string; node: FunctionNode }> = [];
|
|
77
|
+
|
|
78
|
+
// Functions in a head input run when the head is resolved after setup, except callbacks passed to other calls,
|
|
79
|
+
// such as `.map()`, which run while the input is built; `computed()` getters are also evaluated later.
|
|
80
|
+
function enterFunction(node: FunctionNode): void {
|
|
81
|
+
const headCall = headCalls.at(-1);
|
|
82
|
+
if (!headCall) return;
|
|
83
|
+
const { parent } = node;
|
|
84
|
+
if (
|
|
85
|
+
parent.type === 'CallExpression'
|
|
86
|
+
&& parent !== headCall.node
|
|
87
|
+
&& (parent.callee.type !== 'Identifier' || parent.callee.name !== 'computed')
|
|
88
|
+
) return;
|
|
89
|
+
|
|
90
|
+
getters.push({
|
|
91
|
+
head: headCall.head,
|
|
92
|
+
node,
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function exitFunction(node: FunctionNode): void {
|
|
97
|
+
if (getters.at(-1)?.node === node) getters.pop();
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function checkCall(node: CallNode, head: string): void {
|
|
101
|
+
const { callee } = node;
|
|
102
|
+
if (callee.type === 'Identifier' && composablePattern.test(callee.name)) {
|
|
103
|
+
context.report({
|
|
104
|
+
data: {
|
|
105
|
+
composable: callee.name,
|
|
106
|
+
head,
|
|
107
|
+
},
|
|
108
|
+
messageId: 'getter',
|
|
109
|
+
node,
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (!checker || !nodeMap) return;
|
|
116
|
+
const composable = findCalledComposable(getImplementationBodies(checker, nodeMap.get(node) as ts.CallExpression));
|
|
117
|
+
if (!composable) return;
|
|
118
|
+
context.report({
|
|
119
|
+
data: {
|
|
120
|
+
composable,
|
|
121
|
+
function: context.sourceCode.getText(callee),
|
|
122
|
+
head,
|
|
123
|
+
},
|
|
124
|
+
messageId: 'indirectGetter',
|
|
125
|
+
node,
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
|
|
16
129
|
return {
|
|
130
|
+
'ArrowFunctionExpression': enterFunction,
|
|
131
|
+
'ArrowFunctionExpression:exit': exitFunction,
|
|
17
132
|
CallExpression(node) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
if (
|
|
26
|
-
insideFunction
|
|
27
|
-
&& parent.type === 'CallExpression'
|
|
28
|
-
&& parent.callee.type === 'Identifier'
|
|
29
|
-
&& headComposables.has(parent.callee.name)
|
|
30
|
-
) {
|
|
31
|
-
context.report({
|
|
32
|
-
data: {
|
|
33
|
-
composable: node.callee.name,
|
|
34
|
-
head: parent.callee.name,
|
|
35
|
-
},
|
|
36
|
-
messageId: 'getter',
|
|
37
|
-
node,
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
133
|
+
const getter = getters.at(-1);
|
|
134
|
+
if (getter) checkCall(node, getter.head);
|
|
135
|
+
if (node.callee.type === 'Identifier' && headComposables.has(node.callee.name)) {
|
|
136
|
+
headCalls.push({
|
|
137
|
+
head: node.callee.name,
|
|
138
|
+
node,
|
|
139
|
+
});
|
|
42
140
|
}
|
|
43
141
|
},
|
|
142
|
+
'CallExpression:exit': (node) => {
|
|
143
|
+
if (headCalls.at(-1)?.node === node) headCalls.pop();
|
|
144
|
+
},
|
|
145
|
+
'FunctionExpression': enterFunction,
|
|
146
|
+
'FunctionExpression:exit': exitFunction,
|
|
44
147
|
};
|
|
45
148
|
},
|
|
46
149
|
meta: {
|
|
47
150
|
docs: { description: 'Disallow calling composables inside `useHead`/`useSeoMeta` getters.' },
|
|
48
|
-
messages: {
|
|
151
|
+
messages: {
|
|
152
|
+
getter: '`{{ composable }}()` runs when the `{{ head }}` getter is evaluated after setup, outside the component context. Call it in setup and use its result in the getter.',
|
|
153
|
+
indirectGetter: '`{{ function }}()` calls `{{ composable }}()`, which runs when the `{{ head }}` getter is evaluated after setup, outside the component context. Get what it needs in setup, such as a function returned by a composable, and call that in the getter.',
|
|
154
|
+
},
|
|
49
155
|
schema: [],
|
|
50
156
|
type: 'problem',
|
|
51
157
|
},
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { Rule } from 'eslint';
|
|
2
|
+
import type { SignatureDeclaration } from 'typescript';
|
|
3
|
+
|
|
4
|
+
import type { ParserServices } from './utils';
|
|
5
|
+
import { composablePattern } from './utils';
|
|
6
|
+
import { isReactiveProxy } from './vue-reactivity';
|
|
7
|
+
|
|
8
|
+
type FunctionNode = Extract<Rule.Node, { type: 'ArrowFunctionExpression' | 'FunctionDeclaration' | 'FunctionExpression' }>;
|
|
9
|
+
|
|
10
|
+
function isExportedComposable(node: FunctionNode): boolean {
|
|
11
|
+
const { parent } = node;
|
|
12
|
+
if (parent.type === 'ExportDefaultDeclaration') return true;
|
|
13
|
+
if (node.type === 'FunctionDeclaration') {
|
|
14
|
+
return parent.type === 'ExportNamedDeclaration' && !!node.id && composablePattern.test(node.id.name);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return parent.type === 'VariableDeclarator'
|
|
18
|
+
&& parent.id.type === 'Identifier'
|
|
19
|
+
&& composablePattern.test(parent.id.name)
|
|
20
|
+
&& parent.parent.parent?.type === 'ExportNamedDeclaration';
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const noReactiveComposableReturn: Rule.RuleModule = {
|
|
24
|
+
create(context) {
|
|
25
|
+
const services = context.sourceCode.parserServices as Partial<ParserServices> | undefined;
|
|
26
|
+
if (!services?.program || !services.esTreeNodeToTSNodeMap) return {};
|
|
27
|
+
const checker = services.program.getTypeChecker();
|
|
28
|
+
const { esTreeNodeToTSNodeMap } = services;
|
|
29
|
+
|
|
30
|
+
function check(node: FunctionNode): void {
|
|
31
|
+
if (!isExportedComposable(node)) return;
|
|
32
|
+
const signature = checker.getSignatureFromDeclaration(esTreeNodeToTSNodeMap.get(node) as SignatureDeclaration);
|
|
33
|
+
if (!signature || !isReactiveProxy(checker, checker.getReturnTypeOfSignature(signature))) return;
|
|
34
|
+
context.report({
|
|
35
|
+
messageId: 'plainObject',
|
|
36
|
+
node: node.type === 'FunctionDeclaration' && node.id ? node.id : node,
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return {
|
|
41
|
+
ArrowFunctionExpression: check,
|
|
42
|
+
FunctionDeclaration: check,
|
|
43
|
+
FunctionExpression: check,
|
|
44
|
+
};
|
|
45
|
+
},
|
|
46
|
+
meta: {
|
|
47
|
+
docs: {
|
|
48
|
+
description: 'Require exported composables to return a plain object of refs instead of a reactive proxy.',
|
|
49
|
+
requiresTypeChecking: true,
|
|
50
|
+
} as Rule.RuleMetaData['docs'],
|
|
51
|
+
messages: { plainObject: 'Return a plain object of refs, as Vue recommends, so callers can destructure it without losing reactivity. A caller that wants property access can wrap the result in `reactive()`.' },
|
|
52
|
+
schema: [],
|
|
53
|
+
type: 'problem',
|
|
54
|
+
},
|
|
55
|
+
};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { Rule } from 'eslint';
|
|
2
|
+
|
|
3
|
+
import type { ParserServices } from './utils';
|
|
4
|
+
import { composablePattern } from './utils';
|
|
5
|
+
import { isReactiveProxy } from './vue-reactivity';
|
|
6
|
+
|
|
7
|
+
// Destructuring loses reactivity only where it runs once: at module or `<script setup>` top level, in a composable or in
|
|
8
|
+
// `setup()`. Callbacks such as event handlers or `computed()` getters read the current values on every call.
|
|
9
|
+
function runsOnce(node: Rule.Node): boolean {
|
|
10
|
+
let current: null | Rule.Node = node.parent;
|
|
11
|
+
while (current) {
|
|
12
|
+
if (
|
|
13
|
+
current.type === 'ArrowFunctionExpression'
|
|
14
|
+
|| current.type === 'FunctionDeclaration'
|
|
15
|
+
|| current.type === 'FunctionExpression'
|
|
16
|
+
) {
|
|
17
|
+
const { parent } = current;
|
|
18
|
+
const name = current.type === 'FunctionDeclaration'
|
|
19
|
+
? current.id?.name
|
|
20
|
+
: parent.type === 'VariableDeclarator' && parent.id.type === 'Identifier'
|
|
21
|
+
? parent.id.name
|
|
22
|
+
: parent.type === 'Property' && parent.key.type === 'Identifier'
|
|
23
|
+
? parent.key.name
|
|
24
|
+
: undefined;
|
|
25
|
+
|
|
26
|
+
return !!name && (name === 'setup' || composablePattern.test(name));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
current = current.parent;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export const noReactiveDestructure: Rule.RuleModule = {
|
|
36
|
+
create(context) {
|
|
37
|
+
const services = context.sourceCode.parserServices as Partial<ParserServices> | undefined;
|
|
38
|
+
if (!services?.program || !services.esTreeNodeToTSNodeMap) return {};
|
|
39
|
+
const checker = services.program.getTypeChecker();
|
|
40
|
+
const { esTreeNodeToTSNodeMap } = services;
|
|
41
|
+
|
|
42
|
+
return {
|
|
43
|
+
VariableDeclarator(node) {
|
|
44
|
+
if (!node.init || (node.id.type !== 'ObjectPattern' && node.id.type !== 'ArrayPattern') || !runsOnce(node)) return;
|
|
45
|
+
// `toRaw()` keeps the proxy's type but returns the raw object, so destructuring it is an intended snapshot.
|
|
46
|
+
if (node.init.type === 'CallExpression' && node.init.callee.type === 'Identifier' && node.init.callee.name === 'toRaw') return;
|
|
47
|
+
if (!isReactiveProxy(checker, checker.getTypeAtLocation(esTreeNodeToTSNodeMap.get(node.init)))) return;
|
|
48
|
+
context.report({
|
|
49
|
+
messageId: 'destructure',
|
|
50
|
+
node: node.id,
|
|
51
|
+
});
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
},
|
|
55
|
+
meta: {
|
|
56
|
+
docs: {
|
|
57
|
+
description: 'Disallow destructuring values created by Vue\'s reactive(), shallowReactive(), readonly() and proxyRefs().',
|
|
58
|
+
requiresTypeChecking: true,
|
|
59
|
+
} as Rule.RuleMetaData['docs'],
|
|
60
|
+
messages: { destructure: 'Destructuring a reactive object reads each property once and loses reactivity. Keep the object and access its properties where they are needed.' },
|
|
61
|
+
schema: [],
|
|
62
|
+
type: 'problem',
|
|
63
|
+
},
|
|
64
|
+
};
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import type { Rule } from 'eslint';
|
|
2
2
|
|
|
3
|
+
import { isMemberNamed } from './utils';
|
|
4
|
+
|
|
3
5
|
type CallArgument = Extract<Rule.Node, { type: 'CallExpression' }>['arguments'][number];
|
|
4
6
|
type Expression = Exclude<CallArgument, { type: 'SpreadElement' }>;
|
|
5
7
|
|
|
@@ -25,22 +27,14 @@ export const noSilentCatch: Rule.RuleModule = {
|
|
|
25
27
|
const { callee } = node;
|
|
26
28
|
const callback = node.arguments[0];
|
|
27
29
|
if (
|
|
28
|
-
callee
|
|
29
|
-
|| callee.computed
|
|
30
|
-
|| callee.property.type !== 'Identifier'
|
|
31
|
-
|| callee.property.name !== 'catch'
|
|
30
|
+
!isMemberNamed(callee, 'catch')
|
|
32
31
|
|| !callback
|
|
33
32
|
|| !isSilent(callback)
|
|
34
33
|
) return;
|
|
35
34
|
|
|
36
35
|
// A comment right before or inside the callback is the reason the rejection can be ignored.
|
|
37
36
|
const { sourceCode } = context;
|
|
38
|
-
if (
|
|
39
|
-
[
|
|
40
|
-
...sourceCode.getCommentsBefore(callback),
|
|
41
|
-
...sourceCode.getCommentsInside(callback),
|
|
42
|
-
].length
|
|
43
|
-
) return;
|
|
37
|
+
if (sourceCode.getCommentsBefore(callback).length || sourceCode.getCommentsInside(callback).length) return;
|
|
44
38
|
|
|
45
39
|
context.report({
|
|
46
40
|
messageId: 'silent',
|
|
@@ -1,14 +1,11 @@
|
|
|
1
1
|
import type { Rule } from 'eslint';
|
|
2
2
|
import type {
|
|
3
|
-
Node,
|
|
4
3
|
Type,
|
|
5
4
|
TypeChecker,
|
|
5
|
+
TypeReference,
|
|
6
6
|
} from 'typescript';
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
esTreeNodeToTSNodeMap: { get: (node: unknown) => Node };
|
|
10
|
-
program: { getTypeChecker: () => TypeChecker };
|
|
11
|
-
}
|
|
8
|
+
import type { ParserServices } from './utils';
|
|
12
9
|
|
|
13
10
|
const refinementMethods = new Set([
|
|
14
11
|
'check',
|
|
@@ -16,13 +13,21 @@ const refinementMethods = new Set([
|
|
|
16
13
|
'superRefine',
|
|
17
14
|
]);
|
|
18
15
|
|
|
19
|
-
|
|
20
|
-
function isNonPromiseThenable(type: Type): boolean {
|
|
16
|
+
function isNonPromiseThenable(checker: TypeChecker, type: Type): boolean {
|
|
21
17
|
return (type.isUnion() ? type.types : [type]).some(
|
|
22
|
-
(part) => part.getProperty('then') && part
|
|
18
|
+
(part) => part.getProperty('then') && !isPromise(checker, part),
|
|
23
19
|
);
|
|
24
20
|
}
|
|
25
21
|
|
|
22
|
+
// Zod awaits a refinement only when it returns a native Promise (`instanceof Promise`, so subtypes and branded
|
|
23
|
+
// intersections such as `SafePromise` count); any other thenable counts as a truthy result.
|
|
24
|
+
function isPromise(checker: TypeChecker, type: Type): boolean {
|
|
25
|
+
if (type.isIntersection()) return type.types.some((part) => isPromise(checker, part));
|
|
26
|
+
if (type.getSymbol()?.getName() === 'Promise') return true;
|
|
27
|
+
const target = (type as TypeReference).target ?? type;
|
|
28
|
+
return target.isClassOrInterface() && checker.getBaseTypes(target).some((base) => isPromise(checker, base));
|
|
29
|
+
}
|
|
30
|
+
|
|
26
31
|
export const noThenableZodRefinement: Rule.RuleModule = {
|
|
27
32
|
create(context) {
|
|
28
33
|
const services = context.sourceCode.parserServices as ParserServices;
|
|
@@ -46,7 +51,7 @@ export const noThenableZodRefinement: Rule.RuleModule = {
|
|
|
46
51
|
for (const callback of node.arguments) {
|
|
47
52
|
const returnsThenable = getType(callback)
|
|
48
53
|
.getCallSignatures()
|
|
49
|
-
.some((signature) => isNonPromiseThenable(checker.getReturnTypeOfSignature(signature)));
|
|
54
|
+
.some((signature) => isNonPromiseThenable(checker, checker.getReturnTypeOfSignature(signature)));
|
|
50
55
|
|
|
51
56
|
if (!returnsThenable) continue;
|
|
52
57
|
context.report({
|