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
26 changes: 26 additions & 0 deletions docs/rules/no-string-refs.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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.'
}]
}]
});