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

[Fix] version detection: support processor virtual filename #2949

Merged
merged 1 commit into from Mar 24, 2021
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
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'));
JounQin marked this conversation as resolved.
Show resolved Hide resolved

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