@@ -9,27 +9,30 @@ const log = require('./log');
9
9
10
10
let warnedForMissingVersion = false ;
11
11
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
+
12
27
function getReactVersionFromContext ( context ) {
13
28
let confVer = '999.999.999' ;
14
29
// .eslintrc shared settings (http://eslint.org/docs/user-guide/configuring#adding-shared-settings)
15
30
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 ( ) ;
31
34
}
32
- confVer = settingsVer ;
35
+ confVer = settingsVersion ;
33
36
} else if ( ! warnedForMissingVersion ) {
34
37
log ( 'Warning: React version not specified in eslint-plugin-react settings. ' +
35
38
'See https://github.com/yannickcr/eslint-plugin-react#configuration.' ) ;
0 commit comments