Skip to content

Commit dc28d26

Browse files
alexzherdevljharb
authored andcommitted
[New] Support "detect" option for React version setting
1 parent bc6a1ba commit dc28d26

File tree

6 files changed

+69
-3
lines changed

6 files changed

+69
-3
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ logs
1515
reports
1616
build
1717
node_modules
18+
!tests/**/node_modules
1819
npm-debug.log
1920
sftp-config.json
2021

Diff for: lib/util/version.js

+21-1
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,35 @@
44
*/
55
'use strict';
66

7+
const resolve = require('resolve');
78
const log = require('./log');
89

910
let warnedForMissingVersion = false;
1011

12+
function detectReactVersion() {
13+
try {
14+
const reactPath = resolve.sync('react', {basedir: process.cwd()});
15+
const react = require(reactPath);
16+
return react.version;
17+
} catch (e) {
18+
if (e.code === 'MODULE_NOT_FOUND') {
19+
log('Warning: React version was set to "detect" in eslint-plugin-react settings, ' +
20+
'but the "react" package is not installed. Assuming latest React version for linting.');
21+
return '999.999.999';
22+
}
23+
throw e;
24+
}
25+
}
26+
1127
function getReactVersionFromContext(context) {
1228
let confVer = '999.999.999';
1329
// .eslintrc shared settings (http://eslint.org/docs/user-guide/configuring#adding-shared-settings)
1430
if (context.settings.react && context.settings.react.version) {
15-
confVer = context.settings.react.version;
31+
let settingsVersion = context.settings.react.version;
32+
if (settingsVersion === 'detect') {
33+
settingsVersion = detectReactVersion();
34+
}
35+
confVer = settingsVersion;
1636
} else if (!warnedForMissingVersion) {
1737
log('Warning: React version not specified in eslint-plugin-react settings. ' +
1838
'See https://github.com/yannickcr/eslint-plugin-react#configuration.');

Diff for: package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"lint": "eslint ./",
1010
"pretest": "npm run lint",
1111
"test": "npm run unit-test",
12-
"unit-test": "istanbul cover --dir reports/coverage node_modules/mocha/bin/_mocha tests/**/*.js"
12+
"unit-test": "istanbul cover --dir reports/coverage node_modules/mocha/bin/_mocha tests/lib/**/*.js tests/util/**/*.js tests/index.js"
1313
},
1414
"files": [
1515
"LICENSE",
@@ -29,7 +29,8 @@
2929
"has": "^1.0.3",
3030
"jsx-ast-utils": "^2.0.1",
3131
"object.fromentries": "^2.0.0",
32-
"prop-types": "^15.6.2"
32+
"prop-types": "^15.6.2",
33+
"resolve": "^1.8.1"
3334
},
3435
"devDependencies": {
3536
"babel-eslint": "^8.2.5",

Diff for: tests/fixtures/version/detect-version/node_modules/react/index.js

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: tests/fixtures/version/detect-version/node_modules/react/package.json

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: tests/util/version.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* eslint-env mocha */
2+
'use strict';
3+
4+
const path = require('path');
5+
const assert = require('assert');
6+
const versionUtil = require('../../lib/util/version');
7+
8+
describe('Version', () => {
9+
const base = path.resolve(__dirname, '..', 'fixtures', 'version');
10+
let cwd;
11+
12+
beforeEach(() => {
13+
cwd = process.cwd();
14+
process.chdir(base);
15+
});
16+
17+
afterEach(() => {
18+
process.chdir(cwd);
19+
});
20+
21+
describe('Detect version', () => {
22+
const context = {settings: {react: {version: 'detect'}}};
23+
24+
it('matches detected version', () => {
25+
process.chdir('detect-version');
26+
assert.equal(versionUtil.testReactVersion(context, '1.2.3'), true);
27+
assert.equal(versionUtil.testReactVersion(context, '1.2.4'), false);
28+
});
29+
30+
it('assumes latest version if react is not installed', () => {
31+
assert.equal(versionUtil.testReactVersion(context, '999.999.999'), true);
32+
});
33+
});
34+
});

0 commit comments

Comments
 (0)