Skip to content

Commit

Permalink
feat(writer): 增加 components writer
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudcome committed Apr 14, 2023
1 parent 05fffd2 commit 6ca3a71
Show file tree
Hide file tree
Showing 5 changed files with 359 additions and 5 deletions.
28 changes: 28 additions & 0 deletions src/writers/BaseWriter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { TypeAlias, TypeItem } from '../readers/types';
import { WriterOptions } from './types';
import prettier from 'prettier';

export class BaseWriter {
static defaults: WriterOptions = {
document: { components: [], paths: [] },
prettier: {
singleQuote: true,
},
};

options: WriterOptions;
constructor(options: WriterOptions) {
this.options = Object.assign({}, BaseWriter.defaults, options);
}

protected isTypeAlias(type: TypeItem): type is TypeAlias {
return type.kind === 'alias';
}

protected format(text: string) {
return prettier.format(text, {
...this.options.prettier,
parser: 'typescript',
});
}
}
5 changes: 3 additions & 2 deletions src/writers/CommentsWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { isUndefined } from '../utils/type-is';
import { BaseWriter } from './BaseWriter';

export class CommentsWriter extends BaseWriter {
write(type: TypeItem) {
writeComments(type: TypeItem, trailingEndOfLine = false) {
const orders: (keyof TypeComments)[] = ['title', 'description', 'format', 'default', 'example'];
const mainLines = [
type.deprecated ? ' * @deprecated' : '',
Expand All @@ -22,7 +22,8 @@ export class CommentsWriter extends BaseWriter {
return (
'/**\n' + //////////////////////////////////////
mainLines.join('\n') +
'\n */'
'\n */' +
(trailingEndOfLine ? '\n' : '')
);
}
}
51 changes: 51 additions & 0 deletions src/writers/ComponentsWriter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { TypeItem, TypeOrigin } from '../readers/types';
import { toTypePath } from '../utils/string';
import { BaseWriter } from './BaseWriter';
import { CommentsWriter } from './CommentsWriter';

export class ComponentsWriter extends CommentsWriter {
writeComponents() {
const textList: string[] = [];
this.options.document.components.forEach((type) => {
const comments = this.writeComments(type, true);
textList.push(`${comments}export type ${type.name} = ${this.writeType(type)};`);
});
return this.format(textList.join('\n\n'));
}

private writeType(type: TypeItem): string {
if (this.isTypeAlias(type)) return `${type.origin}${toTypePath(type.props)}`;

switch (type.type) {
case 'number':
case 'string':
case 'boolean':
case 'never':
return this.writePrimitive(type);

case 'object':
return this.writeObject(type);

case 'array':
return this.writeArray(type);
}
}

private writePrimitive(type: TypeOrigin) {
return `${type.type}`;
}

private writeObject(type: TypeOrigin) {
const kvList = type.children!.map((t) => {
const c = this.writeComments(t, true);
const v = this.writeType(t);
return `${c}${t.name}: ${v};`;
});
return '{' + kvList.join('\n') + '}';
}

private writeArray(type: TypeOrigin) {
const one = this.writeType(type.children!.at(0)!);
return `Array<${one}>`;
}
}
6 changes: 3 additions & 3 deletions test/writers/CommentsWriter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ test('CommentsWriter', () => {
});

expect(
writer.write({
writer.writeComments({
kind: 'origin',
name: 'A',
type: 'string',
Expand All @@ -18,7 +18,7 @@ test('CommentsWriter', () => {
).toMatchInlineSnapshot('""');

expect(
writer.write({
writer.writeComments({
kind: 'origin',
name: 'A',
type: 'string',
Expand All @@ -32,7 +32,7 @@ test('CommentsWriter', () => {
`);

expect(
writer.write({
writer.writeComments({
kind: 'origin',
name: 'A',
type: 'string',
Expand Down
Loading

0 comments on commit 6ca3a71

Please sign in to comment.