Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for library mode #1184

Closed
wants to merge 17 commits into from
Closed
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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ yarn-error.log
/dist/

typedoc*.tgz
tmp
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ For a complete list of the command line arguments run `typedoc --help` or visit

* `--out <path/to/documentation/>`<br>
Specifies the location the documentation should be written to. Defaults to `./docs`
* `--mode <file|modules>`<br>
Specifies the output mode the project is used to be compiled with.
* `--mode <file|modules|library>`<br>
Specifies the output mode the project is used to be compiled with. See [the documentation](https://typedoc.org/guides/options/#mode) for a comparison of each mode.
* `--options`<br>
Specify a json option file that should be loaded. If not specified TypeDoc will look for 'typedoc.json' in the current directory.
* `--json <path/to/output.json>`<br>
Expand Down
38 changes: 0 additions & 38 deletions UPDATING.md

This file was deleted.

36 changes: 17 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 10 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "typedoc",
"description": "Create api documentation for TypeScript projects.",
"version": "0.16.7",
"version": "0.17.0-3",
"homepage": "https://typedoc.org",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand All @@ -27,33 +27,36 @@
}
],
"engines": {
"node": ">= 6.0.0"
"node": ">= 8.0.0"
},
"dependencies": {
"@types/minimatch": "3.0.3",
"fs-extra": "^8.1.0",
"handlebars": "^4.7.2",
"highlight.js": "^9.17.1",
"highlight.js": "^9.18.0",
"lodash": "^4.17.15",
"marked": "^0.8.0",
"minimatch": "^3.0.0",
"progress": "^2.0.3",
"shelljs": "^0.8.3",
"typedoc-default-themes": "^0.7.2",
"typescript": "3.7.x"
"typedoc-default-themes": "0.8.0-0"
},
"peerDependencies": {
"typescript": ">=3.7"
},
"devDependencies": {
"@types/fs-extra": "^8.0.1",
"@types/lodash": "^4.14.149",
"@types/marked": "^0.7.2",
"@types/mocha": "^5.2.7",
"@types/mockery": "^1.4.29",
"@types/node": "^13.1.6",
"@types/node": "^13.5.0",
"@types/shelljs": "^0.8.6",
"mocha": "^7.0.0",
"mockery": "^2.1.0",
"nyc": "15.0.0",
"tslint": "^5.20.1"
"tslint": "^5.20.1",
"typescript": "3.7.x"
},
"files": [
"bin",
Expand Down
4 changes: 4 additions & 0 deletions scripts/rebuild_specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ const conversions = [
() => app.options.setValue('categorizeByGroup', false),
() => app.options.setValue('categorizeByGroup', true)
],
['specs.lib',
() => app.options.setValue('mode', 'library'),
() => app.options.setValue('mode', 'modules'),
]
];

