Skip to content

Commit 49d89dd

Browse files
authored
Merge pull request #67 from Xvezda/feature/refactor
Refactor
2 parents 4e9ee07 + 4cab660 commit 49d89dd

File tree

4 files changed

+69
-40
lines changed

4 files changed

+69
-40
lines changed

src/rules/check-throws-tag-type.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const {
2626
findIdentifierDeclaration,
2727
toFlattenedTypeArray,
2828
typesToUnionString,
29+
typeStringsToUnionString,
2930
findFunctionCallNodes,
3031
} = require('../utils');
3132

@@ -443,7 +444,7 @@ module.exports = createRule({
443444
toSortedByMetadata([...throwableTypes, ...rejectableTypes])
444445
)
445446
}>`
446-
: [
447+
: typeStringsToUnionString([
447448
throwableTypes.length
448449
? typesToUnionString(
449450
checker, toSortedByMetadata(throwableTypes),
@@ -456,7 +457,7 @@ module.exports = createRule({
456457
toSortedByMetadata(rejectableTypes),
457458
)}>`
458459
: '',
459-
].filter(t => !!t).join(' | ')
460+
].filter(t => !!t))
460461
);
461462
},
462463
});

src/rules/no-implicit-propagation.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const { ESLintUtils } = require('@typescript-eslint/utils');
33
const {
44
TypeMap,
55
getNodeID,
6+
getNodeIndent,
67
createRule,
78
isInHandledContext,
89
typesToUnionString,
@@ -98,9 +99,7 @@ module.exports = createRule({
9899
node,
99100
messageId: 'implicitPropagation',
100101
fix(fixer) {
101-
const lines = sourceCode.getLines();
102-
const currentLine = lines[nodeToComment.loc.start.line - 1];
103-
const indent = currentLine.match(/^\s*/)?.[0] ?? '';
102+
const indent = getNodeIndent(sourceCode, nodeToComment);
104103

105104
return fixer
106105
.insertTextBefore(

src/rules/no-undocumented-throws.js

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ const ts = require('typescript');
55
const {
66
TypeMap,
77
getNodeID,
8+
getNodeIndent,
89
getFirst,
910
createRule,
1011
hasJSDocThrowsTag,
1112
typesToUnionString,
13+
typeStringsToUnionString,
1214
isInHandledContext,
1315
isInAsyncHandledContext,
1416
isNodeReturned,
@@ -126,27 +128,28 @@ module.exports = createRule({
126128
throwStatementsInFunction.get(getNodeID(node));
127129

128130
if (throwStatementNodes) {
129-
/** @type {import('typescript').Type[]} */
130131
const throwStatementTypes =
131-
toFlattenedTypeArray(
132-
throwStatementNodes
133-
.map(n => {
134-
const type = services.getTypeAtLocation(n.argument);
135-
136-
if (
137-
useBaseTypeOfLiteral &&
138-
ts.isLiteralTypeLiteral(
139-
services.esTreeNodeToTSNodeMap.get(n.argument)
140-
)
141-
) {
142-
return checker.getBaseTypeOfLiteralType(type);
143-
}
144-
return type;
145-
})
146-
)
132+
throwStatementNodes
133+
.map(n => {
134+
const type = services.getTypeAtLocation(n.argument);
135+
136+
if (
137+
useBaseTypeOfLiteral &&
138+
ts.isLiteralTypeLiteral(
139+
services.esTreeNodeToTSNodeMap.get(n.argument)
140+
)
141+
) {
142+
return checker.getBaseTypeOfLiteralType(type);
143+
}
144+
return type;
145+
});
146+
147+
const flattenedTypes = toFlattenedTypeArray(throwStatementTypes);
148+
149+
const awaitedTypes = flattenedTypes
147150
.map(t => checker.getAwaitedType(t) ?? t);
148151

149-
throwTypes.add(node, throwStatementTypes);
152+
throwTypes.add(node, awaitedTypes);
150153
}
151154

152155
const throwableTypes =
@@ -176,24 +179,22 @@ module.exports = createRule({
176179
node: nodeToComment,
177180
messageId: 'missingThrowsTag',
178181
fix(fixer) {
179-
const lines = sourceCode.getLines();
180-
const currentLine = lines[node.loc.start.line - 1];
181-
const indent = currentLine.match(/^\s*/)?.[0] ?? '';
182+
const indent = getNodeIndent(sourceCode, node);
182183

183-
const newType =
184+
const newType =
184185
node.async
185-
? `Promise<${typesToUnionString(checker, [
186-
...throwableTypes,
187-
...rejectableTypes,
188-
])}>`
189-
: [
190-
...throwableTypes.length
191-
? [typesToUnionString(checker, throwableTypes)]
192-
: [],
193-
...rejectableTypes.length
194-
? [`Promise<${typesToUnionString(checker, rejectableTypes)}>`]
195-
: [],
196-
].join(' | ');
186+
? `Promise<${typesToUnionString(checker, [
187+
...throwableTypes,
188+
...rejectableTypes,
189+
])}>`
190+
: typeStringsToUnionString([
191+
...throwableTypes.length
192+
? [typesToUnionString(checker, throwableTypes)]
193+
: [],
194+
...rejectableTypes.length
195+
? [`Promise<${typesToUnionString(checker, rejectableTypes)}>`]
196+
: [],
197+
]);
197198

198199
return fixer
199200
.insertTextBefore(

src/utils.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,22 @@ const getNodeID = (node) => {
8888
return `${node.loc.start.line}:${node.loc.start.column}`;
8989
};
9090

91+
/**
92+
* Get indentation of the node in source code.
93+
*
94+
* @public
95+
* @param {Readonly<import('@typescript-eslint/utils').TSESLint.SourceCode>} sourceCode
96+
* @param {import('@typescript-eslint/utils').TSESTree.Node} node
97+
* @returns {string}
98+
*/
99+
const getNodeIndent = (sourceCode, node) => {
100+
const lines = sourceCode.getLines();
101+
const currentLine = lines[node.loc.start.line - 1];
102+
const indent = currentLine.match(/^\s*/)?.[0] ?? '';
103+
104+
return indent;
105+
};
106+
91107
/**
92108
* Check if node has JSDoc comment with @throws or @exception tag.
93109
*
@@ -107,6 +123,16 @@ const hasJSDocThrowsTag = (sourceCode, node) => {
107123
return isCommented;
108124
};
109125

126+
/**
127+
* Combine multiple types into union type string of given types.
128+
*
129+
* @public
130+
* @param {string[]} typeStrings
131+
* @return {string}
132+
*/
133+
const typeStringsToUnionString = (typeStrings) =>
134+
[...new Set(typeStrings)].join(' | ');
135+
110136
/**
111137
* Combine multiple types into union type string of given types.
112138
*
@@ -116,7 +142,7 @@ const hasJSDocThrowsTag = (sourceCode, node) => {
116142
* @return {string}
117143
*/
118144
const typesToUnionString = (checker, types) =>
119-
[...new Set(types.map(t => utils.getTypeName(checker, t)))].join(' | ');
145+
typeStringsToUnionString(types.map(t => utils.getTypeName(checker, t)))
120146

121147
/**
122148
* Find closest node that matches the callback predicate.
@@ -719,9 +745,11 @@ module.exports = {
719745
getFirst,
720746
getLast,
721747
getNodeID,
748+
getNodeIndent,
722749
createRule,
723750
hasThrowsTag,
724751
hasJSDocThrowsTag,
752+
typeStringsToUnionString,
725753
typesToUnionString,
726754
findClosest,
727755
findParent,

0 commit comments

Comments
 (0)