UNPKG

642 BJavaScriptView Raw
1
2import getIdentifierNames from "./util/getIdentifierNames";
3
4export default class NameManager {
5 __init() {this.usedNames = new Set()}
6
7 constructor(code, tokens) {;NameManager.prototype.__init.call(this);
8 this.usedNames = new Set(getIdentifierNames(code, tokens));
9 }
10
11 claimFreeName(name) {
12 const newName = this.findFreeName(name);
13 this.usedNames.add(newName);
14 return newName;
15 }
16
17 findFreeName(name) {
18 if (!this.usedNames.has(name)) {
19 return name;
20 }
21 let suffixNum = 2;
22 while (this.usedNames.has(name + String(suffixNum))) {
23 suffixNum++;
24 }
25 return name + String(suffixNum);
26 }
27}