1+ /**
2+ * An API definition reference that is used to locate the documentation of exported
3+ * API items that may or may not belong to an external package.
4+ *
5+ * The format of the API definition reference is:
6+ * scopeName/packageName:exportName.memberName
7+ *
8+ * The following are valid API definition references:
9+ * \@microsoft/sp-core-library:DisplayMode
10+ * \@microsoft/sp-core-library:Guid
11+ * \@microsoft/sp-core-library:Guid.equals
12+ * es6-collections:Map
13+ */
14+ export interface IApiDefinintionReferenceParts {
15+ /**
16+ * This is an optional property to denote that a package name is scoped under this name.
17+ * For example, a common case is when having the '@microsoft' scope name in the
18+ * API definition reference: '\@microsoft/sp-core-library'.
19+ */
20+ scopeName : string ;
21+ /**
22+ * The name of the package that the exportName belongs to.
23+ */
24+ packageName : string ;
25+ /**
26+ * The name of the export API item.
27+ */
28+ exportName : string ;
29+ /**
30+ * The name of the member API item.
31+ */
32+ memberName : string ;
33+ }
34+
135/**
236 * A scope and package name are semantic information within an API reference expression.
37+ * If there is no scope or package, then the corresponding values will be an empty string.
38+ *
39+ * Example: '@microsoft/Utilities' -> \{ scope: '@microsoft ', package: 'Utilities' \}
40+ * Example: 'Utilities' -> \{ scope: '', package: 'Utilities' \}
341 */
4- export interface IScopePackageName {
42+ export interface IScopedPackageName {
543 /**
644 * The scope name of an API reference expression.
745 */
@@ -14,17 +52,7 @@ export interface IScopePackageName {
1452}
1553
1654/**
17- * An API definition reference that is used to locate the documentation of exported
18- * API items that may or may not belong to an external package.
19- *
20- * The format of the API definition reference is:
21- * scopeName/packageName:exportName.memberName
22- *
23- * The following are valid API definition references:
24- * \@microsoft/sp-core-library:DisplayMode
25- * \@microsoft/sp-core-library:Guid
26- * \@microsoft/sp-core-library:Guid.equals
27- * es6-collections:Map
55+ * {@inheritdoc IApiDefinintionReferenceParts }
2856 */
2957export default class ApiDefinitionReference {
3058 /**
@@ -44,33 +72,28 @@ export default class ApiDefinitionReference {
4472 private static _exportRegEx : RegExp = / ^ \w + / ;
4573
4674 /**
47- * This is an optional property to denote that a package name is scoped under this name.
48- * For example, a common case is when having the '@microsoft' scope name in the
49- * API definition reference: '\@microsoft/sp-core-library'.
75+ * {@inheritdoc IApiDefinintionReferenceParts.scopeName }
5076 */
5177 public scopeName : string ;
5278 /**
53- * The name of the package that the exportName belongs to.
79+ * { @inheritdoc IApiDefinintionReferenceParts.packageName }
5480 */
5581 public packageName : string ;
5682 /**
57- * The name of the export API item.
83+ * { @inheritdoc IApiDefinintionReferenceParts.exportName }
5884 */
5985 public exportName : string ;
6086 /**
61- * The name of the member API item.
87+ * { @inheritdoc IApiDefinintionReferenceParts.memberName }
6288 */
6389 public memberName : string ;
6490
6591 /**
6692 * Creates an ApiDefinitionReference instance given strings that symbolize the public
6793 * properties of ApiDefinitionReference.
6894 */
69- public static createFromParts ( scopeName : string ,
70- packageName : string ,
71- exportName : string ,
72- memberName : string ) : ApiDefinitionReference {
73- return new ApiDefinitionReference ( scopeName , packageName , exportName , memberName ) ;
95+ public static createFromParts ( parts : IApiDefinintionReferenceParts ) : ApiDefinitionReference {
96+ return new ApiDefinitionReference ( parts ) ;
7497 }
7598
7699 /**
@@ -86,26 +109,28 @@ export default class ApiDefinitionReference {
86109 return ;
87110 }
88111
89- let scopeName : string = '' ;
90- let packageName : string = '' ;
91- let exportName : string = '' ;
92- let memberName : string = '' ;
112+ const apiDefRefParts : IApiDefinintionReferenceParts = {
113+ scopeName : '' ,
114+ packageName : '' ,
115+ exportName : '' ,
116+ memberName : ''
117+ } ;
93118
94119 // E.g. @microsoft /sp-core-library:Guid.equals
95120 let parts : string [ ] = apiReferenceExpr . match ( ApiDefinitionReference . _packageRegEx ) ;
96121 if ( parts ) {
97122 // parts[1] is of the form ‘@microsoft/sp-core-library’ or ‘sp-core-library’
98- const scopePackageName : IScopePackageName = ApiDefinitionReference . parseScopedPackageName ( parts [ 1 ] ) ;
99- scopeName = scopePackageName . scope ;
100- packageName = scopePackageName . package ;
123+ const scopePackageName : IScopedPackageName = ApiDefinitionReference . parseScopedPackageName ( parts [ 1 ] ) ;
124+ apiDefRefParts . scopeName = scopePackageName . scope ;
125+ apiDefRefParts . packageName = scopePackageName . package ;
101126 apiReferenceExpr = parts [ 2 ] ; // e.g. Guid.equals
102127 }
103128
104129 // E.g. Guid.equals
105130 parts = apiReferenceExpr . match ( ApiDefinitionReference . _memberRegEx ) ;
106131 if ( parts ) {
107- exportName = parts [ 1 ] ; // e.g. Guid, can never be undefined
108- memberName = parts [ 2 ] ? parts [ 2 ] : '' ; // e.g. equals
132+ apiDefRefParts . exportName = parts [ 1 ] ; // e.g. Guid, can never be undefined
133+ apiDefRefParts . memberName = parts [ 2 ] ? parts [ 2 ] : '' ; // e.g. equals
109134 } else {
110135 // the export name is required
111136 throw reportError ( `Api reference expression contains invalid characters: ${ apiReferenceExpr } ` ) ;
@@ -115,15 +140,15 @@ export default class ApiDefinitionReference {
115140 throw reportError ( `Api reference expression contains invalid characters: ${ apiReferenceExpr } ` ) ;
116141 }
117142
118- return ApiDefinitionReference . createFromParts ( scopeName , packageName , exportName , memberName ) ;
143+ return ApiDefinitionReference . createFromParts ( apiDefRefParts ) ;
119144 }
120145
121146 /**
122147 * For a scoped NPM package name this separates the scope and package parts. For example:
123148 * parseScopedPackageName('@my-scope/myproject') = { scope: '@my -scope', package: 'myproject' }
124149 * parseScopedPackageName('myproject') = { scope: '', package: 'myproject' }
125150 */
126- public static parseScopedPackageName ( scopedName : string ) : IScopePackageName {
151+ public static parseScopedPackageName ( scopedName : string ) : IScopedPackageName {
127152 if ( scopedName . substr ( 0 , 1 ) !== '@' ) {
128153 return { scope : '' , package : scopedName } ;
129154 }
@@ -136,25 +161,11 @@ export default class ApiDefinitionReference {
136161 }
137162 }
138163
139- /**
140- * Seperates the name property of an ApiPackage into a scope name and the actual
141- * name of the package.
142- */
143- public static parseApiPackageName ( apiPackageName : string ) : IScopePackageName {
144- const slashIndex : number = apiPackageName . indexOf ( '/' ) ;
145- if ( slashIndex >= 0 ) {
146- return {
147- scope : apiPackageName . substr ( 0 , slashIndex ) ,
148- package : apiPackageName . substr ( slashIndex + 1 )
149- } ;
150- } else {
151- return { scope : '' , package : apiPackageName . substr ( slashIndex + 1 ) } ;
152- }
153- }
154-
155164 /**
156165 * Stringifies the ApiDefinitionReferenceOptions up and including the
157166 * scope and package name.
167+ *
168+ * Example output: '@microsoft/Utilities'
158169 */
159170 public toScopePackageString ( ) : string {
160171 let result : string = '' ;
@@ -169,6 +180,8 @@ export default class ApiDefinitionReference {
169180 /**
170181 * Stringifies the ApiDefinitionReferenceOptions up and including the
171182 * scope, package and export name.
183+ *
184+ * Example output: '@microsoft/Utilities.Parse'
172185 */
173186 public toExportString ( ) : string {
174187 let result : string = this . toScopePackageString ( ) ;
@@ -181,15 +194,17 @@ export default class ApiDefinitionReference {
181194 /**
182195 * Stringifies the ApiDefinitionReferenceOptions up and including the
183196 * scope, package, export and member name.
197+ *
198+ * Example output: '@microsoft/Utilities.Parse.parseJsonToString'
184199 */
185200 public toMemberString ( ) : string {
186201 return this . toExportString ( ) + `.${ this . memberName } ` ;
187202 }
188203
189- private constructor ( scopeName : string , packageName : string , exportName : string , memberName : string ) {
190- this . scopeName = scopeName ;
191- this . packageName = packageName ;
192- this . exportName = exportName ;
193- this . memberName = memberName ;
204+ private constructor ( parts : IApiDefinintionReferenceParts ) {
205+ this . scopeName = parts . scopeName ;
206+ this . packageName = parts . packageName ;
207+ this . exportName = parts . exportName ;
208+ this . memberName = parts . memberName ;
194209 }
195210}
0 commit comments