@typescript-eslint/eslint-plugin 8.1.1-alpha.10 → 8.1.1-alpha.11

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.
Files changed (38) hide show
  1. package/dist/rules/array-type.js +4 -1
  2. package/dist/rules/array-type.js.map +1 -1
  3. package/dist/rules/enum-utils/shared.js +1 -1
  4. package/dist/rules/enum-utils/shared.js.map +1 -1
  5. package/dist/rules/member-ordering.js +1 -1
  6. package/dist/rules/member-ordering.js.map +1 -1
  7. package/dist/rules/naming-convention-utils/shared.js +1 -1
  8. package/dist/rules/naming-convention-utils/shared.js.map +1 -1
  9. package/dist/rules/no-invalid-void-type.js +2 -2
  10. package/dist/rules/no-invalid-void-type.js.map +1 -1
  11. package/dist/rules/no-redundant-type-constituents.js +10 -2
  12. package/dist/rules/no-redundant-type-constituents.js.map +1 -1
  13. package/dist/rules/no-restricted-types.js +1 -1
  14. package/dist/rules/no-restricted-types.js.map +1 -1
  15. package/dist/rules/no-unnecessary-template-expression.js +2 -2
  16. package/dist/rules/no-unnecessary-template-expression.js.map +1 -1
  17. package/dist/rules/no-unnecessary-type-assertion.js +44 -17
  18. package/dist/rules/no-unnecessary-type-assertion.js.map +1 -1
  19. package/dist/rules/no-unnecessary-type-parameters.js +1 -1
  20. package/dist/rules/no-unnecessary-type-parameters.js.map +1 -1
  21. package/dist/rules/no-unsafe-enum-comparison.js +23 -6
  22. package/dist/rules/no-unsafe-enum-comparison.js.map +1 -1
  23. package/dist/rules/prefer-includes.js +1 -1
  24. package/dist/rules/prefer-includes.js.map +1 -1
  25. package/dist/rules/require-await.js +80 -0
  26. package/dist/rules/require-await.js.map +1 -1
  27. package/dist/rules/use-unknown-in-catch-callback-variable.js +46 -85
  28. package/dist/rules/use-unknown-in-catch-callback-variable.js.map +1 -1
  29. package/dist/util/escapeRegExp.js +1 -1
  30. package/dist/util/escapeRegExp.js.map +1 -1
  31. package/dist/util/index.js +2 -0
  32. package/dist/util/index.js.map +1 -1
  33. package/dist/util/isStartOfExpressionStatement.js +23 -0
  34. package/dist/util/isStartOfExpressionStatement.js.map +1 -0
  35. package/dist/util/needsPrecedingSemiColon.js +98 -0
  36. package/dist/util/needsPrecedingSemiColon.js.map +1 -0
  37. package/docs/rules/use-unknown-in-catch-callback-variable.mdx +13 -4
  38. package/package.json +7 -7
