Skip to content

Commit cc7b98b

Browse files
jedwards1211ljharb
authored andcommitted
[new] add "detect" for flow version
1 parent 5a60432 commit cc7b98b

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

lib/util/version.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,29 @@ function getReactVersionFromContext(context) {
4646
return confVer.split('.').map(part => Number(part));
4747
}
4848

49+
function detectFlowVersion() {
50+
try {
51+
const flowPackageJsonPath = resolve.sync('flow-bin/package.json', {basedir: process.cwd()});
52+
const flowPackageJson = require(flowPackageJsonPath);
53+
return flowPackageJson.version;
54+
} catch (e) {
55+
if (e.code === 'MODULE_NOT_FOUND') {
56+
error('Warning: Flow version was set to "detect" in eslint-plugin-react settings, ' +
57+
'but the "flow-bin" package is not installed. Assuming latest Flow version for linting.');
58+
return '999.999.999';
59+
}
60+
throw e;
61+
}
62+
}
63+
4964
function getFlowVersionFromContext(context) {
5065
let confVer = '999.999.999';
5166
// .eslintrc shared settings (http://eslint.org/docs/user-guide/configuring#adding-shared-settings)
5267
if (context.settings.react && context.settings.react.flowVersion) {
53-
const flowVersion = context.settings.react.flowVersion;
68+
let flowVersion = context.settings.react.flowVersion;
69+
if (flowVersion === 'detect') {
70+
flowVersion = detectFlowVersion();
71+
}
5472
if (typeof flowVersion !== 'string') {
5573
error('Warning: Flow version specified in eslint-plugin-react-settings must be a string; ' +
5674
`got “${typeof flowVersion}”`);

tests/fixtures/version/detect-version/node_modules/flow-bin/package.json

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/util/version.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@ describe('Version', () => {
2626
});
2727

2828
describe('Detect version', () => {
29-
const context = {settings: {react: {version: 'detect'}}};
29+
const context = {settings: {react: {version: 'detect', flowVersion: 'detect'}}};
3030

3131
it('matches detected version', () => {
3232
process.chdir('detect-version');
3333
assert.equal(versionUtil.testReactVersion(context, '1.2.3'), true);
3434
assert.equal(versionUtil.testReactVersion(context, '1.2.4'), false);
35+
assert.equal(versionUtil.testFlowVersion(context, '0.92.0'), true);
3536
});
3637

3738
it('assumes latest version if react is not installed', () => {
@@ -41,6 +42,14 @@ describe('Version', () => {
4142
['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.']
4243
];
4344
});
45+
46+
it('assumes latest version if flow-bin is not installed', () => {
47+
assert.equal(versionUtil.testFlowVersion(context, '999.999.999'), true);
48+
49+
expectedErrorArgs = [
50+
['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.']
51+
];
52+
});
4453
});
4554

4655
describe('string version', () => {

0 commit comments

Comments
 (0)