Skip to content

Commit 4873b12

Browse files
committed
fix: make validation agnostic of union order
1 parent 7c82a21 commit 4873b12

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

lint-rules/validate-jsdoc-codeblocks.js

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -229,18 +229,14 @@ function normalizeUnions(type) {
229229
const typeNode = sourceFile.statements[0].declarationList.declarations[0].type;
230230

231231
const print = node => ts.createPrinter().printNode(ts.EmitHint.Unspecified, node, sourceFile);
232-
const isNumeric = v => v.trim() !== '' && Number.isFinite(Number(v));
233232

234233
const visit = node => {
235234
node = ts.visitEachChild(node, visit, undefined);
236235

237236
if (ts.isUnionTypeNode(node)) {
238237
const types = node.types
239238
.map(t => [print(t), t])
240-
.sort(([a], [b]) =>
241-
// Numbers are sorted only wrt other numbers
242-
isNumeric(a) && isNumeric(b) ? Number(a) - Number(b) : 0,
243-
)
239+
.sort(([a], [b]) => a < b ? -1 : 1)
244240
.map(t => t[1]);
245241

246242
return ts.factory.updateUnionTypeNode(
@@ -311,19 +307,10 @@ function validateTwoslashTypes(context, env, code, codeStartIndex) {
311307
const quickInfo = env.languageService.getQuickInfoAtPosition(FILENAME, previousLineOffset + i);
312308

313309
if (quickInfo?.displayParts) {
314-
let expectedType = normalizeUnions(extractTypeFromQuickInfo(quickInfo));
315-
316-
if (expectedType.length < 80) {
317-
expectedType = expectedType
318-
.replaceAll(/\r?\n\s*/g, ' ') // Collapse into single line
319-
.replaceAll(/{\s+/g, '{') // Remove spaces after `{`
320-
.replaceAll(/\s+}/g, '}') // Remove spaces before `}`
321-
.replaceAll(/;(?=})/g, ''); // Remove semicolons before `}`
322-
}
323-
324-
const expectedComment = TWOSLASH_COMMENT + ' ' + expectedType.replaceAll('\n', '\n// ');
310+
const expectedType = normalizeUnions(extractTypeFromQuickInfo(quickInfo));
311+
const actualType = normalizeUnions(actualComment.slice(TWOSLASH_COMMENT.length).replaceAll('\n// ', '\n'));
325312

326-
if (actualComment !== expectedComment) {
313+
if (actualType !== expectedType) {
327314
const actualCommentIndex = line.indexOf(TWOSLASH_COMMENT);
328315

329316
const actualCommentStartOffset = sourceFile.getPositionOfLineAndCharacter(index, actualCommentIndex);
@@ -332,6 +319,16 @@ function validateTwoslashTypes(context, env, code, codeStartIndex) {
332319
const start = codeStartIndex + actualCommentStartOffset;
333320
const end = codeStartIndex + actualCommentEndOffset;
334321

322+
const collapsedExpectedType = expectedType.length >= 80
323+
? expectedType
324+
: expectedType
325+
.replaceAll(/\r?\n\s*/g, ' ') // Collapse into single line
326+
.replaceAll(/{\s+/g, '{') // Remove spaces after `{`
327+
.replaceAll(/\s+}/g, '}') // Remove spaces before `}`
328+
.replaceAll(/;(?=})/g, ''); // Remove semicolons before `}`
329+
330+
const expectedComment = TWOSLASH_COMMENT + ' ' + collapsedExpectedType.replaceAll('\n', '\n// ');
331+
335332
context.report({
336333
loc: {
337334
start: context.sourceCode.getLocFromIndex(start),

0 commit comments

Comments
 (0)