Skip to content

Commit a6e27bf

Browse files
Pavithra KodmadSunil Pai
authored andcommitted
Issues/181 : manual-bind-to-arrow does not clean up constructor binds when the same bind occurs multiple times (#247)
* Add a failing test * Fails only when method name is same in multiple components * Scope the method removal to the corresponding component declaration
1 parent 6847d62 commit a6e27bf

File tree

6 files changed

+50
-2
lines changed

6 files changed

+50
-2
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Component extends React.Component {
2+
constructor() {
3+
super();
4+
this.onClick = this.onClick.bind(this);
5+
}
6+
onClick() { }
7+
}
8+
9+
class Component2 extends React.Component {
10+
constructor() {
11+
super();
12+
this.onClick = this.onClick.bind(this);
13+
}
14+
onClick() { }
15+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Component extends React.Component {
2+
onClick = () => { };
3+
}
4+
5+
class Component2 extends React.Component {
6+
onClick = () => { };
7+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Component extends React.Component {
2+
constructor() {
3+
super();
4+
this.onClick = this.onClick.bind(this);
5+
}
6+
onClick() { }
7+
}
8+
9+
class Component2 extends React.Component {
10+
constructor() {
11+
super();
12+
this.didClick = this.didClick.bind(this);
13+
}
14+
didClick() { }
15+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Component extends React.Component {
2+
onClick = () => { };
3+
}
4+
5+
class Component2 extends React.Component {
6+
didClick = () => { };
7+
}

transforms/__tests__/manual-bind-to-arrow-test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ var TESTS = [
2525
6,
2626
7,
2727
8,
28+
9,
29+
10
2830
];
2931

3032
TESTS.forEach(test => {

transforms/manual-bind-to-arrow.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ export default function transformer(file, api, options) {
107107

108108
// Find the method() declaration and replace it with an arrow function
109109
var methodName = path.node.left.property.name;
110-
var methods = root
110+
111+
const componentDecl = methodPath.parentPath;
112+
var methods = j(componentDecl)
111113
.find(j.MethodDefinition)
112114
.filter(
113115
path =>
@@ -121,7 +123,7 @@ export default function transformer(file, api, options) {
121123
return;
122124
}
123125
methods.replaceWith(path => createArrowProperty(path.node));
124-
126+
125127
// Remove the line
126128
// this.method = this.method.bind(this);
127129
j(path.parentPath).remove();

0 commit comments

Comments
 (0)