Skip to content

Commit 9db55aa

Browse files
committed
refactor: extract logic into function
1 parent 480f7f9 commit 9db55aa

File tree

1 file changed

+48
-38
lines changed

1 file changed

+48
-38
lines changed

lint-rules/validate-jsdoc-codeblocks.js

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,53 @@ export const validateJSDocCodeblocksRule = /** @type {const} */ ({
186186
},
187187
});
188188

189+
function extractTypeFromQuickInfo(quickInfo) {
190+
let depth = 0;
191+
const separatorIndex = quickInfo.displayParts.findIndex(part => {
192+
if (part.kind === 'punctuation') {
193+
if (['(', '{', '<'].includes(part.text)) {
194+
depth++;
195+
} else if ([')', '}', '>'].includes(part.text)) {
196+
depth--;
197+
} else if (part.text === ':' && depth === 0) {
198+
return true;
199+
}
200+
} else if (part.kind === 'operator' && part.text === '=' && depth === 0) {
201+
return true;
202+
}
203+
204+
return false;
205+
});
206+
207+
let partsToUse = quickInfo.displayParts;
208+
if (separatorIndex !== -1) {
209+
partsToUse = quickInfo.displayParts.slice(separatorIndex + 1);
210+
}
211+
212+
return partsToUse
213+
.map((part, index) => {
214+
const {kind, text} = part;
215+
216+
// Replace spaces used for indentation with tabs
217+
const previousPart = partsToUse[index - 1];
218+
if (kind === 'space' && (index === 0 || previousPart?.kind === 'lineBreak')) {
219+
return text.replaceAll(' ', '\t');
220+
}
221+
222+
// Replace double-quoted string literals with single-quoted ones
223+
if (kind === 'stringLiteral' && text.startsWith('"') && text.endsWith('"')) {
224+
return `'${text
225+
.slice(1, -1)
226+
.replaceAll(String.raw`\"`, '"')
227+
.replaceAll('\'', String.raw`\'`)}'`;
228+
}
229+
230+
return text;
231+
})
232+
.join('')
233+
.trim();
234+
}
235+
189236
function validateTwoslashTypes(context, env, code, codeStartIndex) {
190237
const sourceFile = env.languageService.getProgram().getSourceFile(FILENAME);
191238
const lines = code.split('\n');
@@ -221,44 +268,7 @@ function validateTwoslashTypes(context, env, code, codeStartIndex) {
221268
const quickInfo = env.languageService.getQuickInfoAtPosition(FILENAME, previousLineOffset + i);
222269

223270
if (quickInfo?.displayParts) {
224-
let depth = 0;
225-
const separatorIndex = quickInfo.displayParts.findIndex(part => {
226-
if (part.kind === 'punctuation') {
227-
if (['(', '{', '<'].includes(part.text)) {
228-
depth++;
229-
} else if ([')', '}', '>'].includes(part.text)) {
230-
depth--;
231-
} else if (part.text === ':' && depth === 0) {
232-
return true;
233-
}
234-
} else if (part.kind === 'operator' && part.text === '=' && depth === 0) {
235-
return true;
236-
}
237-
238-
return false;
239-
});
240-
241-
let partsToUse = quickInfo.displayParts;
242-
if (separatorIndex !== -1) {
243-
partsToUse = quickInfo.displayParts.slice(separatorIndex + 1);
244-
}
245-
246-
let expectedType = partsToUse.map((part, index) => {
247-
const {kind, text} = part;
248-
249-
// Replace spaces used for indentation with tabs
250-
const previousPart = partsToUse[index - 1];
251-
if (kind === 'space' && (index === 0 || previousPart?.kind === 'lineBreak')) {
252-
return text.replaceAll(' ', '\t');
253-
}
254-
255-
// Replace double-quoted string literals with single-quoted ones
256-
if (kind === 'stringLiteral' && text.startsWith('"') && text.endsWith('"')) {
257-
return `'${text.slice(1, -1).replaceAll(String.raw`\"`, '"').replaceAll('\'', String.raw`\'`)}'`;
258-
}
259-
260-
return text;
261-
}).join('').trim();
271+
let expectedType = extractTypeFromQuickInfo(quickInfo);
262272

263273
if (expectedType.length < 80) {
264274
expectedType = expectedType

0 commit comments

Comments
 (0)