1 |
|
2 | import getIdentifierNames from "./util/getIdentifierNames";
|
3 |
|
4 | export 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 + suffixNum)) {
|
23 | suffixNum++;
|
24 | }
|
25 | return name + suffixNum;
|
26 | }
|
27 | }
|