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 recursive processor virtual filename #2965

Merged
merged 2 commits into from May 28, 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: 3 additions & 1 deletion CHANGELOG.md
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
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
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -42,7 +42,7 @@
"string.prototype.matchall": "^4.0.4"
},
"devDependencies": {
"@types/eslint": "^7.2.8",
"@types/eslint": "=7.2.10",
"@types/estree": "^0.0.47",
"@types/node": "^14.14.37",
"@typescript-eslint/parser": "^2.34.0",
Expand Down
8 changes: 8 additions & 0 deletions tests/util/version.js
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