-
Notifications
You must be signed in to change notification settings - Fork 2
/
doc-to-types.js
150 lines (123 loc) · 3.88 KB
/
doc-to-types.js
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
// Just a short script to convert the type definitions in the docs into a TypeScript interface
let [,,name = 'RawData', allOptional = 'false'] = process.argv;
allOptional = allOptional === 'true';
readFromStdin(data => process.stdout.write(toInterface(data, name, allOptional)));
function readFromStdin(callback) {
let data = '';
process.stdin.on('data', chunk => data += chunk);
process.stdin.once('end', () => callback(data));
}
/**
* @param {string} value
* @return {string}
*/
function toCamelCase(value) {
return value.replace(/[ -/<>]./g, substring => substring.charAt(1).toUpperCase());
}
/**
* @param {string} value
* @return {string}
*/
function toPascalCase(value) {
return value.replace(/([ -/<>]|^)./g, substring => substring
.charAt(substring.length - 1)
.toUpperCase());
}
/**
* @param {string} key
* @return {string[]}
*/
function toTsKeys(key) {
if (key.endsWith('*'))
key = key.substring(0, key.length - 1);
if (key.startsWith('<') && key.endsWith('>'))
return [`[${toCamelCase(key.substring(1, key.length - 1))}: string]`];
const keys = key.split(', ');
if (key.includes('-') || key.includes(' '))
return keys.map(key => `'${key}'`);
return keys;
}
/**
* @param {string[]} data
* @param {number} from
* @param {string[]} extraInterfaces
* @param {boolean} allOptional
* @return {[string, number]}
*/
function parseSection(data, from, extraInterfaces, allOptional) {
let i = from;
let result = '{';
for (; i < data.length; i++) {
const currentLine = data[i];
if (currentLine === '}') break;
const [key, value] = currentLine.split(/\s*=\s*/, 2);
if (!value) {
if (currentLine.startsWith('<') && currentLine.endsWith('>')) {
return ['Section', i + 2];
}
throw new Error(`Invalid line in section: ${currentLine} (line: ${i + 2})`);
}
const tsKeys = toTsKeys(key);
let valueType;
let docs;
if (value.startsWith('[')) {
valueType = 'string[]';
docs = data[++i];
docs = docs.substring(1, docs.length - 1);
i++;
} else if (value.startsWith('{')) {
const [section, end] = parseSection(data, i + 1, extraInterfaces, allOptional);
i = end;
if ((key.endsWith('*') && !key.includes('<')) || tsKeys.length > 1) {
const interfaceName = 'Raw' + toPascalCase(key.split(',', 2)[0]
.replace('*', ''));
extraInterfaces.push(`export interface ${interfaceName} ${section}`);
valueType = interfaceName;
if (key.endsWith('*')) {
valueType += ' | ' + interfaceName + '[]';
}
} else {
valueType = section;
}
docs = value.includes('#') ? value.substring(value.indexOf('#') + 2) : null;
} else {
valueType = 'string';
docs = value.substring(1, value.length - 1);
const enumSearchResult = /\w+(?:\|\w+)+$/.exec(docs);
if (enumSearchResult) {
valueType = enumSearchResult[0]
.split('|')
.map(constant => `'${constant}'`)
.join(' | ');
} else if (docs.endsWith('yes or no')) {
valueType = `'yes' | 'no'`;
}
}
for (const tsKey of tsKeys) {
if (docs) {
result += `\n/**\n * ${docs}\n */`;
}
result += `\n${tsKey}${allOptional && !tsKey.includes(':') ? '?' : ''}: ${valueType};`;
}
}
result += '\n}';
return [result, i];
}
/**
* @param {string} data
* @param {string} name
* @param {boolean} allOptional
* @return {string}
*/
function toInterface(data, name, allOptional = false) {
const lines = data
.replace(/<[^>]*\n[^>]*>/gm, substring => substring.replace(/\n\s*/gm, ' '))
.split('\n')
.map(value => value.trim())
.filter(value => value);
lines.shift();
lines.pop();
const interfaces = [];
interfaces.push(`export interface ${name} ${parseSection(lines, 0, interfaces, allOptional)[0]}`);
return interfaces.join('\n\n');
}