Skip to content

Commit 114bb7a

Browse files
sergei-startsevljharb
authored andcommitted
[utils] [New] fileExistsWithCaseSync: add strict argument
See import-js#1262.
1 parent aa8d566 commit 114bb7a

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

tests/src/core/resolve.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import eslintPkg from 'eslint/package.json';
33
import semver from 'semver';
44

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

87
import * as path from 'path';
98
import * as fs from 'fs';
@@ -319,7 +318,11 @@ describe('resolve', function () {
319318
const caseDescribe = (!CASE_SENSITIVE_FS ? describe : describe.skip);
320319
caseDescribe('case sensitivity', function () {
321320
let file;
322-
const testContext = utils.testContext({ 'import/resolve': { 'extensions': ['.jsx'] } });
321+
const testContext = utils.testContext({
322+
'import/resolve': { 'extensions': ['.jsx'] },
323+
'import/cache': { lifetime: 0 },
324+
});
325+
const testSettings = testContext.settings;
323326
before('resolve', function () {
324327
file = resolve(
325328
// Note the case difference 'MyUncoolComponent' vs 'MyUnCoolComponent'
@@ -329,14 +332,19 @@ describe('resolve', function () {
329332
expect(file, 'path to ./jsx/MyUncoolComponent').to.exist;
330333
});
331334
it('detects case does not match FS', function () {
332-
expect(fileExistsWithCaseSync(file, ModuleCache.getSettings(testContext)))
335+
expect(fileExistsWithCaseSync(file, testSettings))
333336
.to.be.false;
334337
});
335338
it('detecting case does not include parent folder path (issue #720)', function () {
336339
const f = path.join(process.cwd().toUpperCase(), './tests/files/jsx/MyUnCoolComponent.jsx');
337-
expect(fileExistsWithCaseSync(f, ModuleCache.getSettings(testContext), true))
340+
expect(fileExistsWithCaseSync(f, testSettings))
338341
.to.be.true;
339342
});
343+
it('detecting case should include parent folder path', function () {
344+
const f = path.join(process.cwd().toUpperCase(), './tests/files/jsx/MyUnCoolComponent.jsx');
345+
expect(fileExistsWithCaseSync(f, testSettings, true))
346+
.to.be.false;
347+
});
340348
});
341349

342350
describe('rename cache correctness', function () {

utils/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
55

66
## Unreleased
77

8+
### Added
9+
- `fileExistsWithCaseSync`: add `strict` argument ([#1262], thanks [@sergei-startsev])
10+
811
## v2.6.2 - 2021-08-08
912

1013
### Fixed
@@ -102,6 +105,7 @@ Yanked due to critical issue with cache key resulting from #839.
102105
[#1409]: https://github.com/import-js/eslint-plugin-import/pull/1409
103106
[#1356]: https://github.com/import-js/eslint-plugin-import/pull/1356
104107
[#1290]: https://github.com/import-js/eslint-plugin-import/pull/1290
108+
[#1262]: https://github.com/import-js/eslint-plugin-import/pull/1262
105109
[#1218]: https://github.com/import-js/eslint-plugin-import/pull/1218
106110
[#1166]: https://github.com/import-js/eslint-plugin-import/issues/1166
107111
[#1160]: https://github.com/import-js/eslint-plugin-import/pull/1160
@@ -119,6 +123,7 @@ Yanked due to critical issue with cache key resulting from #839.
119123
[@kaiyoma]: https://github.com/kaiyoma
120124
[@manuth]: https://github.com/manuth
121125
[@pmcelhaney]: https://github.com/pmcelhaney
126+
[@sergei-startsev]: https://github.com/sergei-startsev
122127
[@sompylasar]: https://github.com/sompylasar
123128
[@timkraut]: https://github.com/timkraut
124129
[@vikr01]: https://github.com/vikr01

utils/resolve.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ function tryRequire(target, sourceFile) {
5252
}
5353

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

5959
// null means it resolved to a builtin
6060
if (filepath === null) return true;
61-
if (filepath.toLowerCase() === process.cwd().toLowerCase()) return true;
61+
if (filepath.toLowerCase() === process.cwd().toLowerCase() && !strict) return true;
6262
const parsedPath = path.parse(filepath);
6363
const dir = parsedPath.dir;
6464

@@ -73,7 +73,7 @@ exports.fileExistsWithCaseSync = function fileExistsWithCaseSync(filepath, cache
7373
if (filenames.indexOf(parsedPath.base) === -1) {
7474
result = false;
7575
} else {
76-
result = fileExistsWithCaseSync(dir, cacheSettings);
76+
result = fileExistsWithCaseSync(dir, cacheSettings, strict);
7777
}
7878
}
7979
fileExistsCache.set(filepath, result);

0 commit comments

Comments
 (0)