annotator.js 8.7 KB

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