Skip to content

Commit 0e92240

Browse files
authored
Merge pull request #57 from reergymerej/master
56: identify components with refs as impure
2 parents ad573ab + ead02ad commit 0e92240

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

transforms/__testfixtures__/pure-component.input.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,14 @@ class Impure extends React.Component {
2121
}
2222
}
2323

24+
class ImpureWithRef extends React.Component {
25+
render() {
26+
return (
27+
<div>
28+
<span ref="spanasaurus" />
29+
</div>
30+
);
31+
}
32+
}
33+
2434
var A = props => <div className={props.foo} />;

transforms/__testfixtures__/pure-component.output.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,14 @@ class Impure extends React.Component {
1919
}
2020
}
2121

22+
class ImpureWithRef extends React.Component {
23+
render() {
24+
return (
25+
<div>
26+
<span ref="spanasaurus" />
27+
</div>
28+
);
29+
}
30+
}
31+
2232
var A = props => <div className={props.foo} />;

transforms/pure-component.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ module.exports = function(file, api, options) {
3636
.filter(p => !isRenderMethod(p.value))
3737
.size() === 0;
3838

39+
const hasRefs = path =>
40+
j(path)
41+
.find(j.JSXAttribute, {
42+
name: {
43+
type: 'JSXIdentifier',
44+
name: 'ref',
45+
},
46+
})
47+
.size() > 0;
48+
3949
const THIS_PROPS = {
4050
object: {
4151
type: 'ThisExpression',
@@ -82,7 +92,7 @@ module.exports = function(file, api, options) {
8292

8393
const pureClasses = ReactUtils.findReactES6ClassDeclaration(f)
8494
.filter(path => {
85-
const isPure = onlyHasRenderMethod(path);
95+
const isPure = onlyHasRenderMethod(path) && !hasRefs(path);
8696
if (!isPure && !silenceWarnings) {
8797
reportSkipped(path);
8898
}
@@ -109,4 +119,3 @@ module.exports = function(file, api, options) {
109119

110120
return f.toSource(printOptions);
111121
};
112-

0 commit comments

Comments
 (0)