/
named.js
139 lines (111 loc) · 4.04 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
139
import * as path from 'path'
import Exports from '../ExportMap'
import docsUrl from '../docsUrl'
module.exports = {
meta: {
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
if (node.source == null || node.importKind === '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') 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.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) return
if(node.type !== 'VariableDeclarator') return
if(!node.id || node.id.type !== 'ObjectPattern' || node.id.properties.length === 0) {
// return if it's not an object destructure or it's an empty object destructure
return
}
if(!node.init || node.init.type !== 'CallExpression') {
// return if there is no call expression on the right side
return
}
const call = node.init
const source = call.arguments[0]
const variableImports = node.id.properties
const variableExports = Exports.get(source.value, context)
// return if it's not a commonjs require statement
if (call.callee.type !== 'Identifier') return
if (call.callee.name !== 'require') return
if (call.arguments.length !== 1) return
// return if it's not a string source
if(source.type !== 'Literal') return
if (variableExports == null) return
if (variableExports.errors.length) {
variableExports.reportErrors(context, node)
return
}
variableImports.forEach(function (im) {
if (im.type !== 'Property') return
if (!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,
}
},
}