/
misc.ts
154 lines (140 loc) · 3.71 KB
/
misc.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
* @fileoverview Really small utility functions that didn't deserve their own files
*/
import {
AST_NODE_TYPES,
TSESLint,
TSESTree,
} from '@typescript-eslint/experimental-utils';
/**
* Check if the context file name is *.d.ts or *.d.tsx
*/
function isDefinitionFile(fileName: string): boolean {
return /\.d\.tsx?$/i.test(fileName || '');
}
/**
* Upper cases the first character or the string
*/
function upperCaseFirst(str: string): string {
return str[0].toUpperCase() + str.slice(1);
}
type InferOptionsTypeFromRuleNever<T> = T extends TSESLint.RuleModule<
never,
infer TOptions
>
? TOptions
: unknown;
/**
* Uses type inference to fetch the TOptions type from the given RuleModule
*/
type InferOptionsTypeFromRule<T> = T extends TSESLint.RuleModule<
string,
infer TOptions
>
? TOptions
: InferOptionsTypeFromRuleNever<T>;
/**
* Uses type inference to fetch the TMessageIds type from the given RuleModule
*/
type InferMessageIdsTypeFromRule<T> = T extends TSESLint.RuleModule<
infer TMessageIds,
unknown[]
>
? TMessageIds
: unknown;
/** Return true if both parameters are equal. */
type Equal<T> = (a: T, b: T) => boolean;
function arraysAreEqual<T>(
a: T[] | undefined,
b: T[] | undefined,
eq: (a: T, b: T) => boolean,
): boolean {
return (
a === b ||
(a !== undefined &&
b !== undefined &&
a.length === b.length &&
a.every((x, idx) => eq(x, b[idx])))
);
}
/** Returns the first non-`undefined` result. */
function findFirstResult<T, U>(
inputs: T[],
getResult: (t: T) => U | undefined,
): U | undefined {
for (const element of inputs) {
const result = getResult(element);
if (result !== undefined) {
return result;
}
}
return undefined;
}
/**
* Gets a string representation of the name of the index signature.
*/
export function getNameFromIndexSignature(
node: TSESTree.TSIndexSignature,
): string {
const propName: TSESTree.PropertyName | undefined = node.parameters.find(
(parameter: TSESTree.Parameter): parameter is TSESTree.Identifier =>
parameter.type === AST_NODE_TYPES.Identifier,
);
return propName ? propName.name : '(index signature)';
}
/**
* Gets a string name representation of the name of the given MethodDefinition
* or ClassProperty node, with handling for computed property names.
*/
function getNameFromMember(
member:
| TSESTree.MethodDefinition
| TSESTree.TSMethodSignature
| TSESTree.TSAbstractMethodDefinition
| TSESTree.ClassProperty
| TSESTree.TSAbstractClassProperty
| TSESTree.Property
| TSESTree.TSPropertySignature,
sourceCode: TSESLint.SourceCode,
): string {
if (isLiteralOrIdentifier(member.key)) {
if (member.key.type === AST_NODE_TYPES.Identifier) {
return member.key.name;
}
return `${member.key.value}`;
}
return sourceCode.text.slice(...member.key.range);
}
/**
* This covers both actual property names, as well as computed properties that are either
* an identifier or a literal at the top level.
*/
function isLiteralOrIdentifier(
node: TSESTree.Expression,
): node is TSESTree.Literal | TSESTree.Identifier {
return (
node.type === AST_NODE_TYPES.Literal ||
node.type === AST_NODE_TYPES.Identifier
);
}
type ExcludeKeys<
TObj extends Record<string, unknown>,
TKeys extends keyof TObj
> = { [k in Exclude<keyof TObj, TKeys>]: TObj[k] };
type RequireKeys<
TObj extends Record<string, unknown>,
TKeys extends keyof TObj
> = ExcludeKeys<TObj, TKeys> & { [k in TKeys]-?: Exclude<TObj[k], undefined> };
export {
arraysAreEqual,
Equal,
ExcludeKeys,
findFirstResult,
getNameFromMember,
InferMessageIdsTypeFromRule,
InferOptionsTypeFromRule,
isDefinitionFile,
isLiteralOrIdentifier,
RequireKeys,
upperCaseFirst,
};