/
no-untyped-public-signature.ts
124 lines (114 loc) · 3.39 KB
/
no-untyped-public-signature.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import * as util from '../util';
import {
AST_NODE_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';
type MessageIds = 'noReturnType' | 'untypedParameter';
type Options = [{ ignoredMethods: string[] }];
export default util.createRule<Options, MessageIds>({
name: 'no-unused-public-signature',
meta: {
docs: {
description:
'Requires that all public method arguments and return type will be explicitly typed',
category: 'Best Practices',
recommended: false,
},
messages: {
noReturnType: 'Public method has no return type',
untypedParameter: 'Public method parameters should be typed',
},
schema: [
{
allowAdditionalProperties: false,
properties: {
ignoredMethods: {
type: 'array',
items: {
type: 'string',
},
},
},
type: 'object',
},
],
type: 'suggestion',
},
defaultOptions: [{ ignoredMethods: [] }],
create(context, [options]) {
const ignoredMethods = new Set(options.ignoredMethods);
function isPublicMethod(
node: TSESTree.MethodDefinition | TSESTree.TSAbstractMethodDefinition,
): boolean {
return node.accessibility === 'public' || !node.accessibility;
}
function isIgnoredMethod(
node: TSESTree.MethodDefinition | TSESTree.TSAbstractMethodDefinition,
ignoredMethods: Set<string>,
): boolean {
if (
node.key.type === AST_NODE_TYPES.Literal &&
typeof node.key.value === 'string'
) {
return ignoredMethods.has(node.key.value);
}
if (
node.key.type === AST_NODE_TYPES.TemplateLiteral &&
node.key.expressions.length === 0
) {
return ignoredMethods.has(node.key.quasis[0].value.raw);
}
if (!node.computed && node.key.type === AST_NODE_TYPES.Identifier) {
return ignoredMethods.has(node.key.name);
}
return false;
}
function isParamTyped(node: TSESTree.Identifier): boolean {
return (
!!node.typeAnnotation &&
node.typeAnnotation.typeAnnotation.type !== AST_NODE_TYPES.TSAnyKeyword
);
}
function isReturnTyped(
node: TSESTree.TSTypeAnnotation | undefined,
): boolean {
if (!node) {
return false;
}
return (
node.typeAnnotation &&
node.typeAnnotation.type !== AST_NODE_TYPES.TSAnyKeyword
);
}
return {
'TSAbstractMethodDefinition, MethodDefinition'(
node: TSESTree.MethodDefinition | TSESTree.TSAbstractMethodDefinition,
): void {
if (isPublicMethod(node) && !isIgnoredMethod(node, ignoredMethods)) {
const paramIdentifiers = node.value.params.filter(
param => param.type === AST_NODE_TYPES.Identifier,
) as TSESTree.Identifier[];
const identifiersHaveTypes = paramIdentifiers.every(isParamTyped);
if (!identifiersHaveTypes) {
context.report({
node,
messageId: 'untypedParameter',
data: {},
});
}
if (
node.kind !== 'constructor' &&
node.kind !== 'set' &&
!isReturnTyped(node.value.returnType)
) {
context.report({
node,
messageId: 'noReturnType',
data: {},
});
}
}
},
};
},
});