-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathnamed.js
138 lines (117 loc) · 3.86 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import * as path from 'path';
import Exports from '../ExportMap';
import docsUrl from '../docsUrl';
module.exports = {
meta: {
type: 'problem',
docs: {
url: docsUrl('named'),
},
schema: [
{
type: 'object',
properties: {
commonjs: {
type: 'boolean',
},
},
additionalProperties: false,
},
],
},
create: function (context) {
const options = context.options[0] || {};
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((im) => 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
// ignore type imports
|| 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 + '\'');
}
}
});
}
function checkRequire(node) {
if (
!options.commonjs
|| node.type !== 'VariableDeclarator'
// return if it's not an object destructure or it's an empty object destructure
|| !node.id || node.id.type !== 'ObjectPattern' || node.id.properties.length === 0
// return if there is no call expression on the right side
|| !node.init || node.init.type !== 'CallExpression'
) {
return;
}
const call = node.init;
const [source] = call.arguments;
const variableImports = node.id.properties;
const variableExports = Exports.get(source.value, context);
if (
// return if it's not a commonjs require statement
call.callee.type !== 'Identifier' || call.callee.name !== 'require' || call.arguments.length !== 1
// return if it's not a string source
|| source.type !== 'Literal'
|| variableExports == null
) {
return;
}
if (variableExports.errors.length) {
variableExports.reportErrors(context, node);
return;
}
variableImports.forEach(function (im) {
if (im.type !== 'Property' || !im.key || im.key.type !== 'Identifier') {
return;
}
const deepLookup = variableExports.hasDeep(im.key.name);
if (!deepLookup.found) {
if (deepLookup.path.length > 1) {
const deepPath = deepLookup.path
.map(i => path.relative(path.dirname(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 \'' + source.value + '\'');
}
}
});
}
return {
ImportDeclaration: checkSpecifiers.bind(null, 'imported', 'ImportSpecifier'),
ExportNamedDeclaration: checkSpecifiers.bind(null, 'local', 'ExportSpecifier'),
VariableDeclarator: checkRequire,
};
},
};