Skip to content

Commit a25ee78

Browse files
committed
Refactor
1 parent b8cda6c commit a25ee78

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

src/rules/no-undocumented-throws.js

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const {
1919
findNodeToComment,
2020
findIdentifierDeclaration,
2121
createInsertJSDocBeforeFixer,
22+
toFlattenedTypeArray,
2223
} = require('../utils');
2324

2425

@@ -79,16 +80,18 @@ module.exports = createRule({
7980
if (!throwStatementNodes) return;
8081

8182
/** @type {import('typescript').Type[]} */
82-
const throwTypes = throwStatementNodes
83-
.map(n => {
84-
const type = services.getTypeAtLocation(n.argument);
85-
const tsNode = services.esTreeNodeToTSNodeMap.get(n.argument);
86-
87-
return options.useBaseTypeOfLiteral && ts.isLiteralTypeLiteral(tsNode)
88-
? checker.getBaseTypeOfLiteralType(type)
89-
: type;
90-
})
91-
.flatMap(t => t.isUnion() ? t.types : t)
83+
const throwTypes =
84+
toFlattenedTypeArray(
85+
throwStatementNodes
86+
.map(n => {
87+
const type = services.getTypeAtLocation(n.argument);
88+
const tsNode = services.esTreeNodeToTSNodeMap.get(n.argument);
89+
90+
return options.useBaseTypeOfLiteral && ts.isLiteralTypeLiteral(tsNode)
91+
? checker.getBaseTypeOfLiteralType(type)
92+
: type;
93+
})
94+
)
9295
.map(t => checker.getAwaitedType(t) ?? t);
9396

9497
if (hasJSDocThrowsTag(sourceCode, nodeToComment)) {
@@ -219,7 +222,7 @@ module.exports = createRule({
219222
if (!callbackNode) return;
220223

221224
/** @type {import('typescript').Type[]} */
222-
let rejectTypes = [];
225+
const rejectTypes = [];
223226

224227
const isRejectCallbackNameDeclared =
225228
callbackNode.params.length >= 2;
@@ -247,19 +250,15 @@ module.exports = createRule({
247250
const argumentTypes = callRefs
248251
.map(ref => services.getTypeAtLocation(ref.arguments[0]));
249252

250-
rejectTypes.push(
251-
...argumentTypes.flatMap(t => t.isUnion() ? t.types : t)
252-
);
253+
rejectTypes.push(...toFlattenedTypeArray(argumentTypes));
253254
}
254255

255256
if (throwStatements.has(getNodeID(callbackNode))) {
256257
const throwStatementTypes = throwStatements.get(getNodeID(callbackNode))
257258
?.map(n => services.getTypeAtLocation(n.argument));
258259

259260
if (throwStatementTypes) {
260-
rejectTypes.push(
261-
...throwStatementTypes.flatMap(t => t.isUnion() ? t.types : t)
262-
);
261+
rejectTypes.push(...toFlattenedTypeArray(throwStatementTypes));
263262
}
264263
}
265264

@@ -304,13 +303,14 @@ module.exports = createRule({
304303
if (!throwsTagTypeNodes.length) return;
305304

306305
// Throws tag with `Promise<...>` considered as a reject tag
307-
const rejectTagTypes = getJSDocThrowsTagTypes(checker, functionDeclarationTSNode)
308-
.filter(t =>
309-
utils.isPromiseLike(services.program, t) &&
310-
t.symbol.getName() === 'Promise'
311-
)
312-
.map(t => checker.getAwaitedType(t) ?? t)
313-
.flatMap(t => t.isUnion() ? t.types : t);
306+
const rejectTagTypes = toFlattenedTypeArray(
307+
getJSDocThrowsTagTypes(checker, functionDeclarationTSNode)
308+
.filter(t =>
309+
utils.isPromiseLike(services.program, t) &&
310+
t.symbol.getName() === 'Promise'
311+
)
312+
.map(t => checker.getAwaitedType(t) ?? t)
313+
);
314314

315315
const typeGroups = groupTypesByCompatibility(
316316
services.program,

0 commit comments

Comments
 (0)