Skip to content
Merged
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
17 changes: 6 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,18 @@
},
"author": "pvasek",
"license": "MIT",
"dependencies": {
},
"dependencies": {},
"devDependencies": {
"@types/chai": "^3.4.35",
"@types/chai": "^4.0.0",
"@types/mocha": "^2.2.39",
"@types/node": "^7.0.16",
"@types/react": "^15.0.14",
"@types/react-dom": "^0.14.23",
"babel-core": "^6.8.0",
"babel-loader": "^6.2.4",
"chai": "^3.5.0",
"file-loader": "^0.8.5",
"@types/source-map-support": "^0.4.0",
"chai": "^4.0.2",
"mocha": "^2.5.3",
"react": "^15.4.2",
"react-dom": "^15.4.2",
"react-styleguidist": "^2.2.1",
"typescript": "^2.2.1"
"source-map-support": "^0.4.15",
"typescript": "^2.3.4"
},
"files": [
"lib",
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/docgenConverter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe('docgenConverter', () => {
}]
});

assert.isNotNull(result);
assert.equal('name1', result.displayName);
assert.equal('comment1', result.description);
const prop1Result = result.props['prop1'];
Expand Down
6 changes: 2 additions & 4 deletions src/docgenConverter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { FileDoc, InterfaceDoc, MemberDoc } from './model';
import { StyleguidistComponent, StyleguidistProps, PropItem } from './propTypesParser';

