@typescript-eslint/eslint-plugin 8.7.1-alpha.9 → 8.8.1-alpha.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.
@@ -25,6 +25,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
26
  const tsutils = __importStar(require("ts-api-utils"));
27
27
  const util_1 = require("../util");
28
+ const getForStatementHeadLoc_1 = require("../util/getForStatementHeadLoc");
28
29
  exports.default = (0, util_1.createRule)({
29
30
  name: 'await-thenable',
30
31
  meta: {
@@ -36,7 +37,9 @@ exports.default = (0, util_1.createRule)({
36
37
  hasSuggestions: true,
37
38
  messages: {
38
39
  await: 'Unexpected `await` of a non-Promise (non-"Thenable") value.',
40
+ forAwaitOfNonThenable: 'Unexpected `for await...of` of a value that is not async iterable.',
39
41
  removeAwait: 'Remove unnecessary `await`.',
42
+ convertToOrdinaryFor: 'Convert to an ordinary `for...of` loop.',
40
43
  },
41
44
  schema: [],
42
45
  type: 'problem',
@@ -68,6 +71,30 @@ exports.default = (0, util_1.createRule)({
68
71
  });
69
72
  }
70
73
  },
74
+ 'ForOfStatement[await=true]'(node) {
75
+ const type = services.getTypeAtLocation(node.right);
76
+ if ((0, util_1.isTypeAnyType)(type)) {
77
+ return;
78
+ }
79
+ const asyncIteratorSymbol = tsutils.getWellKnownSymbolPropertyOfType(type, 'asyncIterator', checker);
80
+ if (asyncIteratorSymbol == null) {
81
+ context.report({
82
+ loc: (0, getForStatementHeadLoc_1.getForStatementHeadLoc)(context.sourceCode, node),
83
+ messageId: 'forAwaitOfNonThenable',
84
+ suggest: [
85
+ // Note that this suggestion causes broken code for sync iterables
86
+ // of promises, since the loop variable is not awaited.
87
+ {
88
+ messageId: 'convertToOrdinaryFor',
89
+ fix(fixer) {
90
+ const awaitToken = (0, util_1.nullThrows)(context.sourceCode.getFirstToken(node, util_1.isAwaitKeyword), util_1.NullThrowsReasons.MissingToken('await', 'for await loop'));
91
+ return fixer.remove(awaitToken);
92
+ },
93
+ },
94
+ ],
95
+ });
96
+ }
97
+ },
71
98
  };
72
99
  },
73
100
  });
