-
Notifications
You must be signed in to change notification settings - Fork 0
/
apply.js
146 lines (122 loc) · 3.52 KB
/
apply.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
const path = require('path');
const fs = require('fs/promises');
const parser = require('@babel/parser');
const traverse = require('@babel/traverse').default;
const MagicString = require('magic-string');
function findType(entries, start) {
return entries.find(({ offset }) => offset === start);
}
const FUNC_TYPE = '(...args: any[]) => any';
const RETURN_TYPE_MAP = {
undefined: 'void',
};
function createUnion(types) {
return types
.map((type) => {
if (types.length > 1 && type === FUNC_TYPE) {
return `(${type})`;
}
return type;
})
.join(' | ');
}
async function loadMap() {
try {
return require(path.resolve(process.cwd(), './ts-annotate-map.json'));
} catch (_) {
throw new Error('Failed to load ts-annotate-map.json');
}
}
function getFuncEnd(code, node) {
let funcEnd = node.body.start;
if (node.type === 'ArrowFunctionExpression') {
while (code[funcEnd] !== '=' && code[funcEnd + 1] !== '>') {
funcEnd--;
}
}
// Explicit support for the common case of a space between ) & {
if (code[funcEnd - 1] === ' ') {
funcEnd--;
}
return funcEnd;
}
function needParens(code, node) {
if (node.type !== 'ArrowFunctionExpression') {
return false;
}
if (node.params.length !== 1) {
return false;
}
let funcStart = node.start;
while (funcStart < node.params[0].start) {
if (code[funcStart] === '(') {
return false;
}
funcStart++;
}
return true;
}
function isConstructor(node) {
return node.type === 'ClassMethod' && node.kind === 'constructor';
}
async function apply(files) {
const map = await loadMap();
for (const { url, entries } of map) {
if (!files.includes(url)) continue;
const code = await fs.readFile(url, 'utf8');
const ast = parser.parse(code, {
sourceType: 'module',
});
const ms = new MagicString(code);
function addFuncTypes({ node }) {
let addedType = false;
if (!isConstructor(node)) {
const returnType = findType(entries, node.end - 1);
let funcEnd = getFuncEnd(code, node);
if (returnType) {
const returnTypes = returnType.types.map(
(type) => RETURN_TYPE_MAP[type] ?? type
);
const dec = `: ${createUnion(returnTypes)}`;
ms.appendRight(funcEnd, dec);
addedType = true;
}
}
node.params.forEach((param) => {
let paramPosition = param.start;
let typePosition = param.end;
let node = param;
if (param.type === 'AssignmentPattern') {
paramPosition = param.end - 1;
typePosition = param.left.end;
node = param.left;
}
if (node.type === 'ObjectPattern' || node.type === 'ArrayPattern') {
if (param.type === 'AssignmentPattern') {
paramPosition = param.end - 1;
} else {
paramPosition = node.end - 1;
}
}
const type = findType(entries, paramPosition);
if (type) {
const dec = `: ${createUnion(type.types)}`;
ms.prependLeft(typePosition, dec);
addedType = true;
}
});
if (addedType && needParens(code, node)) {
ms.appendLeft(node.params[0].start, '(');
ms.appendLeft(node.params[0].end, ')');
}
}
traverse(ast, {
FunctionDeclaration: addFuncTypes,
FunctionExpression: addFuncTypes,
ArrowFunctionExpression: addFuncTypes,
ClassMethod: addFuncTypes,
});
fs.writeFile(url, ms.toString());
}
}
module.exports = { apply };