Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added check for template literals in no-string-refs rule #2167

Merged
merged 2 commits into from Feb 18, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/rules/no-string-refs.md
Expand Up @@ -26,6 +26,22 @@ var Hello = createReactClass({
});
```

```jsx
var Hello = createReactClass({
render: function() {
return <div ref={`hello`}>Hello, world.</div>;
}
});
```

```jsx
var Hello = createReactClass({
render: function() {
return <div ref={`hello${index}`}>Hello, world.</div>;
}
});
```

The following patterns are **not** considered warnings:

```jsx
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/no-string-refs.js
Expand Up @@ -75,8 +75,8 @@ module.exports = {
node.value &&
node.value.type === 'JSXExpressionContainer' &&
node.value.expression &&
node.value.expression.type === 'Literal' &&
typeof node.value.expression.value === 'string'
((node.value.expression.type === 'Literal' && typeof node.value.expression.value === 'string') ||
node.value.expression.type === 'TemplateLiteral')
);
}

Expand Down
36 changes: 36 additions & 0 deletions tests/lib/rules/no-string-refs.js
Expand Up @@ -97,5 +97,41 @@ ruleTester.run('no-refs', rule, {
}, {
message: 'Using string literals in ref attributes is deprecated.'
}]
},
{
code: [
'var Hello = createReactClass({',
' componentDidMount: function() {',
' var component = this.refs.hello;',
' },',
' render: function() {',
' return <div ref={`hello`}>Hello {this.props.name}</div>;',
' }',
'});'
].join('\n'),
parser: 'babel-eslint',
errors: [{
message: 'Using this.refs is deprecated.'
}, {
message: 'Using string literals in ref attributes is deprecated.'
}]
},
{
code: [
'var Hello = createReactClass({',
' componentDidMount: function() {',
' var component = this.refs.hello;',
' },',
' render: function() {',
' return <div ref={`hello${index}`}>Hello {this.props.name}</div>;',
' }',
'});'
].join('\n'),
parser: 'babel-eslint',
errors: [{
message: 'Using this.refs is deprecated.'
}, {
message: 'Using string literals in ref attributes is deprecated.'
}]
}]
});