@checkdigit/eslint-plugin 7.13.0-PR.117-6eb3 → 7.13.0-PR.117-c94d

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,7 +1,8 @@
1
1
  // src/no-type-assertion-as.ts
2
- import { ESLintUtils } from "@typescript-eslint/utils";
2
+ import { AST_NODE_TYPES, ESLintUtils } from "@typescript-eslint/utils";
3
3
  var ruleId = "no-as-type-assertion";
4
4
  var NO_AS_TYPE_ASSERTION = "NO_AS_TYPE_ASSERTION";
5
+ var NO_UNKNOWN_TYPE_ASSERTION = "NO_UNKNOWN_TYPE_ASSERTION";
5
6
  var createRule = ESLintUtils.RuleCreator((name) => name);
6
7
  var rule = createRule({
7
8
  name: ruleId,
@@ -12,7 +13,8 @@ var rule = createRule({
12
13
  },
13
14
  schema: [],
14
15
  messages: {
15
- [NO_AS_TYPE_ASSERTION]: "Avoid using `as` type assertions. Use `satisfies` instead."
16
+ [NO_AS_TYPE_ASSERTION]: "Avoid using `as` type assertions. Use `satisfies` instead.",
17
+ [NO_UNKNOWN_TYPE_ASSERTION]: "Avoid using `unknown` type assertions."
16
18
  },
17
19
  fixable: "code"
18
20
  },
@@ -20,16 +22,24 @@ var rule = createRule({
20
22
  create(context) {
21
23
  return {
22
24
  TSAsExpression(node) {
23
- context.report({
24
- node,
25
- messageId: NO_AS_TYPE_ASSERTION,
26
- fix: (fixer) => {
27
- const sourceCode = context.sourceCode;
28
- const typeAnnotation = sourceCode.getText(node.typeAnnotation);
29
- const expression = sourceCode.getText(node.expression);
30
- return fixer.replaceText(node, `${expression} satisfies ${typeAnnotation}`);
25
+ const sourceCode = context.sourceCode;
26
+ const typeAnnotation = sourceCode.getText(node.typeAnnotation);
27
+ const expression = sourceCode.getText(node.expression);
28
+ const isUnknownUnionOrIntersection = (node.typeAnnotation.type === AST_NODE_TYPES.TSUnionType || node.typeAnnotation.type === AST_NODE_TYPES.TSIntersectionType) && node.typeAnnotation.types.some((type) => type.type === AST_NODE_TYPES.TSUnknownKeyword);
29
+ if (node.typeAnnotation.type === AST_NODE_TYPES.TSUnknownKeyword || node.expression.type === AST_NODE_TYPES.TSAsExpression && node.expression.typeAnnotation.type === AST_NODE_TYPES.TSUnknownKeyword || isUnknownUnionOrIntersection) {
30
+ if (node.parent.type !== AST_NODE_TYPES.TSAsExpression) {
31
+ context.report({
32
+ node,
33
+ messageId: NO_UNKNOWN_TYPE_ASSERTION
34
+ });
31
35
  }
32
- });
36
+ } else {
37
+ context.report({
38
+ node,
39
+ messageId: NO_AS_TYPE_ASSERTION,
40
+ fix: (fixer) => fixer.replaceText(node, `${expression} satisfies ${typeAnnotation}`)
41
+ });
42
+ }
33
43
  }
34
44
  };
35
45
  }
@@ -39,4 +49,4 @@ export {
39
49
  no_type_assertion_as_default as default,
40
50
  ruleId
41
51
  };
42
- //# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsiLi4vc3JjL25vLXR5cGUtYXNzZXJ0aW9uLWFzLnRzIl0sCiAgIm1hcHBpbmdzIjogIjtBQVFBLFNBQVMsbUJBQTZCO0FBRS9CLElBQU0sU0FBUztBQUN0QixJQUFNLHVCQUF1QjtBQUU3QixJQUFNLGFBQWEsWUFBWSxZQUFZLENBQUMsU0FBUyxJQUFJO0FBRXpELElBQU0sT0FBNEQsV0FBVztBQUFBLEVBQzNFLE1BQU07QUFBQSxFQUNOLE1BQU07QUFBQSxJQUNKLE1BQU07QUFBQSxJQUNOLE1BQU07QUFBQSxNQUNKLGFBQWE7QUFBQSxJQUNmO0FBQUEsSUFDQSxRQUFRLENBQUM7QUFBQSxJQUNULFVBQVU7QUFBQSxNQUNSLENBQUMsb0JBQW9CLEdBQUc7QUFBQSxJQUMxQjtBQUFBLElBQ0EsU0FBUztBQUFBLEVBQ1g7QUFBQSxFQUNBLGdCQUFnQixDQUFDO0FBQUEsRUFDakIsT0FBTyxTQUFTO0FBQ2QsV0FBTztB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-as-type-assertion";
3
3
  declare const NO_AS_TYPE_ASSERTION = "NO_AS_TYPE_ASSERTION";
4
- declare const rule: ESLintUtils.RuleModule<typeof NO_AS_TYPE_ASSERTION>;
4
+ declare const NO_UNKNOWN_TYPE_ASSERTION = "NO_UNKNOWN_TYPE_ASSERTION";
5
+ declare const rule: ESLintUtils.RuleModule<typeof NO_AS_TYPE_ASSERTION | typeof NO_UNKNOWN_TYPE_ASSERTION>;
5
6
  export default rule;
package/package.json CHANGED
@@ -1 +1 @@
1
- {"name":"@checkdigit/eslint-plugin","version":"7.13.0-PR.117-6eb3","description":"Check Digit eslint plugins","keywords":["eslint","eslintplugin"],"homepage":"https://github.com/checkdigit/eslint-plugin#readme","bugs":{"url":"https://github.com/checkdigit/eslint-plugin/issues"},"repository":{"type":"git","url":"https://github.com/checkdigit/eslint-plugin"},"license":"MIT","author":"Check Digit, LLC","sideEffects":false,"type":"module","exports":{".":{"types":"./dist-types/index.d.ts","import":"./dist-mjs/index.mjs","default":"./dist-mjs/index.mjs"}},"files":["src","dist-types","dist-mjs","!src/**/test/**","!src/**/*.test.ts","!src/**/*.spec.ts","!dist-types/**/test/**","!dist-types/**/*.test.d.ts","!dist-types/**/*.spec.d.ts","!dist-mjs/**/test/**","!dist-mjs/**/*.test.mjs","!dist-mjs/**/*.spec.mjs","SECURITY.md"],"scripts":{"build:dist-mjs":"rimraf dist-mjs && npx builder --type=module --sourceMap --outDir=dist-mjs && node dist-mjs/index.mjs","build:dist-types":"rimraf dist-types && npx builder --type=types --outDir=dist-types","ci:compile":"tsc --noEmit","ci:coverage":"NODE_OPTIONS=\"--disable-warning ExperimentalWarning --experimental-vm-modules\" jest --coverage=true","ci:lint":"npm run lint","ci:style":"npm run prettier","ci:test":"NODE_OPTIONS=\"--disable-warning ExperimentalWarning --experimental-vm-modules\" jest --coverage=false","lint":"eslint --max-warnings 0 .","lint:fix":"eslint --max-warnings 0 --fix .","prepare":"","prepublishOnly":"npm run build:dist-types && npm run build:dist-mjs","prettier":"prettier --ignore-path .gitignore --list-different .","prettier:fix":"prettier --ignore-path .gitignore --write .","test":"npm run ci:compile && npm run ci:test && npm run ci:lint && npm run ci:style"},"prettier":"@checkdigit/prettier-config","jest":{"preset":"@checkdigit/jest-config"},"dependencies":{"@typescript-eslint/type-utils":"^8.23.0","@typescript-eslint/utils":"^8.23.0","http-status-codes":"^2.3.0","ts-api-utils":"^2.0.1"},"devDependencies":{"@checkdigit/jest-config":"^6.0.2","@checkdigit/prettier-config":"^6.1.0","@checkdigit/typescript-config":"^9.0.0","@eslint/js":"^9.19.0","@types/eslint":"^9.6.1","@types/eslint-config-prettier":"^6.11.3","@typescript-eslint/parser":"^8.23.0","@typescript-eslint/rule-tester":"^8.23.0","eslint":"^9.19.0","eslint-config-prettier":"^10.0.1","eslint-import-resolver-typescript":"^3.7.0","eslint-plugin-eslint-plugin":"^6.4.0","eslint-plugin-import":"^2.31.0","eslint-plugin-no-only-tests":"^3.3.0","eslint-plugin-no-secrets":"^2.2.1","eslint-plugin-node":"^11.1.0","eslint-plugin-sonarjs":"1.0.4","rimraf":"^6.0.1","typescript-eslint":"^8.23.0"},"peerDependencies":{"eslint":">=9 <10"},"engines":{"node":">=20.17"}}
1
+ {"name":"@checkdigit/eslint-plugin","version":"7.13.0-PR.117-c94d","description":"Check Digit eslint plugins","keywords":["eslint","eslintplugin"],"homepage":"https://github.com/checkdigit/eslint-plugin#readme","bugs":{"url":"https://github.com/checkdigit/eslint-plugin/issues"},"repository":{"type":"git","url":"https://github.com/checkdigit/eslint-plugin"},"license":"MIT","author":"Check Digit, LLC","sideEffects":false,"type":"module","exports":{".":{"types":"./dist-types/index.d.ts","import":"./dist-mjs/index.mjs","default":"./dist-mjs/index.mjs"}},"files":["src","dist-types","dist-mjs","!src/**/test/**","!src/**/*.test.ts","!src/**/*.spec.ts","!dist-types/**/test/**","!dist-types/**/*.test.d.ts","!dist-types/**/*.spec.d.ts","!dist-mjs/**/test/**","!dist-mjs/**/*.test.mjs","!dist-mjs/**/*.spec.mjs","SECURITY.md"],"scripts":{"build:dist-mjs":"rimraf dist-mjs && npx builder --type=module --sourceMap --outDir=dist-mjs && node dist-mjs/index.mjs","build:dist-types":"rimraf dist-types && npx builder --type=types --outDir=dist-types","ci:compile":"tsc --noEmit","ci:coverage":"NODE_OPTIONS=\"--disable-warning ExperimentalWarning --experimental-vm-modules\" jest --coverage=true","ci:lint":"npm run lint","ci:style":"npm run prettier","ci:test":"NODE_OPTIONS=\"--disable-warning ExperimentalWarning --experimental-vm-modules\" jest --coverage=false","lint":"eslint --max-warnings 0 .","lint:fix":"eslint --max-warnings 0 --fix .","prepare":"","prepublishOnly":"npm run build:dist-types && npm run build:dist-mjs","prettier":"prettier --ignore-path .gitignore --list-different .","prettier:fix":"prettier --ignore-path .gitignore --write .","test":"npm run ci:compile && npm run ci:test && npm run ci:lint && npm run ci:style"},"prettier":"@checkdigit/prettier-config","jest":{"preset":"@checkdigit/jest-config"},"dependencies":{"@typescript-eslint/type-utils":"^8.23.0","@typescript-eslint/utils":"^8.23.0","http-status-codes":"^2.3.0","ts-api-utils":"^2.0.1"},"devDependencies":{"@checkdigit/jest-config":"^6.0.2","@checkdigit/prettier-config":"^6.1.0","@checkdigit/typescript-config":"^9.0.0","@eslint/js":"^9.19.0","@types/eslint":"^9.6.1","@types/eslint-config-prettier":"^6.11.3","@typescript-eslint/parser":"^8.23.0","@typescript-eslint/rule-tester":"^8.23.0","eslint":"^9.19.0","eslint-config-prettier":"^10.0.1","eslint-import-resolver-typescript":"^3.7.0","eslint-plugin-eslint-plugin":"^6.4.0","eslint-plugin-import":"^2.31.0","eslint-plugin-no-only-tests":"^3.3.0","eslint-plugin-no-secrets":"^2.2.1","eslint-plugin-node":"^11.1.0","eslint-plugin-sonarjs":"1.0.4","rimraf":"^6.0.1","typescript-eslint":"^8.23.0"},"peerDependencies":{"eslint":">=9 <10"},"engines":{"node":">=20.17"}}
@@ -6,14 +6,15 @@
6
6
  * This code is licensed under the MIT license (see LICENSE.txt for details).
7
7
  */
8
8
 
9
- import { ESLintUtils, TSESTree } from '@typescript-eslint/utils';
9
+ import { AST_NODE_TYPES, ESLintUtils, TSESTree } from '@typescript-eslint/utils';
10
10
 
11
11
  export const ruleId = 'no-as-type-assertion';
12
12
  const NO_AS_TYPE_ASSERTION = 'NO_AS_TYPE_ASSERTION';
13
+ const NO_UNKNOWN_TYPE_ASSERTION = 'NO_UNKNOWN_TYPE_ASSERTION';
13
14
 
14
15
  const createRule = ESLintUtils.RuleCreator((name) => name);
15
16
 
16
- const rule: ESLintUtils.RuleModule<typeof NO_AS_TYPE_ASSERTION> = createRule({
17
+ const rule: ESLintUtils.RuleModule<typeof NO_AS_TYPE_ASSERTION | typeof NO_UNKNOWN_TYPE_ASSERTION> = createRule({
17
18
  name: ruleId,
18
19
  meta: {
19
20
  type: 'suggestion',
@@ -23,6 +24,7 @@ const rule: ESLintUtils.RuleModule<typeof NO_AS_TYPE_ASSERTION> = createRule({
23
24
  schema: [],
24
25
  messages: {
25
26
  [NO_AS_TYPE_ASSERTION]: 'Avoid using `as` type assertions. Use `satisfies` instead.',
27
+ [NO_UNKNOWN_TYPE_ASSERTION]: 'Avoid using `unknown` type assertions.',
26
28
  },
27
29
  fixable: 'code',
28
30
  },
@@ -30,16 +32,34 @@ const rule: ESLintUtils.RuleModule<typeof NO_AS_TYPE_ASSERTION> = createRule({
30
32
  create(context) {
31
33
  return {
32
34
  TSAsExpression(node: TSESTree.TSAsExpression) {
33
- context.report({
34
- node,
35
- messageId: NO_AS_TYPE_ASSERTION,
36
- fix: (fixer) => {
37
- const sourceCode = context.sourceCode;
38
- const typeAnnotation = sourceCode.getText(node.typeAnnotation);
39
- const expression = sourceCode.getText(node.expression);
40
- return fixer.replaceText(node, `${expression} satisfies ${typeAnnotation}`);
41
- },
42
- });
35
+ const sourceCode = context.sourceCode;
36
+ const typeAnnotation = sourceCode.getText(node.typeAnnotation);
37
+ const expression = sourceCode.getText(node.expression);
38
+
39
+ const isUnknownUnionOrIntersection =
40
+ (node.typeAnnotation.type === AST_NODE_TYPES.TSUnionType ||
41
+ node.typeAnnotation.type === AST_NODE_TYPES.TSIntersectionType) &&
42
+ node.typeAnnotation.types.some((type) => type.type === AST_NODE_TYPES.TSUnknownKeyword);
43
+
44
+ if (
45
+ node.typeAnnotation.type === AST_NODE_TYPES.TSUnknownKeyword ||
46
+ (node.expression.type === AST_NODE_TYPES.TSAsExpression &&
47
+ node.expression.typeAnnotation.type === AST_NODE_TYPES.TSUnknownKeyword) ||
48
+ isUnknownUnionOrIntersection
49
+ ) {
50
+ if (node.parent.type !== AST_NODE_TYPES.TSAsExpression) {
51
+ context.report({
52
+ node,
53
+ messageId: NO_UNKNOWN_TYPE_ASSERTION,
54
+ });
55
+ }
56
+ } else {
57
+ context.report({
58
+ node,
59
+ messageId: NO_AS_TYPE_ASSERTION,
60
+ fix: (fixer) => fixer.replaceText(node, `${expression} satisfies ${typeAnnotation}`),
61
+ });
62
+ }
43
63
  },
44
64
  };
45
65
  },