Skip to content
This repository has been archived by the owner on Nov 27, 2023. It is now read-only.

Commit

Permalink
Merge pull request #339 from KingHenne/feature/extract-array-of-shape-of
Browse files Browse the repository at this point in the history
feat: handle array props with shapes in extractComplexTypes
  • Loading branch information
KnisterPeter authored Mar 21, 2017
2 parents aa9a1ec + efb0fa8 commit 03c7b74
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
25 changes: 19 additions & 6 deletions src/typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,23 +126,25 @@ function extractComplexTypes(m: dom.ModuleDeclaration, interf: dom.InterfaceDecl
if (member.kind === 'property' && isExtractableType(member.type)) {
const name = `${componentName}${pascalCase(member.name)}`;
const extractedMember = createModuleMember(name, member.type);
extractedMember.flags = dom.DeclarationFlags.Export;
m.members.push(extractedMember);
member.type = dom.create.namedTypeReference(name);
if (extractedMember) {
extractedMember.flags = dom.DeclarationFlags.Export;
m.members.push(extractedMember);
member.type = createTypeReference(name, member.type);
}
}
});
}

type ExtractableType = dom.UnionType | dom.IntersectionType | dom.ObjectType;
type ExtractableType = dom.UnionType | dom.IntersectionType | dom.ObjectType | dom.ArrayTypeReference;

function isExtractableType(type: dom.Type): type is ExtractableType {
if (typeof type === 'object') {
return ['union', 'intersection', 'object'].indexOf(type.kind) > -1;
return ['union', 'intersection', 'object', 'array'].indexOf(type.kind) > -1;
}
return false;
}

function createModuleMember(name: string, type: ExtractableType): dom.ModuleMember {
function createModuleMember(name: string, type: ExtractableType): dom.ModuleMember | undefined {
switch (type.kind) {
case 'intersection':
case 'union':
Expand All @@ -151,6 +153,17 @@ function createModuleMember(name: string, type: ExtractableType): dom.ModuleMemb
const interf = dom.create.interface(name);
interf.members = type.members;
return interf;
case 'array':
return isExtractableType(type.type) ? createModuleMember(name, type.type) : undefined;
}
}

function createTypeReference(name: string, type: ExtractableType): dom.TypeReference {
const namedTypeReference = dom.create.namedTypeReference(name);
if (type.kind === 'array') {
return dom.create.array(namedTypeReference);
} else {
return namedTypeReference;
}
}

Expand Down
17 changes: 13 additions & 4 deletions tests/es6-class.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ declare module 'component' {

export type ComponentRequiredUnion = any[] | boolean;

export interface ComponentRequiredArrayOfObjectsWithShape {
color?: string;
fontSize?: number;
}

export interface ComponentDeeplyNested {
arrayInDeeplyNested?: {
foo?: number;
}[];
}

export interface ComponentProps {
/**
* This is a jsdoc comment for optionalAny.
Expand All @@ -36,10 +47,8 @@ declare module 'component' {
requiredAny: any;
requiredUnion: ComponentRequiredUnion;
requiredArrayOf: string[];
requiredArrayOfObjectsWithShape: {
color?: string;
fontSize?: number;
}[];
requiredArrayOfObjectsWithShape: ComponentRequiredArrayOfObjectsWithShape[];
deeplyNested: ComponentDeeplyNested[];
requiredSymbol: typeof Symbol;
}

Expand Down
5 changes: 5 additions & 0 deletions tests/es6-class.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,10 @@ Component.propTypes = {
color: React.PropTypes.string,
fontSize: React.PropTypes.number
})).isRequired,
deeplyNested: React.PropTypes.arrayOf(React.PropTypes.shape({
arrayInDeeplyNested: React.PropTypes.arrayOf(React.PropTypes.shape({
foo: React.PropTypes.number
}))
})).isRequired,
requiredSymbol: React.PropTypes.symbol.isRequired,
};

0 comments on commit 03c7b74

Please sign in to comment.