Skip to content

Commit

Permalink
feat: Add support for comments in output
Browse files Browse the repository at this point in the history
  • Loading branch information
runeh committed Apr 30, 2021
1 parent 8803236 commit f7596a6
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 1 deletion.
63 changes: 63 additions & 0 deletions src/__tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -625,4 +625,67 @@ describe('runtype generation', () => {
"
`);
});

it('comments', () => {
const source = generateRuntypes(
{
name: 'Event',
comment: 'An event object',
type: {
kind: 'record',
fields: [
{
name: 'name',
type: { kind: 'string' },
comment: 'Single line comment',
},
{
name: 'age',
type: { kind: 'string' },
comment: 'Single line comment.\nWith newlines',
},
{
name: 'id',
type: { kind: 'string' },
comment: ['Multi line comment', 'As array'],
},
{
name: 'noComment1',
type: { kind: 'string' },
comment: [],
},
{
name: 'noComment2',
type: { kind: 'string' },
comment: '',
},
],
},
},
{ includeTypes: false },
);

expect(source).toMatchInlineSnapshot(`
"import * as rt from \\"runtypes\\";
// An event object
const event = rt.Record({
// Single line comment
name: rt.String
/**
* Single line comment.
* With newlines
*/,
age: rt.String
/**
* Multi line comment
* As array
*/,
id: rt.String,
noComment1: rt.String,
noComment2: rt.String,
});
"
`);
});
});
25 changes: 25 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ function writeRootType(
const { formatRuntypeName, formatTypeName, includeTypes } = options;
const runtypeName = formatRuntypeName(node.name);
const typeName = formatTypeName(node.name);
if (node.comment) {
writeComment(w, node.comment);
}
w.conditionalWrite(Boolean(node.export), 'export ');
w.write(`const ${runtypeName}=`);
writeAnyType(options, w, node.type);
Expand Down Expand Up @@ -219,6 +222,25 @@ function writeIntersectionType(
w.write(')');
}

function writeComment(w: CodeWriter, comment: string | string[]) {
const lines = (Array.isArray(comment) ? comment : [comment])
.map((e) => e.trim())
.map((e) => e.split('\n'))
.reduce((prev, cur) => [...prev, ...cur], []);

if (lines.length === 0) {
return;
} else if (lines.length === 1) {
w.write(`// ${lines[0]}\n`);
} else {
w.write(`/**\n`);
for (const line of lines) {
w.write(`* ${line}\n`);
}
w.write(`*/\n`);
}
}

/**
* public for testing
*
Expand Down Expand Up @@ -273,6 +295,9 @@ function writeRecordType(
for (const fieldKind of fieldKinds) {
w.write('rt.Record({');
for (const field of fieldKind.fields) {
if (field.comment) {
writeComment(w, field.comment);
}
w.write(field.name);
w.write(':');
writeAnyType(options, w, field.type);
Expand Down
8 changes: 7 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export type RecordField = {
type: AnyType;
readonly?: boolean;
nullable?: boolean;
comment?: string | string[];
};

export type RecordType = {
Expand Down Expand Up @@ -162,7 +163,12 @@ export type AnyType = rt.Static<typeof anyTypeRt>;

export const rootTypeRt = rt.Intersect(
rt.Record({ name: rt.String, type: anyTypeRt }),
rt.Record({ export: rt.Boolean }).asPartial(),
rt
.Record({
export: rt.Boolean,
comment: rt.Union(rt.String, rt.Array(rt.String)),
})
.asPartial(),
);

export type RootType = rt.Static<typeof rootTypeRt>;

0 comments on commit f7596a6

Please sign in to comment.