-
-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathtemplate_data.ts
More file actions
170 lines (138 loc) · 4.12 KB
/
template_data.ts
File metadata and controls
170 lines (138 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import {
Argument,
Decorator,
DocstringParts,
Exception,
KeywordArgument,
Returns,
Yields,
Method,
Attribute
} from "../docstring_parts";
export class TemplateData {
public name: string;
public decorators: Decorator[];
public args: Argument[];
public kwargs: KeywordArgument[];
public exceptions: Exception[];
public returns: Returns;
public yields: Yields;
public classes: Method[];
public methods: Method[];
public attributes: Attribute[];
private includeName: boolean;
private includeExtendedSummary: boolean;
constructor(
docstringParts: DocstringParts,
guessTypes: boolean,
includeName: boolean,
includeExtendedSummary: boolean,
) {
this.name = docstringParts.name;
this.decorators = docstringParts.decorators;
this.args = docstringParts.args;
this.kwargs = docstringParts.kwargs;
this.exceptions = docstringParts.exceptions;
this.returns = docstringParts.returns;
this.yields = docstringParts.yields;
this.classes = docstringParts.classes;
this.methods = docstringParts.methods;
this.attributes = docstringParts.attributes;
this.includeName = includeName;
this.includeExtendedSummary = includeExtendedSummary;
if (!guessTypes) {
this.removeTypes();
}
this.addDefaultTypePlaceholders("[type]");
}
public placeholder() {
return (text: string, render: (text: string) => string) => {
return "${@@@:" + render(text) + "}";
};
}
public summaryPlaceholder(): string {
if (this.includeName) {
return this.name + " ${@@@:[summary]}";
}
return "${@@@:[summary]}";
}
public extendedSummaryPlaceholder(): string {
if (this.includeExtendedSummary) {
return "${@@@:[extended_summary]}";
}
return "";
}
public typePlaceholder(): string {
// @ts-ignore
return "${@@@:" + this.type + "}";
}
public descriptionPlaceholder(): string {
return "${@@@:[description]}";
}
public argsExist(): boolean {
return this.args.length > 0;
}
public kwargsExist(): boolean {
return this.kwargs.length > 0;
}
public parametersExist(): boolean {
return this.args.length > 0 || this.kwargs.length > 0;
}
public exceptionsExist(): boolean {
return this.exceptions.length > 0;
}
public returnsExist(): boolean {
return this.returns !== undefined;
}
public yieldsExist(): boolean {
return this.yields != undefined;
}
public classesExist(): boolean {
return this.classes.length > 0;
}
public methodsExist(): boolean {
return this.methods.length > 0;
}
public attributesExist(): boolean {
return this.attributes.length > 0;
}
private removeTypes(): void {
for (const arg of this.args) {
arg.type = undefined;
}
for (const kwarg of this.kwargs) {
kwarg.type = undefined;
}
if (this.yieldsExist()) {
this.yields.type = undefined;
}
if (this.returnsExist()) {
this.returns.type = undefined;
}
}
private addDefaultTypePlaceholders(placeholder: string): void {
for (const arg of this.args) {
if (arg.type === undefined) {
arg.type = placeholder;
}
}
for (const kwarg of this.kwargs) {
if (kwarg.type === undefined) {
kwarg.type = placeholder;
}
}
const returns = this.returns;
if (returns !== undefined && returns.type === undefined) {
returns.type = placeholder;
}
const yields = this.yields;
if (yields != undefined && yields.type == undefined) {
yields.type = placeholder;
}
for (const attribute of this.attributes) {
if (attribute.type === undefined) {
attribute.type = placeholder;
}
}
}
}