Skip to content

Commit

Permalink
[Fix] version detection: support recursive processor virtual filename
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin authored and ljharb committed Apr 13, 2021
1 parent f0e6700 commit 880ab8c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
### Added
* component detection: add componentWrapperFunctions setting ([#2713][] @@jzabala @LandonSchropp)
* [`no-unused-prop-types`]: add ignore option ([#2972][] @grit96)
* version detection: support recursive processor virtual filename ([#2965][] @JounQin)

### Fixed
* [`jsx-handler-names`]: properly substitute value into message ([#2975][] @G-Rath)
Expand All @@ -32,6 +33,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
[#2975]: https://github.com/yannickcr/eslint-plugin-react/pull/2975
[#2974]: https://github.com/yannickcr/eslint-plugin-react/pull/2974
[#2972]: https://github.com/yannickcr/eslint-plugin-react/pull/2972
[#2965]: https://github.com/yannickcr/eslint-plugin-react/pull/2965
[#2713]: https://github.com/yannickcr/eslint-plugin-react/pull/2713

## [7.23.2] - 2021.04.08
Expand Down Expand Up @@ -3366,4 +3368,4 @@ If you're still not using React 15 you can keep the old behavior by setting the
[`function-component-definition`]: docs/rules/function-component-definition.md
[`jsx-newline`]: docs/rules/jsx-newline.md
[`jsx-no-constructed-context-values`]: docs/rules/jsx-no-constructed-context-values.md
[`no-unstable-nested-components`]: docs/rules/no-unstable-nested-components.md
[`no-unstable-nested-components`]: docs/rules/no-unstable-nested-components.md
15 changes: 7 additions & 8 deletions lib/util/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,24 @@ function resetDetectedVersion() {
cachedDetectedReactVersion = undefined;
}

function resolveBasedir(context) {
let basedir = process.cwd();
if (context) {
const filename = context.getFilename();
function resolveBasedir(contextOrFilename) {
if (contextOrFilename) {
const filename = typeof contextOrFilename === 'string' ? contextOrFilename : contextOrFilename.getFilename();
const dirname = path.dirname(filename);
try {
if (fs.statSync(filename).isFile()) {
// dirname must be dir here
basedir = dirname;
return 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);
// virtual filename could be recursive
return resolveBasedir(dirname);
}
}
}
return basedir;
return process.cwd();
}

// TODO, semver-major: remove context fallback
Expand Down
8 changes: 8 additions & 0 deletions tests/util/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ describe('Version', () => {
assert.equal(versionUtil.testReactVersion(context, '2.3.5'), false);
assert.equal(versionUtil.testFlowVersion(context, '2.92.0'), true);
});

it('works with recursive virtual filename', () => {
sinon.stub(context, 'getFilename').callsFake(() => path.resolve(base, 'detect-version-sibling', 'test.js/0_fake.md/1_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 880ab8c

Please sign in to comment.