Skip to content

Commit f59b3fa

Browse files
committed
[New] no-string-refs: allow this.refs in > 18.3.0
1 parent a2306e7 commit f59b3fa

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

lib/rules/no-string-refs.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
const componentUtil = require('../util/componentUtil');
99
const docsUrl = require('../util/docsUrl');
1010
const report = require('../util/report');
11+
const testReactVersion = require('../util/version').testReactVersion;
1112

1213
// ------------------------------------------------------------------------------
1314
// Rule Definition
@@ -42,6 +43,7 @@ module.exports = {
4243
},
4344

4445
create(context) {
46+
const checkRefsUsage = testReactVersion(context, '< 18.3.0'); // `this.refs` is writeable in React 18.3.0 and later, see https://github.com/facebook/react/pull/28867
4547
const detectTemplateLiterals = context.options[0] ? context.options[0].noTemplateLiterals : false;
4648
/**
4749
* Checks if we are using refs
@@ -93,7 +95,7 @@ module.exports = {
9395

9496
return {
9597
MemberExpression(node) {
96-
if (isRefsUsage(node)) {
98+
if (checkRefsUsage && isRefsUsage(node)) {
9799
report(context, messages.thisRefsDeprecated, 'thisRefsDeprecated', {
98100
node,
99101
});

tests/lib/rules/no-string-refs.js

+23
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ ruleTester.run('no-refs', rule, {
7373
}
7474
});
7575
`,
76+
settings: { react: { version: '18.2.0' } },
7677
errors: [{ messageId: 'thisRefsDeprecated' }],
7778
},
7879
{
@@ -83,6 +84,7 @@ ruleTester.run('no-refs', rule, {
8384
}
8485
});
8586
`,
87+
settings: { react: { version: '18.2.0' } },
8688
errors: [{ messageId: 'stringInRefDeprecated' }],
8789
},
8890
{
@@ -93,6 +95,7 @@ ruleTester.run('no-refs', rule, {
9395
}
9496
});
9597
`,
98+
settings: { react: { version: '18.2.0' } },
9699
errors: [{ messageId: 'stringInRefDeprecated' }],
97100
},
98101
{
@@ -106,6 +109,7 @@ ruleTester.run('no-refs', rule, {
106109
}
107110
});
108111
`,
112+
settings: { react: { version: '18.2.0' } },
109113
errors: [
110114
{ messageId: 'thisRefsDeprecated' },
111115
{ messageId: 'stringInRefDeprecated' },
@@ -123,6 +127,7 @@ ruleTester.run('no-refs', rule, {
123127
});
124128
`,
125129
options: [{ noTemplateLiterals: true }],
130+
settings: { react: { version: '18.2.0' } },
126131
errors: [
127132
{ messageId: 'thisRefsDeprecated' },
128133
{ messageId: 'stringInRefDeprecated' },
@@ -140,10 +145,28 @@ ruleTester.run('no-refs', rule, {
140145
});
141146
`,
142147
options: [{ noTemplateLiterals: true }],
148+
settings: { react: { version: '18.2.0' } },
143149
errors: [
144150
{ messageId: 'thisRefsDeprecated' },
145151
{ messageId: 'stringInRefDeprecated' },
146152
],
147153
},
154+
{
155+
code: `
156+
var Hello = createReactClass({
157+
componentDidMount: function() {
158+
var component = this.refs.hello;
159+
},
160+
render: function() {
161+
return <div ref={\`hello\${index}\`}>Hello {this.props.name}</div>;
162+
}
163+
});
164+
`,
165+
options: [{ noTemplateLiterals: true }],
166+
settings: { react: { version: '18.3.0' } },
167+
errors: [
168+
{ messageId: 'stringInRefDeprecated' },
169+
],
170+
},
148171
]),
149172
});

0 commit comments

Comments
 (0)