Skip to content

Commit

Permalink
feat: More compact raw source output
Browse files Browse the repository at this point in the history
  • Loading branch information
runeh authored and simenandre committed Mar 25, 2021
1 parent 6543c99 commit c76338d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 62 deletions.
61 changes: 11 additions & 50 deletions src/__tests__/main_alt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,7 @@ describe('runtype generation', () => {
);
const formatted = await fmt(raw);
expect(formatted).toMatchInlineSnapshot(`
"const personRt = rt
.Record({
name: rt.String,
age: rt.Number,
})
.asReadonly();
"const personRt = rt.Record({ name: rt.String, age: rt.Number }).asReadonly();
export const smokeTest = rt.Record({
someBoolean: rt.Boolean,
Expand All @@ -122,12 +117,8 @@ describe('runtype generation', () => {
someArray: rt.Array(rt.String).asReadonly(),
someNamedType: personRt,
someIntersection: rt.Intersect(
rt.Record({
member1: rt.String,
}),
rt.Record({
member2: rt.Number,
}),
rt.Record({ member1: rt.String }),
rt.Record({ member2: rt.Number }),
),
someObject: rt.Record({
name: rt.String,
Expand Down Expand Up @@ -175,9 +166,7 @@ describe('runtype generation', () => {
});
const formatted = await fmt(raw);
expect(formatted).toMatchInlineSnapshot(`
"const test = rt.Record({
name: rt.String,
});
"const test = rt.Record({ name: rt.String });
"
`);
});
Expand All @@ -192,11 +181,7 @@ describe('runtype generation', () => {
});
const formatted = await fmt(raw);
expect(formatted).toMatchInlineSnapshot(`
"const test = rt
.Record({
name: rt.String,
})
.asReadonly();
"const test = rt.Record({ name: rt.String }).asReadonly();
"
`);
});
Expand All @@ -211,11 +196,7 @@ describe('runtype generation', () => {
});
const formatted = await fmt(raw);
expect(formatted).toMatchInlineSnapshot(`
"const test = rt
.Record({
name: rt.String,
})
.asPartial();
"const test = rt.Record({ name: rt.String }).asPartial();
"
`);
});
Expand All @@ -237,12 +218,7 @@ describe('runtype generation', () => {
});
const formatted = await fmt(raw);
expect(formatted).toMatchInlineSnapshot(`
"const test = rt
.Record({
name: rt.String,
})
.asPartial()
.asReadonly();
"const test = rt.Record({ name: rt.String }).asPartial().asReadonly();
"
`);
});
Expand Down Expand Up @@ -279,25 +255,10 @@ describe('runtype generation', () => {
const formatted = await fmt(raw);
expect(formatted).toMatchInlineSnapshot(`
"const test = rt.intersect(
rt.Record({
field_1: rt.String,
}),
rt
.Record({
field_2: rt.String,
})
.asPartial(),
rt
.Record({
field_3: rt.String,
})
.asReadonly(),
rt
.Record({
field_4: rt.String,
})
.asPartial()
.asReadonly(),
rt.Record({ field_1: rt.String }),
rt.Record({ field_2: rt.String }).asPartial(),
rt.Record({ field_3: rt.String }).asReadonly(),
rt.Record({ field_4: rt.String }).asPartial().asReadonly(),
);
"
`);
Expand Down
24 changes: 12 additions & 12 deletions src/main_alt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export function generateRuntypes(...roots: RootType[]): string {
const writer = makeWriter();
for (const root of roots) {
writer.conditionalWrite(Boolean(root.export), 'export ');
writer.write(`const ${root.name} = `);
writer.write(`const ${root.name}=`);
writeAnyType(writer, root.type);
writer.write(';\n\n');
}
Expand Down Expand Up @@ -109,21 +109,21 @@ function writeArrayType(w: CodeWriter, node: ArrayType) {
}

function writeUnionType(w: CodeWriter, node: UnionType) {
w.write('\nrt.Union(');
w.write('rt.Union(');
for (const type of node.types) {
writeAnyType(w, type);
w.write(',\n');
w.write(',');
}
w.write('\n) ');
w.write(')');
}

function writeIntersectionType(w: CodeWriter, node: UnionType) {
w.write('\nrt.Intersect(');
w.write('rt.Intersect(');
for (const type of node.types) {
writeAnyType(w, type);
w.write(',\n');
w.write(',');
}
w.write('\n) ');
w.write(')');
}

/**
Expand Down Expand Up @@ -167,19 +167,19 @@ export function groupFieldKinds(
function writeRecordType(w: CodeWriter, node: RecordType) {
const fieldKinds = groupFieldKinds(node.fields);
const hasMultiple = fieldKinds.length > 1;
w.conditionalWrite(hasMultiple, '\nrt.intersect(');
w.conditionalWrite(hasMultiple, 'rt.intersect(');
for (const fieldKind of fieldKinds) {
w.write('rt.Record({\n');
w.write('rt.Record({');
for (const field of fieldKind.fields) {
w.write(field.name);
w.write(': ');
w.write(':');
writeAnyType(w, field.type);
w.write(',\n');
w.write(',');
}
w.write('})');
w.conditionalWrite(fieldKind.nullable ?? false, '.asPartial()');
w.conditionalWrite(fieldKind.readonly ?? false, '.asReadonly()');
w.conditionalWrite(hasMultiple, '\n,');
w.conditionalWrite(hasMultiple, ',');
}
w.conditionalWrite(hasMultiple, '\n)');
}

0 comments on commit c76338d

Please sign in to comment.