11import * as fsx from 'fs-extra' ;
22import * as os from 'os' ;
33import * as path from 'path' ;
4- import { IDocItem , IDocPackage , IDocClass } from './IDocItem' ;
4+ import { IDocItem , IDocPackage , IDocClass , IDocMember } from './IDocItem' ;
55import { IApiDefinitionReference } from './IApiDefinitionReference' ;
6+ import ApiItem from './definitions/ApiItem' ;
7+ import ApiPackage from './definitions/ApiPackage' ;
8+ import ResolvedApiItem from './ResolvedApiItem' ;
69import JsonFile from './JsonFile' ;
710
811/**
@@ -31,7 +34,6 @@ export interface IParsedScopeName {
3134export default class DocItemLoader {
3235 private _cache : Map < string , IDocPackage > ;
3336 private _projectFolder : string ; // Root directory to check for node modules
34- private _errorHandler : ( message : string ) => void ;
3537
3638 /**
3739 * The projectFolder is the top-level folder containing package.json for a project
@@ -49,36 +51,87 @@ export default class DocItemLoader {
4951 /**
5052 * {@inheritdoc ApiDocumentation.IReferenceResolver }
5153 */
52- public resolve ( apiDefinitionRef : IApiDefinitionReference , reportError : ( message : string ) => void ) : IDocItem {
53- if ( ! apiDefinitionRef ) {
54- reportError ( 'Expected reference within {@inheritdoc} tag' ) ;
55- return undefined ;
54+ public resolve ( apiDefinitionRef : IApiDefinitionReference ,
55+ apiPackage : ApiPackage ,
56+ reportError : ( message : string ) => void ) : ResolvedApiItem {
57+
58+ const packageName : string = path . dirname ( apiPackage . name ) . split ( '/' ) . pop ( ) ;
59+
60+ // Resolution for local references
61+ if ( packageName === apiDefinitionRef . packageName || ! apiDefinitionRef . packageName ) {
62+ return this . resolveLocalReference ( apiDefinitionRef , apiPackage , reportError ) ;
5663 }
57- // Try to load the package given the provided packageName into the cache
64+
65+ // Resolution for references in JSON files
5866 const docPackage : IDocPackage = this . getPackage ( apiDefinitionRef , reportError ) ;
5967
6068 // Check if package was not found
6169 if ( ! docPackage ) {
70+ reportError ( 'API definition was not found in JSON files' ) ;
71+ return undefined ;
72+ }
73+
74+ return this . resolveJsonReference ( apiDefinitionRef , docPackage , reportError ) ;
75+ }
76+
77+ /**
78+ *
79+ */
80+ public resolveLocalReference ( apiDefinitionRef : IApiDefinitionReference ,
81+ apiPackage : ApiPackage ,
82+ reportError : ( message : string ) => void ) : ResolvedApiItem {
83+
84+ const apiItem : ApiItem = apiPackage . getMemberItem ( apiDefinitionRef . exportName ) ;
85+ if ( apiItem ) {
86+ let resolvedApiItem : ResolvedApiItem = ResolvedApiItem . createFromApiItem ( apiItem ) ;
87+
88+ // If memberName exists then check for the existense of the name
89+ if ( apiDefinitionRef . memberName ) {
90+ if ( apiDefinitionRef . memberName in resolvedApiItem . members ) {
91+
92+ // We are certain the return type will be ApiItem
93+ const member : ApiItem | IDocMember = resolvedApiItem . members [ apiDefinitionRef . memberName ] ;
94+ if ( member && member instanceof ApiItem ) {
95+ member . canResolveReferences ( ) ;
96+ resolvedApiItem = ResolvedApiItem . createFromApiItem ( member ) ;
97+ }
98+ } else {
99+ // member name was not found, apiDefinitionRef is invalid
100+ reportError ( `\"${ apiDefinitionRef . memberName } \",` +
101+ ` not found as member of \"${ apiDefinitionRef . exportName } \"` ) ;
102+ return undefined ;
103+ }
104+ }
105+ return resolvedApiItem ;
106+ } else {
107+ reportError ( `Unable to resolve ApiItem: "${ apiDefinitionRef . exportName } "` ) ;
62108 return undefined ;
63109 }
110+ }
111+
112+ public resolveJsonReference ( apiDefinitionRef : IApiDefinitionReference ,
113+ docPackage : IDocPackage ,
114+ reportError : ( message : string ) => void ) : ResolvedApiItem {
64115
65116 if ( apiDefinitionRef . exportName in docPackage . exports ) {
66117 let docItem : IDocItem = docPackage . exports [ apiDefinitionRef . exportName ] ;
67118
68119 // If memberName exists then check for the existense of the name
69120 if ( apiDefinitionRef . memberName ) {
70- if ( apiDefinitionRef . memberName in ( docItem as IDocClass ) . members ) {
121+ if ( apiDefinitionRef . memberName in ( docItem as IDocClass ) . members ) {
71122 docItem = ( docItem as IDocClass ) . members [ apiDefinitionRef . memberName ] ;
72123 } else {
73124 // member name was not found, apiDefinitionRef is invalid
125+ reportError ( 'API member name was not found on the export item' ) ;
74126 return undefined ;
75127 }
76128 }
77129
78130 // Correct doc item was found
79- return docItem ;
131+ return ResolvedApiItem . createFromJson ( docItem ) ;
80132 } else {
81133 // Not found
134+ reportError ( 'API export item was not found in the package' ) ;
82135 return undefined ;
83136 }
84137 }
@@ -103,11 +156,6 @@ export default class DocItemLoader {
103156 return this . _cache . get ( cachePackageName ) ;
104157 }
105158
106- if ( ! apiDefinitionRef . packageName ) {
107- // Local export resolution is currently not supported yet
108- return ;
109- }
110-
111159 // Doesn't exist in cache, attempt to load the json file
112160 const packageJsonFilePath : string = path . join (
113161 this . _projectFolder ,
0 commit comments