@definitelytyped/dtslint 0.0.166 → 0.0.167

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 (109) hide show
  1. package/dist/lint.js +3 -1
  2. package/dist/lint.js.map +1 -1
  3. package/dist/rules/dt-header.d.ts +2 -0
  4. package/dist/rules/dt-header.js +62 -0
  5. package/dist/rules/dt-header.js.map +1 -0
  6. package/dist/rules/export-just-namespace.d.ts +2 -0
  7. package/dist/rules/export-just-namespace.js +70 -0
  8. package/dist/rules/export-just-namespace.js.map +1 -0
  9. package/dist/rules/no-any-union.d.ts +4 -0
  10. package/dist/rules/no-any-union.js +34 -0
  11. package/dist/rules/no-any-union.js.map +1 -0
  12. package/dist/rules/no-bad-reference.d.ts +2 -0
  13. package/dist/rules/no-bad-reference.js +55 -0
  14. package/dist/rules/no-bad-reference.js.map +1 -0
  15. package/dist/rules/no-const-enum.d.ts +4 -0
  16. package/dist/rules/no-const-enum.js +30 -0
  17. package/dist/rules/no-const-enum.js.map +1 -0
  18. package/dist/rules/no-dead-reference.d.ts +2 -0
  19. package/dist/rules/no-dead-reference.js +44 -0
  20. package/dist/rules/no-dead-reference.js.map +1 -0
  21. package/dist/rules/no-declare-current-package.d.ts +4 -0
  22. package/dist/rules/no-declare-current-package.js +43 -0
  23. package/dist/rules/no-declare-current-package.js.map +1 -0
  24. package/dist/rules/no-import-default-of-export-equals.d.ts +4 -0
  25. package/dist/rules/no-import-default-of-export-equals.js +87 -0
  26. package/dist/rules/no-import-default-of-export-equals.js.map +1 -0
  27. package/dist/rules/no-outside-dependencies.d.ts +2 -0
  28. package/dist/rules/no-outside-dependencies.js +41 -0
  29. package/dist/rules/no-outside-dependencies.js.map +1 -0
  30. package/dist/rules/no-self-import.d.ts +4 -0
  31. package/dist/rules/no-self-import.js +38 -0
  32. package/dist/rules/no-self-import.js.map +1 -0
  33. package/dist/rules/no-single-element-tuple-type.d.ts +5 -0
  34. package/dist/rules/no-single-element-tuple-type.js +30 -0
  35. package/dist/rules/no-single-element-tuple-type.js.map +1 -0
  36. package/dist/rules/no-unnecessary-generics.d.ts +8 -0
  37. package/dist/rules/no-unnecessary-generics.js +135 -0
  38. package/dist/rules/no-unnecessary-generics.js.map +1 -0
  39. package/dist/rules/no-useless-files.d.ts +2 -0
  40. package/dist/rules/no-useless-files.js +53 -0
  41. package/dist/rules/no-useless-files.js.map +1 -0
  42. package/dist/rules/prefer-declare-function.d.ts +5 -0
  43. package/dist/rules/prefer-declare-function.js +35 -0
  44. package/dist/rules/prefer-declare-function.js.map +1 -0
  45. package/dist/rules/redundant-undefined.d.ts +4 -0
  46. package/dist/rules/redundant-undefined.js +53 -0
  47. package/dist/rules/redundant-undefined.js.map +1 -0
  48. package/dist/rules/trim-file.d.ts +2 -0
  49. package/dist/rules/trim-file.js +43 -0
  50. package/dist/rules/trim-file.js.map +1 -0
  51. package/dist/util.d.ts +4 -0
  52. package/dist/util.js +23 -1
  53. package/dist/util.js.map +1 -1
  54. package/docs/dt-header.md +88 -0
  55. package/docs/export-just-namespace.md +29 -0
  56. package/docs/no-any-union.md +27 -0
  57. package/docs/no-bad-reference.md +28 -0
  58. package/docs/no-const-enum.md +16 -0
  59. package/docs/no-dead-reference.md +17 -0
  60. package/docs/no-declare-current-package.md +35 -0
  61. package/docs/no-import-default-of-export-equals.md +22 -0
  62. package/docs/no-outside-dependencies.md +23 -0
  63. package/docs/no-self-import.md +27 -0
  64. package/docs/no-single-element-tuple-type.md +15 -0
  65. package/docs/no-unnecessary-generics.md +69 -0
  66. package/docs/no-useless-files.md +14 -0
  67. package/docs/prefer-declare-function.md +15 -0
  68. package/docs/redundant-undefined.md +15 -0
  69. package/docs/trim-file.md +17 -0
  70. package/package.json +6 -6
  71. package/src/lint.ts +3 -1
  72. package/src/rules/dt-header.ts +74 -0
  73. package/src/rules/export-just-namespace.ts +83 -0
  74. package/src/rules/no-any-union.ts +34 -0
  75. package/src/rules/no-bad-reference.ts +62 -0
  76. package/src/rules/no-const-enum.ts +30 -0
  77. package/src/rules/no-dead-reference.ts +46 -0
  78. package/src/rules/no-declare-current-package.ts +45 -0
  79. package/src/rules/no-import-default-of-export-equals.ts +68 -0
  80. package/src/rules/no-outside-dependencies.ts +42 -0
  81. package/src/rules/no-self-import.ts +40 -0
  82. package/src/rules/no-single-element-tuple-type.ts +31 -0
  83. package/src/rules/no-unnecessary-generics.ts +126 -0
  84. package/src/rules/no-useless-files.ts +58 -0
  85. package/src/rules/prefer-declare-function.ts +37 -0
  86. package/src/rules/redundant-undefined.ts +62 -0
  87. package/src/rules/trim-file.ts +45 -0
  88. package/src/util.ts +25 -0
  89. package/test/dt-header.test.ts +189 -0
  90. package/test/export-just-namespace.test.ts +70 -0
  91. package/test/no-any-union.test.ts +22 -0
  92. package/test/no-bad-reference.test.ts +66 -0
  93. package/test/no-const-enum.test.ts +22 -0
  94. package/test/no-dead-reference.test.ts +66 -0
  95. package/test/no-declare-current-package.test.ts +61 -0
  96. package/test/no-import-default-of-export-equals.test.ts +49 -0
  97. package/test/no-self-import.test.ts +47 -0
  98. package/test/no-single-element-tuple-type.test.ts +28 -0
  99. package/test/no-unnecessary-generics.test.ts +152 -0
  100. package/test/no-useless-files.test.ts +42 -0
  101. package/test/prefer-declare-function.test.ts +66 -0
  102. package/test/redundant-undefined.test.ts +39 -0
  103. package/test/trim-file.test.ts +46 -0
  104. package/test/tsconfig.json +10 -0
  105. package/test/tsconfig.no-declare-current-package.json +11 -0
  106. package/test/tsconfig.no-declare-current-package2.json +11 -0
  107. package/test/tsconfig.no-import-default-of-export-equals.json +11 -0
  108. package/test/tsconfig.no-self-import.json +7 -0
  109. package/tsconfig.tsbuildinfo +1 -1
