Skip to content

Commit 42d5cf2

Browse files
sergei-startsevljharb
authored andcommitted
[New] no-unresolved: add caseSensitiveStrict option
Fixes #1259.
1 parent ac00c44 commit 42d5cf2

File tree

4 files changed

+55
-8
lines changed

4 files changed

+55
-8
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
77
## [Unreleased]
88

99
### Added
10+
- [`no-unresolved`]: add `caseSensitiveStrict` option ([#1262], thanks [@sergei-startsev])
1011
- [`no-unused-modules`]: add eslint v8 support ([#2194], thanks [@coderaiser])
1112

1213
## [2.24.2] - 2021-08-24
@@ -1051,6 +1052,7 @@ for info on changes for earlier releases.
10511052
[#1294]: https://github.com/import-js/eslint-plugin-import/pull/1294
10521053
[#1290]: https://github.com/import-js/eslint-plugin-import/pull/1290
10531054
[#1277]: https://github.com/import-js/eslint-plugin-import/pull/1277
1055+
[#1262]: https://github.com/import-js/eslint-plugin-import/pull/1262
10541056
[#1257]: https://github.com/import-js/eslint-plugin-import/pull/1257
10551057
[#1253]: https://github.com/import-js/eslint-plugin-import/pull/1253
10561058
[#1248]: https://github.com/import-js/eslint-plugin-import/pull/1248

docs/rules/no-unresolved.md

+17-2
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,25 @@ By default, this rule will report paths whose case do not match the underlying f
7676
const { default: x } = require('./foo') // reported if './foo' is actually './Foo' and caseSensitive: true
7777
```
7878

79+
#### `caseSensitiveStrict`
80+
81+
The `caseSensitive` option does not detect case for the current working directory. The `caseSensitiveStrict` option allows checking `cwd` in resolved path. By default, the option is disabled.
82+
83+
84+
```js
85+
/*eslint import/no-unresolved: [2, { caseSensitiveStrict: true }]*/
86+
87+
// Absolute paths
88+
import Foo from `/Users/fOo/bar/file.js` // reported, /Users/foo/bar/file.js
89+
import Foo from `d:/fOo/bar/file.js` // reported, d:/foo/bar/file.js
90+
91+
// Relative paths, cwd is Users/foo/
92+
import Foo from `./../fOo/bar/file.js` // reported
93+
```
94+
7995
## When Not To Use It
8096

81-
If you're using a module bundler other than Node or Webpack, you may end up with
82-
a lot of false positive reports of missing dependencies.
97+
If you're using a module bundler other than Node or Webpack, you may end up with a lot of false positive reports of missing dependencies.
8398

8499
## Further Reading
85100

src/rules/no-unresolved.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,17 @@ module.exports = {
1818
schema: [
1919
makeOptionsSchema({
2020
caseSensitive: { type: 'boolean', default: true },
21+
caseSensitiveStrict: { type: 'boolean', default: false },
2122
}),
2223
],
2324
},
2425

2526
create(context) {
27+
const options = context.options[0] || {};
28+
2629
function checkSourceValue(source) {
27-
const shouldCheckCase = !CASE_SENSITIVE_FS
28-
&& (!context.options[0] || context.options[0].caseSensitive !== false);
30+
const caseSensitive = !CASE_SENSITIVE_FS && options.caseSensitive !== false;
31+
const caseSensitiveStrict = !CASE_SENSITIVE_FS && options.caseSensitiveStrict;
2932

3033
const resolvedPath = resolve(source.value, context);
3134

@@ -34,9 +37,9 @@ module.exports = {
3437
source,
3538
`Unable to resolve path to module '${source.value}'.`
3639
);
37-
} else if (shouldCheckCase) {
40+
} else if (caseSensitive || caseSensitiveStrict) {
3841
const cacheSettings = ModuleCache.getSettings(context.settings);
39-
if (!fileExistsWithCaseSync(resolvedPath, cacheSettings)) {
42+
if (!fileExistsWithCaseSync(resolvedPath, cacheSettings, caseSensitiveStrict)) {
4043
context.report(
4144
source,
4245
`Casing of ${source.value} does not match the underlying filesystem.`
@@ -45,6 +48,6 @@ module.exports = {
4548
}
4649
}
4750

48-
return moduleVisitor(checkSourceValue, context.options[0]);
51+
return moduleVisitor(checkSourceValue, options);
4952
},
5053
};

tests/src/rules/no-unresolved.js

+28-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function runResolverTests(resolver) {
1515
function rest(specs) {
1616
specs.settings = Object.assign({},
1717
specs.settings,
18-
{ 'import/resolver': resolver },
18+
{ 'import/resolver': resolver, 'import/cache': { lifetime: 0 } },
1919
);
2020

2121
return test(specs);
@@ -227,6 +227,10 @@ function runResolverTests(resolver) {
227227
});
228228

229229
if (!CASE_SENSITIVE_FS) {
230+
const relativePath = './tests/files/jsx/MyUnCoolComponent.jsx';
231+
const cwd = process.cwd();
232+
const mismatchedPath = path.join(cwd.toUpperCase(), relativePath).replace(/\\/g, '/');
233+
230234
ruleTester.run('case sensitivity', rule, {
231235
valid: [
232236
rest({ // test with explicit flag
@@ -247,6 +251,29 @@ function runResolverTests(resolver) {
247251
}),
248252
],
249253
});
254+
255+
ruleTester.run('case sensitivity strict', rule, {
256+
valid: [
257+
// #1259 issue
258+
rest({ // caseSensitiveStrict is disabled by default
259+
code: `import foo from "${mismatchedPath}"`,
260+
}),
261+
],
262+
263+
invalid: [
264+
// #1259 issue
265+
rest({ // test with enabled caseSensitiveStrict option
266+
code: `import foo from "${mismatchedPath}"`,
267+
options: [{ caseSensitiveStrict: true }],
268+
errors: [`Casing of ${mismatchedPath} does not match the underlying filesystem.`],
269+
}),
270+
rest({ // test with enabled caseSensitiveStrict option and disabled caseSensitive
271+
code: `import foo from "${mismatchedPath}"`,
272+
options: [{ caseSensitiveStrict: true, caseSensitive: false }],
273+
errors: [`Casing of ${mismatchedPath} does not match the underlying filesystem.`],
274+
}),
275+
],
276+
});
250277
}
251278

252279
}

0 commit comments

Comments
 (0)