index.js 744 B

1234567891011121314151617181920212223242526272829303132333435
  1. /*!
  2. * esprima-extract-comments <https://github.com/jonschlinkert/esprima-extract-comments>
  3. *
  4. * Copyright (c) 2014 Jon Schlinkert, contributors.
  5. * Licensed under the MIT license.
  6. */
  7. 'use strict';
  8. var esprima = require('esprima');
  9. /**
  10. * Extract code comments from the given `string`.
  11. *
  12. * ```js
  13. * var extract = require('esprima-extract-comments');
  14. * extract('// this is a code comment');
  15. * ```
  16. * @param {String} `string`
  17. * @param {Object} `options` Options to pass to esprima.
  18. * @return {Object} Object of code comments.
  19. * @api public
  20. */
  21. module.exports = function(str) {
  22. var ast = esprima.parse(str, {
  23. tolerant: true,
  24. comment: true,
  25. tokens: true,
  26. range: true,
  27. loc: true
  28. });
  29. return ast.comments;
  30. };