Skip to content

Commit

Permalink
[utils] [New] fileExistsWithCaseSync: add strict argument
Browse files Browse the repository at this point in the history
  • Loading branch information
sergei-startsev authored and ljharb committed Aug 23, 2021
1 parent aa8d566 commit 114bb7a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
16 changes: 12 additions & 4 deletions tests/src/core/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import eslintPkg from 'eslint/package.json';
import semver from 'semver';

import resolve, { CASE_SENSITIVE_FS, fileExistsWithCaseSync } from 'eslint-module-utils/resolve';
import ModuleCache from 'eslint-module-utils/ModuleCache';

import * as path from 'path';
import * as fs from 'fs';
Expand Down Expand Up @@ -319,7 +318,11 @@ describe('resolve', function () {
const caseDescribe = (!CASE_SENSITIVE_FS ? describe : describe.skip);
caseDescribe('case sensitivity', function () {
let file;
const testContext = utils.testContext({ 'import/resolve': { 'extensions': ['.jsx'] } });
const testContext = utils.testContext({
'import/resolve': { 'extensions': ['.jsx'] },
'import/cache': { lifetime: 0 },
});
const testSettings = testContext.settings;
before('resolve', function () {
file = resolve(
// Note the case difference 'MyUncoolComponent' vs 'MyUnCoolComponent'
Expand All @@ -329,14 +332,19 @@ describe('resolve', function () {
expect(file, 'path to ./jsx/MyUncoolComponent').to.exist;
});
it('detects case does not match FS', function () {
expect(fileExistsWithCaseSync(file, ModuleCache.getSettings(testContext)))
expect(fileExistsWithCaseSync(file, testSettings))
.to.be.false;
});
it('detecting case does not include parent folder path (issue #720)', function () {
const f = path.join(process.cwd().toUpperCase(), './tests/files/jsx/MyUnCoolComponent.jsx');
expect(fileExistsWithCaseSync(f, ModuleCache.getSettings(testContext), true))
expect(fileExistsWithCaseSync(f, testSettings))
.to.be.true;
});
it('detecting case should include parent folder path', function () {
const f = path.join(process.cwd().toUpperCase(), './tests/files/jsx/MyUnCoolComponent.jsx');
expect(fileExistsWithCaseSync(f, testSettings, true))
.to.be.false;
});
});

describe('rename cache correctness', function () {
Expand Down
5 changes: 5 additions & 0 deletions utils/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel

## Unreleased

### Added
- `fileExistsWithCaseSync`: add `strict` argument ([#1262], thanks [@sergei-startsev])

## v2.6.2 - 2021-08-08

### Fixed
Expand Down Expand Up @@ -102,6 +105,7 @@ Yanked due to critical issue with cache key resulting from #839.
[#1409]: https://github.com/import-js/eslint-plugin-import/pull/1409
[#1356]: https://github.com/import-js/eslint-plugin-import/pull/1356
[#1290]: https://github.com/import-js/eslint-plugin-import/pull/1290
[#1262]: https://github.com/import-js/eslint-plugin-import/pull/1262
[#1218]: https://github.com/import-js/eslint-plugin-import/pull/1218
[#1166]: https://github.com/import-js/eslint-plugin-import/issues/1166
[#1160]: https://github.com/import-js/eslint-plugin-import/pull/1160
Expand All @@ -119,6 +123,7 @@ Yanked due to critical issue with cache key resulting from #839.
[@kaiyoma]: https://github.com/kaiyoma
[@manuth]: https://github.com/manuth
[@pmcelhaney]: https://github.com/pmcelhaney
[@sergei-startsev]: https://github.com/sergei-startsev
[@sompylasar]: https://github.com/sompylasar
[@timkraut]: https://github.com/timkraut
[@vikr01]: https://github.com/vikr01
6 changes: 3 additions & 3 deletions utils/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ function tryRequire(target, sourceFile) {
}

// http://stackoverflow.com/a/27382838
exports.fileExistsWithCaseSync = function fileExistsWithCaseSync(filepath, cacheSettings) {
exports.fileExistsWithCaseSync = function fileExistsWithCaseSync(filepath, cacheSettings, strict) {
// don't care if the FS is case-sensitive
if (CASE_SENSITIVE_FS) return true;

// null means it resolved to a builtin
if (filepath === null) return true;
if (filepath.toLowerCase() === process.cwd().toLowerCase()) return true;
if (filepath.toLowerCase() === process.cwd().toLowerCase() && !strict) return true;
const parsedPath = path.parse(filepath);
const dir = parsedPath.dir;

Expand All @@ -73,7 +73,7 @@ exports.fileExistsWithCaseSync = function fileExistsWithCaseSync(filepath, cache
if (filenames.indexOf(parsedPath.base) === -1) {
result = false;
} else {
result = fileExistsWithCaseSync(dir, cacheSettings);
result = fileExistsWithCaseSync(dir, cacheSettings, strict);
}
}
fileExistsCache.set(filepath, result);
Expand Down

0 comments on commit 114bb7a

Please sign in to comment.