-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautobind-method.js
More file actions
85 lines (72 loc) · 2.82 KB
/
autobind-method.js
File metadata and controls
85 lines (72 loc) · 2.82 KB
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
function isBindAssignment(node) {
// Returns true if node looks like `this.foo = this.foo.bind(this);`
node = node.value;
if (node.type === 'AssignmentExpression') {
const { type: leftType, property } = node.left;
if (leftType === 'MemberExpression' && property.type === 'Identifier') {
const name = property.name;
const { right } = node;
if (right.type === 'CallExpression') {
// callee should be `this.foo.bind`
const { callee } = right;
const isBind = callee.type === 'MemberExpression'
&& callee.object.type === 'MemberExpression'
&& callee.object.object.type === 'ThisExpression'
&& callee.object.property.name === name;
if (!isBind) {
return false;
}
// arguments should be `[ThisExpression]`
const { arguments: args } = right;
if (args.length === 1 && args[0].type === 'ThisExpression') {
return true;
}
return false;
}
}
}
return false;
}
const transform = (fileInfo, api, options) => {
const j = api.jscodeshift;
function bindMethods(classBody) {
// Find assignments in the constructor of the form `this.foo = this.foo.bind(this)`
const constructor = classBody
.find(j.MethodDefinition, (node) => node.kind === 'constructor');
const bindAssignments = constructor
.find(j.AssignmentExpression)
.filter(isBindAssignment);
const methodNames = [];
bindAssignments.forEach((node) => {
node = node.value;
methodNames.push(node.left.property.name);
});
bindAssignments.forEach((node) => j(node).remove());
function convertMethodToBoundArrowFunc(method) {
const name = method.value.key;
const { params, body } = method.value.value;
const arrowFunc = j.arrowFunctionExpression(
params,
body,
);
const classProp = j.classProperty(
name,
arrowFunc,
null, /* typeAnnotation */
false, /* static */
);
classProp.comments = method.value.comments;
return classProp;
}
classBody
.find(j.MethodDefinition, (node) => node.kind === 'method' && methodNames.includes(node.key.name))
.replaceWith(convertMethodToBoundArrowFunc);
}
const ast = j(fileInfo.source);
const classes = ast.find(j.ClassDeclaration);
classes.forEach(_class => bindMethods(j(_class)));
return ast.toSource({
arrowParensAlways: true,
});
};
export default transform;