This repository was archived by the owner on Sep 21, 2022. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathcoverage.js
More file actions
148 lines (128 loc) Β· 4.38 KB
/
Copy pathcoverage.js
File metadata and controls
148 lines (128 loc) Β· 4.38 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
'use strict';
var inherit = require('inherit'),
path = require('path'),
url = require('url'),
q = require('q'),
qfs = require('q-io/fs'),
http = require('q-io/http'),
css = require('css'),
hb = require('handlebars');
module.exports = inherit({
__constructor: function(config) {
this.config = config;
this.stats = [];
},
addStats: function(stats) {
this.stats.push(stats);
},
processStats: function() {
var byfile = {};
this.stats.forEach(function(stat) {
Object.keys(stat).forEach(function(file) {
var rules = byfile[file] = byfile[file] || {};
Object.keys(stat[file]).forEach(function(rule) {
if (!rules[rule]) {
rules[rule] = stat[file][rule];
}
});
});
});
return qfs.makeDirectory('gemini-coverage')
.fail(function(err) {
// ignoring the fail if directory already exists.
if (err.code !== 'EEXIST') {
return q.reject(err);
}
})
.then(function() {
return qfs.read(path.join(__dirname, 'coverage.hbs'));
})
.then(function(tmpl) {
return q.all(Object.keys(byfile).map(function(file) {
return http.read({url: file})
.then(function(fileContent) {
fileContent += '';
var line = 0,
lines = fileContent.replace(/\\r\\n/g, '\n').split('\n'),
ast = css.parse(fileContent),
ctx = {
media: 0
},
out = [];
for (var r = 0; r < ast.stylesheet.rules.length; r++) {
line = processRule(
ast.stylesheet.rules[r],
{
line: line,
lines: lines,
byfile: byfile,
file: file,
out: out
},
ctx);
}
return qfs.write(
path.join('gemini-coverage', url.parse(file).pathname.replace(/\//g, '_') + '.html'),
hb.compile(tmpl)({blocks: out}));
});
}));
});
}
});
function processRule(rule, opts, ctx) {
var line = opts.line;
if (rule.type !== 'rule') {
if (rule.type === 'media') {
rule.rules.forEach(function(childRule) {
line = processRule(
childRule,
{
line: line,
lines: opts.lines,
byfile: opts.byfile,
file: opts.file,
out: opts.out,
group: true
},
ctx);
});
ctx.media++;
}
if (line < rule.position.end.line) {
line = pushLines(line, rule.position.end.line, opts);
}
return line;
}
line = pushLines(line, rule.position.start.line - 1, opts);
var cls = 'none';
for (var sel = 0; sel < rule.selectors.length; sel++) {
var key = rule.selectors[sel];
if (opts.group) {
key = '?' + ctx.media + ':' + key;
}
var selector = opts.byfile[opts.file][key];
if (selector) {
cls = 'partial';
if (selector.within) {
cls = 'covered';
}
break;
}
}
line = pushLines(line, rule.position.end.line, opts);
opts.out[opts.out.length - 1].type = cls;
return line;
}
function pushLines(line, till, opts) {
var data = [];
for (var l = line; l < till; l++, line = l) {
data.push(opts.lines[l]);
}
opts.out.push(
{
type: 'normal',
content: data.join('\n')
}
);
return line;
}