Skip to content

Commit 4a72e6a

Browse files
authored
Merge pull request #2167 from jenil94/no-string-refs-fix
Added check for template literals in no-string-refs rule
2 parents bc976b8 + c484491 commit 4a72e6a

File tree

3 files changed

+96
-3
lines changed

3 files changed

+96
-3
lines changed

docs/rules/no-string-refs.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,29 @@ var Hello = createReactClass({
3939
}
4040
});
4141
```
42+
43+
## Rule Options
44+
45+
```js
46+
"react/no-string-refs": [<enabled>, {"noTemplateLiterals": <boolean>}]
47+
```
48+
### `noTemplateLiterals`
49+
50+
When set to `true`, it will give warning when using template literals for refs.
51+
The following patterns will be considered warnings:
52+
53+
```jsx
54+
var Hello = createReactClass({
55+
render: function() {
56+
return <div ref={`hello`}>Hello, world.</div>;
57+
}
58+
});
59+
```
60+
61+
```jsx
62+
var Hello = createReactClass({
63+
render: function() {
64+
return <div ref={`hello${index}`}>Hello, world.</div>;
65+
}
66+
});
67+
```

lib/rules/no-string-refs.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,19 @@ module.exports = {
1919
recommended: true,
2020
url: docsUrl('no-string-refs')
2121
},
22-
schema: []
22+
schema: [{
23+
type: 'object',
24+
properties: {
25+
noTemplateLiterals: {
26+
type: 'boolean'
27+
}
28+
},
29+
additionalProperties: false
30+
}]
2331
},
2432

2533
create: Components.detect((context, components, utils) => {
34+
const detectTemplateLiterals = context.options[0] ? context.options[0].noTemplateLiterals : false;
2635
/**
2736
* Checks if we are using refs
2837
* @param {ASTNode} node The AST node being checked.
@@ -75,8 +84,8 @@ module.exports = {
7584
node.value &&
7685
node.value.type === 'JSXExpressionContainer' &&
7786
node.value.expression &&
78-
node.value.expression.type === 'Literal' &&
79-
typeof node.value.expression.value === 'string'
87+
((node.value.expression.type === 'Literal' && typeof node.value.expression.value === 'string') ||
88+
(node.value.expression.type === 'TemplateLiteral' && detectTemplateLiterals))
8089
);
8190
}
8291

tests/lib/rules/no-string-refs.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,26 @@ ruleTester.run('no-refs', rule, {
3838
});
3939
`,
4040
parser: 'babel-eslint'
41+
},
42+
{
43+
code: [
44+
'var Hello = createReactClass({',
45+
' render: function() {',
46+
' return <div ref={`hello`}>Hello {this.props.name}</div>;',
47+
' }',
48+
'});'
49+
].join('\n'),
50+
parser: 'babel-eslint'
51+
},
52+
{
53+
code: [
54+
'var Hello = createReactClass({',
55+
' render: function() {',
56+
' return <div ref={`hello${index}`}>Hello {this.props.name}</div>;',
57+
' }',
58+
'});'
59+
].join('\n'),
60+
parser: 'babel-eslint'
4161
}
4262
],
4363

@@ -97,5 +117,43 @@ ruleTester.run('no-refs', rule, {
97117
}, {
98118
message: 'Using string literals in ref attributes is deprecated.'
99119
}]
120+
},
121+
{
122+
code: [
123+
'var Hello = createReactClass({',
124+
' componentDidMount: function() {',
125+
' var component = this.refs.hello;',
126+
' },',
127+
' render: function() {',
128+
' return <div ref={`hello`}>Hello {this.props.name}</div>;',
129+
' }',
130+
'});'
131+
].join('\n'),
132+
parser: 'babel-eslint',
133+
options: [{noTemplateLiterals: true}],
134+
errors: [{
135+
message: 'Using this.refs is deprecated.'
136+
}, {
137+
message: 'Using string literals in ref attributes is deprecated.'
138+
}]
139+
},
140+
{
141+
code: [
142+
'var Hello = createReactClass({',
143+
' componentDidMount: function() {',
144+
' var component = this.refs.hello;',
145+
' },',
146+
' render: function() {',
147+
' return <div ref={`hello${index}`}>Hello {this.props.name}</div>;',
148+
' }',
149+
'});'
150+
].join('\n'),
151+
parser: 'babel-eslint',
152+
options: [{noTemplateLiterals: true}],
153+
errors: [{
154+
message: 'Using this.refs is deprecated.'
155+
}, {
156+
message: 'Using string literals in ref attributes is deprecated.'
157+
}]
100158
}]
101159
});

0 commit comments

Comments
 (0)