-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathcodecrumbs.js
108 lines (87 loc) · 2.83 KB
/
codecrumbs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
const compact = require('lodash/compact');
const { CC_NODE_TYPE, NO_TRAIL_FLOW } = require('../../../shared-constants');
const CRUMB_REGEX = /cc|codecrumb/;
const DEFAULT_COMMENT_REGEX = /\/\*[\s\S]*?\*\/|([^:]|^)\/\/.*$/gm;
const getCommentNodeValue = node => (node.value || '').trim();
const parseCodecrumbComment = (node = {}) => {
const comment = getCommentNodeValue(node);
const cc = { original: comment };
try {
const afterAlias = comment.split(':')[1];
if (afterAlias) {
const params = afterAlias.split(';');
//check if has flow marker
let counter = 0;
if (params[0].includes('#')) {
const flowParams = params[0].split('#');
cc.flow = flowParams[0];
cc.flowStep = +flowParams[1];
counter++;
} else {
cc.flow = NO_TRAIL_FLOW;
}
cc.name = params[counter];
if (params[counter + 1] && params[counter + 1][0] === '+') {
cc.linesRange = +params[counter + 1].substr(1);
counter++;
}
cc.details = params[counter + 1];
}
} catch (e) {
console.error('Parameters parsing failed: ', e);
}
return cc;
};
const isCodecrumb = node => CRUMB_REGEX.test(getCommentNodeValue(node));
const buildCrumb = (params, crumbNodeLines, path) => ({
type: CC_NODE_TYPE,
id: `${path}-${crumbNodeLines.join('-')}`,
name: params.name || '',
displayLoc: `#${crumbNodeLines[0]}`,
crumbNodeLines: params.linesRange
? [crumbNodeLines[0], crumbNodeLines[1] + params.linesRange]
: crumbNodeLines,
params
});
const setupGetCommentsFromCode = regex => fileCode => {
if (!fileCode) {
return [];
}
const result = compact(regex.exec(fileCode)) || [];
return result.reduce((comments, value) => {
value = value.trim()
const index = fileCode.indexOf(value);
const tempString = fileCode.substring(0, index);
const matchLineNumber = tempString.split('\n').length;
const commentStringCount = value.split('\n').length
const nodeLines = [matchLineNumber, matchLineNumber + commentStringCount - 1];
return [...comments, { value, nodeLines }]
}, []);
};
const getNodeLines = node => node.nodeLines;
const setupGetCrumbs = getCommentsFromCode => (fileCode, path) => {
const crumbsList = [];
const comments = getCommentsFromCode(fileCode);
try {
comments.forEach(comment => {
if (isCodecrumb(comment)) {
const params = parseCodecrumbComment(comment);
const crumbNodeLines = getNodeLines(comment);
crumbsList.push(buildCrumb(params, crumbNodeLines, path));
}
});
return crumbsList;
} catch (e) {
console.log(path, e);
return crumbsList;
}
};
const getCrumbs = setupGetCrumbs(setupGetCommentsFromCode(DEFAULT_COMMENT_REGEX));
module.exports = {
getCrumbs,
setupGetCommentsFromCode,
setupGetCrumbs,
isCodecrumb,
parseCodecrumbComment,
buildCrumb
};