-
Notifications
You must be signed in to change notification settings - Fork 10
/
groups.js
329 lines (301 loc) · 8.65 KB
/
groups.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import { print, visit, types } from 'recast'
import { kebab, pascal } from 'case'
import levenshtein from 'js-levenshtein'
import stemmer from 'stemmer'
const { namedTypes } = types
/**
* @typedef Word
* @prop {string} value
* @prop {number} score
*/
/**
* @typedef StatementGroup
* @prop {Set<import('recast').types.ASTNode>} nodes
* @prop {number} score
* @prop {string[]} declarations
* @prop {Set<string>} dependencies
* @prop {number} usageScore
*/
/**
* @param {import('recast').types.ASTNode[]} nodes
* @param {string[]} setupVariables
*/
export function groupStatements (nodes, setupVariables) {
/** @type {StatementGroup[]} */
let groups = []
// Classify nodes
const wordedNodes = []
const otherNodes = []
for (const node of nodes) {
if (getStatementWords(node, setupVariables).length) {
wordedNodes.push(node)
} else {
otherNodes.push(node)
}
}
// Group nodes together
for (const nodeA of wordedNodes) {
for (const nodeB of wordedNodes) {
if (nodeA !== nodeB) {
const score = getStatementGroupScore(nodeA, nodeB, setupVariables)
if (score > 0) {
let group = groups.find(
g => g.score === score && (g.nodes.has(nodeA) || g.nodes.has(nodeB))
)
if (!group) {
group = {
nodes: new Set(),
score,
declarations: [],
dependencies: new Set(),
usageScore: 0,
}
groups.push(group)
}
[nodeA, nodeB].forEach(node => {
group.nodes.add(node)
})
}
}
}
}
const ungroupedNodes = []
// Remove duplicates
for (const node of wordedNodes) {
let bestGroup
const relevantGroups = []
let isInGroup = false
for (const group of groups) {
if (group.nodes.has(node)) {
isInGroup = true
if (!bestGroup || group.score > bestGroup.score) {
bestGroup = group
}
relevantGroups.push(group)
}
}
if (isInGroup) {
// Remove the duplicated node in the not best groups
for (const group of relevantGroups) {
if (group !== bestGroup) {
group.nodes.delete(node)
// Don't leave groups with only one statement
if (group.nodes.size === 1) {
bestGroup.nodes.add(group.nodes.values().next().value)
group.nodes.clear()
}
}
}
} else {
ungroupedNodes.push(node)
}
}
// Variables declarations & dependency identifiers
for (const group of groups) {
for (const node of group.nodes) {
const varName = mayGetVariableDeclarationName(node)
if (varName) {
group.declarations.push(varName)
}
visit(Array.from(group.nodes), {
visitIdentifier (path) {
let identifier = path.value.name
if (setupVariables.includes(identifier) && !group.declarations.includes(identifier)) {
group.dependencies.add(identifier)
}
this.traverse(path)
},
})
}
}
// Used by other group stats
for (const groupA of groups) {
for (const declaration of groupA.declarations) {
for (const groupB of groups) {
if (groupA !== groupB) {
if (groupB.dependencies.has(declaration)) {
groupA.usageScore++
}
}
}
}
}
// Sort groups
groups = groups
.filter(g => g.nodes.size)
.sort((a, b) => b.score - a.score)
.sort((a, b) => {
const declarationsA = a.declarations.length
const dependenciesA = a.dependencies.size
const scoreA = declarationsA - dependenciesA
const declarationsB = b.declarations.length
const dependenciesB = b.dependencies.size
const scoreB = declarationsB - dependenciesB
if (scoreA === scoreB) {
return declarationsB - declarationsA
}
return scoreB - scoreA
})
.sort((a, b) => b.usageScore - a.usageScore)
// Debug
for (const group of groups) {
console.log(
'group score:', group.score,
'statements:', Array.from(group.nodes).map(node => ({ code: print(node).code, words: JSON.stringify(wordNodeCache.get(node)) })),
'declarations:', group.declarations,
'dependencies:', group.dependencies,
'usage score:', group.usageScore
)
}
const result = []
let index = 0
for (const group of groups) {
// result.push(`// Group ${++index} (score: ${group.score}, dec: ${group.declarations.length}, deps: ${group.dependencies.size}, usage: ${group.usageScore})`)
result.push(`// ${generateGroupName(group, index)}`)
result.push(...group.nodes)
index++
}
if (ungroupedNodes.length || otherNodes.length) {
if (groups.length) {
result.push(`// Misc`)
}
result.push(...ungroupedNodes, ...otherNodes)
}
return result
}
/**
* Returns a measure of how close two statements should be
* @param {import('recast').types.ASTNode} nodeA
* @param {import('recast').types.ASTNode} nodeB
* @param {string[]} variables
*/
function getStatementGroupScore (nodeA, nodeB, variables) {
const wordsA = getStatementWords(nodeA, variables)
const wordsB = getStatementWords(nodeB, variables)
let score = 0
for (const wordA of wordsA) {
for (const wordB of wordsB) {
const distance = levenshtein(wordA.value, wordB.value)
if (distance <= 1) {
score += (wordA.score + wordB.score) / 2
}
}
}
return score
}
function mayGetVariableDeclarationName (node) {
if (namedTypes.VariableDeclaration.check(node)) {
return node.declarations[0].id.name
} else if (namedTypes.FunctionDeclaration.check(node)) {
return node.id.name
}
}
const wordNodeCache = new Map()
const wordCache = new Map()
/**
* @param {import('recast').types.ASTNode} node
* @param {string[]} setupVariables
* @returns {Word[]}
*/
function getStatementWords (node, setupVariables) {
if (!wordNodeCache.has(node)) {
/** @type {Word[]} */
let words = []
// Variable
let varName = mayGetVariableDeclarationName(node)
if (varName) {
words.push({ value: varName, score: 1 })
} else {
// Contained identifiers
visit(node, {
visitIdentifier (path) {
let identifier = path.value.name
if (setupVariables.includes(identifier) && !words.includes(identifier)) {
words.push({ value: identifier, score: 1 })
}
this.traverse(path)
},
})
}
// Processing
const allWords = words.map(n => n.value).join('|')
if (wordCache.has(allWords)) {
words = wordCache.get(allWords)
} else {
words = processWords(words, true)
wordCache.set(allWords, words)
}
wordNodeCache.set(node, words)
return words
} else {
return wordNodeCache.get(node)
}
}
/**
* Separate & stem words
* @param {Word[]} words
* @param {boolean} stemming
* @returns {Word[]}
*/
function processWords (words, stemming = false) {
return words.reduce((list, word) => {
list.push(...kebab(word.value).split('-').map(value => ({
value: stemming ? stemmer(value) : value,
score: word.score,
})))
return list
}, [])
}
/**
* @param {StatementGroup} group
* @param {number} index
*/
function generateGroupName (group, index) {
if (group.declarations) {
const vars = {}
// Count variable identifiers
visit(Array.from(group.nodes), {
visitIdentifier (path) {
let identifier = path.value.name
if (group.declarations.includes(identifier)) {
const words = processWords([{ value: identifier, score: 1 }])
for (const word of words) {
if (!vars[word.value]) {
vars[word.value] = 1
} else {
vars[word.value]++
}
}
}
this.traverse(path)
},
})
// Sort by count
let sortedVars = Object.keys(vars)
.filter(v => vars[v] > 1)
.sort((a, b) => vars[b] - vars[a])
// Cache stemmed variable names
const stemmedVars = {}
for (const v of sortedVars) {
stemmedVars[v] = stemmer(v)
}
// Heuristic word removal
sortedVars = sortedVars.filter(a => {
for (const b of sortedVars) {
if (a !== b && (
// If stemmed names are identical, we keep only the shortest one
(stemmedVars[a] === stemmedVars[b] && a.length > b.length) ||
// If the names are close, we keep only the most used one
(vars[a] < vars[b] && levenshtein(stemmedVars[a], stemmedVars[b]) <= 1)
)) {
return false
}
}
return true
})
// Final formatting
return sortedVars.map(v => pascal(v)).join(' ')
}
// Default dumb name
return `Group #${index + 1}`
}