-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathgenerate-file-content.ts
233 lines (209 loc) · 7.04 KB
/
generate-file-content.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import * as dts from 'dts-element';
import * as dts_fp from 'dts-element-fp';
import * as fs from 'fs';
import * as path from 'path';
import {
placeholder_name,
placeholder_name_abbr,
} from '../../templates/utils/constants';
import { bind_jsdoc } from './bind-jsdoc';
import { bind_mixin } from './bind-mixin';
import { placeholder, selectable } from './constants';
export function generate_file_content(filename: string) {
return dts.emit(get_top_level_members(filename));
}
function get_top_level_members(filename: string): dts.ITopLevelMember[] {
const members: dts.ITopLevelMember[] = [];
const basename = path.basename(filename);
const function_name = basename.match(/^\$?(.+?)\./)![1];
if (basename === '$curriedFunctions.ts') {
push_curried_functions_members();
} else if (basename.endsWith('.d.ts')) {
if (basename.startsWith('$')) {
push_r_ts_members();
} else {
push_d_ts_members();
}
} else if (basename.endsWith('.c.ts')) {
push_c_ts_members();
} else if (basename.endsWith('.r.ts')) {
push_r_ts_members();
} else if (basename.endsWith('.ts')) {
push_ts_members();
} else {
throw new Error(`Unexpected filename ${filename}`);
}
if (!basename.startsWith('$')) {
bind_member_jsdoc();
add_export_equal();
}
if (function_name === 'path') {
bind_mixin(
members,
dts
.parse(
fs.readFileSync(
path.resolve(__dirname, '../mixins/path.d.ts'),
'utf8',
),
)
.members.filter(dts.is_function_declaration)
.map(function_declaration => function_declaration.type!),
);
}
return members;
function add_export_equal() {
const target_member = members.find(member => {
switch (member.kind) {
case dts.ElementKind.VariableDeclaration:
case dts.ElementKind.FunctionDeclaration:
return true;
default:
return false;
}
}) as undefined | dts.IVariableDeclaration | dts.IFunctionDeclaration;
if (target_member === undefined) {
throw new Error(`Cannot find element to set export equal in ${filename}`);
}
members.push(dts.create_export_equal({ value: target_member.name! }));
}
function bind_member_jsdoc() {
const target_member_index = members.findIndex(member => {
switch (member.kind) {
case dts.ElementKind.VariableDeclaration:
return function_name === '__';
case dts.ElementKind.TypeDeclaration:
case dts.ElementKind.FunctionDeclaration:
return true;
default:
return false;
}
});
if (target_member_index === -1) {
throw new Error(`Cannot find element to bind jsdoc in ${filename}`);
}
const target_member = members[target_member_index] as
| dts.IVariableDeclaration
| dts.IFunctionDeclaration
| dts.ITypeDeclaration;
if (target_member.kind === dts.ElementKind.TypeDeclaration) {
const target_overloads = (target_member.type as dts.IObjectType).members!;
bind_jsdoc(filename, target_overloads[0]);
} else {
bind_jsdoc(filename, members[target_member_index]);
}
}
function push_r_ts_members() {
const top_level_element = dts.parse(fs.readFileSync(filename, 'utf8'));
members.push(...top_level_element.members);
}
function push_d_ts_members() {
const top_level_element = dts.parse(fs.readFileSync(filename, 'utf8'));
push_curry_members(top_level_element.members);
}
function push_ts_members() {
const declarations = get_ts_default();
members.push(...declarations);
}
function push_c_ts_members() {
const declarations = get_ts_default();
push_curry_members(declarations);
}
function push_curried_functions_members() {
const curried_functions_generator = get_ts_default(true);
const declarations = curried_functions_generator(selectable, placeholder);
members.push(...declarations);
}
function push_curry_members(the_members: dts.ITopLevelMember[]) {
const imports = the_members.filter(
(member): member is dts.IImportNamed =>
member.kind === dts.ElementKind.ImportNamed,
);
const functions = the_members.filter<dts.IFunctionDeclaration>(
(member): member is dts.IFunctionDeclaration =>
member.kind === dts.ElementKind.FunctionDeclaration,
);
if (!functions.every(fn => fn.name!.startsWith('$'))) {
throw new Error(
`Exported functions in ${filename} should be prefixed with $`,
);
}
const function_names_no_need_to_check_last_overload_name = ['cond'];
if (
function_names_no_need_to_check_last_overload_name.indexOf(
function_name,
) === -1
) {
const valid_last_overload_names = ['$mixed', '$general', '$variadic'];
if (
functions.length > 1 &&
valid_last_overload_names.indexOf(
functions[functions.length - 1].name!,
) === -1
) {
throw new Error(
`Exported multi-overload functions in ${filename} should end with ${valid_last_overload_names.join(
' / ',
)}}`,
);
}
}
const overload_names_should_exist_simultaneously = ['$record', '$keyof'];
if (
functions.some(
func =>
overload_names_should_exist_simultaneously.indexOf(func.name!) !== -1,
)
) {
const is_exist_simultaneously = overload_names_should_exist_simultaneously.every(
overload_name => functions.some(func => func.name === overload_name),
);
if (!is_exist_simultaneously) {
throw new Error(
`Exported multi-overload functions in ${filename} should have ${overload_names_should_exist_simultaneously.join(
' / ',
)} simultaneously`,
);
}
}
const placeholder_imports =
!placeholder || functions[0].type!.parameters!.length <= 1
? []
: dts.parse(`
import {${placeholder_name} as ${placeholder_name_abbr}} from './$placeholder';
`).members;
const curried_declarations = dts_fp.create_curried_declarations(
path.basename(filename).replace(/(\.[a-z])?\.ts$/, ''),
functions.reduce<Record<string, dts.IFunctionType>>(
(current_functions, fn) => ({
...current_functions,
[fn.name!.slice(1)]: fn.type!,
}),
{},
),
{
selectable,
placeholder,
inline_return_type: true,
},
);
members.push(...imports, ...placeholder_imports, ...curried_declarations);
}
function get_ts_default(special_case = false) {
// tslint:disable-next-line:no-require-imports
const required: any = require(filename);
delete require.cache[require.resolve(filename)];
const declarations = required.default;
if (!special_case && !is_valid_export_default(declarations)) {
throw new Error(
`Template.ts should default-export an array of declarations: ${filename}`,
);
}
return declarations;
}
function is_valid_export_default(
export_default: any,
): export_default is dts.ITopLevelMember[] {
return export_default instanceof Array;
}
}