Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange

## Unreleased

### Added
* [`no-string-refs`]: allow this.refs in > 18.3.0 ([#3807][] @henryqdineen)

[#3807]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3807

## [7.35.1] - 2024.09.02

### Fixed
Expand Down
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 writable in React 18.3.0 and later, see https://github.com/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
36 changes: 36 additions & 0 deletions tests/lib/rules/no-string-refs.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,19 @@ ruleTester.run('no-refs', rule, {
});
`,
},
{
code: `
var Hello = createReactClass({
componentDidMount: function() {
var component = this.refs.hello;
},
render: function() {
return <div>Hello {this.props.name}</div>;
}
});
`,
settings: { react: { version: '18.3.0' } },
},
]),

invalid: parsers.all([
Expand All @@ -73,6 +86,7 @@ ruleTester.run('no-refs', rule, {
}
});
`,
settings: { react: { version: '18.2.0' } },
errors: [{ messageId: 'thisRefsDeprecated' }],
},
{
Expand All @@ -83,6 +97,7 @@ ruleTester.run('no-refs', rule, {
}
});
`,
settings: { react: { version: '18.2.0' } },
errors: [{ messageId: 'stringInRefDeprecated' }],
},
{
Expand All @@ -93,6 +108,7 @@ ruleTester.run('no-refs', rule, {
}
});
`,
settings: { react: { version: '18.2.0' } },
errors: [{ messageId: 'stringInRefDeprecated' }],
},
{
Expand All @@ -106,6 +122,7 @@ ruleTester.run('no-refs', rule, {
}
});
`,
settings: { react: { version: '18.2.0' } },
errors: [
{ messageId: 'thisRefsDeprecated' },
{ messageId: 'stringInRefDeprecated' },
Expand All @@ -123,6 +140,7 @@ ruleTester.run('no-refs', rule, {
});
`,
options: [{ noTemplateLiterals: true }],
settings: { react: { version: '18.2.0' } },
errors: [
{ messageId: 'thisRefsDeprecated' },
{ messageId: 'stringInRefDeprecated' },
Expand All @@ -140,10 +158,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' },
],
},
]),
});