-
Notifications
You must be signed in to change notification settings - Fork 15
/
no-unresolved.js
73 lines (65 loc) · 1.65 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
61
62
63
64
65
66
67
68
69
70
71
72
73
'use strict';
var path = require('path');
var helpers = require('./_helpers');
module.exports = function(context) {
var target = context.getFilename();
var ignore = context.options[0] && context.options[0].ignore;
var resolveOpts = {
basedir: path.dirname(target),
extensions: ['.js', '.json', '.node'],
};
if (context.options[0] && context.options[0].paths) {
resolveOpts.paths = context.options[0].paths.map(function(single_path) {
return path.resolve(single_path);
});
}
function validate(node) {
var id = helpers.getModuleId(node);
if (ignore && ignore.indexOf(id) !== -1) return;
var resolved = helpers.resolveSync(id, resolveOpts);
if (!resolved) {
context.report({
node: helpers.getIdNode(node),
data: {id: id},
message: '"{{id}}" does not exist.',
});
}
}
return {
CallExpression: function(node) {
if (helpers.isRequireCall(node) ||
helpers.isRequireResolveCall(node)) {
validate(node);
}
},
ImportDeclaration: function(node) {
if (helpers.isImport(node) || helpers.isImportType(node)) {
validate(node);
}
},
ExportAllDeclaration: function(node) {
if (helpers.isExportFrom(node) || helpers.isExportTypeFrom(node)) {
validate(node);
}
},
ExportNamedDeclaration: function(node) {
if (helpers.isExportFrom(node) || helpers.isExportTypeFrom(node)) {
validate(node);
}
},
};
};
module.exports.schema = {
ignore: {
type: 'array',
items: {
type: 'string',
},
},
paths: {
type: 'array',
items: {
type: 'string',
},
},
};