@@ -0,0 +1,27 @@
1
+ # no-any-union
2
+
3
+ Forbids to include `any` in a union. When `any` is used in a union type, the resulting type is still `any`.
4
+
5
+ **Bad**:
6
+
7
+ ```ts
8
+ function f(x: string | any): void;
9
+ ```
10
+
11
+ **Good**:
12
+
13
+ ```ts
14
+ function f(x: string): void;
15
+ ```
16
+
17
+ Or:
18
+ ```ts
19
+ function f(x: any): void;
20
+ ```
21
+
22
+ Or:
23
+ ```ts
24
+ function f(x: string | object): void;
25
+ ```
26
+
27
+ While the `string` portion of this type annotation may _look_ useful, it in fact offers no additional typechecking over simply using `any`.
@@ -0,0 +1,28 @@
1
+ # no-bad-reference
2
+
3
+ (This rule is specific to DefinitelyTyped.)
4
+ Avoid using `<reference path>`.
5
+
6
+ **Bad**:
7
+
8
+ ```ts
9
+ /// <reference path="../node_modules/@types/foo/index.d.ts" />
10
+ import * as foo from "foo";
11
+ ```
12
+
13
+ **Good**:
14
+
15
+ If "foo" is written in external module style (see `no-single-declare-module`), the import alone should work thanks to [module resolution](http://www.typescriptlang.org/docs/handbook/module-resolution.html):
16
+
17
+ ```ts
18
+ // TypeScript will look for a definition for "foo" using module resolution
19
+ import * as foo from "foo";
20
+ ```
21
+
22
+ If not, use `<reference types>` instead:
23
+
24
+ ```ts
25
+ /// <reference types="foo" />
26
+ ```
27
+
28
+ The only time `<reference path>` should be necessary if for global (not module) libraries that are separated into multiple files; the index file must include references to the others to bring them into the compilation.
@@ -0,0 +1,16 @@
1
+ # no-const-enum
2
+
3
+ Avoid using `const enum`s. These can't be used by JavaScript users or by TypeScript users with [`--isolatedModules`](https://www.typescriptlang.org/docs/handbook/compiler-options.html) enabled.
4
+
5
+ **Bad**:
6
+
7
+ ```ts
8
+ const enum Bit { Off, On }
9
+ export function f(b: Bit): void;
10
+ ```
11
+
12
+ **Good**:
13
+
14
+ ```ts
15
+ export function f(b: 0 | 1): void;
16
+ ```
@@ -0,0 +1,17 @@
1
+ # no-dead-reference
2
+
3
+ A `<reference>` comment should go at the top of a file -- otherwise it is just a normal comment.
4
+
5
+ **Bad**:
6
+
7
+ ```ts
8
+ console.log("Hello world!");
9
+ /// <reference types="jquery" />
10
+ ```
11
+
12
+ **Good**:
13
+
14
+ ```ts
15
+ /// <reference types="jquery" />
16
+ console.log("Hello world!");
17
+ ```
@@ -0,0 +1,35 @@
1
+ # no-declare-current-package
2
+
3
+ Avoid using `declare module`, and prefer to declare module contents in a file.
4
+
5
+ **Bad**:
6
+
7
+ ```ts
8
+ // foo/index.d.ts
9
+ declare module "foo" {
10
+ export const x = 0;
11
+ }
12
+ ```
13
+
14
+ **Good**:
15
+
16
+ ```ts
17
+ // foo/index.d.ts
18
+ export const x = 0;
19
+ ```
20
+
21
+ **Bad**:
22
+
23
+ ```ts
24
+ // foo/index.d.ts
25
+ declare module "foo/bar" {
26
+ export const x = 0;
27
+ }
28
+ ```
29
+
30
+ **Good**:
31
+
32
+ ```ts
33
+ // foo/bar.d.ts
34
+ export const x = 0;
35
+ ```
@@ -0,0 +1,22 @@
1
+ # no-import-default-of-export-equals
2
+
3
+ Don't use a default import of a package that uses `export =`.
4
+ Users who do not have `--allowSyntheticDefaultExports` or `--esModuleInterop` will get different behavior.
5
+ This rule only applies to definition files -- for test files you can use a default import if you prefer.
6
+
7
+ **Bad**:
8
+
9
+ ```ts
10
+ // foo/index.d.ts
11
+ declare interface I {}
12
+ export = I;
13
+
14
+ // bar/index.d.ts
15
+ import I from "foo";
16
+ ```
17
+
18
+ **Good**:
19
+
20
+ ```ts
21
+ import I = require("foo");
22
+ ```
@@ -0,0 +1,23 @@
1
+ # no-outside-dependencies
2
+
3
+ Don't import from `DefinitelyTyped/node_modules`.
4
+
5
+ **Bad**:
6
+
7
+ ```ts
8
+ import * as x from "x";
9
+ // where 'x' is defined only in `DefinitelyTyped/node_modules`
10
+ ```
11
+
12
+ **Good**:
13
+
14
+ Add a `package.json`:
15
+
16
+ ```ts
17
+ {
18
+ "private": true,
19
+ "dependencies": {
20
+ "x": "^1.2.3"
21
+ }
22
+ }
23
+ ```
@@ -0,0 +1,27 @@
1
+ # no-self-import
2
+
3
+ A package should not import components of itself using a globally-qualified name; it should use relative imports instead.
4
+
5
+ **Bad**:
6
+
7
+ ```ts
8
+ import foo from "this-package/foo.d.ts";
9
+ ```
10
+
11
+ **Good**:
12
+
13
+ ```ts
14
+ import foo from "./foo.d.ts";
15
+ ```
16
+
17
+ **Bad**:
18
+
19
+ ```ts
20
+ import myself from "this-package";
21
+ ```
22
+
23
+ **Good**:
24
+
25
+ ```ts
26
+ import myself from ".";
27
+ ```
@@ -0,0 +1,15 @@
1
+ # no-single-element-tuple-type
2
+
3
+ Some users mistakenly write `[T]` when then intend to write an array type `T[]`.
4
+
5
+ **Bad**:
6
+
7
+ ```ts
8
+ export const x: [T];
9
+ ```
10
+
11
+ **Good**:
12
+
13
+ ```ts
14
+ export const x: T[];
15
+ ```
@@ -0,0 +1,69 @@
1
+ # no-unnecessary-generics
2
+
3
+ Forbids a function to use a generic type parameter only once.
4
+ Generic type parameters allow you to relate the type of one thing to another;
5
+ if they are used only once, they can be replaced with their type constraint.
6
+
7
+ **Bad**:
8
+
9
+ ```ts
10
+ function logAnything<T>(x: T): void;
11
+ ```
12
+
13
+ **Good**:
14
+
15
+ ```ts
16
+ function logAnything(x: any): void;
17
+ ```
18
+
19
+ ---
20
+
21
+ **Bad**:
22
+
23
+ ```ts
24
+ function useLogger<T extends Logger>(logger: T): void;
25
+ ```
26
+
27
+ **Good**:
28
+
29
+ ```ts
30
+ function useLogger(logger: Logger): void;
31
+ ```
32
+
33
+ ---
34
+
35
+ **Bad**:
36
+
37
+ ```ts
38
+ function clear<T>(array: T[]): void;
39
+ ```
40
+
41
+ **Good**:
42
+
43
+ ```ts
44
+ function clear(array: any[]): void;
45
+ ```
46
+
47
+ ---
48
+
49
+ `getMeAT<T>(): T`:
50
+ If a type parameter does not appear in the types of any parameters, you don't really have a generic function, you just have a disguised type assertion.
51
+ Prefer to use a real type assertion, e.g. `getMeAT() as number`.
52
+ Example where a type parameter is acceptable: `function id<T>(value: T): T;`.
53
+ Example where it is not acceptable: `function parseJson<T>(json: string): T;`.
54
+ Exception: `new Map<string, number>()` is OK.
55
+
56
+ **Bad**:
57
+
58
+ ```ts
59
+ function parse<T>(): T;
60
+ const x = parse<number>();
61
+ ```
62
+
63
+ **Good**:
64
+
65
+
66
+ ```ts
67
+ function parse(): {};
68
+ const x = parse() as number;
69
+ ```
@@ -0,0 +1,14 @@
1
+ # no-useless-files
2
+
3
+ Don't include empty files.
4
+
5
+ **Bad**:
6
+
7
+ ```ts
8
+ ```
9
+
10
+ **Good**:
11
+
12
+ ```ts
13
+ export function something(): void;
14
+ ```
@@ -0,0 +1,15 @@
1
+ # prefer-declare-function
2
+
3
+ Prefer to declare a function using the `function` keyword instead of a variable of function type.
4
+
5
+ **Bad**:
6
+
7
+ ```ts
8
+ export const f: () => number;
9
+ ```
10
+
11
+ **Good**:
12
+
13
+ ```ts
14
+ export function f(): number;
15
+ ```
@@ -0,0 +1,15 @@
1
+ # redundant-undefined
2
+
3
+ Avoid explicitly specifying `undefined` as a type for a parameter which is already optional.
4
+
5
+ **Bad**:
6
+
7
+ ```ts
8
+ function f(s?: string | undefined): void {}
9
+ ```
10
+
11
+ **Good**:
12
+
13
+ ```ts
14
+ function f(s?: string): void {}
15
+ ```
@@ -0,0 +1,17 @@
1
+ # trim-file
2
+
3
+ Don't include blank lines at the beginning or end of a file.
4
+
5
+ **Bad**:
6
+
7
+ ```ts
8
+
9
+ export function f(): number;
10
+
11
+ ```
12
+
13
+ **Good**:
14
+
15
+ ```ts
16
+ export function f(): number;
17
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@definitelytyped/dtslint",
3
- "version": "0.0.166",
3
+ "version": "0.0.167",
4
4
  "description": "Runs tests on TypeScript definition files",
5
5
  "main": "./dist/index.js",
6
6
  "bin": "./dist/index.js",
@@ -22,10 +22,10 @@
22
22
  "test": "../../node_modules/.bin/jest --config ../../jest.config.js packages/dtslint"
23
23
  },
24
24
  "dependencies": {
25
- "@definitelytyped/dts-critic": "^0.0.166",
26
- "@definitelytyped/header-parser": "^0.0.166",
27
- "@definitelytyped/typescript-versions": "^0.0.166",
28
- "@definitelytyped/utils": "^0.0.166",
25
+ "@definitelytyped/dts-critic": "^0.0.167",
26
+ "@definitelytyped/header-parser": "^0.0.167",
27
+ "@definitelytyped/typescript-versions": "^0.0.167",
28
+ "@definitelytyped/utils": "^0.0.167",
29
29
  "@typescript-eslint/eslint-plugin": "^5.55.0",
30
30
  "@typescript-eslint/parser": "^5.55.0",
31
31
  "@typescript-eslint/types": "^5.56.0",
@@ -52,5 +52,5 @@
52
52
  "node": ">=10.0.0"
53
53
  },
54
54
  "license": "MIT",
55
- "gitHead": "e350e85d1fafe538f7bbe2a700639aaaa4e90814"
55
+ "gitHead": "e12bf210f4a28f20a84052fc049baccc5673d49a"
56
56
  }
package/src/lint.ts CHANGED
@@ -67,7 +67,9 @@ export async function lint(
67
67
  if (!expectOnly) {
68
68
  const cwd = process.cwd();
69
69
  process.chdir(dirPath);
70
- const eslint = new ESLint();
70
+ const eslint = new ESLint({
71
+ rulePaths: [joinPaths(__dirname, "./rules/")],
72
+ });
71
73
  const formatter = await eslint.loadFormatter("stylish");
72
74
  const eresults = await eslint.lintFiles(esfiles);
73
75
  output += formatter.format(eresults);
@@ -0,0 +1,74 @@
1
+ import { renderExpected, validate } from "@definitelytyped/header-parser";
2
+ import { createRule, isMainFile } from "../util";
3
+
4
+ type MessageId =
5
+ | "definitionsBy"
6
+ | "minimumTypeScriptVersion"
7
+ | "parseError"
8
+ | "typeDefinitionsFor"
9
+ | "typescriptVersion";
10
+
11
+ const rule = createRule({
12
+ name: "dt-header",
13
+ defaultOptions: [],
14
+ meta: {
15
+ type: "problem",
16
+ docs: {
17
+ description: "Ensure consistency of DefinitelyTyped headers.",
18
+ recommended: "error",
19
+ },
20
+ messages: {
21
+ definitionsBy: "Author name should be your name, not the default.",
22
+ minimumTypeScriptVersion: "TypeScript version should be specified under header in `index.d.ts`.",
23
+ parseError: "Error parsing header. Expected: {{expected}}",
24
+ typeDefinitionsFor: "Header should only be in `index.d.ts` of the root.",
25
+ typescriptVersion: "Minimum TypeScript version should be specified under header in `index.d.ts`.",
26
+ },
27
+ schema: [],
28
+ },
29
+ create(context) {
30
+ const sourceCode = context.getSourceCode();
31
+ const { lines, text } = sourceCode;
32
+
33
+ const lookFor = (search: string, messageId: MessageId) => {
34
+ for (let i = 0; i < lines.length; i += 1) {
35
+ if (lines[i].startsWith(search)) {
36
+ context.report({
37
+ loc: {
38
+ end: { line: i + 1, column: search.length },
39
+ start: { line: i + 1, column: 0 },
40
+ },
41
+ messageId,
42
+ });
43
+ }
44
+ }
45
+ };
46
+
47
+ if (!isMainFile(context.getFilename(), /*allowNested*/ true)) {
48
+ lookFor("// Type definitions for", "typeDefinitionsFor");
49
+ lookFor("// TypeScript Version", "typescriptVersion");
50
+ lookFor("// Minimum TypeScript Version", "minimumTypeScriptVersion");
51
+ return {};
52
+ }
53
+
54
+ lookFor("// Definitions by: My Self", "definitionsBy");
55
+
56
+ const error = validate(text);
57
+ if (error) {
58
+ context.report({
59
+ data: {
60
+ expected: renderExpected(error.expected),
61
+ },
62
+ loc: {
63
+ column: error.column,
64
+ line: error.line,
65
+ },
66
+ messageId: "parseError",
67
+ });
68
+ }
69
+
70
+ return {};
71
+ },
72
+ });
73
+
74
+ export = rule;
@@ -0,0 +1,83 @@
1
+ import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/utils";
2
+ import { createRule } from "../util";
3
+
4
+ interface ExportAssignmentWithIdentifier extends TSESTree.TSExportAssignment {
5
+ expression: TSESTree.Identifier;
6
+ }
7
+
8
+ const rule = createRule({
9
+ name: "export-just-namespace",
10
+ defaultOptions: [],
11
+ meta: {
12
+ type: "problem",
13
+ docs: {
14
+ description:
15
+ "Forbids `export = foo` where `foo` is a namespace and isn't merged with a function/class/type/interface.",
16
+ recommended: "error",
17
+ },
18
+ messages: {
19
+ useTheBody: "Instead of `export =`-ing a namespace, use the body of the namespace as the module body.",
20
+ },
21
+ schema: [],
22
+ },
23
+ create(context) {
24
+ const ast = context.getSourceCode().ast;
25
+
26
+ const exportEqualsNode = ast.body.find(isExportEqualsWithIdentifier);
27
+ if (!exportEqualsNode) {
28
+ return {};
29
+ }
30
+
31
+ if (isJustNamespace(ast.body, exportEqualsNode.expression.name)) {
32
+ context.report({
33
+ messageId: "useTheBody",
34
+ node: exportEqualsNode,
35
+ });
36
+ }
37
+
38
+ return {};
39
+ },
40
+ });
41
+
42
+ /**
43
+ * @returns Where there is a namespace but there are no functions/classes/etc. with the same name.
44
+ */
45
+ function isJustNamespace(statements: TSESTree.ProgramStatement[], exportEqualsName: string): boolean {
46
+ let anyNamespace = false;
47
+
48
+ for (const statement of statements) {
49
+ switch (statement.type) {
50
+ case AST_NODE_TYPES.TSModuleDeclaration:
51
+ anyNamespace ||= nameMatches(statement.id);
52
+ break;
53
+ case AST_NODE_TYPES.VariableDeclaration:
54
+ if (statement.declarations.some((d) => nameMatches(d.id))) {
55
+ // OK. It's merged with a variable.
56
+ return false;
57
+ }
58
+ break;
59
+ case AST_NODE_TYPES.ClassDeclaration:
60
+ case AST_NODE_TYPES.FunctionDeclaration:
61
+ case AST_NODE_TYPES.TSDeclareFunction:
62
+ case AST_NODE_TYPES.TSTypeAliasDeclaration:
63
+ case AST_NODE_TYPES.TSInterfaceDeclaration:
64
+ if (nameMatches(statement.id)) {
65
+ // OK. It's merged with a function/class/type/interface.
66
+ return false;
67
+ }
68
+ break;
69
+ }
70
+ }
71
+
72
+ return anyNamespace;
73
+
74
+ function nameMatches(nameNode: TSESTree.Node | undefined | null): boolean {
75
+ return !!nameNode && nameNode.type === AST_NODE_TYPES.Identifier && nameNode.name === exportEqualsName;
76
+ }
77
+ }
78
+
79
+ function isExportEqualsWithIdentifier(node: TSESTree.Node): node is ExportAssignmentWithIdentifier {
80
+ return node.type === AST_NODE_TYPES.TSExportAssignment && node.expression.type === AST_NODE_TYPES.Identifier;
81
+ }
82
+
83
+ export = rule;
@@ -0,0 +1,34 @@
1
+ import { createRule } from "../util";
2
+ import { AST_NODE_TYPES } from "@typescript-eslint/utils";
3
+
4
+ const rule = createRule({
5
+ name: "no-any-union",
6
+ defaultOptions: [],
7
+ meta: {
8
+ type: "problem",
9
+ docs: {
10
+ description: "Forbid a union to contain `any`",
11
+ recommended: "error",
12
+ },
13
+ messages: {
14
+ anyUnion: "Including `any` in a union will override all other members of the union.",
15
+ },
16
+ schema: [],
17
+ },
18
+ create(context) {
19
+ return {
20
+ // eslint-disable-next-line @typescript-eslint/naming-convention
21
+ TSUnionType(node) {
22
+ const hasAnyType = node.types.some((t) => t.type === AST_NODE_TYPES.TSAnyKeyword);
23
+ if (hasAnyType) {
24
+ context.report({
25
+ messageId: "anyUnion",
26
+ node,
27
+ });
28
+ }
29
+ },
30
+ };
31
+ },
32
+ });
33
+
34
+ export = rule;
@@ -0,0 +1,62 @@
1
+ import { TSESTree } from "@typescript-eslint/utils";
2
+ import { createRule } from "../util";
3
+
4
+ type MessageId = "referencePathPackage" | "referencePathTest";
5
+
6
+ const rule = createRule({
7
+ name: "no-bad-reference",
8
+ defaultOptions: [],
9
+ meta: {
10
+ type: "problem",
11
+ docs: {
12
+ description: `Forbids <reference path="../etc"/> in any file, and forbid <reference path> in test files.`,
13
+ recommended: "error",
14
+ },
15
+ messages: {
16
+ referencePathPackage:
17
+ "Don't use <reference path> to reference another package. Use an import or <reference types> instead.",
18
+ referencePathTest:
19
+ "Don't use <reference path> in test files. Use <reference types> or include the file in 'tsconfig.json'.",
20
+ },
21
+ schema: [],
22
+ },
23
+ create(context) {
24
+ const { comments } = context.getSourceCode().ast;
25
+ const isDeclarationFile = context.getFilename().endsWith(".d.ts");
26
+
27
+ for (const comment of comments) {
28
+ const referenceMatch = comment.value.match(/<reference\s+path\s*=\s*"(.+)"\s*\/>/)?.[1];
29
+ if (!referenceMatch) {
30
+ continue;
31
+ }
32
+
33
+ if (isDeclarationFile) {
34
+ if (referenceMatch.startsWith("..")) {
35
+ report(comment, "referencePathPackage");
36
+ }
37
+ } else {
38
+ report(comment, "referencePathTest");
39
+ }
40
+ }
41
+
42
+ return {};
43
+
44
+ function report(comment: TSESTree.Comment, messageId: MessageId) {
45
+ context.report({
46
+ loc: {
47
+ end: {
48
+ column: comment.value.lastIndexOf(`"`),
49
+ line: comment.loc.end.line,
50
+ },
51
+ start: {
52
+ column: comment.value.indexOf(`"`) + 1,
53
+ line: comment.loc.start.line,
54
+ },
55
+ },
56
+ messageId,
57
+ });
58
+ }
59
+ },
60
+ });
61
+
62
+ export = rule;
@@ -0,0 +1,30 @@
1
+ import { createRule } from "../util";
2
+
3
+ const rule = createRule({
4
+ name: "no-const-enum",
5
+ defaultOptions: [],
6
+ meta: {
7
+ type: "problem",
8
+ docs: {
9
+ description: "Forbid `const enum`",
10
+ recommended: "error",
11
+ },
12
+ messages: {
13
+ constEnum: "Use of `const enum` is forbidden.",
14
+ },
15
+ schema: [],
16
+ },
17
+ create(context) {
18
+ return {
19
+ // eslint-disable-next-line @typescript-eslint/naming-convention
20
+ "TSEnumDeclaration[const]"(node) {
21
+ context.report({
22
+ messageId: "constEnum",
23
+ node,
24
+ });
25
+ },
26
+ };
27
+ },
28
+ });
29
+
30
+ export = rule;