-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
200 lines (159 loc) · 4.64 KB
/
index.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
const reactUtils = require('react-codemod/transforms/utils/ReactUtils')
const { CLIEngine } = require('eslint')
function getClassMembersSortOrderConfig(filePath) {
let order
const cli = new CLIEngine({ useEslintrc: true })
try {
const config = cli.getConfigForFile(filePath)
const { rules } = config
const sortOrderConfig = rules['sort-class-members/sort-class-members']
order = sortOrderConfig[1]
} catch (e) {
throw new Error(`Could not locate eslint configuration in ${filePath}`)
}
return order
}
function getNameComparer(name) {
if (name[0] === '/') {
let namePattern = name.substr(1, name.length - 2)
if (namePattern[0] !== '^') {
namePattern = `^${namePattern}`
}
if (namePattern[namePattern.length - 1] !== '$') {
namePattern += '$'
}
const re = new RegExp(namePattern)
return n => re.test(n)
}
return n => n === name
}
function expandSlot(input, groups) {
if (Array.isArray(input)) {
return input.map(x => expandSlot(x, groups))
}
let slot
if (typeof input === 'string') {
slot = input[0] === '[' // check for [groupName] shorthand
? { group: input.substr(1, input.length - 2) }
: { name: input }
} else {
slot = Object.assign({}, input)
}
if (slot.group) {
if (groups.hasOwnProperty(slot.group)) {
return expandSlot(groups[slot.group], groups)
}
// ignore undefined groups
return []
}
const testName = slot.name && getNameComparer(slot.name)
if (testName) {
slot.testName = testName
}
return [slot]
}
function flatten(collection) {
const result = []
for (const item of collection) {
if (Array.isArray(item)) {
[].push.apply(result, flatten(item))
} else {
result.push(item)
}
}
return result
}
function getMemberInfo(node) {
let name
let type
let propertyType
if (node.type === 'ClassProperty') {
type = 'property'
name = node.key.name
propertyType = node.value ? node.value.type : node.value
} else {
name = node.key.name
type = 'method'
}
return {
name, type, static: node.static, kind: node.kind, propertyType, node,
}
}
function scoreMember(node, expectedOrder) {
let score = 0
let isScored = false
expectedOrder.forEach((group, index) => {
let matches = true
if (isScored) {
return
}
if (Object.keys(group).length === 0) {
matches = false
score = index
}
const memberInfo = getMemberInfo(node)
if ('kind' in group && !(memberInfo.kind === group.kind)) {
matches = false
}
if ('static' in group && !(memberInfo.static === group.static)) {
matches = false
}
if ('type' in group && !(memberInfo.type === group.type)) {
matches = false
}
if (group.type === 'property' && 'propertyType' in group && !(group.propertyType === memberInfo.propertyType)) {
matches = false
}
if ('name' in group) {
matches = getNameComparer(group.name)(memberInfo.name)
}
if (matches) {
isScored = true
score = index
}
})
return score
}
const builtInGroups = {
'properties': { type: 'property' },
'getters': { kind: 'get' },
'setters': { kind: 'set' },
'static-properties': { type: 'property', static: true },
'conventional-private-properties': { type: 'property', name: '/_.+/' },
'arrow-function-properties': { propertyType: 'ArrowFunctionExpression' },
'methods': { type: 'method' },
'static-methods': { type: 'method', static: true },
'conventional-private-methods': { type: 'method', name: '/_.+/' },
'everything-else': {},
}
function getExpectedOrder(order, groups) {
return flatten(order.map(s => expandSlot(s, groups)))
}
module.exports = function transformer(fileInfo, api, options = {}) {
let j = api.jscodeshift
const ReactUtils = reactUtils(j)
const root = j(fileInfo.source)
const eslintOptions = getClassMembersSortOrderConfig(fileInfo.path)
const expectedOrder = getExpectedOrder(
eslintOptions.order,
Object.assign({}, builtInGroups, eslintOptions.groups)
)
const propertyComparator = (a, b) => {
return scoreMember(a, expectedOrder) - scoreMember(b, expectedOrder)
}
const sortClassProperties = classPath => {
const spec = ReactUtils.getClassExtendReactSpec(classPath)
if (spec) {
spec.body.sort(propertyComparator)
}
}
if (options.reactOnly && ReactUtils.hasReact(root)) {
const es6ClassSortCandidates = ReactUtils.findReactES6ClassDeclaration(root)
if (es6ClassSortCandidates.size() > 0) {
es6ClassSortCandidates.forEach(sortClassProperties)
}
} else {
root.find(j.ClassDeclaration).forEach(sortClassProperties)
}
return root.toSource()
}