@@ -9,27 +9,30 @@ const log = require('./log');
99
1010let warnedForMissingVersion = false ;
1111
12+ function detectReactVersion ( ) {
13+ try {
14+ const reactPath = resolve . sync ( 'react' , { basedir : process . cwd ( ) } ) ;
15+ const react = require ( reactPath ) ;
16+ return react . version ;
17+ } catch ( e ) {
18+ if ( e . code === 'MODULE_NOT_FOUND' ) {
19+ log ( 'Warning: React version was set to "detect" in eslint-plugin-react settings, ' +
20+ 'but the "react" package is not installed. Assuming latest React version for linting.' ) ;
21+ return '999.999.999' ;
22+ }
23+ throw e ;
24+ }
25+ }
26+
1227function getReactVersionFromContext ( context ) {
1328 let confVer = '999.999.999' ;
1429 // .eslintrc shared settings (http://eslint.org/docs/user-guide/configuring#adding-shared-settings)
1530 if ( context . settings . react && context . settings . react . version ) {
16- let settingsVer = context . settings . react . version ;
17- if ( settingsVer === 'detect' ) {
18- try {
19- const reactPath = resolve . sync ( 'react' , { basedir : process . cwd ( ) } ) ;
20- const react = require ( reactPath ) ;
21- settingsVer = react . version ;
22- } catch ( e ) {
23- if ( e . code === 'MODULE_NOT_FOUND' ) {
24- log ( 'Warning: React version was set to "detect" in eslint-plugin-react settings, ' +
25- 'but the "react" package is not installed. Assuming latest React version for linting.' ) ;
26- settingsVer = '999.999.999' ;
27- } else {
28- throw e ;
29- }
30- }
31+ let settingsVersion = context . settings . react . version ;
32+ if ( settingsVersion === 'detect' ) {
33+ settingsVersion = detectReactVersion ( ) ;
3134 }
32- confVer = settingsVer ;
35+ confVer = settingsVersion ;
3336 } else if ( ! warnedForMissingVersion ) {
3437 log ( 'Warning: React version not specified in eslint-plugin-react settings. ' +
3538 'See https://github.com/yannickcr/eslint-plugin-react#configuration.' ) ;
0 commit comments