-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
named.js
72 lines (59 loc) · 1.89 KB
/
named.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
import * as path from 'path';
import Exports from '../ExportMap';
import docsUrl from '../docsUrl';
module.exports = {
meta: {
type: 'problem',
docs: {
url: docsUrl('named'),
},
schema: [],
},
create: function (context) {
function checkSpecifiers(key, type, node) {
// ignore local exports and type imports/exports
if (node.source == null || node.importKind === 'type' ||
node.importKind === 'typeof' || node.exportKind === 'type') {
return;
}
if (!node.specifiers
.some(function (im) { return im.type === type; })) {
return; // no named imports/exports
}
const imports = Exports.get(node.source.value, context);
if (imports == null) return;
if (imports.errors.length) {
imports.reportErrors(context, node);
return;
}
node.specifiers.forEach(function (im) {
if (im.type !== type) return;
// ignore type imports
if (im.importKind === 'type' || im.importKind === 'typeof') return;
const deepLookup = imports.hasDeep(im[key].name);
if (!deepLookup.found) {
if (deepLookup.path.length > 1) {
const deepPath = deepLookup.path
.map(i => path.relative(path.dirname(context.getPhysicalFilename ? context.getPhysicalFilename() : context.getFilename()), i.path))
.join(' -> ');
context.report(im[key],
`${im[key].name} not found via ${deepPath}`);
} else {
context.report(im[key],
im[key].name + ' not found in \'' + node.source.value + '\'');
}
}
});
}
return {
'ImportDeclaration': checkSpecifiers.bind( null
, 'imported'
, 'ImportSpecifier'
),
'ExportNamedDeclaration': checkSpecifiers.bind( null
, 'local'
, 'ExportSpecifier'
),
};
},
};