-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruleComponentNames.js
161 lines (134 loc) · 4.79 KB
/
ruleComponentNames.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
const { Minimatch } = require('minimatch')
const isIgnored = new Minimatch(
'**/src/{components/UI/**,**/__tests__/**,**/assets/**,**/*.test.js,**/!(index.js)}',
)
const isUiComponent = new Minimatch('**/src/UI/**')
const isPageComponent = new Minimatch('**/src/pages/**')
const isFormComponent = new Minimatch('**/src/forms/**')
const isContainerComponent = new Minimatch('**/src/containers/**')
const isBasicComponent = new Minimatch('**/src/components/**')
const isPotentialComponent = filepath =>
!isIgnored.match(filepath) &&
(isUiComponent.match(filepath) ||
isPageComponent.match(filepath) ||
isFormComponent.match(filepath) ||
isContainerComponent.match(filepath) ||
isBasicComponent.match(filepath))
function getExpectedName(filepath) {
if (isUiComponent.match(filepath)) {
return filepath
.replace(/^.+\/src\/(UI\/.+)\/[\w.]+\.js/, '$1')
.replace(/\//g, '')
}
if (isPageComponent.match(filepath)) {
return `Page${filepath
.replace(/^.+\/src\/pages((?:\/[A-Z][A-z]*)+).+$/, '$1')
.replace(/\//g, '')}`
}
if (isFormComponent.match(filepath)) {
return `Form${filepath
.replace(/^.+\/src\/forms\/(.+)\/\w+\.js/, '$1')
.replace(/\//g, '')}`
}
if (isContainerComponent.match(filepath)) {
return `Container${filepath
.replace(/^.+\/src\/containers((?:\/[A-Z][A-z]*)+).+$/, '$1')
.replace(/\//g, '')}`
}
if (isBasicComponent.match(filepath)) {
return filepath
.replace(/^.+\/src\/components\/(.+)\/\w+\.js/, '$1')
.replace(/\//g, '')
}
return filepath.replace(/^.+\/src\/(.+)\.js$/, '$1')
}
function isComponentIdentifier(token) {
return token.type === 'Identifier' && /^[A-Z][A-z0-9]+$/.test(token.value)
}
module.exports = {
create(context) {
const filepath = context.getFilename()
const sourceCode = context.getSourceCode()
let exportedIdentifiers = []
let exportIdentifier = null
function findUsage(identifiers, target) {
// If the target mentioned in default export node, then we consider
// the target as used
if (identifiers.some(item => item.value === target)) {
return true
}
// Otherwise, we find where every identifier was defined (as
// VariableDeclarator) and then try to find usage of target in definitions
return identifiers.some(tokenInExport => {
const defs = sourceCode.getTokensBefore(tokenInExport, {
filter: item =>
item.type === 'Identifier' && item.value === tokenInExport.value,
})
return defs.some(def => {
const parentNode = sourceCode.getNodeByRangeIndex(def.start).parent
if (parentNode.type === 'VariableDeclarator') {
const identifiersInDef = sourceCode
.getTokensBetween(def, tokenInExport)
.filter(isComponentIdentifier)
return findUsage(identifiersInDef, target)
}
if (
parentNode.type === 'MemberExpression' &&
parentNode.parent.type === 'AssignmentExpression'
) {
if (
parentNode.object.name === def.value &&
parentNode.property.name === 'displayName'
) {
exportedIdentifiers = [
{
value: parentNode.parent.right.value,
loc: parentNode.parent.right.loc,
},
]
return parentNode.parent.right.value === target
}
}
return false
})
})
}
return {
ExportDefaultDeclaration(node) {
if (isPotentialComponent(filepath)) {
const identifiers = sourceCode.getFirstTokens(node, {
filter: isComponentIdentifier,
})
if (identifiers.length > 0) {
exportedIdentifiers = identifiers
exportIdentifier = node.declaration
}
}
},
'Program:exit': function programExit() {
if (isPotentialComponent(filepath)) {
const expectedName = getExpectedName(filepath)
if (exportedIdentifiers.length === 0) {
// eslint-disable-next-line no-console
console.warn(`* Could not recognize name for ${expectedName}`)
}
if (exportedIdentifiers.length > 0) {
if (!findUsage(exportedIdentifiers, expectedName)) {
let message = `Expected '${expectedName}' name`
let loc = exportIdentifier.loc
if (exportedIdentifiers.length === 1) {
message += ` but got ${exportedIdentifiers[0].value}`
loc = exportedIdentifiers[0].loc
}
context.report({
node: exportIdentifier,
loc,
message,
})
}
}
}
},
}
},
}