Skip to content

Commit

Permalink
[New] Support "detect" option for React version setting
Browse files Browse the repository at this point in the history
  • Loading branch information
alexzherdev committed Oct 14, 2018
1 parent 5dddbf4 commit 64ffe23
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
19 changes: 18 additions & 1 deletion lib/util/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
'use strict';

const resolve = require('resolve');
const log = require('./log');

let warnedForMissingVersion = false;
Expand All @@ -12,7 +13,23 @@ function getReactVersionFromContext(context) {
let confVer = '999.999.999';
// .eslintrc shared settings (http://eslint.org/docs/user-guide/configuring#adding-shared-settings)
if (context.settings.react && context.settings.react.version) {
confVer = context.settings.react.version;
let settingsVer = context.settings.react.version;
if (settingsVer === 'detect') {
try {
const reactPath = resolve.sync('react', {basedir: process.cwd()});
const react = require(reactPath);
settingsVer = react.version;
} catch (e) {
if (e.code === 'MODULE_NOT_FOUND') {
log('Warning: React version was set to "detect" in eslint-plugin-react settings, ' +
'but the "react" package is not installed. Assuming latest React version for linting.');
settingsVer = '999.999.999';
} else {
throw e;
}
}
}
confVer = settingsVer;
} else if (!warnedForMissingVersion) {
log('Warning: React version not specified in eslint-plugin-react settings. ' +
'See https://github.com/yannickcr/eslint-plugin-react#configuration.');
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"has": "^1.0.3",
"jsx-ast-utils": "^2.0.1",
"object.fromentries": "^2.0.0",
"prop-types": "^15.6.2"
"prop-types": "^15.6.2",
"resolve": "^1.8.1"
},
"devDependencies": {
"babel-eslint": "^8.2.5",
Expand Down

0 comments on commit 64ffe23

Please sign in to comment.