-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
no-unresolved.js
60 lines (51 loc) · 1.84 KB
/
no-unresolved.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* @fileOverview Ensures that an imported path exists, given resolution rules.
* @author Ben Mosher
*/
import resolve, { CASE_SENSITIVE_FS, fileExistsWithCaseSync } from 'eslint-module-utils/resolve';
import ModuleCache from 'eslint-module-utils/ModuleCache';
import moduleVisitor, { makeOptionsSchema } from 'eslint-module-utils/moduleVisitor';
import docsUrl from '../docsUrl';
module.exports = {
meta: {
type: 'problem',
docs: {
category: 'Static analysis',
description: 'Ensure imports point to a file/module that can be resolved.',
url: docsUrl('no-unresolved'),
},
schema: [
makeOptionsSchema({
caseSensitive: { type: 'boolean', default: true },
caseSensitiveStrict: { type: 'boolean', default: false },
}),
],
},
create(context) {
const options = context.options[0] || {};
function checkSourceValue(source, node) {
// ignore type-only imports and exports
if (node.importKind === 'type' || node.exportKind === 'type') {
return;
}
const caseSensitive = !CASE_SENSITIVE_FS && options.caseSensitive !== false;
const caseSensitiveStrict = !CASE_SENSITIVE_FS && options.caseSensitiveStrict;
const resolvedPath = resolve(source.value, context);
if (resolvedPath === undefined) {
context.report(
source,
`Unable to resolve path to module '${source.value}'.`,
);
} else if (caseSensitive || caseSensitiveStrict) {
const cacheSettings = ModuleCache.getSettings(context.settings);
if (!fileExistsWithCaseSync(resolvedPath, cacheSettings, caseSensitiveStrict)) {
context.report(
source,
`Casing of ${source.value} does not match the underlying filesystem.`,
);
}
}
}
return moduleVisitor(checkSourceValue, options);
},
};