Skip to content
This repository was archived by the owner on Mar 31, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions typescript/src/api-doc-types/ModuleDoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export class ModuleDoc implements ApiDoc {
path: string;
outputPath: string;
content: string;
importFrom?: string;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is adding this property a good way to solve my use case?


constructor(public symbol: ModuleSymbol, public basePath: string, public namespacesToInclude: string[], public hidePrivateMembers: boolean, public typeChecker: TypeChecker) {}
}
3 changes: 2 additions & 1 deletion typescript/src/processors/readTypeScriptModules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const path = require('canonical-path');
import { DocCollection, Processor } from 'dgeni';
import { getCombinedModifierFlags, getLineAndCharacterOfPosition, ModifierFlags, Node, SourceFile, Symbol, SymbolFlags, TypeChecker } from 'typescript';

import { getContent, getExportDocType, ModuleSymbols, TsParser } from '../../services/TsParser';
import { getContent, getExportDocType, ModuleSymbols, resolveModulePath, TsParser } from '../../services/TsParser';
import { Location } from '../../services/TsParser/Location';

import { ApiDoc } from '../../api-doc-types/ApiDoc';
Expand Down Expand Up @@ -80,6 +80,7 @@ export class ReadTypeScriptModules implements Processor {
moduleSymbols.forEach(moduleSymbol => {
// Create a doc for this module and add it to the module lookup collection and the docs collection
const moduleDoc = new ModuleDoc(moduleSymbol, basePath, this.namespacesToInclude, this.hidePrivateMembers, moduleSymbols.typeChecker!);
moduleDoc.importFrom = resolveModulePath(moduleSymbol, this.tsParser.options);
this.modules[moduleDoc.id] = moduleDoc;
docs.push(moduleDoc);
this.addExportDocs(docs, moduleDoc);
Expand Down
1 change: 1 addition & 0 deletions typescript/src/services/TsParser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const path = require('canonical-path');
export { getExportDocType } from './getExportDocType';
export { getContent } from './getContent';
export { getAccessibility } from './getAccessibility';
export { resolveModulePath } from './resolveModulePath';

export interface ModuleSymbols extends Array<ModuleSymbol> {
typeChecker?: TypeChecker;
Expand Down
23 changes: 23 additions & 0 deletions typescript/src/services/TsParser/resolveModulePath.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { TsParser } from '.';
import { resolveModulePath } from './resolveModulePath';
const path = require('canonical-path');

describe('resolveModulePath', () => {
let parser: TsParser;
let basePath: string;
beforeEach(() => {
parser = new TsParser(require('dgeni/lib/mocks/log')(false));
basePath = path.resolve(__dirname, '../../mocks');
});

it('should return the resolved module path derived by compilerOptions.paths', () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I add another test w/o options.paths?

parser.options.paths = {
'@foo/bar': ['./tsParser/getExportDocType.test.ts']
};

const parseInfo = parser.parse(['tsParser/getExportDocType.test.ts'], basePath);
const moduleSymbol = parseInfo.moduleSymbols[0];

expect(resolveModulePath(moduleSymbol, parser.options)).toEqual('@foo/bar');
});
});
27 changes: 27 additions & 0 deletions typescript/src/services/TsParser/resolveModulePath.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import * as path from 'path';
import { CompilerOptions, Symbol } from 'typescript';

/**
* Resolves the TypeScript module import path derived by the `paths` in `TypeScriptPackagesDef`.
*
* Implements a reverse lookup of the TypeScript `compilerOptions.paths` mapping.
*
* Example: `'@my/foo: ['./src/index.ts']` is the paths mapping.
* TODO: add test spec
*/
export function resolveModulePath(symbol: Symbol, options: CompilerOptions): string | undefined {
if (!options.paths) {
return;
}
const paths = options.paths || [];
const tsModules = Object.keys(paths);

// Relative file path of this document, e.g. 'a/b/public_api.ts'
const fileName = symbol.valueDeclaration!.getSourceFile().fileName;

// Find the TypeScript module path, e.g. '@my/foo'
const typeScriptModule = tsModules.find(tsModule => paths[tsModule]
.some(p => path.normalize(p) === fileName));

return typeScriptModule;
}