forked from rowanmanning/joblint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
joblint.js
120 lines (106 loc) · 2.92 KB
/
joblint.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
109
110
111
112
113
114
115
116
117
118
119
120
'use strict';
var extend = require('extend');
module.exports = joblint;
module.exports.defaults = {
rules: require('./rules')
};
function joblint (text, options) {
options = defaultOptions(options);
var result = {
counts: {},
issues: []
};
options.rules.forEach(function (rule) {
rule.triggers.forEach(function (trigger) {
var match;
while ((match = trigger.exec(text)) !== null) {
incrementKeys(rule.increment, result.counts);
result.issues.push(buildIssueFromMatch(match, rule));
}
});
});
Object.keys(result.counts).forEach(function (key) {
result.counts[key] = Math.max(result.counts[key], 0);
});
result.issues = result.issues.sort(sortByPosition);
return result;
}
function defaultOptions (options) {
options = extend({}, module.exports.defaults, options);
options.rules = buildRules(options.rules);
return options;
}
function buildRules (rules) {
return rules.map(buildRule);
}
function buildRule (rule) {
rule = extend(true, {}, rule);
rule.increment = rule.increment || {};
rule.triggers = rule.triggers.map(function (trigger) {
return new RegExp('\\b(' + trigger + ')\\b', 'gim');
});
return rule;
}
function incrementKeys (amounts, store) {
Object.keys(amounts).forEach(function (key) {
if (!store[key]) {
store[key] = 0;
}
store[key] += amounts[key];
});
}
function buildIssueFromMatch (match, rule) {
var issue = {
name: rule.name,
reason: rule.reason,
solution: rule.solution,
level: rule.level,
increment: rule.increment,
occurance: match[1],
position: match.index
};
issue.context = buildIssueContext(match.input, issue.occurance, issue.position);
return issue;
}
function buildIssueContext (input, occurance, position) {
var context = '{{occurance}}';
input
.substr(0, position)
.split(/[\r\n]+/)
.pop()
.replace(/\s+/g, ' ')
.split(/(\s+)/)
.reverse()
.forEach(function (word) {
if (context.length < 32) {
context = word + context;
}
else if (!/^…/.test(context)) {
context = '…' + context.trim();
}
});
input
.substr(position + occurance.length)
.split(/[\r\n]+/)
.shift()
.replace(/\s+/g, ' ')
.split(/(\s+)/)
.forEach(function (word) {
if (context.length < 52) {
context += word;
}
else if (!/…$/.test(context)) {
context = context.trim() + '…';
}
});
return context.trim();
}
function sortByPosition (a, b) {
if (a.position > b.position) {
return 1;
}
if (a.position < b.position) {
return -1;
}
return 0;
}