Skip to content

Commit 5dd50fa

Browse files
committed
[Fix] version settings: avoid a crash with an invalid version
Fixes #3219
1 parent a5dc2a2 commit 5dd50fa

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
99
* [`jsx-key`]: prevent false "missing array key" warning ([#3215][] @ljharb)
1010
* [`jsx-indent`]: avoid checking returns sans jsx ([#3218][] @ljharb)
1111
* [`jsx-key`]: avoid a crash ([#3220][] @ljharb)
12+
* version settings: avoid a crash with an invalid version ([#3219][] @ljharb)
1213

1314
[#3220]: https://github.com/yannickcr/eslint-plugin-react/issues/3220
15+
[#3219]: https://github.com/yannickcr/eslint-plugin-react/issues/3219
1416
[#3218]: https://github.com/yannickcr/eslint-plugin-react/issues/3218
1517
[#3215]: https://github.com/yannickcr/eslint-plugin-react/issues/3215
1618

lib/util/version.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,10 @@ function detectReactVersion(context) {
7070
}
7171
}
7272

73+
const defaultVersion = '999.999.999';
74+
7375
function getReactVersionFromContext(context) {
74-
let confVer = '999.999.999';
76+
let confVer = defaultVersion;
7577
// .eslintrc shared settings (https://eslint.org/docs/user-guide/configuring#adding-shared-settings)
7678
if (context.settings && context.settings.react && context.settings.react.version) {
7779
let settingsVersion = context.settings.react.version;
@@ -89,7 +91,11 @@ function getReactVersionFromContext(context) {
8991
warnedForMissingVersion = true;
9092
}
9193
confVer = /^[0-9]+\.[0-9]+$/.test(confVer) ? `${confVer}.0` : confVer;
92-
return semver.coerce(confVer.split('.').map((part) => Number(part)).join('.')).version;
94+
const result = semver.coerce(confVer.split('.').map((part) => Number(part)).join('.'));
95+
if (!result) {
96+
error(`Warning: React version specified in eslint-plugin-react-settings must be a valid semver version, or "detect"; got “${confVer}”`);
97+
}
98+
return result ? result.version : defaultVersion;
9399
}
94100

95101
// TODO, semver-major: remove context fallback
@@ -111,7 +117,7 @@ function detectFlowVersion(context) {
111117
}
112118

113119
function getFlowVersionFromContext(context) {
114-
let confVer = '999.999.999';
120+
let confVer = defaultVersion;
115121
// .eslintrc shared settings (https://eslint.org/docs/user-guide/configuring#adding-shared-settings)
116122
if (context.settings.react && context.settings.react.flowVersion) {
117123
let flowVersion = context.settings.react.flowVersion;
@@ -127,7 +133,11 @@ function getFlowVersionFromContext(context) {
127133
throw 'Could not retrieve flowVersion from settings'; // eslint-disable-line no-throw-literal
128134
}
129135
confVer = /^[0-9]+\.[0-9]+$/.test(confVer) ? `${confVer}.0` : confVer;
130-
return semver.coerce(confVer.split('.').map((part) => Number(part)).join('.')).version;
136+
const result = semver.coerce(confVer.split('.').map((part) => Number(part)).join('.'));
137+
if (!result) {
138+
error(`Warning: Flow version specified in eslint-plugin-react-settings must be a valid semver version, or "detect"; got “${confVer}”`);
139+
}
140+
return result ? result.version : defaultVersion;
131141
}
132142

133143
function test(semverRange, confVer) {

tests/util/version.js

+15
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ describe('Version', () => {
103103

104104
describe('string version', () => {
105105
const context = { settings: { react: { version: '15.0', flowVersion: '1.2' } } };
106+
const invalidContext = { settings: { react: { version: 'latest', flowVersion: 'not semver' } } };
106107

107108
it('works with react', () => {
108109
assert.equal(versionUtil.testReactVersion(context, '>= 0.14.0'), true);
@@ -115,6 +116,20 @@ describe('Version', () => {
115116
assert.equal(versionUtil.testFlowVersion(context, '>= 1.2.0'), true);
116117
assert.equal(versionUtil.testFlowVersion(context, '>= 1.3.0'), false);
117118
});
119+
120+
it('fails nicely with an invalid react version', () => {
121+
assert.equal(versionUtil.testReactVersion(invalidContext, '>= 15.0'), true);
122+
expectedErrorArgs = [
123+
['Warning: React version specified in eslint-plugin-react-settings must be a valid semver version, or "detect"; got “latest”'],
124+
];
125+
});
126+
127+
it('fails nicely with an invalid flow version', () => {
128+
assert.equal(versionUtil.testFlowVersion(invalidContext, '>= 1.0'), true);
129+
expectedErrorArgs = [
130+
['Warning: Flow version specified in eslint-plugin-react-settings must be a valid semver version, or "detect"; got “not semver”'],
131+
];
132+
});
118133
});
119134

120135
describe('non-string version', () => {

0 commit comments

Comments
 (0)