export function convertToDocgen(doc: FileDoc): StyleguidistComponent {
export function convertToDocgen(doc: FileDoc): StyleguidistComponent | null {
const components = doc.components;

if (components.length === 0) {
Expand All @@ -20,8 +20,6 @@ export function convertToDocgen(doc: FileDoc): StyleguidistComponent {
};
}



function getProps(props: InterfaceDoc): StyleguidistProps {
return props.members.reduce((acc, i) => {
const item: PropItem = {
Expand All @@ -36,5 +34,5 @@ function getProps(props: InterfaceDoc): StyleguidistProps {

acc[i.name] = item;
return acc;
}, {});
}, ({} as any));
}
12 changes: 10 additions & 2 deletions src/getFileDocumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ function getInterfaceDoc(entry: InterfaceEntry): InterfaceDoc {

function getClassPropInterface(interfaces: InterfaceEntry[], classEntry: ClassEntry): InterfaceDoc {
if (classEntry.baseType.typeArguments.length === 0) {
return null;
return {
name: 'none',
comment: '',
members: [],
};
}

const propInterfaceName = classEntry.baseType.typeArguments[0];
Expand All @@ -85,7 +89,11 @@ function getPropInterface(interfaces: InterfaceEntry[], propInterfaceName: strin
const matchedInterfaces = interfaces.filter(j => j.name === propInterfaceName);
if (matchedInterfaces.length === 0) {
console.warn(`Property interface ${propInterfaceName} cannot be found`)
return null;
return {
name: propInterfaceName,
comment: '',
members: [],
};
}

return getInterfaceDoc(matchedInterfaces[0]);
Expand Down
2 changes: 1 addition & 1 deletion src/nodeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function navigate(node: ts.Node, ...path: ts.SyntaxKind[]): ts.Node {
}

export function getFlatChildren(node: ts.Node): ts.Node[] {
const result = [];
const result: ts.Node[] = [];

function f(node: ts.Node) {
result.push(node);
Expand Down
2 changes: 1 addition & 1 deletion src/printUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function isNodeExported(node: ts.Node): boolean {
}

function symbolMapToString(map: ts.Map<ts.Symbol>): string {
const values = [];
const values: string[] = [];
map.forEach((value, key) => {
values.push(value.name);
});
Expand Down
48 changes: 32 additions & 16 deletions src/transformAST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
VeriableKind,
InterfaceEntry,
ClassEntry,
PropertyEntry
PropertyEntry,
BaseClassEntry
} from './model';
import { simplePrint, syntaxKindToName, flagsToText, symbolFlagsToText } from "./printUtils";

Expand Down Expand Up @@ -45,13 +46,17 @@ function getType(prop: ts.PropertySignature): MemberType {
function getMethods(checker: ts.TypeChecker, type: ts.Type, classDeclaratinNode: ts.ClassDeclaration) {
return classDeclaratinNode.members
.filter(x => x.name !== undefined)
.map(i => ({ name: i.name.getText() }));
.map(i => ({ name: i.name ? i.name.getText() : 'unknown' }));
}

function getProperties(checker: ts.TypeChecker, type: ts.Type, parent: ts.Node): PropertyEntry[] {
return type.getProperties()
//.filter(i => parent === null || i.valueDeclaration.parent === parent)
.filter(i => i.valueDeclaration)
.map(i => {
if (i.valueDeclaration === undefined || i.valueDeclaration === null) {
throw Error('The valueDeclaration does not exist')
}

const symbol = checker.getSymbolAtLocation(i.valueDeclaration.name);
const prop = i.valueDeclaration as ts.PropertySignature;
const typeInfo = getType(prop);
Expand Down Expand Up @@ -88,24 +93,31 @@ export function transformAST(sourceFile: ts.SourceFile, checker: ts.TypeChecker)
.map(i => i as ts.VariableStatement)
.filter(i => i.declarationList.declarations
&& i.declarationList.declarations.length === 1
&& i.declarationList.declarations[0].name.kind === ts.SyntaxKind.Identifier)
&& i.declarationList.declarations[0].name.kind === ts.SyntaxKind.Identifier
&& i.declarationList.declarations[0].initializer)
.map(i => {
const d = i.declarationList.declarations[0] as ts.VariableDeclaration;
const identifier = d.name as ts.Identifier;

if (!d.initializer) {
throw Error('The initializer property does not exist')
}

const symbol = checker.getSymbolAtLocation(identifier);
let arrowFunctionType: string = null;
let literalFlags: ts.TypeFlags = null;
let arrowFunctionType: string = 'undefined';
let kind: VeriableKind = 'unknown';
const varType = checker.getTypeAtLocation(d);

const initializerType = checker.getTypeAtLocation(d.initializer);
const initializerFlags = initializerType.flags;
let arrowFunctionParams = [];
let callExpressionArguments = [];
let arrowFunctionParams: string[] = [];
let callExpressionArguments: string[] = [];
if (d.initializer.kind === ts.SyntaxKind.ArrowFunction) {
const arrowFunc = d.initializer as ts.ArrowFunction;
if (arrowFunc.parameters) {
arrowFunctionParams = arrowFunc.parameters.map(i => i.type.getText())
arrowFunctionParams = arrowFunc
.parameters
.map(p => p.type ? p.type.getText() : 'unknown')
}
arrowFunctionType = arrowFunc.type ? arrowFunc.type.getText() : 'undefined';
kind = 'arrowFunction'
Expand All @@ -124,12 +136,11 @@ export function transformAST(sourceFile: ts.SourceFile, checker: ts.TypeChecker)
name: identifier.text,
exported: isNodeExported(i),
comment: symbol ? ts.displayPartsToString(symbol.getDocumentationComment()).trim() : '',
type: varType.symbol ? varType.symbol.getName() : 'unknown',
kind,
type: varType.symbol ? varType.symbol.getName() : null,
arrowFunctionType,
arrowFunctionParams,
callExpressionArguments,
literalFlags,
initializerFlags,
};
});
Expand All @@ -155,15 +166,20 @@ export function transformAST(sourceFile: ts.SourceFile, checker: ts.TypeChecker)
const symbol = checker.getSymbolAtLocation(i.name);
const type = checker.getTypeAtLocation(i.name);
const baseTypes = type.getBaseTypes();
let baseType = null;
let baseType: BaseClassEntry = {
name: 'unknown',
typeArguments: [],
};

if (baseTypes.length) {
const t = baseTypes[0];
const typeArguments = navigate(i,
ts.SyntaxKind.HeritageClause,
ts.SyntaxKind.ExpressionWithTypeArguments) as ts.ExpressionWithTypeArguments;
baseType = {
name: t.symbol.getName(),
typeArguments: typeArguments ? typeArguments.typeArguments.map(t => t.getText()) : []
name: t.symbol ? t.symbol.getName() : 'unknown',
typeArguments: typeArguments && typeArguments.typeArguments ?
typeArguments.typeArguments.map(t => t.getText()) : []
};
}

Expand All @@ -181,10 +197,10 @@ export function transformAST(sourceFile: ts.SourceFile, checker: ts.TypeChecker)
.map(i => i as ts.TypeAliasDeclaration)
.map(i => {
const type = checker.getTypeAtLocation(i.name) as ts.IntersectionType;
const properties = [];
const properties: PropertyEntry[] = [];
type.types.forEach(t => {
const props = (t as any).properties;
let ownProperties = [];
let ownProperties: string[] = [];
if (props) {
ownProperties = props
.map((p: ts.Symbol) => p.getName());
Expand Down
6 changes: 4 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"noImplicitAny": false,
"noImplicitThis": true,
"alwaysStrict": true,
"noImplicitAny": true,
"strictNullChecks": false,
"sourceMap": true,
"outDir": "lib",
"strict": false,
"jsx": "react",
"typeRoots": [
"node_modules/@types"
Expand Down
Loading