Skip to content

Commit

Permalink
Merge pull request #2167 from jenil94/no-string-refs-fix
Browse files Browse the repository at this point in the history
Added check for template literals in no-string-refs rule
  • Loading branch information
ljharb committed Feb 18, 2019
2 parents bc976b8 + c484491 commit 4a72e6a
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 3 deletions.
26 changes: 26 additions & 0 deletions docs/rules/no-string-refs.md
Expand Up @@ -39,3 +39,29 @@ var Hello = createReactClass({
}
});
```

## Rule Options

```js
"react/no-string-refs": [<enabled>, {"noTemplateLiterals": <boolean>}]
```
### `noTemplateLiterals`

When set to `true`, it will give warning when using template literals for refs.
The following patterns will be considered warnings:

```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>;
}
});
```
15 changes: 12 additions & 3 deletions lib/rules/no-string-refs.js
Expand Up @@ -19,10 +19,19 @@ module.exports = {
recommended: true,
url: docsUrl('no-string-refs')
},
schema: []
schema: [{
type: 'object',
properties: {
noTemplateLiterals: {
type: 'boolean'
}
},
additionalProperties: false
}]
},

create: Components.detect((context, components, utils) => {
const detectTemplateLiterals = context.options[0] ? context.options[0].noTemplateLiterals : false;
/**
* Checks if we are using refs
* @param {ASTNode} node The AST node being checked.
Expand Down Expand Up @@ -75,8 +84,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' && detectTemplateLiterals))
);
}

Expand Down
58 changes: 58 additions & 0 deletions tests/lib/rules/no-string-refs.js
Expand Up @@ -38,6 +38,26 @@ ruleTester.run('no-refs', rule, {
});
`,
parser: 'babel-eslint'
},
{
code: [
'var Hello = createReactClass({',
' render: function() {',
' return <div ref={`hello`}>Hello {this.props.name}</div>;',
' }',
'});'
].join('\n'),
parser: 'babel-eslint'
},
{
code: [
'var Hello = createReactClass({',
' render: function() {',
' return <div ref={`hello${index}`}>Hello {this.props.name}</div>;',
' }',
'});'
].join('\n'),
parser: 'babel-eslint'
}
],

Expand Down Expand Up @@ -97,5 +117,43 @@ 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',
options: [{noTemplateLiterals: true}],
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',
options: [{noTemplateLiterals: true}],
errors: [{
message: 'Using this.refs is deprecated.'
}, {
message: 'Using string literals in ref attributes is deprecated.'
}]
}]
});

0 comments on commit 4a72e6a

Please sign in to comment.