UNPKG

1.58 kBJavaScriptView Raw
1import meta from './meta';
2
3const COMMENT_PATTERN = /^\s*@?(adana|coverage|test|istanbul):?\s*(.*)\s*/;
4
5function within(a, b) {
6 return a.start.line >= b.start.line &&
7 a.start.column >= b.start.column &&
8 a.end.line <= b.end.line &&
9 a.end.column <= b.end.column;
10}
11
12export function applyRules(state) {
13 const coverage = meta(state);
14 coverage.entries.forEach(entry => {
15 const result = { };
16 const output = [ ];
17 entry.tags.forEach(tag => result[tag] = true);
18 coverage.rules.forEach(rule => {
19 if (within(rule.loc, entry.loc)) {
20 result[rule.tag] = rule.value;
21 }
22 });
23 Object.keys(result).forEach(tag => {
24 const value = result[tag];
25 if (value) {
26 output.push(tag);
27 }
28 });
29 entry.tags = output;
30 });
31}
32
33export function extract(comment) {
34 const output = { };
35 const result = COMMENT_PATTERN.exec(comment);
36 if (result) {
37 const entries = result[2].split(/\s+/);
38 entries.forEach(entry => {
39 switch (entry.charAt(0)) {
40 case '+':
41 output[entry.substr(1)] = true;
42 break;
43 case '-':
44 output[entry.substr(1)] = false;
45 break;
46 default:
47 break;
48 }
49 });
50 }
51 return output;
52}
53
54export function addRules(state, loc, comments) {
55 if (comments) {
56 const coverage = meta(state);
57 comments.forEach(comment => {
58 const values = extract(comment.value);
59 Object.keys(values).forEach(tag => {
60 coverage.rules.push({
61 tag,
62 value: values[tag],
63 loc,
64 });
65 });
66 });
67 }
68}