@typescript-eslint/eslint-plugin 8.68.1-alpha.0 → 8.68.1-alpha.2

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.
@@ -1,11 +1,15 @@
1
1
  import type { TSESLint } from '@typescript-eslint/utils';
2
2
  export type Options = [
3
3
  {
4
- checksConditionals?: boolean;
4
+ checksConditionals?: boolean | ChecksConditionalsOptions;
5
5
  checksSpreads?: boolean;
6
6
  checksVoidReturn?: boolean | ChecksVoidReturnOptions;
7
7
  }
8
8
  ];
9
+ export type FlagUnionsOptions = 'all' | 'none' | 'strict';
10
+ export interface ChecksConditionalsOptions {
11
+ flagUnions?: FlagUnionsOptions;
12
+ }
9
13
  export interface ChecksVoidReturnOptions {
10
14
  arguments?: boolean;
11
15
  attributes?: boolean;
@@ -89,8 +89,25 @@ exports.default = (0, util_1.createRule)({
89
89
  additionalProperties: false,
90
90
  properties: {
91
91
  checksConditionals: {
92
- type: 'boolean',
93
92
  description: 'Whether to warn when a Promise is provided to conditional statements.',
93
+ oneOf: [
94
+ {
95
+ type: 'boolean',
96
+ description: 'Whether to check conditionals.',
97
+ },
98
+ {
99
+ type: 'object',
100
+ additionalProperties: false,
101
+ description: 'Detailed settings for conditional inspection.',
102
+ properties: {
103
+ flagUnions: {
104
+ type: 'string',
105
+ description: 'Configures how union types containing Promise-like types are checked.',
106
+ enum: ['all', 'strict', 'none'],
107
+ },
108
+ },
109
+ },
110
+ ],
94
111
  },
95
112
  checksSpreads: {
96
113
  type: 'boolean',
@@ -151,6 +168,7 @@ exports.default = (0, util_1.createRule)({
151
168
  const services = (0, util_1.getParserServices)(context);
152
169
  const checker = services.program.getTypeChecker();
153
170
  const checkedNodes = new Set();
171
+ const flagUnionsOption = normalizeFlagUnionsOption(checksConditionals);
154
172
  const conditionalChecks = {
155
173
  'CallExpression > MemberExpression': checkArrayPredicates,
156
174
  ConditionalExpression: checkTestConditional,
@@ -284,6 +302,22 @@ exports.default = (0, util_1.createRule)({
284
302
  node,
285
303
  messageId: 'conditional',
286
304
  });
305
+ return;
306
+ }
307
+ if (
308
+ // none -> Report `Promise` but not `Promise | ...`
309
+ (flagUnionsOption === 'none' && isAlwaysThenable(checker, tsNode)) ||
310
+ //
311
+ // all -> Report `Promise` and `Promise | ...`
312
+ (flagUnionsOption === 'all' && isSometimesThenable(checker, tsNode)) ||
313
+ //
314
+ // strict -> Report `Promise<T> | T` but not `Promise<T> | NotT`
315
+ (flagUnionsOption === 'strict' &&
316
+ hasMatchingPromiseTypeArgument(checker, tsNode))) {
317
+ context.report({
318
+ node,
319
+ messageId: 'conditional',
320
+ });
287
321
  }
288
322
  }
289
323
  function checkArrayPredicates(node) {
@@ -801,4 +835,30 @@ function hasWellKnownSymbolWithVoidReturn(checker, node, type, symbolName) {
801
835
  return isVoidReturningFunctionType(checker, node, checker.getTypeOfSymbolAtLocation(symbol, node));
802
836
  });
803
837
  }
838
+ /**
839
+ * Check that the Promise argument is the same as the rest of the type when it is a Union that contains Promise.
840
+ */
841
+ function hasMatchingPromiseTypeArgument(checker, node) {
842
+ const type = checker.getTypeAtLocation(node);
843
+ const unionConstituents = tsutils.unionConstituents(checker.getApparentType(type));
844
+ const promiseType = unionConstituents.find(type => tsutils.isThenableType(checker, node, type));
845
+ if (!promiseType) {
846
+ return false;
847
+ }
848
+ const nonPromiseUnionConstituents = unionConstituents.filter(type => type !== promiseType);
849
+ const awaitedType = checker.getAwaitedType(promiseType);
850
+ if (!awaitedType) {
851
+ return false;
852
+ }
853
+ const awaitedTypeConstituents = tsutils.unionConstituents(awaitedType);
854
+ return (nonPromiseUnionConstituents.length === awaitedTypeConstituents.length &&
855
+ nonPromiseUnionConstituents.every(type => awaitedTypeConstituents.some(awaited => checker.isTypeAssignableTo(type, awaited) &&
856
+ checker.isTypeAssignableTo(awaited, type))));
857
+ }
858
+ function normalizeFlagUnionsOption(checksConditionals) {
859
+ if (!checksConditionals || checksConditionals === true) {
860
+ return 'none';
861
+ }
862
+ return checksConditionals.flagUnions ?? 'none';
863
+ }
804
864
  //# sourceMappingURL=no-misused-promises.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@typescript-eslint/eslint-plugin",
3
- "version": "8.68.1-alpha.0",
3
+ "version": "8.68.1-alpha.2",
4
4
  "description": "TypeScript plugin for ESLint",
5
5
  "files": [
6
6
  "dist",
@@ -47,10 +47,10 @@
47
47
  ],
48
48
  "dependencies": {
49
49
  "@eslint-community/regexpp": "^4.12.2",
50
- "@typescript-eslint/scope-manager": "8.68.1-alpha.0",
51
- "@typescript-eslint/type-utils": "8.68.1-alpha.0",
52
- "@typescript-eslint/utils": "8.68.1-alpha.0",
53
- "@typescript-eslint/visitor-keys": "8.68.1-alpha.0",
50
+ "@typescript-eslint/scope-manager": "8.68.1-alpha.2",
51
+ "@typescript-eslint/type-utils": "8.68.1-alpha.2",
52
+ "@typescript-eslint/utils": "8.68.1-alpha.2",
53
+ "@typescript-eslint/visitor-keys": "8.68.1-alpha.2",
54
54
  "ignore": "^7.0.5",
55
55
  "natural-compare": "^1.4.0",
56
56
  "ts-api-utils": "^2.5.0"
@@ -60,8 +60,8 @@
60
60
  "@types/mdast": "^4.0.4",
61
61
  "@types/natural-compare": "^1.4.3",
62
62
  "@types/react": "^18.3.21",
63
- "@typescript-eslint/rule-schema-to-typescript-types": "8.68.1-alpha.0",
64
- "@typescript-eslint/rule-tester": "8.68.1-alpha.0",
63
+ "@typescript-eslint/rule-schema-to-typescript-types": "8.68.1-alpha.2",
64
+ "@typescript-eslint/rule-tester": "8.68.1-alpha.2",
65
65
  "@typescript/native": "npm:typescript@^7.0.2",
66
66
  "@vitest/coverage-v8": "^4.0.18",
67
67
  "ajv": "^6.12.6",
@@ -81,7 +81,7 @@
81
81
  "vitest": "^4.0.18"
82
82
  },
83
83
  "peerDependencies": {
84
- "@typescript-eslint/parser": "^8.68.1-alpha.0",
84
+ "@typescript-eslint/parser": "^8.68.1-alpha.2",
85
85
  "eslint": "^8.57.0 || ^9.0.0 || ^10.0.0",
86
86
  "typescript": ">=4.8.4 <6.1.0"
87
87
  },