Skip to content

Commit 6d9e7bb

Browse files
committed
[Fix] ensure that react and flow versions can be numbers
Fixes #2056.
1 parent e133f1c commit 6d9e7bb

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

lib/util/version.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ function getReactVersionFromContext(context) {
3232
if (settingsVersion === 'detect') {
3333
settingsVersion = detectReactVersion();
3434
}
35-
confVer = settingsVersion;
35+
if (typeof settingsVersion !== 'string') {
36+
log('Warning: React version specified in eslint-plugin-react-settings must be a string; ' +
37+
`got “${typeof settingsVersion}”`);
38+
}
39+
confVer = String(settingsVersion);
3640
} else if (!warnedForMissingVersion) {
3741
log('Warning: React version not specified in eslint-plugin-react settings. ' +
3842
'See https://github.com/yannickcr/eslint-plugin-react#configuration.');
@@ -46,19 +50,31 @@ function getFlowVersionFromContext(context) {
4650
let confVer = '999.999.999';
4751
// .eslintrc shared settings (http://eslint.org/docs/user-guide/configuring#adding-shared-settings)
4852
if (context.settings.react && context.settings.react.flowVersion) {
49-
confVer = context.settings.react.flowVersion;
53+
const flowVersion = context.settings.react.flowVersion;
54+
if (typeof flowVersion !== 'string') {
55+
log('Warning: Flow version specified in eslint-plugin-react-settings must be a string; ' +
56+
`got “${typeof flowVersion}”`);
57+
}
58+
confVer = String(flowVersion);
5059
} else {
5160
throw 'Could not retrieve flowVersion from settings';
5261
}
5362
confVer = /^[0-9]+\.[0-9]+$/.test(confVer) ? `${confVer}.0` : confVer;
5463
return confVer.split('.').map(part => Number(part));
5564
}
5665

66+
function normalizeParts(parts) {
67+
return Array.from({length: 3}, (_, i) => (parts[i] || 0));
68+
}
69+
5770
function test(context, methodVer, confVer) {
58-
methodVer = String(methodVer || '').split('.').map(part => Number(part));
59-
const higherMajor = methodVer[0] < confVer[0];
60-
const higherMinor = methodVer[0] === confVer[0] && methodVer[1] < confVer[1];
61-
const higherOrEqualPatch = methodVer[0] === confVer[0] && methodVer[1] === confVer[1] && methodVer[2] <= confVer[2];
71+
const methodVers = normalizeParts(String(methodVer || '').split('.').map(part => Number(part)));
72+
const confVers = normalizeParts(confVer);
73+
const higherMajor = methodVers[0] < confVers[0];
74+
const higherMinor = methodVers[0] === confVers[0] && methodVers[1] < confVers[1];
75+
const higherOrEqualPatch = methodVers[0] === confVers[0]
76+
&& methodVers[1] === confVers[1]
77+
&& methodVers[2] <= confVers[2];
6278

6379
return higherMajor || higherMinor || higherOrEqualPatch;
6480
}

tests/util/version.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,20 @@ describe('Version', () => {
3131
assert.equal(versionUtil.testReactVersion(context, '999.999.999'), true);
3232
});
3333
});
34+
35+
describe('non-string version', () => {
36+
const context = {settings: {react: {version: 15.0, flowVersion: 1.2}}};
37+
38+
it('works with react', () => {
39+
assert.equal(versionUtil.testReactVersion(context, '0.14.0'), true);
40+
assert.equal(versionUtil.testReactVersion(context, '15.0.0'), true);
41+
assert.equal(versionUtil.testReactVersion(context, '16.0.0'), false);
42+
});
43+
44+
it('works with flow', () => {
45+
assert.equal(versionUtil.testFlowVersion(context, '1.1.0'), true);
46+
assert.equal(versionUtil.testFlowVersion(context, '1.2.0'), true);
47+
assert.equal(versionUtil.testFlowVersion(context, '1.3.0'), false);
48+
});
49+
});
3450
});

0 commit comments

Comments
 (0)