annotator.js 10 KB

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