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

feat: add code examples for properties #435

Merged
merged 4 commits into from
Sep 13, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
27 changes: 25 additions & 2 deletions src/docgen/transpile/java.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ const toCamelCase = (text?: string) => {
return Case.camel(text ?? '');
};

const toUpperCamelCase = (test?: string) => {
const camelCase = toCamelCase(test);
return camelCase.charAt(0).toUpperCase() + camelCase.slice(1);
};

// [1, 2, 3] -> [[], [1], [1, 2], [1, 2, 3]]
const prefixArrays = <T>(arr: T[]): T[][] => {
const out: T[][] = [[]];
Expand Down Expand Up @@ -166,20 +171,24 @@ export class JavaTranspile extends transpile.TranspileBase {
public parameter(
parameter: reflect.Parameter,
): transpile.TranspiledParameter {
const typeRef = this.typeReference(parameter.type);
return {
name: parameter.name,
parentType: this.type(parameter.parentType),
typeReference: this.typeReference(parameter.type),
typeReference: typeRef,
optional: parameter.optional,
declaration: this.formatProperty(parameter.name, typeRef),
};
}

public property(property: reflect.Property): transpile.TranspiledProperty {
const typeRef = this.typeReference(property.type);
return {
name: property.name,
parentType: this.type(property.parentType),
typeReference: this.typeReference(property.type),
typeReference: typeRef,
optional: property.optional,
declaration: this.formatProperty(property.name, typeRef),
};
}

Expand Down Expand Up @@ -373,4 +382,18 @@ export class JavaTranspile extends transpile.TranspileBase {
parameters.splice(parameters.indexOf(firstStruct), 1);
return struct;
}

private formatProperty(
name: string,
typeReference: transpile.TranspiledTypeReference,
): string {
const tf = typeReference.toString({
typeFormatter: (t) => t.name,
});
if (tf.includes(' OR ')) {
return `public java.lang.Object get${toUpperCamelCase(name)}();`;
} else {
return `public ${tf} get${toUpperCamelCase(name)}();`;
}
}
}
24 changes: 20 additions & 4 deletions src/docgen/transpile/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,14 @@ export class PythonTranspile extends transpile.TranspileBase {
}

public property(property: reflect.Property): transpile.TranspiledProperty {
const name = property.const ? property.name : toSnakeCase(property.name);
const typeRef = this.typeReference(property.type);
return {
name: property.const ? property.name : toSnakeCase(property.name),
name,
parentType: this.type(property.parentType),
typeReference: this.typeReference(property.type),
typeReference: typeRef,
optional: property.optional,
declaration: this.formatProperty(name, typeRef),
};
}

Expand All @@ -156,11 +159,14 @@ export class PythonTranspile extends transpile.TranspileBase {
public parameter(
parameter: reflect.Parameter,
): transpile.TranspiledParameter {
const name = toSnakeCase(parameter.name);
const typeRef = this.typeReference(parameter.type);
return {
name: toSnakeCase(parameter.name),
name,
parentType: this.type(parameter.parentType),
typeReference: this.typeReference(parameter.type),
typeReference: typeRef,
optional: parameter.optional,
declaration: this.formatProperty(name, typeRef),
};
}

Expand Down Expand Up @@ -267,4 +273,14 @@ export class PythonTranspile extends transpile.TranspileBase {
});
return `${transpiled.name}: ${tf}${transpiled.optional ? ' = None' : ''}`;
}

private formatProperty(
name: string,
typeReference: transpile.TranspiledTypeReference,
): string {
const tf = typeReference.toString({
typeFormatter: (t) => t.name,
});
return `${name}: ${tf}`;
}
}
5 changes: 5 additions & 0 deletions src/docgen/transpile/transpile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ export interface TranspiledParameter {
* Whether or not the parameter is optional.
*/
readonly optional: boolean;
/**
* The signature of the property, or its getter if the language
* supports that.
*/
readonly declaration: string;
}

/**
Expand Down
18 changes: 16 additions & 2 deletions src/docgen/transpile/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,13 @@ export class TypeScriptTranspile extends transpile.TranspileBase {
}

public property(property: reflect.Property): transpile.TranspiledProperty {
const typeRef = this.typeReference(property.type);
return {
name: property.name,
parentType: this.type(property.parentType),
typeReference: this.typeReference(property.type),
typeReference: typeRef,
optional: property.optional,
declaration: this.formatProperty(property.name, typeRef),
};
}

Expand All @@ -130,11 +132,13 @@ export class TypeScriptTranspile extends transpile.TranspileBase {
public parameter(
parameter: reflect.Parameter,
): transpile.TranspiledParameter {
const typeRef = this.typeReference(parameter.type);
return {
name: parameter.name,
parentType: this.type(parameter.parentType),
typeReference: this.typeReference(parameter.type),
typeReference: typeRef,
optional: parameter.optional,
declaration: this.formatProperty(parameter.name, typeRef),
};
}

Expand Down Expand Up @@ -209,4 +213,14 @@ export class TypeScriptTranspile extends transpile.TranspileBase {
});
return `${transpiled.name}${transpiled.optional ? '?' : ''}: ${tf}`;
}

private formatProperty(
name: string,
typeReference: transpile.TranspiledTypeReference,
): string {
const tf = typeReference.toString({
typeFormatter: (t) => t.name,
});
return `public readonly ${name}: ${tf};`;
}
}
6 changes: 5 additions & 1 deletion src/docgen/view/property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Transpile, TranspiledProperty, TranspiledType } from '../transpile/tran
export class Property {
private readonly transpiled: TranspiledProperty;
constructor(
transpile: Transpile,
private readonly transpile: Transpile,
private readonly property: reflect.Property,
private readonly linkFormatter: (type: TranspiledType) => string,
) {
Expand Down Expand Up @@ -38,6 +38,10 @@ export class Property {
md.lines('');
}

if (!this.property.const) {
md.code(this.transpile.language.toString(), this.transpiled.declaration);
}

const metadata: any = {
Type: this.transpiled.typeReference.toString({
typeFormatter: (t) => `[${Markdown.pre(t.fqn)}](${this.linkFormatter(t)})`,
Expand Down
Loading