Skip to content

Commit

Permalink
fix: improve dts code indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Dec 13, 2022
1 parent c1636a0 commit eefe4c9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 41 deletions.
12 changes: 7 additions & 5 deletions src/generator/dts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,10 @@ export function generateTypes(schema: Schema, opts: GenerateTypesOptions = {}) {
opts = { ...GenerateTypesDefaults, ...opts };
const baseIden = " ".repeat(opts.indentation);
const interfaceCode =
`interface ${opts.interfaceName} {\n ` +
_genTypes(schema, baseIden + " ", opts).join("\n ") +
`interface ${opts.interfaceName} {\n` +
_genTypes(schema, baseIden + " ", opts)
.map((l) => (l.trim().length > 0 ? l : ""))
.join("\n") +
`\n${baseIden}}`;
if (!opts.addExport) {
return baseIden + interfaceCode;
Expand All @@ -116,7 +118,7 @@ function _genTypes(
} else if (val.type === "object") {
buff.push(
`${escapeKey(key)}${opts.partial ? "?" : ""}: {`,
..._genTypes(val, spaces + " ", opts),
..._genTypes(val, spaces, opts),
"},\n"
);
} else {
Expand Down Expand Up @@ -144,7 +146,7 @@ function _genTypes(
buff.push("[key: string]: any");
}

return buff.map((i) => spaces + i);
return buff.flatMap((l) => l.split("\n")).map((l) => spaces + l);
}

function getTsType(type: TypeDescriptor | TypeDescriptor[]): string {
Expand All @@ -169,7 +171,7 @@ function getTsType(type: TypeDescriptor | TypeDescriptor[]): string {
return `Array<${getTsType(type.items)}>`;
}
if (type.type === "object") {
return `{\n` + _genTypes(type, " ", {}).join(" \n") + `\n }`;
return `{\n` + _genTypes(type, " ", {}).join("\n") + `\n}`;
}
return TYPE_MAP[type.type] || type.type;
}
Expand Down
72 changes: 36 additions & 36 deletions test/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ describe("resolveSchema", () => {
);
expect(types).toMatchInlineSnapshot(`
"export interface Untyped {
test: {
/**
* Test
* this is test
* @default \\"test value\\"
*/
foo: string,
},
test: {
/**
* Test
* this is test
* @default \\"test value\\"
*/
foo: string,
},
}"
`);
});
Expand All @@ -41,17 +41,17 @@ describe("resolveSchema", () => {
);
expect(types).toMatchInlineSnapshot(`
"interface Untyped {
test?: {
a?: number,
test?: {
a?: number,
foo?: {
bar?: number,
foo?: {
bar?: number,
baz?: {
x?: number,
},
},
baz?: {
x?: number,
},
},
},
}"
`);
});
Expand Down Expand Up @@ -82,23 +82,23 @@ describe("resolveSchema", () => {

expect(types).toMatchInlineSnapshot(`
"export interface Untyped {
empty: Array<any>,
empty: Array<any>,
/** @default [1,2,3] */
numbers: Array<number>,
/** @default [1,2,3] */
numbers: Array<number>,
/** @default [true,123] */
mixed: Array<boolean|number>,
/** @default [true,123] */
mixed: Array<boolean|number>,
/** @default [{\\"foo\\":[123]}] */
object: Array<{
[key: string]: any
}>,
/** @default [{\\"foo\\":[123]}] */
object: Array<{
[key: string]: any
}>,
manual: Array<{
/** This is foo prop */
foo: number,
}>,
manual: Array<{
/** This is foo prop */
foo: number,
}>,
}"
`);
});
Expand Down Expand Up @@ -139,7 +139,7 @@ describe("resolveSchema", () => {
expect(types).toBe(
`
export interface Untyped {
add: (test?: Array<string | number>, append?: false) => any,
add: (test?: Array<string | number>, append?: false) => any,
}
`.trim()
);
Expand Down Expand Up @@ -176,15 +176,15 @@ export interface Untyped {
"import type { VueConfig, OtherImport } from 'vue'
import type { VueConfig as VueConfig0 } from 'other-lib'
export interface Untyped {
test: {
foo: VueConfig,
test: {
foo: VueConfig,
bar: VueConfig,
bar: VueConfig,
baz: OtherImport,
baz: OtherImport,
quf: VueConfig0,
},
quf: VueConfig0,
},
}"
`);
});
Expand Down

0 comments on commit eefe4c9

Please sign in to comment.