| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
'use strict'; |
| 6 |
|
| 7 |
const InsertionText = require('./insertion-text'); |
| 8 |
const lt = '\u0001'; |
| 9 |
const gt = '\u0002'; |
| 10 |
const RE_LT = /</g; |
| 11 |
const RE_GT = />/g; |
| 12 |
const RE_AMP = /&/g; |
| 13 |
|
| 14 |
var RE_lt = /\u0001/g; |
| 15 |
|
| 16 |
var RE_gt = /\u0002/g; |
| 17 |
|
| 18 |
function title(str) { |
| 19 |
return ' title="' + str + '" '; |
| 20 |
} |
| 21 |
|
| 22 |
function customEscape(text) { |
| 23 |
text = String(text); |
| 24 |
return text |
| 25 |
.replace(RE_AMP, '&') |
| 26 |
.replace(RE_LT, '<') |
| 27 |
.replace(RE_GT, '>') |
| 28 |
.replace(RE_lt, '<') |
| 29 |
.replace(RE_gt, '>'); |
| 30 |
} |
| 31 |
|
| 32 |
function annotateLines(fileCoverage, structuredText) { |
| 33 |
const lineStats = fileCoverage.getLineCoverage(); |
| 34 |
if (!lineStats) { |
| 35 |
return; |
| 36 |
} |
| 37 |
Object.entries(lineStats).forEach(([lineNumber, count]) => { |
| 38 |
if (structuredText[lineNumber]) { |
| 39 |
structuredText[lineNumber].covered = count > 0 ? 'yes' : 'no'; |
| 40 |
structuredText[lineNumber].hits = count; |
| 41 |
} |
| 42 |
}); |
| 43 |
} |
| 44 |
|
| 45 |
function annotateStatements(fileCoverage, structuredText) { |
| 46 |
const statementStats = fileCoverage.s; |
| 47 |
const statementMeta = fileCoverage.statementMap; |
| 48 |
Object.entries(statementStats).forEach(([stName, count]) => { |
| 49 |
const meta = statementMeta[stName]; |
| 50 |
const type = count > 0 ? 'yes' : 'no'; |
| 51 |
const startCol = meta.start.column; |
| 52 |
let endCol = meta.end.column + 1; |
| 53 |
const startLine = meta.start.line; |
| 54 |
const endLine = meta.end.line; |
| 55 |
const openSpan = |
| 56 |
lt + |
| 57 |
'span class="' + |
| 58 |
(meta.skip ? 'cstat-skip' : 'cstat-no') + |
| 59 |
'"' + |
| 60 |
title('statement not covered') + |
| 61 |
gt; |
| 62 |
const closeSpan = lt + '/span' + gt; |
| 63 |
let text; |
| 64 |
|
| 65 |
if (type === 'no' && structuredText[startLine]) { |
| 66 |
if (endLine !== startLine) { |
| 67 |
endCol = structuredText[startLine].text.originalLength(); |
| 68 |
} |
| 69 |
text = structuredText[startLine].text; |
| 70 |
text.wrap( |
| 71 |
startCol, |
| 72 |
openSpan, |
| 73 |
startCol < endCol ? endCol : text.originalLength(), |
| 74 |
closeSpan |
| 75 |
); |
| 76 |
} |
| 77 |
}); |
| 78 |
} |
| 79 |
|
| 80 |
function annotateFunctions(fileCoverage, structuredText) { |
| 81 |
const fnStats = fileCoverage.f; |
| 82 |
const fnMeta = fileCoverage.fnMap; |
| 83 |
if (!fnStats) { |
| 84 |
return; |
| 85 |
} |
| 86 |
Object.entries(fnStats).forEach(([fName, count]) => { |
| 87 |
const meta = fnMeta[fName]; |
| 88 |
const type = count > 0 ? 'yes' : 'no'; |
| 89 |
|
| 90 |
|
| 91 |
const decl = meta.decl || meta.loc; |
| 92 |
const startCol = decl.start.column; |
| 93 |
let endCol = decl.end.column + 1; |
| 94 |
const startLine = decl.start.line; |
| 95 |
const endLine = decl.end.line; |
| 96 |
const openSpan = |
| 97 |
lt + |
| 98 |
'span class="' + |
| 99 |
(meta.skip ? 'fstat-skip' : 'fstat-no') + |
| 100 |
'"' + |
| 101 |
title('function not covered') + |
| 102 |
gt; |
| 103 |
const closeSpan = lt + '/span' + gt; |
| 104 |
let text; |
| 105 |
|
| 106 |
if (type === 'no' && structuredText[startLine]) { |
| 107 |
if (endLine !== startLine) { |
| 108 |
endCol = structuredText[startLine].text.originalLength(); |
| 109 |
} |
| 110 |
text = structuredText[startLine].text; |
| 111 |
text.wrap( |
| 112 |
startCol, |
| 113 |
openSpan, |
| 114 |
startCol < endCol ? endCol : text.originalLength(), |
| 115 |
closeSpan |
| 116 |
); |
| 117 |
} |
| 118 |
}); |
| 119 |
} |
| 120 |
|
| 121 |
function annotateBranches(fileCoverage, structuredText) { |
| 122 |
const branchStats = fileCoverage.b; |
| 123 |
const branchMeta = fileCoverage.branchMap; |
| 124 |
if (!branchStats) { |
| 125 |
return; |
| 126 |
} |
| 127 |
|
| 128 |
Object.entries(branchStats).forEach(([branchName, branchArray]) => { |
| 129 |
const sumCount = branchArray.reduce((p, n) => p + n, 0); |
| 130 |
const metaArray = branchMeta[branchName].locations; |
| 131 |
let i; |
| 132 |
let count; |
| 133 |
let meta; |
| 134 |
let startCol; |
| 135 |
let endCol; |
| 136 |
let startLine; |
| 137 |
let endLine; |
| 138 |
let openSpan; |
| 139 |
let closeSpan; |
| 140 |
let text; |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
if (sumCount > 0 || (sumCount === 0 && branchArray.length === 1)) { |
| 145 |
for ( |
| 146 |
i = 0; |
| 147 |
i < branchArray.length && i < metaArray.length; |
| 148 |
i += 1 |
| 149 |
) { |
| 150 |
count = branchArray[i]; |
| 151 |
meta = metaArray[i]; |
| 152 |
startCol = meta.start.column; |
| 153 |
endCol = meta.end.column + 1; |
| 154 |
startLine = meta.start.line; |
| 155 |
endLine = meta.end.line; |
| 156 |
openSpan = |
| 157 |
lt + |
| 158 |
'span class="branch-' + |
| 159 |
i + |
| 160 |
' ' + |
| 161 |
(meta.skip ? 'cbranch-skip' : 'cbranch-no') + |
| 162 |
'"' + |
| 163 |
title('branch not covered') + |
| 164 |
gt; |
| 165 |
closeSpan = lt + '/span' + gt; |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
if ( |
| 172 |
count === 0 && |
| 173 |
startLine === undefined && |
| 174 |
branchMeta[branchName].type === 'if' |
| 175 |
) { |
| 176 |
const prevMeta = metaArray[i - 1]; |
| 177 |
startCol = prevMeta.start.column; |
| 178 |
endCol = prevMeta.end.column + 1; |
| 179 |
startLine = prevMeta.start.line; |
| 180 |
endLine = prevMeta.end.line; |
| 181 |
} |
| 182 |
|
| 183 |
if (count === 0 && structuredText[startLine]) { |
| 184 |
|
| 185 |
if (endLine !== startLine) { |
| 186 |
endCol = structuredText[ |
| 187 |
startLine |
| 188 |
].text.originalLength(); |
| 189 |
} |
| 190 |
text = structuredText[startLine].text; |
| 191 |
if (branchMeta[branchName].type === 'if') { |
| 192 |
|
| 193 |
|
| 194 |
text.insertAt( |
| 195 |
startCol, |
| 196 |
lt + |
| 197 |
'span class="' + |
| 198 |
(meta.skip |
| 199 |
? 'skip-if-branch' |
| 200 |
: 'missing-if-branch') + |
| 201 |
'"' + |
| 202 |
title( |
| 203 |
(i === 0 ? 'if' : 'else') + |
| 204 |
' path not taken' |
| 205 |
) + |
| 206 |
gt + |
| 207 |
(i === 0 ? 'I' : 'E') + |
| 208 |
lt + |
| 209 |
'/span' + |
| 210 |
gt, |
| 211 |
true, |
| 212 |
false |
| 213 |
); |
| 214 |
} else { |
| 215 |
text.wrap( |
| 216 |
startCol, |
| 217 |
openSpan, |
| 218 |
startCol < endCol ? endCol : text.originalLength(), |
| 219 |
closeSpan |
| 220 |
); |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
}); |
| 226 |
} |
| 227 |
|
| 228 |
function annotateSourceCode(fileCoverage, sourceStore) { |
| 229 |
let codeArray; |
| 230 |
let lineCoverageArray; |
| 231 |
try { |
| 232 |
const sourceText = sourceStore.getSource(fileCoverage.path); |
| 233 |
const code = sourceText.split(/(?:\r?\n)|\r/); |
| 234 |
let count = 0; |
| 235 |
const structured = code.map(str => { |
| 236 |
count += 1; |
| 237 |
return { |
| 238 |
line: count, |
| 239 |
covered: 'neutral', |
| 240 |
hits: 0, |
| 241 |
text: new InsertionText(str, true) |
| 242 |
}; |
| 243 |
}); |
| 244 |
structured.unshift({ |
| 245 |
line: 0, |
| 246 |
covered: null, |
| 247 |
text: new InsertionText('') |
| 248 |
}); |
| 249 |
annotateLines(fileCoverage, structured); |
| 250 |
|
| 251 |
|
| 252 |
annotateBranches(fileCoverage, structured); |
| 253 |
annotateFunctions(fileCoverage, structured); |
| 254 |
annotateStatements(fileCoverage, structured); |
| 255 |
structured.shift(); |
| 256 |
|
| 257 |
codeArray = structured.map( |
| 258 |
item => customEscape(item.text.toString()) || ' ' |
| 259 |
); |
| 260 |
|
| 261 |
lineCoverageArray = structured.map(item => ({ |
| 262 |
covered: item.covered, |
| 263 |
hits: item.hits > 0 ? item.hits + 'x' : ' ' |
| 264 |
})); |
| 265 |
|
| 266 |
return { |
| 267 |
annotatedCode: codeArray, |
| 268 |
lineCoverage: lineCoverageArray, |
| 269 |
maxLines: structured.length |
| 270 |
}; |
| 271 |
} catch (ex) { |
| 272 |
codeArray = [ex.message]; |
| 273 |
lineCoverageArray = [{ covered: 'no', hits: 0 }]; |
| 274 |
String(ex.stack || '') |
| 275 |
.split(/\r?\n/) |
| 276 |
.forEach(line => { |
| 277 |
codeArray.push(line); |
| 278 |
lineCoverageArray.push({ covered: 'no', hits: 0 }); |
| 279 |
}); |
| 280 |
return { |
| 281 |
annotatedCode: codeArray, |
| 282 |
lineCoverage: lineCoverageArray, |
| 283 |
maxLines: codeArray.length |
| 284 |
}; |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
module.exports = annotateSourceCode; |