sucrase
Version:
Super-fast alternative to Babel for when you can target modern JS runtimes
29 lines (28 loc) • 732 B
JavaScript
export class NameManager {
constructor(tokens) {
this.tokens = tokens;
this.usedNames = new Set();
}
preprocessNames(tokens) {
for (const token of this.tokens.tokens) {
if (token.type.label === 'name') {
this.usedNames.add(token.value);
}
}
}
claimFreeName(name) {
const newName = this.findFreeName(name);
this.usedNames.add(newName);
return newName;
}
findFreeName(name) {
if (!this.usedNames.has(name)) {
return name;
}
let suffixNum = 2;
while (this.usedNames.has(name + suffixNum)) {
suffixNum++;
}
return name + suffixNum;
}
}