@@ -0,0 +1,98 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.needsPrecedingSemicolon = needsPrecedingSemicolon;
4
+ const utils_1 = require("@typescript-eslint/utils");
5
+ const ast_utils_1 = require("@typescript-eslint/utils/ast-utils");
6
+ // The following is adapted from `eslint`'s source code.
7
+ // https://github.com/eslint/eslint/blob/3a4eaf921543b1cd5d1df4ea9dec02fab396af2a/lib/rules/utils/ast-utils.js#L1043-L1132
8
+ // Could be export { isStartOfExpressionStatement } from 'eslint/lib/rules/utils/ast-utils'
9
+ const BREAK_OR_CONTINUE = new Set([
10
+ utils_1.AST_NODE_TYPES.BreakStatement,
11
+ utils_1.AST_NODE_TYPES.ContinueStatement,
12
+ ]);
13
+ // Declaration types that must contain a string Literal node at the end.
14
+ const DECLARATIONS = new Set([
15
+ utils_1.AST_NODE_TYPES.ExportAllDeclaration,
16
+ utils_1.AST_NODE_TYPES.ExportNamedDeclaration,
17
+ utils_1.AST_NODE_TYPES.ImportDeclaration,
18
+ ]);
19
+ const IDENTIFIER_OR_KEYWORD = new Set([
20
+ utils_1.AST_NODE_TYPES.Identifier,
21
+ utils_1.AST_TOKEN_TYPES.Keyword,
22
+ ]);
23
+ // Keywords that can immediately precede an ExpressionStatement node, mapped to the their node types.
24
+ const NODE_TYPES_BY_KEYWORD = {
25
+ __proto__: null,
26
+ break: utils_1.AST_NODE_TYPES.BreakStatement,
27
+ continue: utils_1.AST_NODE_TYPES.ContinueStatement,
28
+ debugger: utils_1.AST_NODE_TYPES.DebuggerStatement,
29
+ do: utils_1.AST_NODE_TYPES.DoWhileStatement,
30
+ else: utils_1.AST_NODE_TYPES.IfStatement,
31
+ return: utils_1.AST_NODE_TYPES.ReturnStatement,
32
+ yield: utils_1.AST_NODE_TYPES.YieldExpression,
33
+ };
34
+ /*
35
+ * Before an opening parenthesis, postfix `++` and `--` always trigger ASI;
36
+ * the tokens `:`, `;`, `{` and `=>` don't expect a semicolon, as that would count as an empty statement.
37
+ */
38
+ const PUNCTUATORS = new Set([':', ';', '{', '=>', '++', '--']);
39
+ /*
40
+ * Statements that can contain an `ExpressionStatement` after a closing parenthesis.
41
+ * DoWhileStatement is an exception in that it always triggers ASI after the closing parenthesis.
42
+ */
43
+ const STATEMENTS = new Set([
44
+ utils_1.AST_NODE_TYPES.DoWhileStatement,
45
+ utils_1.AST_NODE_TYPES.ForInStatement,
46
+ utils_1.AST_NODE_TYPES.ForOfStatement,
47
+ utils_1.AST_NODE_TYPES.ForStatement,
48
+ utils_1.AST_NODE_TYPES.IfStatement,
49
+ utils_1.AST_NODE_TYPES.WhileStatement,
50
+ utils_1.AST_NODE_TYPES.WithStatement,
51
+ ]);
52
+ /**
53
+ * Determines whether an opening parenthesis `(`, bracket `[` or backtick ``` ` ``` needs to be preceded by a semicolon.
54
+ * This opening parenthesis or bracket should be at the start of an `ExpressionStatement`, a `MethodDefinition` or at
55
+ * the start of the body of an `ArrowFunctionExpression`.
56
+ * @param sourceCode The source code object.
57
+ * @param node A node at the position where an opening parenthesis or bracket will be inserted.
58
+ * @returns Whether a semicolon is required before the opening parenthesis or bracket.
59
+ */
60
+ function needsPrecedingSemicolon(sourceCode, node) {
61
+ const prevToken = sourceCode.getTokenBefore(node);
62
+ if (!prevToken ||
63
+ (prevToken.type === utils_1.AST_TOKEN_TYPES.Punctuator &&
64
+ PUNCTUATORS.has(prevToken.value))) {
65
+ return false;
66
+ }
67
+ const prevNode = sourceCode.getNodeByRangeIndex(prevToken.range[0]);
68
+ if (!prevNode) {
69
+ return false;
70
+ }
71
+ if ((0, ast_utils_1.isClosingParenToken)(prevToken)) {
72
+ return !STATEMENTS.has(prevNode.type);
73
+ }
74
+ if ((0, ast_utils_1.isClosingBraceToken)(prevToken)) {
75
+ return ((prevNode.type === utils_1.AST_NODE_TYPES.BlockStatement &&
76
+ prevNode.parent.type === utils_1.AST_NODE_TYPES.FunctionExpression &&
77
+ prevNode.parent.parent.type !== utils_1.AST_NODE_TYPES.MethodDefinition) ||
78
+ (prevNode.type === utils_1.AST_NODE_TYPES.ClassBody &&
79
+ prevNode.parent.type === utils_1.AST_NODE_TYPES.ClassExpression) ||
80
+ prevNode.type === utils_1.AST_NODE_TYPES.ObjectExpression);
81
+ }
82
+ if (!prevNode.parent) {
83
+ return false;
84
+ }
85
+ if (IDENTIFIER_OR_KEYWORD.has(prevToken.type)) {
86
+ if (BREAK_OR_CONTINUE.has(prevNode.parent.type)) {
87
+ return false;
88
+ }
89
+ const keyword = prevToken.value;
90
+ const nodeType = NODE_TYPES_BY_KEYWORD[keyword];
91
+ return prevNode.type !== nodeType;
92
+ }
93
+ if (prevToken.type === utils_1.AST_TOKEN_TYPES.String) {
94
+ return !DECLARATIONS.has(prevNode.parent.type);
95
+ }
96
+ return true;
97
+ }
98
+ //# sourceMappingURL=needsPrecedingSemiColon.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"needsPrecedingSemiColon.js","sourceRoot":"","sources":["../../src/util/needsPrecedingSemiColon.ts"],"names":[],"mappings":";;AAqEA,0DAuDC;AA3HD,oDAA2E;AAC3E,kEAG4C;AAG5C,wDAAwD;AACxD,0HAA0H;AAC1H,2FAA2F;AAE3F,MAAM,iBAAiB,GAAG,IAAI,GAAG,CAAC;IAChC,sBAAc,CAAC,cAAc;IAC7B,sBAAc,CAAC,iBAAiB;CACjC,CAAC,CAAC;AAEH,wEAAwE;AACxE,MAAM,YAAY,GAAG,IAAI,GAAG,CAAC;IAC3B,sBAAc,CAAC,oBAAoB;IACnC,sBAAc,CAAC,sBAAsB;IACrC,sBAAc,CAAC,iBAAiB;CACjC,CAAC,CAAC;AA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Ae,CAAC,MAAM,EAAE,CAAC;QAC9C,OAAO,CAAC,YAAY,CAAC,GAAG,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IACjD,CAAC;IAED,OAAO,IAAI,CAAC;AACd,CAAC"}
@@ -1,5 +1,5 @@
1
1
  ---
