Skip to content

Commit 7c394ff

Browse files
committed
Convert PackageJsonHelpers into a instance object
1 parent 933a763 commit 7c394ff

5 files changed

Lines changed: 20 additions & 14 deletions

File tree

‎common/reviews/api/api-extractor.api.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ class Extractor {
9797
// (undocumented)
9898
public package: ApiPackage;
9999
public readonly packageFolder: string;
100+
// WARNING: The type "PackageJsonLookup" needs to be exported by the package (e.g. added to index.ts)
101+
// (undocumented)
102+
public packageJsonLookup: PackageJsonLookup;
100103
public reportError(message: string, sourceFile: ts.SourceFile, start: number): void;
101104
// (undocumented)
102105
public typeChecker: ts.TypeChecker;

‎libraries/api-extractor/src/Extractor.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as fsx from 'fs-extra';
33
import * as path from 'path';
44
import ApiPackage from './definitions/ApiPackage';
55
import DocItemLoader from './DocItemLoader';
6-
import PackageJsonHelpers from './PackageJsonHelpers';
6+
import PackageJsonLookup from './PackageJsonLookup';
77

88
export type ApiErrorHandler = (message: string, fileName: string, lineNumber: number) => void;
99

@@ -50,14 +50,16 @@ export interface IExtractorAnalyzeOptions {
5050
* abstract syntax tree.
5151
*/
5252
export default class Extractor {
53-
public errorHandler: ApiErrorHandler;
53+
public readonly errorHandler: ApiErrorHandler;
5454
public typeChecker: ts.TypeChecker;
5555
public package: ApiPackage;
5656
/**
5757
* One DocItemLoader is needed per analyzer to look up external API members
5858
* as needed.
5959
*/
60-
public docItemLoader: DocItemLoader;
60+
public readonly docItemLoader: DocItemLoader;
61+
62+
public readonly packageJsonLookup: PackageJsonLookup;
6163

6264
private _compilerOptions: ts.CompilerOptions;
6365

@@ -75,6 +77,7 @@ export default class Extractor {
7577
constructor(options: IExtractorOptions) {
7678
this._compilerOptions = options.compilerOptions;
7779
this.docItemLoader = new DocItemLoader(options.compilerOptions.rootDir);
80+
this.packageJsonLookup = new PackageJsonLookup();
7881
this.errorHandler = options.errorHandler || Extractor.defaultErrorHandler;
7982
}
8083

@@ -110,7 +113,7 @@ export default class Extractor {
110113
// Assign _packageFolder by probing upwards from entryPointFile until we find a package.json
111114
const currentPath: string = path.resolve(options.entryPointFile);
112115
// This is guaranteed to succeed since we do check prior to this point
113-
this._packageFolder = PackageJsonHelpers.tryFindPackagePathUpwards(currentPath);
116+
this._packageFolder = this.packageJsonLookup.tryFindPackagePathUpwards(currentPath);
114117

115118
this.package = new ApiPackage(this, rootFile); // construct members
116119
this.package.completeInitialization(); // creates ApiDocumentation

‎libraries/api-extractor/src/PackageJsonLookup.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ interface IPackageJson {
1212
}
1313

1414
/**
15-
* Utilities for navigating packages.
15+
* This class provides methods for finding the nearest "package.json" for a folder
16+
* and retrieving the name of the package. The results are cached.
1617
*/
17-
export default class PackageJsonHelpers {
18-
18+
export default class PackageJsonLookup {
1919
/**
2020
* Finds the path to the package folder of a given currentPath, by probing
2121
* upwards from the currentPath until a package.json file is found.
@@ -24,7 +24,7 @@ export default class PackageJsonHelpers {
2424
* @param currentPath - a path (relative or absolute) of the current location
2525
* @returns a relative path to the package folder
2626
*/
27-
public static tryFindPackagePathUpwards(currentPath: string): string {
27+
public tryFindPackagePathUpwards(currentPath: string): string {
2828
let packageFolder: string = '';
2929

3030
// no-constant-condition
@@ -49,7 +49,7 @@ export default class PackageJsonHelpers {
4949
* package.json file, it does not include the 'package.json' suffix.
5050
* @returns the name of the package (E.g. @microsoft/api-extractor)
5151
*/
52-
public static readPackageName(packageJsonPath: string): string {
52+
public readPackageName(packageJsonPath: string): string {
5353
const packageJson: IPackageJson = JsonFile.loadJsonFile(path.join(packageJsonPath, 'package.json')) as IPackageJson;
5454
return packageJson.name;
5555
}

‎libraries/api-extractor/src/definitions/ApiItem.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import Extractor from '../Extractor';
77
import ApiDocumentation, { ApiTag } from './ApiDocumentation';
88
import TypeScriptHelpers from '../TypeScriptHelpers';
99
import DocElementParser from '../DocElementParser';
10-
import PackageJsonHelpers from '../PackageJsonHelpers';
1110
import ResolvedApiItem from '../ResolvedApiItem';
1211
import ApiDefinitionReference,
1312
{ IScopedPackageName, IApiDefinintionReferenceParts } from '../ApiDefinitionReference';
@@ -503,7 +502,8 @@ abstract class ApiItem {
503502
// Walk upwards from that directory until you find a directory containing package.json,
504503
// this is where the referenced type is located.
505504
// Example: "c:\users\<username>\sp-client\spfx-core\sp-core-library"
506-
const typeReferencePackagePath: string = PackageJsonHelpers.tryFindPackagePathUpwards(sourceFile.path);
505+
const typeReferencePackagePath: string = this.extractor.packageJsonLookup
506+
.tryFindPackagePathUpwards(sourceFile.path);
507507
// Example: "@microsoft/sp-core-library"
508508
let typeReferencePackageName: string = '';
509509

@@ -512,7 +512,8 @@ abstract class ApiItem {
512512
if (!typeReferencePackagePath) {
513513
typeReferencePackageName = this.extractor.package.name;
514514
} else {
515-
typeReferencePackageName = PackageJsonHelpers.readPackageName(typeReferencePackagePath);
515+
typeReferencePackageName = this.extractor.packageJsonLookup
516+
.readPackageName(typeReferencePackagePath);
516517

517518
typingsScopeNames.every(typingScopeName => {
518519
if (typeReferencePackageName.indexOf(typingScopeName) > -1) {

‎libraries/api-extractor/src/definitions/ApiPackage.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import ApiItem, { ApiItemKind, IApiItemOptions } from './ApiItem';
99
import ApiItemContainer from './ApiItemContainer';
1010
import ApiNamespace from './ApiNamespace';
1111
import TypeScriptHelpers from '../TypeScriptHelpers';
12-
import PackageJsonHelpers from '../PackageJsonHelpers';
1312
import { IExportedSymbol } from '../IExportedSymbol';
1413

1514
/**
@@ -46,7 +45,7 @@ export default class ApiPackage extends ApiItemContainer {
4645
super(ApiPackage._getOptions(extractor, rootFile));
4746
this.kind = ApiItemKind.Package;
4847
// The scoped package name. (E.g. "@microsoft/api-extractor")
49-
this.name = PackageJsonHelpers.readPackageName(this.extractor.packageFolder);
48+
this.name = this.extractor.packageJsonLookup.readPackageName(this.extractor.packageFolder);
5049

5150
const exportSymbols: ts.Symbol[] = this.typeChecker.getExportsOfModule(this.declarationSymbol);
5251
if (exportSymbols) {

0 commit comments

Comments
 (0)