Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion lib/util/version.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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