From 5aa4116204f080ea6241916388fd49c4d67f77bd Mon Sep 17 00:00:00 2001 From: Dmitry Gozman Date: Mon, 17 Aug 2020 10:47:21 -0700 Subject: [PATCH] docs: sort all enums in doclint (#3488) Currently, the order depends on some internals of typescript compiler and changes from time to time. Sorting makes it stable. --- utils/doclint/check_public_api/JSBuilder.js | 15 ++++++++++----- utils/doclint/check_public_api/MDBuilder.js | 7 ++++++- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/utils/doclint/check_public_api/JSBuilder.js b/utils/doclint/check_public_api/JSBuilder.js index b287e1233f..0c01f47100 100644 --- a/utils/doclint/check_public_api/JSBuilder.js +++ b/utils/doclint/check_public_api/JSBuilder.js @@ -197,11 +197,16 @@ function checkSources(sources) { return new Documentation.Type('Object', properties); } if (type.isUnion() && (typeName.includes('|') || type.types.every(type => type.isStringLiteral() || type.intrinsicName === 'number'))) { - const types = type.types - .filter(isNotUndefined) - .map(type => serializeType(type, circular)); - const name = types.map(type => type.name).join('|'); - const properties = [].concat(...types.map(type => type.properties)); + const types = type.types.filter(isNotUndefined).map((type, index) => { + return { isLiteral: type.isStringLiteral(), serialized: serializeType(type, circular), index }; + }); + types.sort((a, b) => { + if (!a.isLiteral || !b.isLiteral) + return a.index - b.index; + return a.serialized.name.localeCompare(b.serialized.name); + }); + const name = types.map(type => type.serialized.name).join('|'); + const properties = [].concat(...types.map(type => type.serialized.properties)); return new Documentation.Type(name.replace(/false\|true/g, 'boolean'), properties); } if (type.typeArguments && type.symbol) { diff --git a/utils/doclint/check_public_api/MDBuilder.js b/utils/doclint/check_public_api/MDBuilder.js index 7899b93e4c..3179dca194 100644 --- a/utils/doclint/check_public_api/MDBuilder.js +++ b/utils/doclint/check_public_api/MDBuilder.js @@ -53,7 +53,12 @@ class MDOutline { const ul = clone.querySelector(':scope > ul'); const str = parseComment(extractSiblingsIntoFragment(clone.firstChild, ul)); const name = str.substring(0, str.indexOf('<')).replace(/\`/g, '').trim(); - const type = findType(str); + let type = findType(str); + const literals = type.match(/("[^"]+"(\|"[^"]+")*)/); + if (literals) { + const sorted = literals[1].split('|').sort((a, b) => a.localeCompare(b)).join('|'); + type = type.substring(0, literals.index) + sorted + type.substring(literals.index + literals[0].length); + } const properties = []; let comment = str.substring(str.indexOf('<') + type.length + 2).trim(); const hasNonEnumProperties = type.split('|').some(part => {