UNPKG

791 BJavaScriptView Raw
1import {TokenType as tt} from "./parser/tokenizer/types";
2
3
4export default class NameManager {
5 __init() {this.usedNames = new Set()}
6
7 constructor( tokens) {;this.tokens = tokens;NameManager.prototype.__init.call(this);}
8
9 preprocessNames() {
10 for (let i = 0; i < this.tokens.tokens.length; i++) {
11 if (this.tokens.matches1AtIndex(i, tt.name)) {
12 this.usedNames.add(this.tokens.identifierNameAtIndex(i));
13 }
14 }
15 }
16
17 claimFreeName(name) {
18 const newName = this.findFreeName(name);
19 this.usedNames.add(newName);
20 return newName;
21 }
22
23 findFreeName(name) {
24 if (!this.usedNames.has(name)) {
25 return name;
26 }
27 let suffixNum = 2;
28 while (this.usedNames.has(name + suffixNum)) {
29 suffixNum++;
30 }
31 return name + suffixNum;
32 }
33}