NameManager.js 927 B

123456789101112131415161718192021222324252627
  1. "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  2. var _getIdentifierNames = require('./util/getIdentifierNames'); var _getIdentifierNames2 = _interopRequireDefault(_getIdentifierNames);
  3. class NameManager {
  4. __init() {this.usedNames = new Set()}
  5. constructor(code, tokens) {;NameManager.prototype.__init.call(this);
  6. this.usedNames = new Set(_getIdentifierNames2.default.call(void 0, code, tokens));
  7. }
  8. claimFreeName(name) {
  9. const newName = this.findFreeName(name);
  10. this.usedNames.add(newName);
  11. return newName;
  12. }
  13. findFreeName(name) {
  14. if (!this.usedNames.has(name)) {
  15. return name;
  16. }
  17. let suffixNum = 2;
  18. while (this.usedNames.has(name + String(suffixNum))) {
  19. suffixNum++;
  20. }
  21. return name + String(suffixNum);
  22. }
  23. } exports.default = NameManager;