Skip to content

Commit

Permalink
feat!: Library mode
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Renames external modules to modules, and modules to namespaces. Closes #109.
  • Loading branch information
Gerrit0 committed Jan 18, 2020
1 parent f2d9c18 commit 362138e
Show file tree
Hide file tree
Showing 13 changed files with 63 additions and 23 deletions.
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
19 changes: 15 additions & 4 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 @@ -366,9 +366,20 @@ export class Converter extends ChildableComponent<Application, ConverterComponen
.filter(file => !isExcluded(file));
const isRelevantError = ({ file }: ts.Diagnostic) => !file || includedSourceFiles.includes(file);

includedSourceFiles.forEach((sourceFile) => {
this.convertNode(context, sourceFile);
});
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);
});
}

if (this.application.ignoreCompilerErrors) {
return [];
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
22 changes: 21 additions & 1 deletion src/lib/converter/nodes/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,17 @@ export class BlockConverter extends ConverterNodeComponent<ts.SourceFile|ts.Bloc

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) {
result = createDeclaration(context, node, ReflectionKind.Module, node.fileName);
context.withScope(result, () => {
this.convertVisibleDeclarations(context, node);
result!.setFlag(ReflectionFlag.Exported);
});
} else {
this.convertStatements(context, node);
}
Expand All @@ -85,4 +91,18 @@ 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)) {
for (const declaration of symbol.declarations) {
this.owner.convertNode(context, declaration);
}
}
}
}
2 changes: 1 addition & 1 deletion src/lib/converter/nodes/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class ModuleConverter extends ConverterNodeComponent<ts.ModuleDeclaration
convert(context: Context, node: ts.ModuleDeclaration): Reflection | undefined {
const reflection = context.isInherit && context.inheritParent === node
? <DeclarationReflection> context.scope
: createDeclaration(context, node, ReflectionKind.Module);
: createDeclaration(context, node, ReflectionKind.Namespace);
context.withScope(reflection, () => {
if (node.body) {
this.owner.convertNode(context, node.body);
Expand Down
4 changes: 2 additions & 2 deletions src/lib/converter/plugins/CommentPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export class CommentPlugin extends ConverterComponent {
CommentPlugin.removeTags(comment, 'event');
}

if (reflection.kindOf(ReflectionKind.ExternalModule)) {
if (reflection.kindOf(ReflectionKind.Module)) {
CommentPlugin.removeTags(comment, 'packagedocumentation');
}
}
Expand Down Expand Up @@ -172,7 +172,7 @@ export class CommentPlugin extends ConverterComponent {
if (reflection.kindOf(ReflectionKind.FunctionOrMethod) || (reflection.kindOf(ReflectionKind.Event) && reflection['signatures'])) {
const comment = parseComment(rawComment, reflection.comment);
this.applyModifiers(reflection, comment);
} else if (reflection.kindOf(ReflectionKind.Module)) {
} else if (reflection.kindOf(ReflectionKind.Namespace)) {
this.storeModuleComment(rawComment, reflection);
} else {
const comment = parseComment(rawComment, reflection.comment);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/converter/plugins/DynamicModulePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class DynamicModulePlugin extends ConverterComponent {
* @param node The node that is currently processed if available.
*/
private onDeclaration(context: Context, reflection: Reflection, node?: ts.Node) {
if (reflection.kindOf(ReflectionKind.ExternalModule)) {
if (reflection.kindOf(ReflectionKind.Module)) {
let name = reflection.name;
if (!name.includes('/')) {
return;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/converter/plugins/GroupPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ export class GroupPlugin extends ConverterComponent {
*/
static WEIGHTS = [
ReflectionKind.Global,
ReflectionKind.ExternalModule,
ReflectionKind.Module,
ReflectionKind.Namespace,
ReflectionKind.Enum,
ReflectionKind.EnumMember,
ReflectionKind.Class,
Expand Down
6 changes: 3 additions & 3 deletions src/lib/models/reflections/abstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ export function resetReflectionID() {
*/
export enum ReflectionKind {
Global = 0,
ExternalModule = 1 << 0,
Module = 1 << 1,
Module = 1 << 0,
Namespace = 1 << 1,
Enum = 1 << 2,
EnumMember = 1 << 4,
Variable = 1 << 5,
Expand Down Expand Up @@ -66,7 +66,7 @@ export enum ReflectionKind {
FunctionOrMethod = ReflectionKind.Function | Method,
ClassMember = Accessor | Constructor | Method | Property | Event,
SomeSignature = CallSignature | IndexSignature | ConstructorSignature | GetSignature | SetSignature,
SomeModule = Module | ExternalModule,
SomeModule = Namespace | Module,
SomeType = Interface | TypeLiteral | TypeParameter | TypeAlias,
SomeValue = Variable | Function | ObjectLiteral
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/output/themes/DefaultTheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class DefaultTheme extends Theme {
directory: 'enums',
template: 'reflection.hbs'
}, {
kind: [ReflectionKind.Module, ReflectionKind.ExternalModule],
kind: [ReflectionKind.Namespace, ReflectionKind.Module],
isLeaf: false,
directory: 'modules',
template: 'reflection.hbs'
Expand Down Expand Up @@ -375,7 +375,7 @@ export class NavigationBuilder {
let target = someModule.parent;
let inScope = (someModule === this.entryPoint);
while (target) {
if (target.kindOf(ReflectionKind.ExternalModule)) {
if (target.kindOf(ReflectionKind.Module)) {
return;
}
if (this.entryPoint === target) {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/utils/options/declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export type TypeDocAndTSOptions = TypeDocOptions
& Pick<CompilerOptions, Exclude<KnownKeys<CompilerOptions>, IgnoredTsOptionKeys>>;

export enum SourceFileMode {
File, Modules
File, Modules, Library
}

/**
Expand All @@ -43,7 +43,7 @@ export interface TypeDocOptionMap {
tsconfig: string;

inputFiles: string[];
mode: { file: SourceFileMode.File, modules: SourceFileMode.Modules };
mode: { file: SourceFileMode.File, modules: SourceFileMode.Modules, library: SourceFileMode.Library };
includeDeclarations: boolean;
entryPoint: string;
exclude: string[];
Expand Down
5 changes: 3 additions & 2 deletions src/lib/utils/options/sources/typedoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ export function addTypeDocOptions(options: Options) {
help: "Specifies the output mode the project is used to be compiled with: 'file' or 'modules'",
type: ParameterType.Map,
map: {
'file': SourceFileMode.File,
'modules': SourceFileMode.Modules
file: SourceFileMode.File,
modules: SourceFileMode.Modules,
library: SourceFileMode.Library
},
defaultValue: SourceFileMode.Modules
});
Expand Down
4 changes: 4 additions & 0 deletions src/test/converter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ describe('Converter', function() {
['specs-with-lump-categories',
() => app.options.setValue('categorizeByGroup', false),
() => app.options.setValue('categorizeByGroup', true)
],
['specs.lib',
() => app.options.setValue('mode', 'library'),
() => app.options.setValue('mode', 'modules')
]
];

Expand Down

0 comments on commit 362138e

Please sign in to comment.