1 line
13 KiB
Plaintext
1 line
13 KiB
Plaintext
{"version":3,"file":"ModelReferenceResolver.js","sourceRoot":"","sources":["../../src/model/ModelReferenceResolver.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;AAE3D,4CAAsG;AACtG,8CAA6D;AAI7D,2EAAwE;AACxE,2EAAwE;AAoBxE;;;;;;;GAOG;AACH,MAAa,sBAAsB;IAGjC,YAAmB,QAAkB;QACnC,IAAI,CAAC,SAAS,GAAG,QAAQ,CAAC;IAC5B,CAAC;IAEM,OAAO,CACZ,oBAA6C,EAC7C,cAAmC;QAEnC,MAAM,MAAM,GAAuC;YACjD,eAAe,EAAE,SAAS;YAC1B,YAAY,EAAE,SAAS;SACxB,CAAC;QAEF,IAAI,UAAU,GAA2B,SAAS,CAAC;QAEnD,iCAAiC;QACjC,IAAI,oBAAoB,CAAC,WAAW,KAAK,SAAS,EAAE;YAClD,UAAU,GAAG,IAAI,CAAC,SAAS,CAAC,mBAAmB,CAAC,oBAAoB,CAAC,WAAW,CAAC,CAAC;YAClF,IAAI,UAAU,KAAK,SAAS,EAAE;gBAC5B,MAAM,CAAC,YAAY,GAAG,gBAAgB,oBAAoB,CAAC,WAAW,wBAAwB,CAAC;gBAC/F,OAAO,MAAM,CAAC;aACf;SACF;aAAM;YACL,mEAAmE;YACnE,IAAI,cAAc,KAAK,SAAS,EAAE;gBAChC,UAAU,GAAG,cAAc,CAAC,oBAAoB,EAAE,CAAC;aACpD;YAED,IAAI,UAAU,KAAK,SAAS,EAAE;gBAC5B,MAAM,CAAC,YAAY;oBACjB,sFAAsF;wBACtF,mBAAmB,CAAC;gBACtB,OAAO,MAAM,CAAC;aACf;SACF;QAED,MAAM,UAAU,GAAW,oBAAoB,CAAC,UAAU,IAAI,EAAE,CAAC;QAEjE,MAAM,gBAAgB,GAAiC,UAAU,CAAC,qBAAqB,CAAC,UAAU,CAAC,CAAC;QACpG,IAAI,gBAAgB,CAAC,MAAM,KAAK,CAAC,EAAE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aAAa,CAAC,WAAW,CAAC,EAAE;gBACpD,IAAI,WAAW,CAAC,aAAa,KAAK,qBAAqB,EAAE;oBACvD,eAAe,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;iBACnC;aACF;SACF;QAED,IAAI,eAAe,CAAC,MAAM,KAAK,CAAC,EAAE;YAChC,MAAM,CAAC,YAAY;gBACjB,mBAAmB,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,6BAA6B;oBAC1E,yBAAyB,qBAAqB,GAAG,CAAC;YACpD,OAAO,MAAM,CAAC;SACf;QAED,IAAI,eAAe,CAAC,MAAM,KAAK,CAAC,EAAE;YAChC,MAAM,CAAC,eAAe,GAAG,eAAe,CAAC,CAAC,CAAC,CAAC;YAC5C,OAAO,MAAM,CAAC;SACf;QAED,MAAM,CAAC,YAAY,GAAG,wBAAwB,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,gBAAgB,CAAC;QACzF,OAAO,MAAM,CAAC;IAChB,CAAC;CACF;AArMD,wDAqMC","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 { type DocDeclarationReference, type DocMemberSelector, SelectorKind } from '@microsoft/tsdoc';\nimport { type ApiItem, ApiItemKind } from '../items/ApiItem';\nimport type { ApiModel } from './ApiModel';\nimport type { ApiPackage } from './ApiPackage';\nimport type { ApiEntryPoint } from './ApiEntryPoint';\nimport { ApiItemContainerMixin } from '../mixins/ApiItemContainerMixin';\nimport { ApiParameterListMixin } from '../mixins/ApiParameterListMixin';\n\n/**\n * Result object for {@link ApiModel.resolveDeclarationReference}.\n *\n * @public\n */\nexport interface IResolveDeclarationReferenceResult {\n /**\n * The referenced ApiItem, if the declaration reference could be resolved.\n */\n resolvedApiItem: ApiItem | undefined;\n\n /**\n * If resolvedApiItem is undefined, then this will always contain an error message explaining why the\n * resolution failed.\n */\n errorMessage: string | undefined;\n}\n\n/**\n * This resolves a TSDoc declaration reference by walking the `ApiModel` hierarchy.\n *\n * @remarks\n *\n * This class is analogous to `AstReferenceResolver` from the `@microsoft/api-extractor` project,\n * which resolves declaration references by walking the compiler state.\n */\nexport class ModelReferenceResolver {\n private readonly _apiModel: ApiModel;\n\n public constructor(apiModel: ApiModel) {\n this._apiModel = apiModel;\n }\n\n public resolve(\n declarationReference: DocDeclarationReference,\n contextApiItem: ApiItem | undefined\n ): IResolveDeclarationReferenceResult {\n const result: IResolveDeclarationReferenceResult = {\n resolvedApiItem: undefined,\n errorMessage: undefined\n };\n\n let apiPackage: ApiPackage | undefined = undefined;\n\n // Is this an absolute reference?\n if (declarationReference.packageName !== undefined) {\n apiPackage = this._apiModel.tryGetPackageByName(declarationReference.packageName);\n if (apiPackage === undefined) {\n result.errorMessage = `The package \"${declarationReference.packageName}\" could not be located`;\n return result;\n }\n } else {\n // If the package name is omitted, try to infer it from the context\n if (contextApiItem !== undefined) {\n apiPackage = contextApiItem.getAssociatedPackage();\n }\n\n if (apiPackage === undefined) {\n result.errorMessage =\n `The reference does not include a package name, and the package could not be inferred` +\n ` from the context`;\n return result;\n }\n }\n\n const importPath: string = declarationReference.importPath || '';\n\n const foundEntryPoints: ReadonlyArray<ApiEntryPoint> = apiPackage.findEntryPointsByPath(importPath);\n if (foundEntryPoints.length !== 1) {\n result.errorMessage = `The import path \"${importPath}\" could not be resolved`;\n return result;\n }\n\n let currentItem: ApiItem = foundEntryPoints[0];\n\n // Now search for the member reference\n for (const memberReference of declarationReference.memberReferences) {\n if (memberReference.memberSymbol !== undefined) {\n result.errorMessage = `Symbols are not yet supported in declaration references`;\n return result;\n }\n\n if (memberReference.memberIdentifier === undefined) {\n result.errorMessage = `Missing member identifier`;\n return result;\n }\n\n const identifier: string = memberReference.memberIdentifier.identifier;\n\n if (!ApiItemContainerMixin.isBaseClassOf(currentItem)) {\n // For example, {@link MyClass.myMethod.X} is invalid because methods cannot contain members\n result.errorMessage = `Unable to resolve ${JSON.stringify(\n identifier\n )} because ${currentItem.getScopedNameWithinPackage()} cannot act as a container`;\n return result;\n }\n\n const foundMembers: ReadonlyArray<ApiItem> = currentItem.findMembersByName(identifier);\n if (foundMembers.length === 0) {\n result.errorMessage = `The member reference ${JSON.stringify(identifier)} was not found`;\n return result;\n }\n\n const memberSelector: DocMemberSelector | undefined = memberReference.selector;\n if (memberSelector === undefined) {\n if (foundMembers.length > 1) {\n result.errorMessage = `The member reference ${JSON.stringify(identifier)} was ambiguous`;\n return result;\n }\n currentItem = foundMembers[0];\n } else {\n let memberSelectorResult: IResolveDeclarationReferenceResult;\n switch (memberSelector.selectorKind) {\n case SelectorKind.System:\n memberSelectorResult = this._selectUsingSystemSelector(foundMembers, memberSelector, identifier);\n break;\n case SelectorKind.Index:\n memberSelectorResult = this._selectUsingIndexSelector(foundMembers, memberSelector, identifier);\n break;\n default:\n result.errorMessage = `The selector \"${memberSelector.selector}\" is not a supported selector type`;\n return result;\n }\n if (memberSelectorResult.resolvedApiItem === undefined) {\n return memberSelectorResult;\n }\n currentItem = memberSelectorResult.resolvedApiItem;\n }\n }\n result.resolvedApiItem = currentItem;\n return result;\n }\n\n private _selectUsingSystemSelector(\n foundMembers: ReadonlyArray<ApiItem>,\n memberSelector: DocMemberSelector,\n identifier: string\n ): IResolveDeclarationReferenceResult {\n const result: IResolveDeclarationReferenceResult = {\n resolvedApiItem: undefined,\n errorMessage: undefined\n };\n\n const selectorName: string = memberSelector.selector;\n\n let selectorItemKind: ApiItemKind;\n switch (selectorName) {\n case 'class':\n selectorItemKind = ApiItemKind.Class;\n break;\n case 'enum':\n selectorItemKind = ApiItemKind.Enum;\n break;\n case 'function':\n selectorItemKind = ApiItemKind.Function;\n break;\n case 'interface':\n selectorItemKind = ApiItemKind.Interface;\n break;\n case 'namespace':\n selectorItemKind = ApiItemKind.Namespace;\n break;\n case 'type':\n selectorItemKind = ApiItemKind.TypeAlias;\n break;\n case 'variable':\n selectorItemKind = ApiItemKind.Variable;\n break;\n default:\n result.errorMessage = `Unsupported system selector \"${selectorName}\"`;\n return result;\n }\n\n const matches: ApiItem[] = foundMembers.filter((x) => x.kind === selectorItemKind);\n if (matches.length === 0) {\n result.errorMessage =\n `A declaration for \"${identifier}\" was not found that matches the` +\n ` TSDoc selector \"${selectorName}\"`;\n return result;\n }\n if (matches.length > 1) {\n result.errorMessage = `More than one declaration \"${identifier}\" matches the TSDoc selector \"${selectorName}\"`;\n }\n result.resolvedApiItem = matches[0];\n return result;\n }\n\n private _selectUsingIndexSelector(\n foundMembers: ReadonlyArray<ApiItem>,\n memberSelector: DocMemberSelector,\n identifier: string\n ): IResolveDeclarationReferenceResult {\n const result: IResolveDeclarationReferenceResult = {\n resolvedApiItem: undefined,\n errorMessage: undefined\n };\n\n const selectedMembers: ApiItem[] = [];\n\n const selectorOverloadIndex: number = parseInt(memberSelector.selector, 10);\n for (const foundMember of foundMembers) {\n if (ApiParameterListMixin.isBaseClassOf(foundMember)) {\n if (foundMember.overloadIndex === selectorOverloadIndex) {\n selectedMembers.push(foundMember);\n }\n }\n }\n\n if (selectedMembers.length === 0) {\n result.errorMessage =\n `An overload for ${JSON.stringify(identifier)} was not found that matches` +\n ` the TSDoc selector \":${selectorOverloadIndex}\"`;\n return result;\n }\n\n if (selectedMembers.length === 1) {\n result.resolvedApiItem = selectedMembers[0];\n return result;\n }\n\n result.errorMessage = `The member reference ${JSON.stringify(identifier)} was ambiguous`;\n return result;\n }\n}\n"]} |