1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | import {Declaration} from './declarations';
|
13 | import {formatComment} from './formatting';
|
14 | import {Export, Node} from './index';
|
15 |
|
16 | export interface TsLintDisable {
|
17 | ruleName: string;
|
18 | why: string;
|
19 | }
|
20 |
|
21 | export class Document {
|
22 | readonly kind = 'document';
|
23 | path: string;
|
24 | members: Declaration[];
|
25 | referencePaths: Set<string>;
|
26 | header: string;
|
27 | isEsModule: boolean;
|
28 | tsLintDisables: TsLintDisable[];
|
29 |
|
30 | constructor(data: {
|
31 | path: string,
|
32 | members?: Declaration[],
|
33 | referencePaths?: Iterable<string>,
|
34 | header?: string,
|
35 | isEsModule?: boolean, tsLintDisables: TsLintDisable[],
|
36 | }) {
|
37 | this.path = data.path;
|
38 | this.members = data.members || [];
|
39 | this.referencePaths = new Set(Array.from(data.referencePaths || []));
|
40 | this.header = data.header || '';
|
41 | this.isEsModule = data.isEsModule || false;
|
42 | this.tsLintDisables = data.tsLintDisables;
|
43 | }
|
44 |
|
45 | |
46 |
|
47 |
|
48 |
|
49 | * traverse(): Iterable<Node> {
|
50 | for (const m of this.members) {
|
51 | yield* m.traverse();
|
52 | }
|
53 | yield this;
|
54 | }
|
55 |
|
56 | |
57 |
|
58 |
|
59 | simplify() {
|
60 | for (const node of this.traverse()) {
|
61 | if (node.kind === 'union') {
|
62 | node.simplify();
|
63 | }
|
64 | }
|
65 | }
|
66 |
|
67 | serialize(): string {
|
68 | let out = '';
|
69 | if (this.header) {
|
70 | out += formatComment(this.header, 0) + '\n';
|
71 | }
|
72 | const disables = this.tsLintDisables.slice();
|
73 | for (const node of this.traverse()) {
|
74 | if (node.kind === 'name' && node.name === 'any') {
|
75 |
|
76 | disables.push({
|
77 | ruleName: 'no-any',
|
78 | why: 'describes the API as best we are able today'
|
79 | });
|
80 | break;
|
81 | }
|
82 | }
|
83 | out += '\n';
|
84 | for (const disable of disables) {
|
85 | out += `// tslint:disable:${disable.ruleName} ${disable.why}\n`;
|
86 | }
|
87 | out += '\n';
|
88 | if (this.referencePaths.size > 0) {
|
89 | for (const ref of this.referencePaths) {
|
90 | out += `/// <reference path="${ref}" />\n`;
|
91 | }
|
92 | out += '\n';
|
93 | }
|
94 | out += this.members.map((m) => m.serialize()).join('\n');
|
95 |
|
96 |
|
97 | if (this.isEsModule === true &&
|
98 | !this.members.some((m) => m.kind === 'import' || m.kind === 'export')) {
|
99 | out += '\n' + (new Export({identifiers: []})).serialize();
|
100 | }
|
101 | return out;
|
102 | }
|
103 | }
|