Skip to content

Commit

Permalink
Merge pull request #1978 from alexzherdev/1955-react-version-detect
Browse files Browse the repository at this point in the history
[New] Support "detect" option for React version setting
  • Loading branch information
ljharb committed Oct 16, 2018
2 parents bc6a1ba + dc28d26 commit 24044e0
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -15,6 +15,7 @@ logs
reports
build
node_modules
!tests/**/node_modules
npm-debug.log
sftp-config.json

Expand Down
22 changes: 21 additions & 1 deletion lib/util/version.js
Expand Up @@ -4,15 +4,35 @@
*/
'use strict';

const resolve = require('resolve');
const log = require('./log');

let warnedForMissingVersion = false;

function detectReactVersion() {
try {
const reactPath = resolve.sync('react', {basedir: process.cwd()});
const react = require(reactPath);
return react.version;
} catch (e) {
if (e.code === 'MODULE_NOT_FOUND') {
log('Warning: React version was set to "detect" in eslint-plugin-react settings, ' +
'but the "react" package is not installed. Assuming latest React version for linting.');
return '999.999.999';
}
throw e;
}
}

function getReactVersionFromContext(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.version) {
confVer = context.settings.react.version;
let settingsVersion = context.settings.react.version;
if (settingsVersion === 'detect') {
settingsVersion = detectReactVersion();
}
confVer = 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 Down
5 changes: 3 additions & 2 deletions package.json
Expand Up @@ -9,7 +9,7 @@
"lint": "eslint ./",
"pretest": "npm run lint",
"test": "npm run unit-test",
"unit-test": "istanbul cover --dir reports/coverage node_modules/mocha/bin/_mocha tests/**/*.js"
"unit-test": "istanbul cover --dir reports/coverage node_modules/mocha/bin/_mocha tests/lib/**/*.js tests/util/**/*.js tests/index.js"
},
"files": [
"LICENSE",
Expand All @@ -29,7 +29,8 @@
"has": "^1.0.3",
"jsx-ast-utils": "^2.0.1",
"object.fromentries": "^2.0.0",
"prop-types": "^15.6.2"
"prop-types": "^15.6.2",
"resolve": "^1.8.1"
},
"devDependencies": {
"babel-eslint": "^8.2.5",
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions tests/util/version.js
@@ -0,0 +1,34 @@
/* eslint-env mocha */
'use strict';

const path = require('path');
const assert = require('assert');
const versionUtil = require('../../lib/util/version');

describe('Version', () => {
const base = path.resolve(__dirname, '..', 'fixtures', 'version');
let cwd;

beforeEach(() => {
cwd = process.cwd();
process.chdir(base);
});

afterEach(() => {
process.chdir(cwd);
});

describe('Detect version', () => {
const context = {settings: {react: {version: 'detect'}}};

it('matches detected version', () => {
process.chdir('detect-version');
assert.equal(versionUtil.testReactVersion(context, '1.2.3'), true);
assert.equal(versionUtil.testReactVersion(context, '1.2.4'), false);
});

it('assumes latest version if react is not installed', () => {
assert.equal(versionUtil.testReactVersion(context, '999.999.999'), true);
});
});
});

0 comments on commit 24044e0

Please sign in to comment.