Skip to content

Commit

Permalink
Add support for properties of values in an array (#119)
Browse files Browse the repository at this point in the history
* add tests for properties of values in an array

* add support for properties of values in an array

* cosmetic changes
  • Loading branch information
aidinabedi authored Apr 5, 2020
1 parent fcae945 commit 5038b4f
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 11 deletions.
7 changes: 6 additions & 1 deletion src/PropTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ export class PropTree
if (parts.length > 1)
{
parts.pop();
const parentName = parts.join('.');
let parentName = parts.join('.');
if (parentName.endsWith('[]'))
{
parentName = parentName.substring(0, parentName.length - '[]'.length);
}

const parent = this.nodes[parentName];

if (!parent)
Expand Down
2 changes: 1 addition & 1 deletion src/create_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ export function createClass(doclet: IClassDoclet, children?: ts.Node[]): ts.Clas
{
const node = tree.roots[i];
const opt = node.prop.optional ? ts.createToken(ts.SyntaxKind.QuestionToken) : undefined;
const t = node.children.length ? createTypeLiteral(node.children) : resolveType(node.prop.type);
const t = node.children.length ? createTypeLiteral(node.children, node) : resolveType(node.prop.type);

const property = ts.createProperty(
undefined,
Expand Down
25 changes: 18 additions & 7 deletions src/type_resolve_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ export function resolveTypeName(name: string, doclet?: TTypedDoclet): ts.TypeNod
return resolveComplexTypeName(name);
}

export function resolveTypeLiteral(props?: IDocletProp[]): ts.TypeLiteralNode
export function resolveTypeLiteral(props?: IDocletProp[]): ts.TypeNode
{
if (!props)
return ts.createTypeLiteralNode([]);
Expand All @@ -697,15 +697,15 @@ export function resolveTypeLiteral(props?: IDocletProp[]): ts.TypeLiteralNode
return createTypeLiteral(tree.roots);
}

export function createTypeLiteral(nodes: IPropDesc[]): ts.TypeLiteralNode
export function createTypeLiteral(children: IPropDesc[], parent?: IPropDesc): ts.TypeNode
{
const members: ts.PropertySignature[] = [];

for (let i = 0; i < nodes.length; ++i)
for (let i = 0; i < children.length; ++i)
{
const node = nodes[i];
const node = children[i];
const opt = node.prop.optional ? ts.createToken(ts.SyntaxKind.QuestionToken) : undefined;
const t = node.children.length ? createTypeLiteral(node.children) : resolveType(node.prop.type);
const t = node.children.length ? createTypeLiteral(node.children, node) : resolveType(node.prop.type);

members.push(ts.createPropertySignature(
undefined, // modifiers
Expand All @@ -716,7 +716,18 @@ export function createTypeLiteral(nodes: IPropDesc[]): ts.TypeLiteralNode
));
}

return ts.createTypeLiteralNode(members);
let node: ts.TypeNode = ts.createTypeLiteralNode(members);

if (parent)
{
const names = parent.prop.type.names;
if (names.length === 1 && names[0].toLowerCase() === 'array.<object>')
{
node = ts.createArrayTypeNode(node);
}
}

return node;
}

export function createFunctionParams(doclet: IFunctionDoclet | ITypedefDoclet | IClassDoclet): ts.ParameterDeclaration[]
Expand Down Expand Up @@ -747,7 +758,7 @@ export function createFunctionParams(doclet: IFunctionDoclet | ITypedefDoclet |
const node = tree.roots[i];
const opt = resolveOptionalParameter(node.prop);
const dots = resolveVariableParameter(node.prop);
let type = node.children.length ? createTypeLiteral(node.children) : resolveType(node.prop.type);
let type = node.children.length ? createTypeLiteral(node.children, node) : resolveType(node.prop.type);

if (dots)
{
Expand Down
9 changes: 7 additions & 2 deletions test/expected/function_all.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ declare function test1(a?: number, input: {

declare function test2(x: any[], y: any[], z: any[], w: any[][]): void;

declare function test3(myObjs: {
foo: number;
bar: boolean;
test1: string;
test2?: string[];
}[]): void;

declare class Test12345 {
static f(): number[];
static f(key: string): number;
}


10 changes: 10 additions & 0 deletions test/expected/property_all.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,13 @@ declare class PropTest6 {
myProp: boolean;
myProp: boolean;
}

declare class PropTest7 {
constructor();
myProps: {
foo: number;
bar: boolean;
test1: string;
test2?: string[];
}[];
}
11 changes: 11 additions & 0 deletions test/fixtures/function_all.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ function test1(input) {
function test2(x, y, z, w) {
}

/**
* @function
* @param {object[]} myObjs
* @param {number} myObjs[].foo
* @param {boolean} myObjs[].bar
* @param {string} myObjs[].test1
* @param {string[]} [myObjs[].test2]
*/
function test3(myObjs) {
}

/**
* @class
*/
Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/property_all.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,14 @@ var PropTest6 = function() {
*/
this.myProp = true;
}

/**
* @constructor
* @property {object[]} myProps
* @property {number} myProps[].foo
* @property {boolean} myProps[].bar
* @property {string} myProps[].test1
* @property {string[]} [myProps[].test2]
*/
var PropTest7 = function() {
}

0 comments on commit 5038b4f

Please sign in to comment.