Skip to content

Commit

Permalink
[Fix] version detection: support processor virtual filename
Browse files Browse the repository at this point in the history
Fixes #2948
  • Loading branch information
JounQin authored and ljharb committed Mar 23, 2021
1 parent 3d7fd57 commit bc37220
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,9 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel

## Unreleased

### Fixed
* version detection: support processor virtual filename ([#2949][] @JounQin)

## [7.23.0] - 2021.03.22

### Added
Expand Down Expand Up @@ -33,6 +36,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
* [readme] Replace lists of rules with tables in readme ([#2908][] @motato1)
* [Docs] added missing curly braces ([#2923][] @Muditxofficial)

[#2949]: https://github.com/yannickcr/eslint-plugin-react/pull/2949
[#2943]: https://github.com/yannickcr/eslint-plugin-react/pull/2943
[#2935]: https://github.com/yannickcr/eslint-plugin-react/pull/2935
[#2933]: https://github.com/yannickcr/eslint-plugin-react/pull/2933
Expand Down
30 changes: 28 additions & 2 deletions lib/util/version.js
Expand Up @@ -5,6 +5,7 @@

'use strict';

const fs = require('fs');
const resolve = require('resolve');
const path = require('path');
const error = require('./error');
Expand All @@ -21,14 +22,37 @@ function resetDetectedVersion() {
cachedDetectedReactVersion = undefined;
}

function resolveBasedir(context) {
let basedir = process.cwd();
if (context) {
const filename = context.getFilename();
const dirname = path.dirname(filename);
try {
if (fs.statSync(filename).isFile()) {
// dirname must be dir here
basedir = dirname;
}
} catch (err) {
// https://github.com/eslint/eslint/issues/11989
if (err.code === 'ENOTDIR') {
// the error code alreay indicates that dirname is a file
basedir = path.dirname(dirname);
}
}
}
return basedir;
}

// TODO, semver-major: remove context fallback
function detectReactVersion(context) {
if (cachedDetectedReactVersion) {
return cachedDetectedReactVersion;
}

const basedir = resolveBasedir(context);

try {
const reactPath = resolve.sync('react', {basedir: context ? path.dirname(context.getFilename()) : process.cwd()});
const reactPath = resolve.sync('react', {basedir});
const react = require(reactPath); // eslint-disable-line global-require, import/no-dynamic-require
cachedDetectedReactVersion = react.version;
return cachedDetectedReactVersion;
Expand Down Expand Up @@ -70,8 +94,10 @@ function getReactVersionFromContext(context) {

// TODO, semver-major: remove context fallback
function detectFlowVersion(context) {
const basedir = resolveBasedir(context);

try {
const flowPackageJsonPath = resolve.sync('flow-bin/package.json', {basedir: context ? path.dirname(context.getFilename()) : process.cwd()});
const flowPackageJsonPath = resolve.sync('flow-bin/package.json', {basedir});
const flowPackageJson = require(flowPackageJsonPath); // eslint-disable-line global-require, import/no-dynamic-require
return flowPackageJson.version;
} catch (e) {
Expand Down
8 changes: 8 additions & 0 deletions tests/util/version.js
Expand Up @@ -79,6 +79,14 @@ describe('Version', () => {
['Warning: Flow version was set to "detect" in eslint-plugin-react settings, but the "flow-bin" package is not installed. Assuming latest Flow version for linting.']
];
});

it('works with virtual filename', () => {
sinon.stub(context, 'getFilename').callsFake(() => path.resolve(base, 'detect-version-sibling', 'test.js/0_fake.js'));

assert.equal(versionUtil.testReactVersion(context, '2.3.4'), true);
assert.equal(versionUtil.testReactVersion(context, '2.3.5'), false);
assert.equal(versionUtil.testFlowVersion(context, '2.92.0'), true);
});
});

describe('string version', () => {
Expand Down

0 comments on commit bc37220

Please sign in to comment.