11// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
22// See LICENSE in the project root for license information.
33
4+ import { IParsedPackageName , PackageName } from '@microsoft/node-core-library' ;
45import { IApiItemReference } from './api/ApiItem' ;
56
67/**
@@ -37,25 +38,6 @@ export interface IApiDefinitionReferenceParts {
3738 memberName : string ;
3839}
3940
40- /**
41- * A scope and package name are semantic information within an API reference expression.
42- * If there is no scope or package, then the corresponding values will be an empty string.
43- *
44- * Example: '@microsoft/Utilities' -> \{ scope: '@microsoft ', package: 'Utilities' \}
45- * Example: 'Utilities' -> \{ scope: '', package: 'Utilities' \}
46- */
47- export interface IScopedPackageName {
48- /**
49- * The scope name of an API reference expression.
50- */
51- scope : string ;
52-
53- /**
54- * The package name of an API reference expression.
55- */
56- package : string ;
57- }
58-
5941/**
6042 * {@inheritdoc IApiDefinitionReferenceParts }
6143 */
@@ -124,9 +106,9 @@ export class ApiDefinitionReference {
124106 let parts : string [ ] | null = apiReferenceExpr . match ( ApiDefinitionReference . _packageRegEx ) ;
125107 if ( parts ) {
126108 // parts[1] is of the form ‘@microsoft/sp-core-library’ or ‘sp-core-library’
127- const scopePackageName : IScopedPackageName = ApiDefinitionReference . parseScopedPackageName ( parts [ 1 ] ) ;
128- apiDefRefParts . scopeName = scopePackageName . scope ;
129- apiDefRefParts . packageName = scopePackageName . package ;
109+ const parsedPackageName : IParsedPackageName = PackageName . parse ( parts [ 1 ] ) ;
110+ apiDefRefParts . scopeName = parsedPackageName . scope ;
111+ apiDefRefParts . packageName = parsedPackageName . unscopedName ;
130112 apiReferenceExpr = parts [ 2 ] ; // e.g. Guid.equals
131113 }
132114
@@ -150,38 +132,17 @@ export class ApiDefinitionReference {
150132 return ApiDefinitionReference . createFromParts ( apiDefRefParts ) ;
151133 }
152134
153- /**
154- * For a scoped NPM package name this separates the scope and package parts. For example:
155- * parseScopedPackageName('@my-scope/myproject') = { scope: '@my -scope', package: 'myproject' }
156- * parseScopedPackageName('myproject') = { scope: '', package: 'myproject' }
157- */
158- public static parseScopedPackageName ( scopedName : string ) : IScopedPackageName {
159- if ( scopedName . substr ( 0 , 1 ) !== '@' ) {
160- return { scope : '' , package : scopedName } ;
161- }
162-
163- const slashIndex : number = scopedName . indexOf ( '/' ) ;
164- if ( slashIndex >= 0 ) {
165- return { scope : scopedName . substr ( 0 , slashIndex ) , package : scopedName . substr ( slashIndex + 1 ) } ;
166- } else {
167- throw new Error ( 'Invalid scoped name: ' + scopedName ) ;
168- }
169- }
170-
171135 /**
172136 * Stringifies the ApiDefinitionReferenceOptions up and including the
173137 * scope and package name.
174138 *
175139 * Example output: '@microsoft/Utilities'
176140 */
177141 public toScopePackageString ( ) : string {
178- let result : string = '' ;
179- if ( this . scopeName ) {
180- result += `${ this . scopeName } /${ this . packageName } ` ;
181- } else if ( this . packageName ) {
182- result += this . packageName ;
142+ if ( ! this . packageName ) {
143+ return '' ;
183144 }
184- return result ;
145+ return PackageName . combineParts ( this . scopeName , this . packageName ) ;
185146 }
186147
187148 /**
0 commit comments