diff --git a/build-tools/intToNumber.js b/build-tools/intToNumber.js index 310be81d728c6ce95e91c9d654e8deeb8e11aa8f..92a43c7f00d84e6b899ec179176da281ab2eb6d3 100644 --- a/build-tools/intToNumber.js +++ b/build-tools/intToNumber.js @@ -299,11 +299,15 @@ function parseJSDocVisitEachChild1(context, node) { * @param {content} 文本内容 */ function applJSDocTransformations(typeExpr, newTypeExpr, tagDataList, isChange) { + if (!typeExpr && !newTypeExpr) { + return; + } const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed }); const finalContent = printer.printNode(ts.EmitHint.Unspecified, newTypeExpr); if (finalContent.includes('number') && typeExpr.kind === ts.SyntaxKind.JSDocNullableType && !finalContent.includes('?number') && isChange) { if (typeExpr.type.type && typeExpr.type.type.kind === ts.SyntaxKind.UnionType) { const data = { + isDelete: false, pos: typeExpr.pos, end: typeExpr.end, convertedText: '?' + `(${finalContent})` @@ -311,6 +315,7 @@ function applJSDocTransformations(typeExpr, newTypeExpr, tagDataList, isChange) tagDataList.push(data); } else { const data = { + isDelete: false, pos: typeExpr.pos, end: typeExpr.end, convertedText: '?' + finalContent @@ -319,6 +324,7 @@ function applJSDocTransformations(typeExpr, newTypeExpr, tagDataList, isChange) } } else if (finalContent.includes('number')) { const data = { + isDelete: true, pos: typeExpr.pos, end: typeExpr.end, convertedText: finalContent @@ -338,7 +344,11 @@ function changeContent(tagDataList) { for (const data of tagDataList) { const before = jsDocContent.substring(0, data.pos); const after = jsDocContent.substring(data.end); - jsDocContent = before + ` ${data.convertedText}` + after; + if (data.isDelete) { + jsDocContent = before + `${data.convertedText}` + after; + } else { + jsDocContent = before + ` ${data.convertedText}` + after; + } } } @@ -374,8 +384,13 @@ function parseJSDocVisitEachChild2(context, node) { } function writeDataToFile(tag) { const typeExpr = tag.typeExpression; - const newTypeExpr = parseTypeExpression(typeExpr.type); - applJSDocTransformations(typeExpr.type, newTypeExpr, tagDataList, false); + if (ts.isJSDocNullableType(node) && typeExpr.type.type) { + const newTypeExpr = parseTypeExpression(typeExpr.type.type); + applJSDocTransformations(typeExpr.type.type, newTypeExpr, tagDataList, false); + } else { + const newTypeExpr = parseTypeExpression(typeExpr.type); + applJSDocTransformations(typeExpr.type, newTypeExpr, tagDataList, false); + } } function parseTypeExpression(node) { if (ts.isUnionTypeNode(node)) {