-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
67 lines (58 loc) · 1.39 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
var d = require('defined')
var phonetic = require('phonetic')
var kRenamed = Symbol('renamed')
module.exports = function (b, opts) {
var maxLength = d(opts && opts.maxLength, 3)
var except = d(opts.except, [])
return {
visitor: {
Program: {
enter: function () {
this.names = new NameGenerator(this.file.opts.filename)
},
exit: function () {
this.names.close()
}
},
ReferencedIdentifier: function (path) {
if (path.node.name.length > maxLength) {
return
}
if (except.indexOf(path.node.name) !== -1) {
return
}
var binding = path.scope.getBinding(path.node.name)
if (!binding) {
return
}
if (binding[kRenamed]) {
return
}
path.scope.rename(path.node.name, this.names.nextName())
binding[kRenamed] = true
}
}
}
}
function NameGenerator (seed) {
this.last = seed || null
this.used = new Set()
this.length = 2
}
NameGenerator.prototype.nextName = function nextName () {
var word = phonetic.generate({
capFirst: false,
syllables: this.length,
seed: this.last
})
if (this.used.has(word)) {
this.length++
return this.nextName()
}
this.used.add(word)
this.last = word
return this.last
}
NameGenerator.prototype.close = function close () {
this.used.clear()
}