Skip to content

Commit

Permalink
[New] no-string-refs: allow this.refs in > 18.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
henryqdineen committed Aug 27, 2024
1 parent a2306e7 commit f59b3fa
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/rules/no-string-refs.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
const componentUtil = require('../util/componentUtil');
const docsUrl = require('../util/docsUrl');
const report = require('../util/report');
const testReactVersion = require('../util/version').testReactVersion;

// ------------------------------------------------------------------------------
// Rule Definition
Expand Down Expand Up @@ -42,6 +43,7 @@ module.exports = {
},

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

return {
MemberExpression(node) {
if (isRefsUsage(node)) {
if (checkRefsUsage && isRefsUsage(node)) {
report(context, messages.thisRefsDeprecated, 'thisRefsDeprecated', {
node,
});
Expand Down
23 changes: 23 additions & 0 deletions tests/lib/rules/no-string-refs.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ ruleTester.run('no-refs', rule, {
}
});
`,
settings: { react: { version: '18.2.0' } },
errors: [{ messageId: 'thisRefsDeprecated' }],
},
{
Expand All @@ -83,6 +84,7 @@ ruleTester.run('no-refs', rule, {
}
});
`,
settings: { react: { version: '18.2.0' } },
errors: [{ messageId: 'stringInRefDeprecated' }],
},
{
Expand All @@ -93,6 +95,7 @@ ruleTester.run('no-refs', rule, {
}
});
`,
settings: { react: { version: '18.2.0' } },
errors: [{ messageId: 'stringInRefDeprecated' }],
},
{
Expand All @@ -106,6 +109,7 @@ ruleTester.run('no-refs', rule, {
}
});
`,
settings: { react: { version: '18.2.0' } },
errors: [
{ messageId: 'thisRefsDeprecated' },
{ messageId: 'stringInRefDeprecated' },
Expand All @@ -123,6 +127,7 @@ ruleTester.run('no-refs', rule, {
});
`,
options: [{ noTemplateLiterals: true }],
settings: { react: { version: '18.2.0' } },
errors: [
{ messageId: 'thisRefsDeprecated' },
{ messageId: 'stringInRefDeprecated' },
Expand All @@ -140,10 +145,28 @@ ruleTester.run('no-refs', rule, {
});
`,
options: [{ noTemplateLiterals: true }],
settings: { react: { version: '18.2.0' } },
errors: [
{ messageId: 'thisRefsDeprecated' },
{ messageId: 'stringInRefDeprecated' },
],
},
{
code: `
var Hello = createReactClass({
componentDidMount: function() {
var component = this.refs.hello;
},
render: function() {
return <div ref={\`hello\${index}\`}>Hello {this.props.name}</div>;
}
});
`,
options: [{ noTemplateLiterals: true }],
settings: { react: { version: '18.3.0' } },
errors: [
{ messageId: 'stringInRefDeprecated' },
],
},
]),
});

0 comments on commit f59b3fa

Please sign in to comment.