forked from omegaup/omegaup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrefactor.js
executable file
·231 lines (219 loc) · 5.86 KB
/
refactor.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
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
#!/usr/bin/env node
// Multi-purpose refactor script.
// Currently it only enforces that all Promise objects have a .fail().
const fs = require('fs');
const process = require('process');
const babelParser = require('@babel/parser');
const t = require('@babel/types');
const traverse = require('@babel/traverse').default;
if (process.argv.length != 4) {
console.error(
`Usage: ${process.argv[1]} <filename> <original filename>`,
);
process.exit(1);
}
const filename = process.argv[2];
const sourceFilename = process.argv[3];
const buf = fs.readFileSync(filename, 'utf8');
const isModule = sourceFilename.indexOf('/js/omegaup/') != -1;
const ast = (() => {
try {
return babelParser.parse(buf, {
sourceType: isModule ? 'module' : 'script',
sourceFilename: sourceFilename,
plugins: ['typescript'],
});
} catch (e) {
if (e instanceof SyntaxError) {
const lines = buf.split('\n');
// Do a best-effort attempt at getting the token involved in the error.
// Just grab the non-whitespace characters at the point in which the error
// occurred.
const token = buf.slice(e.pos).match(/^(\S+)/)[1];
console.error(
`Syntax error in ${sourceFilename}:${e.loc.line}: ${e.message}\n`);
console.error(`\t${lines[e.loc.line - 1]}`);
console.error(
`\t${''.padEnd(e.loc.column)}${''.padEnd(token.length, '^')}`);
} else {
console.log(e);
}
process.exit(1);
}
})();
function hasRefactorLintDisableComment(path) {
const comments = path.getStatementParent().node.trailingComments;
if (!comments || comments.length == 0) {
return false;
}
return comments[0].value.trim() == 'refactor-lint-disable';
}
const fixes = [];
const promiseVisitor = {
CallExpression(path) {
if (hasRefactorLintDisableComment(path)) {
return;
}
const callee = path.node.callee;
if (callee.type != 'MemberExpression') {
return;
}
if (callee.property.name != 'then' && callee.property.name != 'catch') {
return;
}
const p = path.getStatementParent();
if (p.node.type != 'ExpressionStatement') {
throw new Error('Unexpected statement type: ' + p.node.type);
}
if (callee.property.name == 'catch') {
p.hasCatch = true;
} else if (callee.property.name == 'then') {
if (p.visited) {
return;
}
p.visited = true;
if (!p.hasCatch) {
fixes.push({
start: path.node.end,
end: path.node.end,
contents: '.catch(' + (isModule ? 'UI' : 'omegaup.UI') + '.apiError)',
});
p.hasCatch = true;
valid = false;
}
}
},
};
traverse(ast, promiseVisitor);
// From https://github.com/jquery/jquery/blob/2d4f53416e5f74fa98e0c1d66b6f3c285a12f0ce/src/selector-native.js#L212
const jQueryBooleanProperties = [
'checked',
'selected',
'async',
'autofocus',
'autoplay',
'controls',
'defer',
'disabled',
'hidden',
'ismap',
'loop',
'multiple',
'open',
'readonly',
'required',
'scoped',
];
const jQueryRemoveAttrVisitor = {
CallExpression(path) {
if (hasRefactorLintDisableComment(path)) {
return;
}
let callee = path.node.callee;
if (callee.type != 'MemberExpression') {
return;
}
const identifier = callee.property;
if (identifier.name != 'removeAttr') {
return;
}
if (
path.node.arguments.length != 1 ||
path.node.arguments[0].type != 'StringLiteral'
) {
return;
}
let attributeName = path.node.arguments[0].value;
if (jQueryBooleanProperties.indexOf(attributeName) == -1) {
return;
}
fixes.push({
start: identifier.start - 1,
end: path.node.end,
contents: ".prop('" + attributeName + "', false)",
});
},
};
traverse(ast, jQueryRemoveAttrVisitor);
// From https://github.com/jquery/jquery/blob/305f193aa57014dc7d8fa0739a3fefd47166cd44/src/event/alias.js
const jQueryDeprecatedFunctions = [
'blur',
'focus',
'focusin',
'focusout',
'resize',
'scroll',
'click',
'dblclick',
'mousedown',
'mouseup',
'mousemove',
'mouseover',
'mouseout',
'mouseenter',
'mouseleave',
'change',
'select',
'submit',
'keydown',
'keypress',
'keyup',
'contextmenu',
];
const jQueryDeprecatedFunctionVisitor = {
CallExpression(path) {
if (hasRefactorLintDisableComment(path)) {
return;
}
let callee = path.node.callee;
if (callee.type != 'MemberExpression') {
return;
}
const identifier = callee.property;
if (jQueryDeprecatedFunctions.indexOf(identifier.name) == -1) {
return;
}
if (path.node.arguments.length == 0) {
// Excluding expressions that do not belong to jQuery
if (callee.name != '$' && callee.name != 'jQuery') {
return;
}
// This is a trigger.
fixes.push({
start: identifier.start,
end: path.node.end,
contents: "trigger('" + identifier.name + "')",
});
return;
}
// This is an event handler.
while (callee.object && callee.object.type == 'CallExpression') {
callee = callee.object.callee;
if (callee.type != 'Identifier') {
continue;
}
if (callee.name != '$' && callee.name != 'jQuery') {
continue;
}
fixes.push({
start: identifier.start,
end: identifier.end + 1,
contents: "on('" + identifier.name + "', ",
});
return;
}
},
};
traverse(ast, jQueryDeprecatedFunctionVisitor);
fixes.sort((a, b) => a.start - b.start);
// Manually write the results instead of relying on babel-generator.
// babel-generator moves stuff around too much :/
var lastPos = 0;
const fd = fs.openSync(filename, 'w');
for (var i = 0; i < fixes.length; i++) {
fs.writeSync(fd, buf.slice(lastPos, fixes[i].start));
fs.writeSync(fd, fixes[i].contents);
lastPos = fixes[i].end;
}
fs.writeSync(fd, buf.slice(lastPos));
fs.closeSync(fd);