From 3b42438088f95dea1866daf37650427046d95c15 Mon Sep 17 00:00:00 2001 From: Sergei Startsev Date: Mon, 23 Aug 2021 21:28:57 +0200 Subject: [PATCH] [utils] [New] `fileExistsWithCaseSync`: add `strict` argument See #1262. --- tests/src/core/resolve.js | 16 ++++++++++++---- utils/CHANGELOG.md | 5 +++++ utils/resolve.js | 6 +++--- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/tests/src/core/resolve.js b/tests/src/core/resolve.js index ccfe5f6c27..02a2177c60 100644 --- a/tests/src/core/resolve.js +++ b/tests/src/core/resolve.js @@ -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'; @@ -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' @@ -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 () { diff --git a/utils/CHANGELOG.md b/utils/CHANGELOG.md index 9160055145..f33001b6f9 100644 --- a/utils/CHANGELOG.md +++ b/utils/CHANGELOG.md @@ -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 @@ -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 @@ -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 \ No newline at end of file diff --git a/utils/resolve.js b/utils/resolve.js index f488ea798f..ec9397fa14 100644 --- a/utils/resolve.js +++ b/utils/resolve.js @@ -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; @@ -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);