diff --git a/lib/util/version.js b/lib/util/version.js index 4aa472ccaa..6d3587ea7f 100644 --- a/lib/util/version.js +++ b/lib/util/version.js @@ -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.'); @@ -46,7 +50,12 @@ 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'; } @@ -54,11 +63,18 @@ function getFlowVersionFromContext(context) { 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; } diff --git a/tests/util/version.js b/tests/util/version.js index 5112f8afcd..a4d7733720 100644 --- a/tests/util/version.js +++ b/tests/util/version.js @@ -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); + }); + }); });