transformation.js 796 B

123456789101112131415161718192021222324252627282930313233
  1. 'use strict';
  2. function Transformation () {
  3. this.mutations = {};
  4. this.nameCounts = {};
  5. }
  6. Transformation.prototype.register = function (espath, callback) {
  7. if (!this.mutations[espath]) {
  8. this.mutations[espath] = [];
  9. }
  10. this.mutations[espath].unshift(callback);
  11. };
  12. Transformation.prototype.apply = function (espath, node) {
  13. this.mutations[espath].forEach(function (callback) {
  14. callback(node);
  15. });
  16. };
  17. Transformation.prototype.isTarget = function (espath) {
  18. return !!this.mutations[espath];
  19. };
  20. Transformation.prototype.generateUniqueName = function (name) {
  21. if (!this.nameCounts[name]) {
  22. this.nameCounts[name] = 0;
  23. }
  24. this.nameCounts[name] += 1;
  25. return '_' + name + this.nameCounts[name];
  26. };
  27. module.exports = Transformation;