-
-
Notifications
You must be signed in to change notification settings - Fork 162
/
parse_parameters.ts
171 lines (131 loc) · 4.06 KB
/
parse_parameters.ts
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
171
import { guessType } from ".";
import {
Argument,
Decorator,
DocstringParts,
Exception,
KeywordArgument,
Returns,
Yields,
} from "../docstring_parts";
export function parseParameters(
parameterTokens: string[],
body: string[],
functionName: string,
): DocstringParts {
return {
name: functionName,
decorators: parseDecorators(parameterTokens),
args: parseArguments(parameterTokens),
kwargs: parseKeywordArguments(parameterTokens),
returns: parseReturn(parameterTokens, body),
yields: parseYields(parameterTokens, body),
exceptions: parseExceptions(body),
};
}
function parseDecorators(parameters: string[]): Decorator[] {
const decorators: Decorator[] = [];
const pattern = /^@(\w+)/;
for (const param of parameters) {
const match = param.trim().match(pattern);
if (match == null) {
continue;
}
decorators.push({
name: match[1],
});
}
return decorators;
}
function parseArguments(parameters: string[]): Argument[] {
const args: Argument[] = [];
const excludedArgs = ["self", "cls"];
const pattern = /^(\w+)/;
for (const param of parameters) {
const match = param.trim().match(pattern);
if (match == null || param.includes("=") || inArray(param, excludedArgs)) {
continue;
}
args.push({
var: match[1],
type: guessType(param),
});
}
return args;
}
function parseKeywordArguments(parameters: string[]): KeywordArgument[] {
const kwargs: KeywordArgument[] = [];
const pattern = /^(\w+)(?:\s*:[^=]+)?\s*=\s*(.+)/;
for (const param of parameters) {
const match = param.trim().match(pattern);
if (match == null) {
continue;
}
kwargs.push({
var: match[1],
default: match[2],
type: guessType(param),
});
}
return kwargs;
}
function parseReturn(parameters: string[], body: string[]): Returns {
const returnType = parseReturnFromDefinition(parameters);
if (returnType == null || isIterator(returnType.type)) {
return parseFromBody(body, /return /);
}
return returnType;
}
function parseYields(parameters: string[], body: string[]): Yields {
const returnType = parseReturnFromDefinition(parameters);
if (returnType != null && isIterator(returnType.type)) {
return returnType as Yields;
}
// To account for functions that yield but don't have a yield signature
const yieldType = returnType ? returnType.type : undefined;
const yieldInBody = parseFromBody(body, /yield /);
if (yieldInBody != null && yieldType != undefined) {
yieldInBody.type = `Iterator[${yieldType}]`;
}
return yieldInBody;
}
function parseReturnFromDefinition(parameters: string[]): Returns | null {
const pattern = /^->\s*(["']?)(['"\w\[\], |\.]*)\1/;
for (const param of parameters) {
const match = param.trim().match(pattern);
if (match == null) {
continue;
}
// Skip "-> None" annotations
return match[2] === "None" ? null : { type: match[2] };
}
return null;
}
function parseExceptions(body: string[]): Exception[] {
const exceptions: Exception[] = [];
const pattern = /(?<!#.*)raise\s+([\w.]+)/;
for (const line of body) {
const match = line.match(pattern);
if (match == null) {
continue;
}
exceptions.push({ type: match[1] });
}
return exceptions;
}
export function inArray<type>(item: type, array: type[]) {
return array.some((x) => item === x);
}
function parseFromBody(body: string[], pattern: RegExp): Returns | Yields {
for (const line of body) {
const match = line.match(pattern);
if (match == null) {
continue;
}
return { type: undefined };
}
return undefined;
}
function isIterator(type: string): boolean {
return type.startsWith("Generator") || type.startsWith("Iterator");
}