Skip to content

Commit

Permalink
[Fix] ensure that react and flow versions can be numbers
Browse files Browse the repository at this point in the history
Fixes #2056.
  • Loading branch information
ljharb committed Nov 26, 2018
1 parent e133f1c commit 6d9e7bb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
28 changes: 22 additions & 6 deletions lib/util/version.js
Expand Up @@ -32,7 +32,11 @@ function getReactVersionFromContext(context) {
if (settingsVersion === 'detect') {
settingsVersion = detectReactVersion();
}
confVer = settingsVersion;
if (typeof settingsVersion !== 'string') {
log('Warning: React version specified in eslint-plugin-react-settings must be a string; ' +
`got “${typeof settingsVersion}”`);
}
confVer = String(settingsVersion);
} else if (!warnedForMissingVersion) {
log('Warning: React version not specified in eslint-plugin-react settings. ' +
'See https://github.com/yannickcr/eslint-plugin-react#configuration.');
Expand All @@ -46,19 +50,31 @@ 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) {
confVer = context.settings.react.flowVersion;
const flowVersion = context.settings.react.flowVersion;
if (typeof flowVersion !== 'string') {
log('Warning: Flow version specified in eslint-plugin-react-settings must be a string; ' +
`got “${typeof flowVersion}”`);
}
confVer = String(flowVersion);
} else {
throw 'Could not retrieve flowVersion from settings';
}
confVer = /^[0-9]+\.[0-9]+$/.test(confVer) ? `${confVer}.0` : confVer;
return confVer.split('.').map(part => Number(part));
}

function normalizeParts(parts) {
return Array.from({length: 3}, (_, i) => (parts[i] || 0));
}

function test(context, methodVer, confVer) {
methodVer = String(methodVer || '').split('.').map(part => Number(part));
const higherMajor = methodVer[0] < confVer[0];
const higherMinor = methodVer[0] === confVer[0] && methodVer[1] < confVer[1];
const higherOrEqualPatch = methodVer[0] === confVer[0] && methodVer[1] === confVer[1] && methodVer[2] <= confVer[2];
const methodVers = normalizeParts(String(methodVer || '').split('.').map(part => Number(part)));
const confVers = normalizeParts(confVer);
const higherMajor = methodVers[0] < confVers[0];
const higherMinor = methodVers[0] === confVers[0] && methodVers[1] < confVers[1];
const higherOrEqualPatch = methodVers[0] === confVers[0]
&& methodVers[1] === confVers[1]
&& methodVers[2] <= confVers[2];

return higherMajor || higherMinor || higherOrEqualPatch;
}
Expand Down
16 changes: 16 additions & 0 deletions tests/util/version.js
Expand Up @@ -31,4 +31,20 @@ describe('Version', () => {
assert.equal(versionUtil.testReactVersion(context, '999.999.999'), true);
});
});

describe('non-string version', () => {
const context = {settings: {react: {version: 15.0, flowVersion: 1.2}}};

it('works with react', () => {
assert.equal(versionUtil.testReactVersion(context, '0.14.0'), true);
assert.equal(versionUtil.testReactVersion(context, '15.0.0'), true);
assert.equal(versionUtil.testReactVersion(context, '16.0.0'), false);
});

it('works with flow', () => {
assert.equal(versionUtil.testFlowVersion(context, '1.1.0'), true);
assert.equal(versionUtil.testFlowVersion(context, '1.2.0'), true);
assert.equal(versionUtil.testFlowVersion(context, '1.3.0'), false);
});
});
});

0 comments on commit 6d9e7bb

Please sign in to comment.