strip-comments.js 652 B

123456789101112131415161718192021222324
  1. "use strict";
  2. var repeat = require("es5-ext/string/#/repeat")
  3. , esniff = require("./");
  4. module.exports = exports = function (code/*, options*/) {
  5. var options = Object(arguments[1]);
  6. var comments = esniff(code, function (emitter, accessor) {
  7. accessor.shouldCollectComments = true;
  8. });
  9. if (!comments.length) return code;
  10. var i = 0, result = [];
  11. comments.forEach(function (commentMeta) {
  12. result.push(code.slice(i, commentMeta.point));
  13. if (options.preserveLocation) {
  14. result.push(repeat.call(" ", commentMeta.endPoint - commentMeta.point));
  15. }
  16. i = commentMeta.endPoint;
  17. });
  18. result.push(code.slice(i));
  19. return result.join("");
  20. };