@@ -1 +1 @@
1
- {"version":3,"file":"await-thenable.js","sourceRoot":"","sources":["../../src/rules/await-thenable.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AACA,sDAAwC;AAExC,kCAQiB;AAEjB,kBAAe,IAAA,iBAAU,EAAC;IACxB,IAAI,EAAE,gBAAgB;IACtB,IAAI,EAAE;QACJ,IAAI,EAAE;YACJ,WAAW,EAAE,kDAAkD;YAC/D,WAAW,EAAE,aAAa;YAC1B,oBAAoB,EAAE,IAAI;SAC3B;QACD,cAAc,EAAE,IAAI;QACpB,QAAQ,EAAE;YACR,KAAK,EAAE,6DAA6D;YACpE,WAAW,EAAE,6BAA6B;SAC3C;QACD,MAAM,EAAE,EAAE;QACV,IAAI,EAAE,SAAS;KAChB;IACD,cAAc,EAAE,EAAE;IAElB,MAAM,CAAC,OAAO;QACZ,MAAM,QAAQ,GAAG,IAAA,wBAAiB,EAAC,OAAO,CAAC,CAAC;QAC5C,MAAM,OAAO,GAAG,QAAQ,CAAC,OAAO,CAAC,cAAc,EAAE,CAAC;QAElD,OAAO;YACL,eAAe,CAAC,IAAI;gBAClB,MAAM,IAAI,GAAG,QAAQ,CAAC,iBAAiB,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;gBACvD,IAAI,IAAA,oBAAa,EAAC,IAAI,CAAC,IAAI,IAAA,wBAAiB,EAAC,IAAI,CAAC,EAAE,CAAC;oBACnD,OAAO;gBACT,CAAC;gBAED,MAAM,YAAY,GAAG,QAAQ,CAAC,qBAAqB,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;gBAE9D,IAAI,CAAC,OAAO,CAAC,cAAc,CAAC,OAAO,EAAE,YAAY,CAAC,UAAU,EAAE,IAAI,CAAC,EAAE,CAAC;oBACpE,OAAO,CAAC,MAAM,CAAC;wBACb,S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,CAAC;wBACrD,SAAS,EAAE,uBAAuB;wBAClC,OAAO,EAAE;4BACP,kEAAkE;4BAClE,uDAAuD;4BACvD;gCACE,SAAS,EAAE,sBAAsB;gCACjC,GAAG,CAAC,KAAK;oCACP,MAAM,UAAU,GAAG,IAAA,iBAAU,EAC3B,OAAO,CAAC,UAAU,CAAC,aAAa,CAAC,IAAI,EAAE,qBAAc,CAAC,EACtD,wBAAiB,CAAC,YAAY,CAAC,OAAO,EAAE,gBAAgB,CAAC,CAC1D,CAAC;oCACF,OAAO,KAAK,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;gCAClC,CAAC;6BACF;yBACF;qBACF,CAAC,CAAC;gBACL,CAAC;YACH,CAAC;SACF,CAAC;IACJ,CAAC;CACF,CAAC,CAAC"}
@@ -59,48 +59,48 @@ exports.default = (0, util_1.createRule)({
59
59
  }
60
60
  return false;
61
61
  }
62
- function isAllowedBitwiseOperand(decl, node) {
63
- return (node.type === utils_1.AST_NODE_TYPES.Literal || isSelfEnumMember(decl, node));
64
- }
65
62
  return {
66
63
  TSEnumMember(node) {
67
64
  // If there is no initializer, then this node is just the name of the member, so ignore.
68
65
  if (node.initializer == null) {
69
66
  return;
70
67
  }
71
- // any old literal
72
- if (node.initializer.type === utils_1.AST_NODE_TYPES.Literal) {
73
- return;
74
- }
75
- // TemplateLiteral without expressions
76
- if (node.initializer.type === utils_1.AST_NODE_TYPES.TemplateLiteral &&
77
- node.initializer.expressions.length === 0) {
78
- return;
79
- }
80
68
  const declaration = node.parent.parent;
81
- // -1 and +1
82
- if (node.initializer.type === utils_1.AST_NODE_TYPES.UnaryExpression) {
83
- if (node.initializer.argument.type === utils_1.AST_NODE_TYPES.Literal &&
84
- ['+', '-'].includes(node.initializer.operator)) {
85
- return;
69
+ function isAllowedInitializerExpressionRecursive(node, partOfBitwiseComputation) {
70
+ // You can only refer to an enum member if it's part of a bitwise computation.
71
+ // so C = B isn't allowed (special case), but C = A | B is.
72
+ if (partOfBitwiseComputation && isSelfEnumMember(declaration, node)) {
73
+ return true;
86
74
  }
87
- if (allowBitwiseExpressions &&
88
- node.initializer.operator === '~' &&
89
- isAllowedBitwiseOperand(declaration, node.initializer.argument)) {
90
- return;
75
+ switch (node.type) {
76
+ // any old literal
77
+ case utils_1.AST_NODE_TYPES.Literal:
78
+ return true;
79
+ // TemplateLiteral without expressions
80
+ case utils_1.AST_NODE_TYPES.TemplateLiteral:
81
+ return node.expressions.length === 0;
82
+ case utils_1.AST_NODE_TYPES.UnaryExpression:
83
+ // +123, -123, etc.
84
+ if (['+', '-'].includes(node.operator)) {
85
+ return isAllowedInitializerExpressionRecursive(node.argument, partOfBitwiseComputation);
86
+ }
87
+ if (allowBitwiseExpressions) {
88
+ return (node.operator === '~' &&
89
+ isAllowedInitializerExpressionRecursive(node.argument, true));
90
+ }
91
+ return false;
92
+ case utils_1.AST_NODE_TYPES.BinaryExpression:
93
+ if (allowBitwiseExpressions) {
94
+ return (['|', '&', '^', '<<', '>>', '>>>'].includes(node.operator) &&
95
+ isAllowedInitializerExpressionRecursive(node.left, true) &&
96
+ isAllowedInitializerExpressionRecursive(node.right, true));
97
+ }
98
+ return false;
99
+ default:
100
+ return false;
91
101
  }
92
102
  }
93
- if (node.initializer.type === utils_1.AST_NODE_TYPES.UnaryExpression &&
94
- node.initializer.argument.type === utils_1.AST_NODE_TYPES.Literal &&
95
- (['+', '-'].includes(node.initializer.operator) ||
96
- (allowBitwiseExpressions && node.initializer.operator === '~'))) {
97
- return;
98
- }
99
- if (allowBitwiseExpressions &&
100
- node.initializer.type === utils_1.AST_NODE_TYPES.BinaryExpression &&
101
- ['|', '&', '^', '<<', '>>', '>>>'].includes(node.initializer.operator) &&
102
- isAllowedBitwiseOperand(declaration, node.initializer.left) &&
103
- isAllowedBitwiseOperand(declaration, node.initializer.right)) {
103
+ if (isAllowedInitializerExpressionRecursive(node.initializer, false)) {
104
104
  return;
105
105
  }
106
106
  context.report({
@@ -1 +1 @@
1
- {"version":3,"file":"prefer-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AAW,CAAC,QAAQ,CAC1B;oBACD,uBAAuB,CAAC,WAAW,EAAE,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC;oBAC3D,uBAAuB,CAAC,WAAW,EAAE,IAAI,CAAC,WAAW,CAAC,KAAK,CAAC,EAC5D,CAAC;oBACD,OAAO;gBACT,CAAC;gBAED,OAAO,CAAC,MAAM,CAAC;oBACb,IAAI,EAAE,IAAI,CAAC,EAAE;oBACb,SAAS,EAAE,YAAY;iBACxB,CAAC,CAAC;YACL,CAAC;SACF,CAAC;IACJ,CAAC;CACF,CAAC,CAAC"}
1
+ {"version":3,"file":"prefer-literal-enum-member.js","sourceRoot":"","sources":["../../src/rules/prefer-literal-enum-member.ts"],"names":[],"mappings":";;AACA,oDAA0D;AAE1D,kCAA2D;AAE3D,kBAAe,IAAA,iBAAU,EAAC;IACxB,IAAI,EAAE,4BAA4B;IAClC,IAAI,EAAE;QACJ,IAAI,EAAE,YAAY;QAClB,IAAI,EAAE;YACJ,WAAW,EAAE,+CAA+C;YAC5D,WAAW,EAAE,QAAQ;YACrB,oBAAoB,EAAE,KAAK;SAC5B;QACD,QAAQ,EAAE;YACR,UAAU,EAAE,sEAAsE;SACnF;QACD,MAAM,EAAE;YACN;gBACE,IAAI,EAAE,QAAQ;gBACd,UAAU,EAAE;oBACV,uBAAuB,EAAE;wBACvB,WAAW,EACT,kEAAkE;wBACpE,IAAI,EAAE,SAAS;qBAChB;iBACF;gBACD,oBAAoB,EAAE,KAAK;aAC5B;SACF;KACF;IACD,cAAc,EAAE;QACd;YACE,uBAAuB,EAAE,KAAK;SAC/B;KACF;IACD,MAAM,CAAC,OAAO,EAAE,CAAC,EAAE,uBAAuB,EAAE,CAAC;QAC3C,SAAS,oBAAoB,CAAC,IAAmB,EAAE,IAAY;YAC7D,OAAO,IAAI,CAAC,IAAI,KAAK,sBAAc,CAAC,UAAU,IAAI,IAAI,CAAC,IAAI,KAAK,IAAI,CAAC;QACvE,CAAC;QAED,SAAS,aAAa,CAC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,CAAC,KAAK,EAAE,IAAI,CAAC,CAC1D,CAAC;4BACJ,CAAC;4BACD,OAAO,KAAK,CAAC;wBAEf;4BACE,OAAO,KAAK,CAAC;oBACjB,CAAC;gBACH,CAAC;gBAED,IAAI,uCAAuC,CAAC,IAAI,CAAC,WAAW,EAAE,KAAK,CAAC,EAAE,CAAC;oBACrE,OAAO;gBACT,CAAC;gBAED,OAAO,CAAC,MAAM,CAAC;oBACb,IAAI,EAAE,IAAI,CAAC,EAAE;oBACb,SAAS,EAAE,YAAY;iBACxB,CAAC,CAAC;YACL,CAAC;SACF,CAAC;IACJ,CAAC;CACF,CAAC,CAAC"}
@@ -34,6 +34,7 @@ __exportStar(require("./misc"), exports);
34
34
  __exportStar(require("./needsPrecedingSemiColon"), exports);
35
35
  __exportStar(require("./objectIterators"), exports);
36
36
  __exportStar(require("./scopeUtils"), exports);
37
+ __exportStar(require("./types"), exports);
37
38
  __exportStar(require("./isAssignee"), exports);
38
39
  __exportStar(require("./getFixOrSuggest"), exports);
39
40
  __exportStar(require("./isArrayMethodCallWithPredicate"), exports);
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/util/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,oDAAuD;AAEvD,6CAA2B;AAC3B,2DAAyC;AACzC,+CAA6B;AAC7B,uDAAqC;AACrC,0DAAwC;AACxC,yDAAuC;AACvC,oDAAkC;AAClC,2DAAyC;AACzC,sDAAoC;AACpC,qDAAmC;AACnC,gDAA8B;AAC9B,kDAAgC;AAChC,iEAA+C;AAC/C,0DAAwC;AACxC,yCAAuB;AACvB,4DAA0C;AAC1C,oDAAkC;AAClC,+CAA6B;AAE7B,+CAA6B;AAC7B,oDAAkC;AAClC,mEAAiD;AAEjD,sEAAsE;AACtE,gEAA8C;AAC9C,MAAM,EACJ,YAAY,EACZ,SAAS,EACT,gBAAgB,EAChB,iBAAiB,EACjB,UAAU,EACV,iBAAiB,GAClB,GAAG,mBAAW,CAAC;AAMd,oCAAY;AACZ,8BAAS;AACT,4CAAgB;AAChB,8CAAiB;AACjB,gCAAU;AAGV,8CAAiB"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/util/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,oDAAuD;AAEvD,6CAA2B;AAC3B,2DAAyC;AACzC,+CAA6B;AAC7B,uDAAqC;AACrC,0DAAwC;AACxC,yDAAuC;AACvC,oDAAkC;AAClC,2DAAyC;AACzC,sDAAoC;AACpC,qDAAmC;AACnC,gDAA8B;AAC9B,kDAAgC;AAChC,iEAA+C;AAC/C,0DAAwC;AACxC,yCAAuB;AACvB,4DAA0C;AAC1C,oDAAkC;AAClC,+CAA6B;AAC7B,0CAAwB;AACxB,+CAA6B;AAC7B,oDAAkC;AAClC,mEAAiD;AAEjD,sEAAsE;AACtE,gEAA8C;AAC9C,MAAM,EACJ,YAAY,EACZ,SAAS,EACT,gBAAgB,EAChB,iBAAiB,EACjB,UAAU,EACV,iBAAiB,GAClB,GAAG,mBAAW,CAAC;AAMd,oCAAY;AACZ,8BAAS;AACT,4CAAgB;AAChB,8CAAiB;AACjB,gCAAU;AAGV,8CAAiB"}
@@ -10,9 +10,9 @@ import TabItem from '@theme/TabItem';
10
10
  > See **https://typescript-eslint.io/rules/await-thenable** for documentation.
11
11
 
12
12
  A "Thenable" value is an object which has a `then` method, such as a Promise.
13
- The `await` keyword is generally used to retrieve the result of calling a Thenable's `then` method.
13
+ The [`await` keyword](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await) is generally used to retrieve the result of calling a Thenable's `then` method.
14
14
 
15
- If the `await` keyword is used on a value that is not a Thenable, the value is directly resolved immediately.
15
+ If the `await` keyword is used on a value that is not a Thenable, the value is directly resolved, but will still pause execution until the next microtask.
16
16
  While doing so is valid JavaScript, it is often a programmer error, such as forgetting to add parenthesis to call a function that returns a Promise.
17
17
 
18
18
  ## Examples
@@ -40,6 +40,86 @@ await createValue();
40
40
  </TabItem>
41
41
  </Tabs>
42
42
 
43
+ ## Async Iteration (`for await...of` Loops)
44
+
45
+ This rule also inspects [`for await...of` statements](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of), and reports if the value being iterated over is not async-iterable.
46
+
47
+ :::info[Why does the rule report on `for await...of` loops used on an array of Promises?]
48
+
49
+ While `for await...of` can be used with synchronous iterables, and it will await each promise produced by the iterable, it is inadvisable to do so.
50
+ There are some tiny nuances that you may want to consider.
51
+
52
+ The biggest difference between using `for await...of` and using `for...of` (plus awaiting each result yourself) is error handling.
53
+ When an error occurs within the loop body, `for await...of` does _not_ close the original sync iterable, while `for...of` does.
54
+ For detailed examples of this, see the [MDN documentation on using `for await...of` with sync-iterables](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of#iterating_over_sync_iterables_and_generators).
55
+
56
+ Also consider whether you need sequential awaiting at all. Using `for await...of` may obscure potential opportunities for concurrent processing, such as those reported by [`no-await-in-loop`](https://eslint.org/docs/latest/rules/no-await-in-loop). Consider instead using one of the [promise concurrency methods](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise#promise_concurrency) for better performance.
57
+
58
+ :::
59
+
60
+ ### Examples
61
+
62
+ <Tabs>
63
+ <TabItem value="❌ Incorrect">
64
+
65
+ ```ts
66
+ async function syncIterable() {
67
+ const arrayOfValues = [1, 2, 3];
68
+ for await (const value of arrayOfValues) {
69
+ console.log(value);
70
+ }
71
+ }
72
+
73
+ async function syncIterableOfPromises() {
74
+ const arrayOfPromises = [
75
+ Promise.resolve(1),
76
+ Promise.resolve(2),
77
+ Promise.resolve(3),
78
+ ];
79
+ for await (const promisedValue of arrayOfPromises) {
80
+ console.log(promisedValue);
81
+ }
82
+ }
83
+ ```
84
+
85
+ </TabItem>
86
+ <TabItem value="✅ Correct">
87
+
88
+ ```ts
89
+ async function syncIterable() {
90
+ const arrayOfValues = [1, 2, 3];
91
+ for (const value of arrayOfValues) {
92
+ console.log(value);
93
+ }
94
+ }
95
+
96
+ async function syncIterableOfPromises() {
97
+ const arrayOfPromises = [
98
+ Promise.resolve(1),
99
+ Promise.resolve(2),
100
+ Promise.resolve(3),
101
+ ];
102
+ for (const promisedValue of await Promise.all(arrayOfPromises)) {
103
+ console.log(promisedValue);
104
+ }
105
+ }
106
+
107
+ async function validUseOfForAwaitOnAsyncIterable() {
108
+ async function* yieldThingsAsynchronously() {
109
+ yield 1;
110
+ await new Promise(resolve => setTimeout(resolve, 1000));
111
+ yield 2;
112
+ }
113
+
114
+ for await (const promisedValue of yieldThingsAsynchronously()) {
115
+ console.log(promisedValue);
116
+ }
117
+ }
118
+ ```
119
+
120
+ </TabItem>
121
+ </Tabs>
122
+
43
123
  ## When Not To Use It
44
124
 
45
125
  If you want to allow code to `await` non-Promise values.
@@ -9,7 +9,9 @@ import TabItem from '@theme/TabItem';
9
9
  >
10
10
  > See **https://typescript-eslint.io/rules/no-unsafe-enum-comparison** for documentation.
11
11
 
12
- The TypeScript compiler can be surprisingly lenient when working with enums. String enums are widely considered to be safer than number enums, but even string enums have some pitfalls. For example, it is allowed to compare enum values against literals:
12
+ The TypeScript compiler can be surprisingly lenient when working with enums.
13
+ While overt safety problems with enums were [resolved in TypeScript 5.0](https://devblogs.microsoft.com/typescript/announcing-typescript-5-0/#all-enums-are-union-enums), some logical pitfalls remain permitted.
14
+ For example, it is allowed to compare enum values against non-enum values:
13
15
 
14
16
  ```ts
15
17
  enum Vegetable {
@@ -21,7 +23,9 @@ declare const vegetable: Vegetable;
21
23
  vegetable === 'asparagus'; // No error
22
24
  ```
23
25
 
24
- The above code snippet should instead be written as `vegetable === Vegetable.Asparagus`. Allowing literals in comparisons subverts the point of using enums in the first place. By enforcing comparisons with properly typed enums:
26
+ The above code snippet should instead be written as `vegetable === Vegetable.Asparagus`.
27
+ Allowing non-enums in comparisons subverts the point of using enums in the first place.
28
+ By enforcing comparisons with properly typed enums:
25
29
 
26
30
  - It makes a codebase more resilient to enum members changing values.
27
31
  - It allows for code IDEs to use the "Rename Symbol" feature to quickly rename an enum.
@@ -39,17 +43,22 @@ enum Fruit {
39
43
 
40
44
  declare let fruit: Fruit;
41
45
 
46
+ // bad - comparison between enum and explicit value instead of named enum member
42
47
  fruit === 0;
43
- ```
44
48
 
45
- ```ts
46
49
  enum Vegetable {
47
50
  Asparagus = 'asparagus',
48
51
  }
49
52
 
50
53
  declare let vegetable: Vegetable;
51
54
 
55
+ // bad - comparison between enum and explicit value instead of named enum member
52
56
  vegetable === 'asparagus';
57
+
58
+ declare let anyString: string;
59
+
60
+ // bad - comparison between enum and non-enum value
61
+ anyString === Vegetable.Asparagus;
53
62
  ```
54
63
 
55
64
  </TabItem>
@@ -63,9 +72,7 @@ enum Fruit {
63
72
  declare let fruit: Fruit;
64
73
 
65
74
  fruit === Fruit.Apple;
66
- ```
67
75
 
68
- ```ts
69
76
  enum Vegetable {
70
77
  Asparagus = 'asparagus',
71
78
  }
@@ -80,7 +87,12 @@ vegetable === Vegetable.Asparagus;
80
87
 
81
88
  ## When Not To Use It
82
89
 
83
- If you don't mind number and/or literal string constants being compared against enums, you likely don't need this rule.
90
+ If you don't mind enums being treated as a namespaced bag of values, rather than opaque identifiers, you likely don't need this rule.
91
+
92
+ Sometimes, you may want to ingest a value from an API or user input, then use it as an enum throughout your application.
93
+ While validating the input, it may be appropriate to disable the rule.
94
+ Alternately, you might consider making use of a validation library like [Zod](https://zod.dev/?id=native-enums).
95
+ See further discussion of this topic in [#8557](https://github.com/typescript-eslint/typescript-eslint/issues/8557).
84
96
 
85
- Separately, in the rare case of relying on an third party enums that are only imported as `type`s, it may be difficult to adhere to this rule.
97
+ Finally, in the rare case of relying on an third party enums that are only imported as `type`s, it may be difficult to adhere to this rule.
86
98
  You might consider using [ESLint disable comments](https://eslint.org/docs/latest/use/configure/rules#using-configuration-comments-1) for those specific situations instead of completely disabling this rule.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@typescript-eslint/eslint-plugin",
3
- "version": "8.7.1-alpha.9",
3
+ "version": "8.8.1-alpha.0",
4
4
  "description": "TypeScript plugin for ESLint",
5
5
  "files": [
6
6
  "dist",
@@ -60,10 +60,10 @@
60
60
  },
61
61
  "dependencies": {
62
62
  "@eslint-community/regexpp": "^4.10.0",
63
- "@typescript-eslint/scope-manager": "8.7.1-alpha.9",
64
- "@typescript-eslint/type-utils": "8.7.1-alpha.9",
65
- "@typescript-eslint/utils": "8.7.1-alpha.9",
66
- "@typescript-eslint/visitor-keys": "8.7.1-alpha.9",
63
+ "@typescript-eslint/scope-manager": "8.8.1-alpha.0",
64
+ "@typescript-eslint/type-utils": "8.8.1-alpha.0",
65
+ "@typescript-eslint/utils": "8.8.1-alpha.0",
66
+ "@typescript-eslint/visitor-keys": "8.8.1-alpha.0",
67
67
  "graphemer": "^1.4.0",
68
68
  "ignore": "^5.3.1",
69
69
  "natural-compare": "^1.4.0",
@@ -74,8 +74,8 @@
74
74
  "@types/marked": "^5.0.2",
75
75
  "@types/mdast": "^4.0.3",
76
76
  "@types/natural-compare": "*",
77
- "@typescript-eslint/rule-schema-to-typescript-types": "8.7.1-alpha.9",
78
- "@typescript-eslint/rule-tester": "8.7.1-alpha.9",
77
+ "@typescript-eslint/rule-schema-to-typescript-types": "8.8.1-alpha.0",
78
+ "@typescript-eslint/rule-tester": "8.8.1-alpha.0",
79
79
  "ajv": "^6.12.6",
80
80
  "cross-env": "^7.0.3",
81
81
  "cross-fetch": "*",