Skip to content

[New] Support "detect" option for React version setting #1978

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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);
});
});
});