-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruleUseAtoms.js
135 lines (106 loc) · 3.21 KB
/
ruleUseAtoms.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
const _ = require('lodash')
const MESSAGE_IMPORT_EACH = 'Import each atom separately'
const MESSAGE_IMPORT_ALL = 'Need to use all atoms but "{{name}}" missed'
const isAtomsFile = filepath => /\/atoms\.js$/.test(filepath)
const isIndexFile = filepath => /\/index\.js$/.test(filepath)
const isIgnored = filepath => /(\.test\.js$|\/__tests__\/.+)/.test(filepath)
const getFileKey = filepath => filepath.replace(/^.+\/src\/(.+)\/[\w.]+$/, '$1')
/* eslint-disable no-underscore-dangle */
class Dependencies {
constructor() {
this._map = {}
}
_ref(filepath) {
const key = getFileKey(filepath)
if (this._map[key]) {
return this._map[key]
}
this._map[key] = {
defs: [],
used: [],
import: null,
export: null,
}
return this._map[key]
}
addDefinition(filepath, atom) {
this._ref(filepath).defs.push(atom)
}
addUsage(filepath, name) {
this._ref(filepath).used.push(name)
}
addImportNode(filepath, node) {
this._ref(filepath).import = node
}
addExportNode(filepath, node) {
this._ref(filepath).export = node
}
get(filepath) {
return this._ref(filepath)
}
}
/* eslint-enable no-underscore-dangle */
const deps = new Dependencies()
module.exports = {
create(context) {
const filepath = context.getFilename()
return {
'Program:exit': node => {
if (isIndexFile(filepath) && !isIgnored(filepath)) {
const resources = deps.get(filepath)
const definedButNotUsed = _.differenceWith(
resources.defs,
resources.used,
_.isEqual,
)
definedButNotUsed.forEach(variable =>
context.report({
node: resources.import || resources.export || node,
message: MESSAGE_IMPORT_ALL,
data: { name: variable.name },
}),
)
}
},
'ExportNamedDeclaration[exportKind="value"]': node => {
if (isAtomsFile(filepath) && !isIgnored(filepath)) {
const name = _.get(node, 'declaration.declarations[0].id.name')
if (name) {
deps.addDefinition(filepath, { name })
} else {
// eslint-disable-next-line no-console
console.log('No name', filepath)
}
}
},
'ImportDeclaration[source.value="./atoms"]': node => {
if (isIgnored(filepath)) {
return null
}
if (
node.specifiers.find(
specifier => specifier.type === 'ImportNamespaceSpecifier',
)
) {
return context.report({
node,
message: MESSAGE_IMPORT_EACH,
})
}
const imports = node.specifiers.map(
specifier => specifier.imported.name,
)
deps.addImportNode(filepath, node)
return imports.forEach(name => deps.addUsage(filepath, { name }))
},
'ExportNamedDeclaration[source.value="./atoms"]': node => {
if (isIgnored(filepath)) {
return null
}
const exports = node.specifiers.map(specifier => specifier.local.name)
deps.addExportNode(filepath, node)
return exports.forEach(name => deps.addUsage(filepath, { name }))
},
}
},
}