@definitelytyped/dtslint 0.0.141 → 0.0.142-next.10
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.
- package/dist/rules/no-any-union.d.ts +4 -0
- package/dist/rules/no-any-union.js +34 -0
- package/dist/rules/no-any-union.js.map +1 -0
- package/dist/rules/no-unnecessary-generics.d.ts +8 -0
- package/dist/rules/no-unnecessary-generics.js +135 -0
- package/dist/rules/no-unnecessary-generics.js.map +1 -0
- package/dtslint.json +0 -2
- package/package.json +3 -3
- package/src/rules/no-any-union.ts +34 -0
- package/src/rules/no-unnecessary-generics.ts +126 -0
- package/test/no-any-union.test.ts +22 -0
- package/test/no-unnecessary-generics.test.ts +152 -0
- package/test/tsconfig.json +8 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/dist/rules/noAnyUnionRule.d.ts +0 -7
- package/dist/rules/noAnyUnionRule.js +0 -53
- package/dist/rules/noAnyUnionRule.js.map +0 -1
- package/dist/rules/noUnnecessaryGenericsRule.d.ts +0 -8
- package/dist/rules/noUnnecessaryGenericsRule.js +0 -134
- package/dist/rules/noUnnecessaryGenericsRule.js.map +0 -1
- package/src/rules/noAnyUnionRule.ts +0 -33
- package/src/rules/noUnnecessaryGenericsRule.ts +0 -115
- package/test/no-any-union/test.d.ts.lint +0 -4
- package/test/no-any-union/tslint.json +0 -6
- package/test/no-unnecessary-generics/test.ts.lint +0 -38
- package/test/no-unnecessary-generics/tsconfig.json +0 -1
- package/test/no-unnecessary-generics/tslint.json +0 -6
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
import * as Lint from "tslint";
|
|
2
|
-
import * as ts from "typescript";
|
|
3
|
-
|
|
4
|
-
import { failure } from "../util";
|
|
5
|
-
|
|
6
|
-
export class Rule extends Lint.Rules.TypedRule {
|
|
7
|
-
static metadata: Lint.IRuleMetadata = {
|
|
8
|
-
ruleName: "no-unnecessary-generics",
|
|
9
|
-
description: "Forbids signatures using a generic parameter only once.",
|
|
10
|
-
optionsDescription: "Not configurable.",
|
|
11
|
-
options: null,
|
|
12
|
-
type: "style",
|
|
13
|
-
typescriptOnly: true,
|
|
14
|
-
};
|
|
15
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
16
|
-
static FAILURE_STRING(typeParameter: string) {
|
|
17
|
-
return failure(Rule.metadata.ruleName, `Type parameter ${typeParameter} is used only once.`);
|
|
18
|
-
}
|
|
19
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
20
|
-
static FAILURE_STRING_NEVER(typeParameter: string) {
|
|
21
|
-
return failure(Rule.metadata.ruleName, `Type parameter ${typeParameter} is never used.`);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): Lint.RuleFailure[] {
|
|
25
|
-
return this.applyWithFunction(sourceFile, (ctx) => walk(ctx, program.getTypeChecker()));
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
function walk(ctx: Lint.WalkContext<void>, checker: ts.TypeChecker): void {
|
|
30
|
-
const { sourceFile } = ctx;
|
|
31
|
-
sourceFile.forEachChild(function cb(node) {
|
|
32
|
-
if (ts.isFunctionLike(node)) {
|
|
33
|
-
checkSignature(node);
|
|
34
|
-
}
|
|
35
|
-
node.forEachChild(cb);
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
function checkSignature(sig: ts.SignatureDeclaration) {
|
|
39
|
-
if (!sig.typeParameters) {
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
for (const tp of sig.typeParameters) {
|
|
44
|
-
const typeParameter = tp.name.text;
|
|
45
|
-
const res = getSoleUse(sig, assertDefined(checker.getSymbolAtLocation(tp.name)), checker);
|
|
46
|
-
switch (res.type) {
|
|
47
|
-
case "ok":
|
|
48
|
-
break;
|
|
49
|
-
case "sole":
|
|
50
|
-
ctx.addFailureAtNode(res.soleUse, Rule.FAILURE_STRING(typeParameter));
|
|
51
|
-
break;
|
|
52
|
-
case "never":
|
|
53
|
-
ctx.addFailureAtNode(tp, Rule.FAILURE_STRING_NEVER(typeParameter));
|
|
54
|
-
break;
|
|
55
|
-
default:
|
|
56
|
-
assertNever(res);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
type Result = { type: "ok" | "never" } | { type: "sole"; soleUse: ts.Identifier };
|
|
63
|
-
function getSoleUse(sig: ts.SignatureDeclaration, typeParameterSymbol: ts.Symbol, checker: ts.TypeChecker): Result {
|
|
64
|
-
const exit = {};
|
|
65
|
-
let soleUse: ts.Identifier | undefined;
|
|
66
|
-
|
|
67
|
-
try {
|
|
68
|
-
if (sig.typeParameters) {
|
|
69
|
-
for (const tp of sig.typeParameters) {
|
|
70
|
-
if (tp.constraint) {
|
|
71
|
-
recur(tp.constraint);
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
for (const param of sig.parameters) {
|
|
76
|
-
if (param.type) {
|
|
77
|
-
recur(param.type);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
if (sig.type) {
|
|
81
|
-
recur(sig.type);
|
|
82
|
-
}
|
|
83
|
-
} catch (err) {
|
|
84
|
-
if (err === exit) {
|
|
85
|
-
return { type: "ok" };
|
|
86
|
-
}
|
|
87
|
-
throw err;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
return soleUse ? { type: "sole", soleUse } : { type: "never" };
|
|
91
|
-
|
|
92
|
-
function recur(node: ts.Node): void {
|
|
93
|
-
if (ts.isIdentifier(node)) {
|
|
94
|
-
if (checker.getSymbolAtLocation(node) === typeParameterSymbol) {
|
|
95
|
-
if (soleUse === undefined) {
|
|
96
|
-
soleUse = node;
|
|
97
|
-
} else {
|
|
98
|
-
throw exit;
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
} else {
|
|
102
|
-
node.forEachChild(recur);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
function assertDefined<T>(value: T | undefined): T {
|
|
108
|
-
if (value === undefined) {
|
|
109
|
-
throw new Error("unreachable");
|
|
110
|
-
}
|
|
111
|
-
return value;
|
|
112
|
-
}
|
|
113
|
-
function assertNever(_: never) {
|
|
114
|
-
throw new Error("unreachable");
|
|
115
|
-
}
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
interface I {
|
|
2
|
-
<T>(value: T): void;
|
|
3
|
-
~ [0]
|
|
4
|
-
m<T>(x: T): void;
|
|
5
|
-
~ [0]
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
class C {
|
|
9
|
-
constructor<T>(x: T) {}
|
|
10
|
-
~ [0]
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
type Fn = <T>() => T;
|
|
14
|
-
~ [0]
|
|
15
|
-
type Ctr = new<T>() => T;
|
|
16
|
-
~ [0]
|
|
17
|
-
|
|
18
|
-
function f<T>(): T { }
|
|
19
|
-
~ [0]
|
|
20
|
-
|
|
21
|
-
const f = function<T>(): T {};
|
|
22
|
-
~ [0]
|
|
23
|
-
const f2 = <T>(): T => {};
|
|
24
|
-
~ [0]
|
|
25
|
-
|
|
26
|
-
function f<T>(x: { T: number }): void;
|
|
27
|
-
~ [Type parameter T is never used. See: https://github.com/microsoft/DefinitelyTyped-tools/blob/master/packages/dtslint/docs/no-unnecessary-generics.md]
|
|
28
|
-
|
|
29
|
-
function f<T, U extends T>(u: U): U;
|
|
30
|
-
~ [0]
|
|
31
|
-
|
|
32
|
-
// OK:
|
|
33
|
-
// Uses type parameter twice
|
|
34
|
-
function foo<T>(m: Map<T, T>): void {}
|
|
35
|
-
// `T` appears in a constraint, so it appears twice.
|
|
36
|
-
function f<T, U extends T>(t: T, u: U): U;
|
|
37
|
-
|
|
38
|
-
[0]: Type parameter T is used only once. See: https://github.com/microsoft/DefinitelyTyped-tools/blob/master/packages/dtslint/docs/no-unnecessary-generics.md
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{}
|