/**
Expand Down
75 changes: 47 additions & 28 deletions src/lib/converter/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Reflection, Type, ProjectReflection } from '../models/index';
import { Context } from './context';
import { ConverterComponent, ConverterNodeComponent, ConverterTypeComponent, TypeTypeConverter, TypeNodeConverter } from './components';
import { Component, ChildableComponent, ComponentClass } from '../utils/component';
import { BindOption } from '../utils';
import { BindOption, SourceFileMode } from '../utils';
import { normalizePath } from '../utils/fs';
import { createMinimatch } from '../utils/paths';

Expand Down Expand Up @@ -358,40 +358,29 @@ export class Converter extends ChildableComponent<Application, ConverterComponen
*/
private compile(context: Context): ReadonlyArray<ts.Diagnostic> {
const program = context.program;

const exclude = createMinimatch(this.application.exclude || []);
const isExcluded = (file: ts.SourceFile) => exclude.some(mm => mm.match(file.fileName));

const includedSourceFiles = program.getSourceFiles()
.filter(file => !isExcluded(file));
const isRelevantError = ({ file }: ts.Diagnostic) => !file || includedSourceFiles.includes(file);

includedSourceFiles.forEach((sourceFile) => {
this.convertNode(context, sourceFile);
});

if (this.application.ignoreCompilerErrors) {
return [];
}

let diagnostics = program.getOptionsDiagnostics().filter(isRelevantError);
if (diagnostics.length) {
return diagnostics;
}

diagnostics = program.getSyntacticDiagnostics().filter(isRelevantError);
if (diagnostics.length) {
return diagnostics;
}

diagnostics = program.getGlobalDiagnostics().filter(isRelevantError);
if (diagnostics.length) {
return diagnostics;
const errors = this.getCompilerErrors(program, includedSourceFiles);
if (errors.length) {
return errors;
}

diagnostics = program.getSemanticDiagnostics().filter(isRelevantError);
if (diagnostics.length) {
return diagnostics;
if (this.application.options.getValue('mode') === SourceFileMode.Library) {
for (const fileName of context.fileNames) {
const sourceFile = includedSourceFiles.find(file => fileName === file.fileName);
if (sourceFile) {
this.convertNode(context, sourceFile);
} else {
this.application.logger.warn(`Failed to find source file of entry point ${fileName}`);
}
}
} else {
includedSourceFiles.forEach((sourceFile) => {
this.convertNode(context, sourceFile);
});
}

return [];
Expand All @@ -418,6 +407,36 @@ export class Converter extends ChildableComponent<Application, ConverterComponen
return project;
}

private getCompilerErrors(program: ts.Program, includedSourceFiles: readonly ts.SourceFile[]): ReadonlyArray<ts.Diagnostic> {
if (this.application.ignoreCompilerErrors) {
return [];
}

const isRelevantError = ({ file }: ts.Diagnostic) => !file || includedSourceFiles.includes(file);

let diagnostics = program.getOptionsDiagnostics().filter(isRelevantError);
if (diagnostics.length) {
return diagnostics;
}

diagnostics = program.getSyntacticDiagnostics().filter(isRelevantError);
if (diagnostics.length) {
return diagnostics;
}

diagnostics = program.getGlobalDiagnostics().filter(isRelevantError);
if (diagnostics.length) {
return diagnostics;
}

diagnostics = program.getSemanticDiagnostics().filter(isRelevantError);
if (diagnostics.length) {
return diagnostics;
}

return [];
}

/**
* Return the basename of the default library that should be used.
*
Expand Down
8 changes: 4 additions & 4 deletions src/lib/converter/factories/declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { createReferenceType } from './reference';
const nonStaticKinds = [
ReflectionKind.Class,
ReflectionKind.Interface,
ReflectionKind.Module
ReflectionKind.Namespace
];

/**
Expand Down Expand Up @@ -71,12 +71,12 @@ export function createDeclaration(context: Context, node: ts.Declaration, kind:

// Test whether the node is exported
let isExported: boolean;
if (kind === ReflectionKind.ExternalModule || kind === ReflectionKind.Global) {
if (kind === ReflectionKind.Module || kind === ReflectionKind.Global) {
isExported = true;
} else if (container.kind === ReflectionKind.Global) {
// In file mode, everything is exported.
isExported = true;
} else if (container.kindOf([ReflectionKind.Module, ReflectionKind.ExternalModule])) {
} else if (container.kindOf([ReflectionKind.Namespace, ReflectionKind.Module])) {
const symbol = context.getSymbolAtLocation(node);
if (!symbol) {
isExported = false;
Expand Down Expand Up @@ -213,7 +213,7 @@ function canMergeReflectionsByKind(kind1: ReflectionKind, kind2: ReflectionKind)
*/
function mergeDeclarations(context: Context, reflection: DeclarationReflection, node: ts.Node, kind: ReflectionKind) {
if (reflection.kind !== kind) {
const weights = [ReflectionKind.Module, ReflectionKind.Enum, ReflectionKind.Class];
const weights = [ReflectionKind.Namespace, ReflectionKind.Enum, ReflectionKind.Class];
const kindWeight = weights.indexOf(kind);
const childKindWeight = weights.indexOf(reflection.kind);
if (kindWeight > childKindWeight) {
Expand Down
34 changes: 33 additions & 1 deletion src/lib/converter/nodes/block.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as ts from 'typescript';
import { resolve } from 'path';

import { Reflection, ReflectionKind, ReflectionFlag } from '../../models/index';
import { createDeclaration } from '../factories/index';
Expand All @@ -17,6 +18,9 @@ export class BlockConverter extends ConverterNodeComponent<ts.SourceFile|ts.Bloc
@BindOption('mode')
mode!: SourceFileMode;

@BindOption('inputFiles')
inputFiles!: string[];

/**
* List of supported TypeScript syntax kinds.
*/
Expand Down Expand Up @@ -53,13 +57,26 @@ export class BlockConverter extends ConverterNodeComponent<ts.SourceFile|ts.Bloc
private convertSourceFile(context: Context, node: ts.SourceFile): Reflection | undefined {
let result: Reflection | undefined = context.scope;

const inputFiles = this.inputFiles.map(file => resolve(file).replace(/\\/g, '/'));
const isInputFile = (node: ts.SourceFile) => inputFiles.includes(node.fileName);

context.withSourceFile(node, () => {
if (this.mode === SourceFileMode.Modules) {
result = createDeclaration(context, node, ReflectionKind.ExternalModule, node.fileName);
result = createDeclaration(context, node, ReflectionKind.Module, node.fileName);
context.withScope(result, () => {
this.convertStatements(context, node);
result!.setFlag(ReflectionFlag.Exported);
});
} else if (this.mode === SourceFileMode.Library) {
if (inputFiles.length > 1 || !isInputFile(node)) {
result = createDeclaration(context, node, ReflectionKind.Module, node.fileName);
context.withScope(result, () => {
this.convertVisibleDeclarations(context, node);
result!.setFlag(ReflectionFlag.Exported);
});
} else {
this.convertVisibleDeclarations(context, node);
}
} else {
this.convertStatements(context, node);
}
Expand All @@ -85,4 +102,19 @@ export class BlockConverter extends ConverterNodeComponent<ts.SourceFile|ts.Bloc
});
}
}

private convertVisibleDeclarations(context: Context, node: ts.SourceFile) {
const moduleSymbol = context.getSymbolAtLocation(node);
if (!moduleSymbol) {
this.application.logger.warn(`File ${node.fileName} is not a module and cannot be converted in library mode`);
return;
}

for (const symbol of context.checker.getExportsOfModule(moduleSymbol)) {
const resolved = context.resolveAliasedSymbol(symbol);
for (const declaration of resolved.declarations) {
this.owner.convertNode(context, declaration);
}
}
}
}
Loading