1 | 'use strict';
|
2 | var eol = require('eol');
|
3 | var commentsUtil = require('../utils/comments');
|
4 |
|
5 | var rBlockComment = /\/\*(?:[\s\S]*?)\*\//gmi;
|
6 |
|
7 |
|
8 |
|
9 | function getLineFromPos(str, pos) {
|
10 | if (pos === 0) {
|
11 | return 1;
|
12 | }
|
13 |
|
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 |
|
21 | module.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 |
|
43 | var match = rBlockComment.exec(contents);
|
44 | while (match) {
|
45 | if (!match || !match.length) {
|
46 | break;
|
47 | }
|
48 |
|
49 | var baseMatch = match[0];
|
50 |
|
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 | };
|