Skip to content
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

Cache detected React version #2673

Merged
merged 1 commit into from Jun 15, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 16 additions & 3 deletions lib/util/version.js
Expand Up @@ -14,19 +14,31 @@ function resetWarningFlag() {
warnedForMissingVersion = false;
}

let cachedDetectedReactVersion;

function resetDetectedVersion() {
cachedDetectedReactVersion = undefined;
}

function detectReactVersion() {
if (cachedDetectedReactVersion) {
return cachedDetectedReactVersion;
}

try {
const reactPath = resolve.sync('react', {basedir: process.cwd()});
const react = require(reactPath); // eslint-disable-line global-require, import/no-dynamic-require
return react.version;
cachedDetectedReactVersion = react.version;
ljharb marked this conversation as resolved.
Show resolved Hide resolved
return cachedDetectedReactVersion;
} catch (e) {
if (e.code === 'MODULE_NOT_FOUND') {
if (!warnedForMissingVersion) {
error('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.');
warnedForMissingVersion = true;
}
return '999.999.999';
cachedDetectedReactVersion = '999.999.999';
return cachedDetectedReactVersion;
}
throw e;
}
Expand Down Expand Up @@ -116,5 +128,6 @@ function testFlowVersion(context, methodVer) {
module.exports = {
testReactVersion,
testFlowVersion,
resetWarningFlag
resetWarningFlag,
resetDetectedVersion
};
1 change: 1 addition & 0 deletions tests/util/version.js
Expand Up @@ -16,6 +16,7 @@ describe('Version', () => {
sinon.stub(console, 'error');
expectedErrorArgs = [];
versionUtil.resetWarningFlag();
versionUtil.resetDetectedVersion();
});

afterEach(() => {
Expand Down