Repository: yannickcr/eslint-plugin-react
Pull request #2233 (2019-04-10 20:44:13)

implement flow version detection
not sure why you guys implemented React version detection without doing the same for Flow


Patch:
diff --git a/lib/util/version.js b/lib/util/version.js
index 8d22a3e4..f311d7b8 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}”`);


Test patch:
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 00000000..83d4741f
--- /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 65336bcf..cf0eaf03 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', () => {