annotator.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. // Some versions of the instrumenter in the wild populate 'func'
  83. // but not 'decl':
  84. const decl = meta.decl || meta.loc;
  85. const startCol = decl.start.column;
  86. let endCol = decl.end.column + 1;
  87. const startLine = decl.start.line;
  88. const endLine = 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.entries(branchStats).forEach(([branchName, branchArray]) => {
  119. const sumCount = branchArray.reduce((p, n) => p + n, 0);
  120. const metaArray = branchMeta[branchName].locations;
  121. let i;
  122. let count;
  123. let meta;
  124. let startCol;
  125. let endCol;
  126. let startLine;
  127. let endLine;
  128. let openSpan;
  129. let closeSpan;
  130. let text;
  131. // only highlight if partial branches are missing or if there is a
  132. // single uncovered branch.
  133. if (sumCount > 0 || (sumCount === 0 && branchArray.length === 1)) {
  134. for (
  135. i = 0;
  136. i < branchArray.length && i < metaArray.length;
  137. i += 1
  138. ) {
  139. count = branchArray[i];
  140. meta = metaArray[i];
  141. startCol = meta.start.column;
  142. endCol = meta.end.column + 1;
  143. startLine = meta.start.line;
  144. endLine = meta.end.line;
  145. openSpan =
  146. lt +
  147. 'span class="branch-' +
  148. i +
  149. ' ' +
  150. (meta.skip ? 'cbranch-skip' : 'cbranch-no') +
  151. '"' +
  152. title('branch not covered') +
  153. gt;
  154. closeSpan = lt + '/span' + gt;
  155. // If the branch is an implicit else from an if statement,
  156. // then the coverage report won't show a statistic.
  157. // Therefore, the previous branch will be used to report that
  158. // there is no coverage on that implicit branch.
  159. if (
  160. count === 0 &&
  161. startLine === undefined &&
  162. branchMeta[branchName].type === 'if'
  163. ) {
  164. const prevMeta = metaArray[i - 1];
  165. startCol = prevMeta.start.column;
  166. endCol = prevMeta.end.column + 1;
  167. startLine = prevMeta.start.line;
  168. endLine = prevMeta.end.line;
  169. }
  170. if (count === 0 && structuredText[startLine]) {
  171. //skip branches taken
  172. if (endLine !== startLine) {
  173. endCol = structuredText[
  174. startLine
  175. ].text.originalLength();
  176. }
  177. text = structuredText[startLine].text;
  178. if (branchMeta[branchName].type === 'if') {
  179. // 'if' is a special case
  180. // since the else branch might not be visible, being non-existent
  181. text.insertAt(
  182. startCol,
  183. lt +
  184. 'span class="' +
  185. (meta.skip
  186. ? 'skip-if-branch'
  187. : 'missing-if-branch') +
  188. '"' +
  189. title(
  190. (i === 0 ? 'if' : 'else') +
  191. ' path not taken'
  192. ) +
  193. gt +
  194. (i === 0 ? 'I' : 'E') +
  195. lt +
  196. '/span' +
  197. gt,
  198. true,
  199. false
  200. );
  201. } else {
  202. text.wrap(
  203. startCol,
  204. openSpan,
  205. startCol < endCol ? endCol : text.originalLength(),
  206. closeSpan
  207. );
  208. }
  209. }
  210. }
  211. }
  212. });
  213. }
  214. function annotateSourceCode(fileCoverage, sourceStore) {
  215. let codeArray;
  216. let lineCoverageArray;
  217. try {
  218. const sourceText = sourceStore.getSource(fileCoverage.path);
  219. const code = sourceText.split(/(?:\r?\n)|\r/);
  220. let count = 0;
  221. const structured = code.map(str => {
  222. count += 1;
  223. return {
  224. line: count,
  225. covered: 'neutral',
  226. hits: 0,
  227. text: new InsertionText(str, true)
  228. };
  229. });
  230. structured.unshift({
  231. line: 0,
  232. covered: null,
  233. text: new InsertionText('')
  234. });
  235. annotateLines(fileCoverage, structured);
  236. //note: order is important, since statements typically result in spanning the whole line and doing branches late
  237. //causes mismatched tags
  238. annotateBranches(fileCoverage, structured);
  239. annotateFunctions(fileCoverage, structured);
  240. annotateStatements(fileCoverage, structured);
  241. structured.shift();
  242. codeArray = structured.map(
  243. item => customEscape(item.text.toString()) || '&nbsp;'
  244. );
  245. lineCoverageArray = structured.map(item => ({
  246. covered: item.covered,
  247. hits: item.hits > 0 ? item.hits + 'x' : '&nbsp;'
  248. }));
  249. return {
  250. annotatedCode: codeArray,
  251. lineCoverage: lineCoverageArray,
  252. maxLines: structured.length
  253. };
  254. } catch (ex) {
  255. codeArray = [ex.message];
  256. lineCoverageArray = [{ covered: 'no', hits: 0 }];
  257. String(ex.stack || '')
  258. .split(/\r?\n/)
  259. .forEach(line => {
  260. codeArray.push(line);
  261. lineCoverageArray.push({ covered: 'no', hits: 0 });
  262. });
  263. return {
  264. annotatedCode: codeArray,
  265. lineCoverage: lineCoverageArray,
  266. maxLines: codeArray.length
  267. };
  268. }
  269. }
  270. module.exports = annotateSourceCode;