Skip to content

Commit d9fa5b3

Browse files
author
Rebecca Stevens
committed
refactor: Improve function names, add JSDoc to functions.
1 parent 0bfd477 commit d9fa5b3

File tree

8 files changed

+106
-19
lines changed

8 files changed

+106
-19
lines changed

src/common/ignore-options.ts

+54-10
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ export const ignoreNewArrayOptionSchema: JSONSchema4 = {
125125
additionalProperties: false
126126
};
127127

128+
/**
129+
* Recursive callback of `getNodeText`.
130+
*
131+
* This function not be called from anywhere else.
132+
*/
128133
function _getNodeText(
129134
node: TSESTree.Node,
130135
context: RuleContext<string, BaseOptions>
@@ -139,6 +144,9 @@ function _getNodeText(
139144
: context.getSourceCode().getText(node);
140145
}
141146

147+
/**
148+
* Get the text of the given node.
149+
*/
142150
function getNodeText(
143151
node: TSESTree.Node,
144152
context: RuleContext<string, BaseOptions>
@@ -156,6 +164,9 @@ function getNodeText(
156164
: _getNodeText(node, context);
157165
}
158166

167+
/**
168+
* Get all the important bits of texts from the given node.
169+
*/
159170
function getNodeTexts(
160171
node: TSESTree.Node,
161172
context: RuleContext<string, BaseOptions>
@@ -166,7 +177,12 @@ function getNodeTexts(
166177
).filter(name => name !== undefined) as ReadonlyArray<string>;
167178
}
168179

169-
function isIgnoredPattern(
180+
/**
181+
* Should the given text be ignore?
182+
*
183+
* Test using the given pattern(s).
184+
*/
185+
function shouldIgnoreViaPattern(
170186
text: string,
171187
ignorePattern: ReadonlyArray<string> | string
172188
): boolean {
@@ -178,7 +194,14 @@ function isIgnoredPattern(
178194
return patterns.some(pattern => new RegExp(pattern).test(text));
179195
}
180196

181-
function findMatch(
197+
/**
198+
* Recursive callback of `shouldIgnoreViaAccessorPattern`.
199+
*
200+
* This function not be called from anywhere else.
201+
*
202+
* Does the given text match the given pattern.
203+
*/
204+
function accessorPatternMatch(
182205
[pattern, ...remainingPatternParts]: ReadonlyArray<string>,
183206
textParts: ReadonlyArray<string>,
184207
allowExtra: boolean = false
@@ -188,23 +211,41 @@ function findMatch(
188211
: // Match any depth (including 0)?
189212
pattern === "**"
190213
? textParts.length === 0
191-
? findMatch(remainingPatternParts, [], allowExtra)
214+
? accessorPatternMatch(remainingPatternParts, [], allowExtra)
192215
: Array.from({ length: textParts.length })
193216
.map((_element, index) => index)
194217
.some(offset =>
195-
findMatch(remainingPatternParts, textParts.slice(offset), true)
218+
accessorPatternMatch(
219+
remainingPatternParts,
220+
textParts.slice(offset),
221+
true
222+
)
196223
)
197224
: // Match anything?
198225
pattern === "*"
199226
? textParts.length > 0 &&
200-
findMatch(remainingPatternParts, textParts.slice(1), allowExtra)
227+
accessorPatternMatch(
228+
remainingPatternParts,
229+
textParts.slice(1),
230+
allowExtra
231+
)
201232
: // Text matches pattern?
202233
new RegExp("^" + escapeRegExp(pattern).replace(/\\\*/g, ".*") + "$").test(
203234
textParts[0]
204-
) && findMatch(remainingPatternParts, textParts.slice(1), allowExtra);
235+
) &&
236+
accessorPatternMatch(
237+
remainingPatternParts,
238+
textParts.slice(1),
239+
allowExtra
240+
);
205241
}
206242

207-
function isIgnoredAccessorPattern(
243+
/**
244+
* Should the given text be ignore?
245+
*
246+
* Test using the given accessor pattern(s).
247+
*/
248+
function shouldIgnoreViaAccessorPattern(
208249
text: string,
209250
ignorePattern: ReadonlyArray<string> | string
210251
): boolean {
@@ -214,7 +255,7 @@ function isIgnoredAccessorPattern(
214255

215256
// One or more patterns match?
216257
return patterns.some(pattern =>
217-
findMatch(pattern.split("."), text.split("."))
258+
accessorPatternMatch(pattern.split("."), text.split("."))
218259
);
219260
}
220261

@@ -238,12 +279,15 @@ export function shouldIgnore(
238279
? // Ignore if ignorePattern is set and a pattern matches.
239280
(options.ignorePattern !== undefined &&
240281
texts.every(text =>
241-
isIgnoredPattern(text, options.ignorePattern!)
282+
shouldIgnoreViaPattern(text, options.ignorePattern!)
242283
)) ||
243284
// Ignore if ignoreAccessorPattern is set and an accessor pattern matches.
244285
(options.ignoreAccessorPattern !== undefined &&
245286
texts.every(text =>
246-
isIgnoredAccessorPattern(text, options.ignoreAccessorPattern!)
287+
shouldIgnoreViaAccessorPattern(
288+
text,
289+
options.ignoreAccessorPattern!
290+
)
247291
))
248292
: false)(getNodeTexts(node, context))
249293
);

src/rules/functional-parameters.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,11 @@ const meta: RuleMetaData<keyof typeof errorMessages> = {
8383
schema
8484
};
8585

86+
/**
87+
* Get the rest parameter violations.
88+
*/
8689
function getRestParamViolations(
87-
allowRestParameter: boolean,
90+
allowRestParameter: Options["allowRestParameter"],
8891
node:
8992
| TSESTree.FunctionDeclaration
9093
| TSESTree.FunctionExpression
@@ -102,6 +105,9 @@ function getRestParamViolations(
102105
: [];
103106
}
104107

108+
/**
109+
* Get the parameter count violations.
110+
*/
105111
function getParamCountViolations(
106112
enforceParameterCount: Options["enforceParameterCount"],
107113
node:

src/rules/no-conditional-statement.ts

+17-3
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ const meta: RuleMetaData<keyof typeof errorMessages> = {
7272
schema
7373
};
7474

75+
/**
76+
* Get all of the violations in the given if statement assuming if statements
77+
* are allowed.
78+
*/
7579
function getIfBranchViolations(
7680
node: TSESTree.IfStatement
7781
): RuleResult<keyof typeof errorMessages, Options>["descriptors"] {
@@ -97,7 +101,11 @@ function getIfBranchViolations(
97101
return nodes.flatMap(node => [{ node, messageId: "incompleteBranch" }]);
98102
}
99103

100-
function getSwitchCaseViolations(
104+
/**
105+
* Get all of the violations in the given switch statement assuming switch
106+
* statements are allowed.
107+
*/
108+
function getSwitchViolations(
101109
node: TSESTree.SwitchStatement
102110
): RuleResult<keyof typeof errorMessages, Options>["descriptors"] {
103111
const nodes = node.cases.reduce<ReadonlyArray<TSESTree.Node>>(
@@ -116,10 +124,16 @@ function getSwitchCaseViolations(
116124
return nodes.flatMap(node => [{ node, messageId: "incompleteBranch" }]);
117125
}
118126

127+
/**
128+
* Does the given if statement violate this rule if it must be exhaustive.
129+
*/
119130
function isExhaustiveIfViolation(node: TSESTree.IfStatement): boolean {
120131
return node.alternate === null;
121132
}
122133

134+
/**
135+
* Does the given switch statement violate this rule if it must be exhaustive.
136+
*/
123137
function isExhaustiveSwitchViolation(node: TSESTree.SwitchStatement): boolean {
124138
return (
125139
// No cases defined.
@@ -163,8 +177,8 @@ function checkSwitchStatement(
163177
? options.allowReturningBranches === "ifExhaustive"
164178
? isExhaustiveSwitchViolation(node)
165179
? [{ node, messageId: "incompleteSwitch" }]
166-
: getSwitchCaseViolations(node)
167-
: getSwitchCaseViolations(node)
180+
: getSwitchViolations(node)
181+
: getSwitchViolations(node)
168182
: [{ node, messageId: "unexpectedSwitch" }]
169183
};
170184
}

src/util/misc.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
2-
* Returns a function that checks if the given value is the same as the expected value.
2+
* Returns a function that checks if the given value is the same as the expected
3+
* value.
34
*/
45
export function isExpected<T>(expected: T): (actual: T) => boolean {
56
return actual => actual === expected;

src/util/rule.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ export function createRule<
6565
} as any) as any;
6666
}
6767

68+
// This function can't be functional as it needs to interact with 3rd-party
69+
// libraries that aren't functional.
70+
/* eslint-disable ts-immutable/no-return-void, ts-immutable/no-conditional-statement, ts-immutable/no-expression-statement */
6871
/**
6972
* Create a function that processes common options and then runs the given
7073
* check.
7174
*/
72-
// This function can't be functional as it needs to interact with 3rd-party
73-
// libraries that aren't functional.
74-
/* eslint-disable ts-immutable/no-return-void, ts-immutable/no-conditional-statement, ts-immutable/no-expression-statement */
7575
export function checkNode<
7676
MessageIds extends string,
7777
Context extends RuleContext<MessageIds, BaseOptions>,

src/util/tree.ts

+6
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ export function isInReturnType(node: TSESTree.Node): boolean {
7272
);
7373
}
7474

75+
/**
76+
* Is the given identifier a property of an object?
77+
*/
7578
export function isPropertyAccess(node: TSESTree.Identifier): boolean {
7679
return (
7780
node.parent !== undefined &&
@@ -80,6 +83,9 @@ export function isPropertyAccess(node: TSESTree.Identifier): boolean {
8083
);
8184
}
8285

86+
/**
87+
* Is the given identifier a property name?
88+
*/
8389
export function isPropertyName(node: TSESTree.Identifier): boolean {
8490
return (
8591
node.parent !== undefined &&

src/util/typeguard.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* This file has functions that typeguard the given node/type.
2+
* @file Functions that typeguard the given node/type.
33
*/
44

55
import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/typescript-estree";
@@ -8,6 +8,9 @@ import { Type, UnionType } from "typescript";
88
// TS import - conditionally imported only when typescript is avaliable.
99
import ts from "../util/conditional-imports/typescript";
1010

11+
// Any JSDoc for these functions would be tedious.
12+
/* eslint-disable jsdoc/require-jsdoc */
13+
1114
/*
1215
* TS Types.
1316
*/
@@ -64,6 +67,11 @@ export function isCallExpression(
6467
return node.type === AST_NODE_TYPES.CallExpression;
6568
}
6669

70+
/**
71+
* Is the given node a class node?
72+
*
73+
* It doesn't matter what type of class.
74+
*/
6775
export function isClassLike(
6876
node: TSESTree.Node
6977
): node is TSESTree.ClassDeclaration | TSESTree.ClassExpression {
@@ -73,6 +81,11 @@ export function isClassLike(
7381
);
7482
}
7583

84+
/**
85+
* Is the given node a function node?
86+
*
87+
* It doesn't matter what type of function.
88+
*/
7689
export function isFunctionLike(
7790
node: TSESTree.Node
7891
): node is

tests/util.ts

+3
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ export function processValidTestCase(
6565
return processInvalidTestCase(testCases as any);
6666
}
6767

68+
/**
69+
* Create a dummy rule for testing.
70+
*/
6871
export function createDummyRule(
6972
create: (
7073
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */

0 commit comments

Comments
 (0)