Skip to content

Commit 426b228

Browse files
committed
Add shared setting for React version
1 parent 2226204 commit 426b228

File tree

5 files changed

+44
-23
lines changed

5 files changed

+44
-23
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ You can also specify some settings that will be shared across all the plugin rul
3737
{
3838
"settings": {
3939
"react": {
40-
"pragma": "React" // Pragma to use, default to "React"
40+
"pragma": "React", // Pragma to use, default to "React"
41+
"version": "0.14.0" // React version, default to the latest React stable release
4142
}
4243
}
4344
}

docs/rules/no-deprecated.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ ReactDOM.findDOMNode(this.refs.foo);
2929

3030
## Rule Options
3131

32+
**Deprecation notice**: This option is deprecated, please use the [shared settings](/README.md#configuration) to specify the React version.
33+
3234
By default this rule will warn to every methods marked as deprecated. You can limit it to an older React version if you are not using the latest one:
3335

3436
```js

lib/rules/no-deprecated.js

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
'use strict';
77

88
var pragmaUtil = require('../util/pragma');
9+
var versionUtil = require('../util/version');
910

1011
// ------------------------------------------------------------------------------
1112
// Constants
@@ -20,15 +21,6 @@ var DEPRECATED_MESSAGE = '{{oldMethod}} is deprecated since React {{version}}{{n
2021
module.exports = function(context) {
2122

2223
var sourceCode = context.getSourceCode();
23-
24-
// Validate React version passed in options
25-
// (append the patch version if missing, allowing shorthands like 0.12 for React 0.12.0)
26-
var optVer = context.options[0] ? context.options[0].react : '999.999.999';
27-
optVer = /^[0-9]+\.[0-9]+$/.test(optVer) ? optVer + '.0' : optVer;
28-
optVer = optVer.split('.').map(function(part) {
29-
return Number(part);
30-
});
31-
3224
var pragma = pragmaUtil.getFromContext(context);
3325

3426
function getDeprecated() {
@@ -60,24 +52,13 @@ module.exports = function(context) {
6052
return deprecated;
6153
}
6254

63-
function checkVersion(methodVer) {
64-
methodVer = methodVer.split('.').map(function(part) {
65-
return Number(part);
66-
});
67-
var higherMajor = methodVer[0] < optVer[0];
68-
var higherMinor = methodVer[0] === optVer[0] && methodVer[1] < optVer[1];
69-
var higherOrEqualPatch = methodVer[0] === optVer[0] && methodVer[1] === optVer[1] && methodVer[2] <= optVer[2];
70-
71-
return higherMajor || higherMinor || higherOrEqualPatch;
72-
}
73-
7455
function isDeprecated(type, method) {
7556
var deprecated = getDeprecated();
7657

7758
return (
7859
deprecated[type] &&
7960
deprecated[type][method] &&
80-
checkVersion(deprecated[type][method][0])
61+
versionUtil.test(context, deprecated[type][method][0])
8162
);
8263
}
8364

lib/util/version.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @fileoverview Utility functions for React version configuration
3+
* @author Yannick Croissant
4+
*/
5+
'use strict';
6+
7+
function getFromContext(context) {
8+
var confVer = '999.999.999';
9+
// .eslintrc shared settings (http://eslint.org/docs/user-guide/configuring#adding-shared-settings)
10+
if (context.settings.react && context.settings.react.version) {
11+
confVer = context.settings.react.version;
12+
// Deprecated react option, here for backward compatibility
13+
} else if (context.options[0] && context.options[0].react) {
14+
confVer = context.options[0].react;
15+
}
16+
confVer = /^[0-9]+\.[0-9]+$/.test(confVer) ? confVer + '.0' : confVer;
17+
return confVer.split('.').map(function(part) {
18+
return Number(part);
19+
});
20+
}
21+
22+
function test(context, methodVer) {
23+
var confVer = getFromContext(context);
24+
methodVer = methodVer.split('.').map(function(part) {
25+
return Number(part);
26+
});
27+
var higherMajor = methodVer[0] < confVer[0];
28+
var higherMinor = methodVer[0] === confVer[0] && methodVer[1] < confVer[1];
29+
var higherOrEqualPatch = methodVer[0] === confVer[0] && methodVer[1] === confVer[1] && methodVer[2] <= confVer[2];
30+
31+
return higherMajor || higherMinor || higherOrEqualPatch;
32+
}
33+
34+
module.exports = {
35+
test: test
36+
};

tests/lib/rules/no-deprecated.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ ruleTester.run('no-deprecated', rule, {
3838
'ReactDOMServer.renderToString(element);',
3939
'ReactDOMServer.renderToStaticMarkup(element);',
4040
// Deprecated in a later version
41-
{code: 'React.renderComponent()', options: [{react: '0.11.0'}]}
41+
{code: 'React.renderComponent()', options: [{react: '0.11.0'}]},
42+
{code: 'React.renderComponent()', settings: {react: {version: '0.11.0'}}}
4243
],
4344

4445
invalid: [{

0 commit comments

Comments
 (0)