diff --git a/lib/util/version.js b/lib/util/version.js index 8d22a3e41c..f311d7b84b 100644 --- a/lib/util/version.js +++ b/lib/util/version.js @@ -46,11 +46,29 @@ function getReactVersionFromContext(context) { return confVer.split('.').map(part => Number(part)); } +function detectFlowVersion() { + try { + const flowPackageJsonPath = resolve.sync('flow-bin/package.json', {basedir: process.cwd()}); + const flowPackageJson = require(flowPackageJsonPath); + return flowPackageJson.version; + } catch (e) { + if (e.code === 'MODULE_NOT_FOUND') { + error('Warning: Flow version was set to "detect" in eslint-plugin-react settings, ' + + 'but the "flow-bin" package is not installed. Assuming latest Flow version for linting.'); + return '999.999.999'; + } + throw e; + } +} + function getFlowVersionFromContext(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.flowVersion) { - const flowVersion = context.settings.react.flowVersion; + let flowVersion = context.settings.react.flowVersion; + if (flowVersion === 'detect') { + flowVersion = detectFlowVersion(); + } if (typeof flowVersion !== 'string') { error('Warning: Flow version specified in eslint-plugin-react-settings must be a string; ' + `got “${typeof flowVersion}”`); diff --git a/tests/fixtures/version/detect-version/node_modules/flow-bin/package.json b/tests/fixtures/version/detect-version/node_modules/flow-bin/package.json new file mode 100644 index 0000000000..83d4741ff2 --- /dev/null +++ b/tests/fixtures/version/detect-version/node_modules/flow-bin/package.json @@ -0,0 +1,4 @@ +{ + "name": "flow-bin", + "version": "0.92.0" +} diff --git a/tests/util/version.js b/tests/util/version.js index 65336bcf2f..cf0eaf034c 100644 --- a/tests/util/version.js +++ b/tests/util/version.js @@ -26,12 +26,13 @@ describe('Version', () => { }); describe('Detect version', () => { - const context = {settings: {react: {version: 'detect'}}}; + const context = {settings: {react: {version: 'detect', flowVersion: 'detect'}}}; it('matches detected version', () => { process.chdir('detect-version'); assert.equal(versionUtil.testReactVersion(context, '1.2.3'), true); assert.equal(versionUtil.testReactVersion(context, '1.2.4'), false); + assert.equal(versionUtil.testFlowVersion(context, '0.92.0'), true); }); it('assumes latest version if react is not installed', () => { @@ -41,6 +42,14 @@ describe('Version', () => { ['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.'] ]; }); + + it('assumes latest version if flow-bin is not installed', () => { + assert.equal(versionUtil.testFlowVersion(context, '999.999.999'), true); + + expectedErrorArgs = [ + ['Warning: Flow version was set to "detect" in eslint-plugin-react settings, but the "flow-bin" package is not installed. Assuming latest Flow version for linting.'] + ]; + }); }); describe('string version', () => {