UNPKG

2.26 kBJavaScriptView Raw
1'use strict';
2var eol = require('eol');
3var commentsUtil = require('../utils/comments');
4
5var rBlockComment = /\/\*(?:[\s\S]*?)\*\//gmi;
6
7// Bases on get-line-from-pos to support Windows as well
8// See https://github.com/pgilad/get-line-from-pos/blob/master/index.js
9function getLineFromPos(str, pos) {
10 if (pos === 0) {
11 return 1;
12 }
13 //adjust for negative pos
14 if (pos < 0) {
15 pos = str.length + pos;
16 }
17 var lines = eol.split(str.substr(0, pos));
18 return lines.length;
19}
20
21module.exports = function (params) {
22 params = params || {};
23 var regex = commentsUtil.getRegex(params.customTags);
24 var rLineComment = new RegExp('^\\s*\\/\\/' + regex + '$', 'mig');
25 var rInnerBlock = new RegExp('^\\s*(?:\\/\\*)?\\**!?' + regex + '(?:\\**\\/)?$', 'mig');
26
27 return function parse(contents, file) {
28 var comments = [];
29
30 eol.split(contents).forEach(function (line, index) {
31 var match = rLineComment.exec(line);
32 while (match) {
33 var comment = commentsUtil.prepareComment(match, index + 1, file);
34 if (!comment) {
35 break;
36 }
37 comments.push(comment);
38 match = rLineComment.exec(line);
39 }
40 });
41
42 //look for block comments
43 var match = rBlockComment.exec(contents);
44 while (match) {
45 if (!match || !match.length) {
46 break;
47 }
48 //use first match as basis to look into todos/fixmes
49 var baseMatch = match[0];
50 // jshint loopfunc:true
51 eol.split(baseMatch).forEach(function (line, index) {
52 var subMatch = rInnerBlock.exec(line);
53 while (subMatch) {
54 var adjustedLine = getLineFromPos(contents, match.index) + index;
55 var comment = commentsUtil.prepareComment(subMatch, adjustedLine, file);
56 if (!comment) {
57 break;
58 }
59 comments.push(comment);
60 subMatch = rInnerBlock.exec(line);
61 }
62 });
63 match = rBlockComment.exec(contents);
64 }
65
66 return comments;
67 };
68};