Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epidemian
GitHub Repository: epidemian/eslint-plugin-import
Path: blob/main/src/rules/no-unresolved.js
829 views
1
/**
2
* @fileOverview Ensures that an imported path exists, given resolution rules.
3
* @author Ben Mosher
4
*/
5
6
import resolve, { CASE_SENSITIVE_FS, fileExistsWithCaseSync } from 'eslint-module-utils/resolve';
7
import ModuleCache from 'eslint-module-utils/ModuleCache';
8
import moduleVisitor, { makeOptionsSchema } from 'eslint-module-utils/moduleVisitor';
9
import docsUrl from '../docsUrl';
10
11
module.exports = {
12
meta: {
13
type: 'problem',
14
docs: {
15
url: docsUrl('no-unresolved'),
16
},
17
18
schema: [
19
makeOptionsSchema({
20
caseSensitive: { type: 'boolean', default: true },
21
caseSensitiveStrict: { type: 'boolean', default: false },
22
}),
23
],
24
},
25
26
create(context) {
27
const options = context.options[0] || {};
28
29
function checkSourceValue(source, node) {
30
// ignore type-only imports
31
if (node.importKind === 'type') {
32
return;
33
}
34
35
const caseSensitive = !CASE_SENSITIVE_FS && options.caseSensitive !== false;
36
const caseSensitiveStrict = !CASE_SENSITIVE_FS && options.caseSensitiveStrict;
37
38
const resolvedPath = resolve(source.value, context);
39
40
if (resolvedPath === undefined) {
41
context.report(
42
source,
43
`Unable to resolve path to module '${source.value}'.`,
44
);
45
} else if (caseSensitive || caseSensitiveStrict) {
46
const cacheSettings = ModuleCache.getSettings(context.settings);
47
if (!fileExistsWithCaseSync(resolvedPath, cacheSettings, caseSensitiveStrict)) {
48
context.report(
49
source,
50
`Casing of ${source.value} does not match the underlying filesystem.`,
51
);
52
}
53
}
54
}
55
56
return moduleVisitor(checkSourceValue, options);
57
},
58
};
59
60