annotator.js 8.6 KB

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