Skip to content

Commit

Permalink
[new] add "detect" for flow version
Browse files Browse the repository at this point in the history
  • Loading branch information
jedwards1211 authored and ljharb committed Apr 10, 2019
1 parent 5a60432 commit cc7b98b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
20 changes: 19 additions & 1 deletion lib/util/version.js
Expand Up @@ -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}”`);
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion tests/util/version.js
Expand Up @@ -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', () => {
Expand All @@ -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', () => {
Expand Down

0 comments on commit cc7b98b

Please sign in to comment.