2
- description: 'Enforce typing arguments in `.catch()` callbacks as `unknown`.'
2
+ description: 'Enforce typing arguments in Promise rejection callbacks as `unknown`.'
3
3
  ---
4
4
 
5
5
  import Tabs from '@theme/Tabs';
@@ -9,7 +9,7 @@ import TabItem from '@theme/TabItem';
9
9
  >
10
10
  > See **https://typescript-eslint.io/rules/use-unknown-in-catch-callback-variable** for documentation.
11
11
 
12
- This rule enforces that you always use the `unknown` type for the parameter of a `Promise.prototype.catch()` callback.
12
+ This rule enforces that you always use the `unknown` type for the parameter of a Promise rejection callback.
13
13
 
14
14
  <Tabs>
15
15
  <TabItem value="❌ Incorrect">
@@ -26,6 +26,15 @@ Promise.reject(new Error('I will reject!')).catch((err: any) => {
26
26
  Promise.reject(new Error('I will reject!')).catch((err: Error) => {
27
27
  console.log(err);
28
28
  });
29
+
30
+ Promise.reject(new Error('I will reject!')).then(
31
+ result => {
32
+ console.log(result);
33
+ },
34
+ err => {
35
+ console.log(err);
36
+ },
37
+ );
29
38
  ```
30
39
 
31
40
  </TabItem>
@@ -70,7 +79,7 @@ Promise.reject(x).catch((err: unknown) => {
70
79
  ```
71
80
 
72
81
  :::info
73
- There is actually a way to have the `catch()` callback variable use the `unknown` type _without_ an explicit type annotation at the call sites, but it has the drawback that it involves overriding global type declarations.
82
+ There is actually a way to have the `catch()` and `then()` callback variables use the `unknown` type _without_ an explicit type annotation at the call sites, but it has the drawback that it involves overriding global type declarations.
74
83
  For example, the library [better-TypeScript-lib](https://github.com/uhyo/better-typescript-lib) sets this up globally for your project (see [the relevant lines in the better-TypeScript-lib source code](https://github.com/uhyo/better-typescript-lib/blob/c294e177d1cc2b1d1803febf8192a4c83a1fe028/lib/lib.es5.d.ts#L635) for details on how).
75
84
 
76
85
  For further reading on this, you may also want to look into
@@ -81,4 +90,4 @@ For further reading on this, you may also want to look into
81
90
 
82
91
  If your codebase is not yet able to enable `useUnknownInCatchVariables`, it likely would be similarly difficult to enable this rule.
83
92
 
84
- If you have modified the global type declarations in order to make `catch()` callbacks use the `unknown` type without an explicit type annotation, you do not need this rule.
93
+ If you have modified the global type declarations in order to make `then()` and `catch()` callbacks use the `unknown` type without an explicit type annotation, you do not need this rule.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@typescript-eslint/eslint-plugin",
3
- "version": "8.1.1-alpha.10",
3
+ "version": "8.1.1-alpha.11",
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.1.1-alpha.10",
64
- "@typescript-eslint/type-utils": "8.1.1-alpha.10",
65
- "@typescript-eslint/utils": "8.1.1-alpha.10",
66
- "@typescript-eslint/visitor-keys": "8.1.1-alpha.10",
63
+ "@typescript-eslint/scope-manager": "8.1.1-alpha.11",
64
+ "@typescript-eslint/type-utils": "8.1.1-alpha.11",
65
+ "@typescript-eslint/utils": "8.1.1-alpha.11",
66
+ "@typescript-eslint/visitor-keys": "8.1.1-alpha.11",
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.1.1-alpha.10",
78
- "@typescript-eslint/rule-tester": "8.1.1-alpha.10",
77
+ "@typescript-eslint/rule-schema-to-typescript-types": "8.1.1-alpha.11",
78
+ "@typescript-eslint/rule-tester": "8.1.1-alpha.11",
79
79
  "ajv": "^6.12.6",
80
80
  "cross-env": "^7.0.3",
81
81
  "cross-fetch": "*",