utils/node_modules/@microsoft/api-extractor/lib/generators/DtsEmitHelpers.js.map
2024-02-07 01:33:07 -05:00

1 line
12 KiB
Plaintext

{"version":3,"file":"DtsEmitHelpers.js","sourceRoot":"","sources":["../../src/generators/DtsEmitHelpers.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;;;;;;;;;;;;;;;;;;;;;;;;AAE3D,+CAAiC;AAEjC,oEAA6D;AAE7D,qDAAiE;AACjE,+DAA4D;AAI5D,yFAAsF;AAEtF;;GAEG;AACH,MAAa,cAAc;IAClB,MAAM,CAAC,UAAU,CACtB,MAAsB,EACtB,eAAgC,EAChC,SAAoB;QAEpB,MAAM,YAAY,GAAW,SAAS,CAAC,oBAAoB,CAAC,CAAC,CAAC,aAAa,CAAC,CAAC,CAAC,QAAQ,CAAC;QAEvF,QAAQ,SAAS,CAAC,UAAU,EAAE;YAC5B,KAAK,yBAAa,CAAC,aAAa;gBAC9B,IAAI,eAAe,CAAC,WAAW,KAAK,SAAS,CAAC,UAAU,EAAE;oBACxD,MAAM,CAAC,KAAK,CAAC,GAAG,YAAY,iBAAiB,eAAe,CAAC,WAAW,IAAI,CAAC,CAAC;iBAC/E;qBAAM;oBACL,MAAM,CAAC,KAAK,CAAC,GAAG,YAAY,IAAI,SAAS,CAAC,UAAU,EAAE,CAAC,CAAC;iBACzD;gBACD,MAAM,CAAC,SAAS,CAAC,UAAU,SAAS,CAAC,UAAU,IAAI,CAAC,CAAC;gBACrD,MAAM;YACR,KAAK,yBAAa,CAAC,WAAW;gBAC5B,IAAI,eAAe,CAAC,WAAW,KAAK,SAAS,CAAC,UAAU,EAAE;oBACxD,MAAM,CAAC,KAAK,CAAC,GAAG,YAAY,MAAM,SAAS,CAAC,UAAU,IAAI,CAAC,CAAC;iBAC7D;qBAAM;oBACL,MAAM,CAAC,KAAK,CAAC,GAAG,YAAY,MAAM,SAAS,CAAC,UAAU,OAAO,eAAe,CAAC,WAAW,IAAI,CAAC,CAAC;iBAC/F;g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S,YAAY,qBAAS;gBAC/C,gBAAgB,CAAC,SAAS,CAAC,UAAU,KAAK,yBAAa,CAAC,UAAU;gBAClE,gBAAgB,CAAC,SAAS,CAAC,UAAU,EACrC;gBACA,gFAAgF;gBAChF,mEAAmE;gBACnE,MAAM,cAAc,GAAW,MAAA,MAAA,IAAI,CAAC,SAAS,0CAAE,OAAO,EAAE,mCAAI,EAAE,CAAC;gBAC/D,MAAM,qBAAqB,GAAW,cAAc,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;gBAClE,4BAA4B;gBAC5B,MAAM,oBAAoB,GACxB,qBAAqB,IAAI,CAAC,CAAC,CAAC,CAAC,cAAc,CAAC,SAAS,CAAC,qBAAqB,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;gBAEpF,MAAM,WAAW,GAAW,GAAG,gBAAgB,CAAC,WAAW,GAAG,oBAAoB,GAAG,iBAAiB,GAAG,cAAc,EAAE,CAAC;gBAE1H,IAAI,CAAC,YAAY,CAAC,OAAO,EAAE,CAAC;gBAC5B,IAAI,CAAC,YAAY,CAAC,MAAM,GAAG,WAAW,CAAC;aACxC;iBAAM;gBACL,4CAA4C;gBAC5C,IAAI,CAAC,YAAY,CAAC,OAAO,EAAE,CAAC;gBAC5B,IAAI,CAAC,YAAY,CAAC,MAAM,GAAG,GAAG,gBAAgB,CAAC,WAAW,GAAG,iBAAiB,GAAG,cAAc,EAAE,CAAC;aACnG;SACF;IACH,CAAC;CACF;AA3JD,wCA2JC","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\nimport * as ts from 'typescript';\n\nimport { InternalError } from '@rushstack/node-core-library';\nimport type { CollectorEntity } from '../collector/CollectorEntity';\nimport { AstImport, AstImportKind } from '../analyzer/AstImport';\nimport { AstDeclaration } from '../analyzer/AstDeclaration';\nimport type { Collector } from '../collector/Collector';\nimport type { Span } from '../analyzer/Span';\nimport type { IndentedWriter } from './IndentedWriter';\nimport { SourceFileLocationFormatter } from '../analyzer/SourceFileLocationFormatter';\n\n/**\n * Some common code shared between DtsRollupGenerator and ApiReportGenerator.\n */\nexport class DtsEmitHelpers {\n public static emitImport(\n writer: IndentedWriter,\n collectorEntity: CollectorEntity,\n astImport: AstImport\n ): void {\n const importPrefix: string = astImport.isTypeOnlyEverywhere ? 'import type' : 'import';\n\n switch (astImport.importKind) {\n case AstImportKind.DefaultImport:\n if (collectorEntity.nameForEmit !== astImport.exportName) {\n writer.write(`${importPrefix} { default as ${collectorEntity.nameForEmit} }`);\n } else {\n writer.write(`${importPrefix} ${astImport.exportName}`);\n }\n writer.writeLine(` from '${astImport.modulePath}';`);\n break;\n case AstImportKind.NamedImport:\n if (collectorEntity.nameForEmit === astImport.exportName) {\n writer.write(`${importPrefix} { ${astImport.exportName} }`);\n } else {\n writer.write(`${importPrefix} { ${astImport.exportName} as ${collectorEntity.nameForEmit} }`);\n }\n writer.writeLine(` from '${astImport.modulePath}';`);\n break;\n case AstImportKind.StarImport:\n writer.writeLine(\n `${importPrefix} * as ${collectorEntity.nameForEmit} from '${astImport.modulePath}';`\n );\n break;\n case AstImportKind.EqualsImport:\n writer.writeLine(\n `${importPrefix} ${collectorEntity.nameForEmit} = require('${astImport.modulePath}');`\n );\n break;\n case AstImportKind.ImportType:\n if (!astImport.exportName) {\n writer.writeLine(\n `${importPrefix} * as ${collectorEntity.nameForEmit} from '${astImport.modulePath}';`\n );\n } else {\n const topExportName: string = astImport.exportName.split('.')[0];\n if (collectorEntity.nameForEmit === topExportName) {\n writer.write(`${importPrefix} { ${topExportName} }`);\n } else {\n writer.write(`${importPrefix} { ${topExportName} as ${collectorEntity.nameForEmit} }`);\n }\n writer.writeLine(` from '${astImport.modulePath}';`);\n }\n break;\n default:\n throw new InternalError('Unimplemented AstImportKind');\n }\n }\n\n public static emitNamedExport(\n writer: IndentedWriter,\n exportName: string,\n collectorEntity: CollectorEntity\n ): void {\n if (exportName === ts.InternalSymbolName.Default) {\n writer.writeLine(`export default ${collectorEntity.nameForEmit};`);\n } else if (collectorEntity.nameForEmit !== exportName) {\n writer.writeLine(`export { ${collectorEntity.nameForEmit} as ${exportName} }`);\n } else {\n writer.writeLine(`export { ${exportName} }`);\n }\n }\n\n public static emitStarExports(writer: IndentedWriter, collector: Collector): void {\n if (collector.starExportedExternalModulePaths.length > 0) {\n writer.writeLine();\n for (const starExportedExternalModulePath of collector.starExportedExternalModulePaths) {\n writer.writeLine(`export * from \"${starExportedExternalModulePath}\";`);\n }\n }\n }\n\n public static modifyImportTypeSpan(\n collector: Collector,\n span: Span,\n astDeclaration: AstDeclaration,\n modifyNestedSpan: (childSpan: Span, childAstDeclaration: AstDeclaration) => void\n ): void {\n const node: ts.ImportTypeNode = span.node as ts.ImportTypeNode;\n const referencedEntity: CollectorEntity | undefined = collector.tryGetEntityForNode(node);\n\n if (referencedEntity) {\n if (!referencedEntity.nameForEmit) {\n // This should never happen\n\n throw new InternalError('referencedEntry.nameForEmit is undefined');\n }\n\n let typeArgumentsText: string = '';\n\n if (node.typeArguments && node.typeArguments.length > 0) {\n // Type arguments have to be processed and written to the document\n const lessThanTokenPos: number = span.children.findIndex(\n (childSpan) => childSpan.node.kind === ts.SyntaxKind.LessThanToken\n );\n const greaterThanTokenPos: number = span.children.findIndex(\n (childSpan) => childSpan.node.kind === ts.SyntaxKind.GreaterThanToken\n );\n\n if (lessThanTokenPos < 0 || greaterThanTokenPos <= lessThanTokenPos) {\n throw new InternalError(\n `Invalid type arguments: ${node.getText()}\\n` +\n SourceFileLocationFormatter.formatDeclaration(node)\n );\n }\n\n const typeArgumentsSpans: Span[] = span.children.slice(lessThanTokenPos + 1, greaterThanTokenPos);\n\n // Apply modifications to Span elements of typeArguments\n typeArgumentsSpans.forEach((childSpan) => {\n const childAstDeclaration: AstDeclaration = AstDeclaration.isSupportedSyntaxKind(childSpan.kind)\n ? collector.astSymbolTable.getChildAstDeclarationByNode(childSpan.node, astDeclaration)\n : astDeclaration;\n\n modifyNestedSpan(childSpan, childAstDeclaration);\n });\n\n const typeArgumentsStrings: string[] = typeArgumentsSpans.map((childSpan) =>\n childSpan.getModifiedText()\n );\n typeArgumentsText = `<${typeArgumentsStrings.join(', ')}>`;\n }\n\n const separatorAfter: string = /(\\s*)$/.exec(span.getText())?.[1] ?? '';\n\n if (\n referencedEntity.astEntity instanceof AstImport &&\n referencedEntity.astEntity.importKind === AstImportKind.ImportType &&\n referencedEntity.astEntity.exportName\n ) {\n // For an ImportType with a namespace chain, only the top namespace is imported.\n // Must add the original nested qualifiers to the rolled up import.\n const qualifiersText: string = node.qualifier?.getText() ?? '';\n const nestedQualifiersStart: number = qualifiersText.indexOf('.');\n // Including the leading \".\"\n const nestedQualifiersText: string =\n nestedQualifiersStart >= 0 ? qualifiersText.substring(nestedQualifiersStart) : '';\n\n const replacement: string = `${referencedEntity.nameForEmit}${nestedQualifiersText}${typeArgumentsText}${separatorAfter}`;\n\n span.modification.skipAll();\n span.modification.prefix = replacement;\n } else {\n // Replace with internal symbol or AstImport\n span.modification.skipAll();\n span.modification.prefix = `${referencedEntity.nameForEmit}${typeArgumentsText}${separatorAfter}`;\n }\n }\n }\n}\n"]}