source.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. const CovLine = require('./line')
  2. const { sliceRange } = require('./range')
  3. const { originalPositionFor, generatedPositionFor, GREATEST_LOWER_BOUND, LEAST_UPPER_BOUND } = require('@jridgewell/trace-mapping')
  4. module.exports = class CovSource {
  5. constructor (sourceRaw, wrapperLength) {
  6. sourceRaw = sourceRaw ? sourceRaw.trimEnd() : ''
  7. this.lines = []
  8. this.eof = sourceRaw.length
  9. this.shebangLength = getShebangLength(sourceRaw)
  10. this.wrapperLength = wrapperLength - this.shebangLength
  11. this._buildLines(sourceRaw)
  12. }
  13. _buildLines (source) {
  14. let position = 0
  15. let ignoreCount = 0
  16. let ignoreAll = false
  17. for (const [i, lineStr] of source.split(/(?<=\r?\n)/u).entries()) {
  18. const line = new CovLine(i + 1, position, lineStr)
  19. if (ignoreCount > 0) {
  20. line.ignore = true
  21. ignoreCount--
  22. } else if (ignoreAll) {
  23. line.ignore = true
  24. }
  25. this.lines.push(line)
  26. position += lineStr.length
  27. const ignoreToken = this._parseIgnore(lineStr)
  28. if (!ignoreToken) continue
  29. line.ignore = true
  30. if (ignoreToken.count !== undefined) {
  31. ignoreCount = ignoreToken.count
  32. }
  33. if (ignoreToken.start || ignoreToken.stop) {
  34. ignoreAll = ignoreToken.start
  35. ignoreCount = 0
  36. }
  37. }
  38. }
  39. /**
  40. * Parses for comments:
  41. * c8 ignore next
  42. * c8 ignore next 3
  43. * c8 ignore start
  44. * c8 ignore stop
  45. * And equivalent ones for v8, e.g. v8 ignore next.
  46. * @param {string} lineStr
  47. * @return {{count?: number, start?: boolean, stop?: boolean}|undefined}
  48. */
  49. _parseIgnore (lineStr) {
  50. const testIgnoreNextLines = lineStr.match(/^\W*\/\* [c|v]8 ignore next (?<count>[0-9]+)/)
  51. if (testIgnoreNextLines) {
  52. return { count: Number(testIgnoreNextLines.groups.count) }
  53. }
  54. // Check if comment is on its own line.
  55. if (lineStr.match(/^\W*\/\* [c|v]8 ignore next/)) {
  56. return { count: 1 }
  57. }
  58. if (lineStr.match(/\/\* [c|v]8 ignore next/)) {
  59. // Won't ignore successive lines, but the current line will be ignored.
  60. return { count: 0 }
  61. }
  62. const testIgnoreStartStop = lineStr.match(/\/\* [c|v]8 ignore (?<mode>start|stop)/)
  63. if (testIgnoreStartStop) {
  64. if (testIgnoreStartStop.groups.mode === 'start') return { start: true }
  65. if (testIgnoreStartStop.groups.mode === 'stop') return { stop: true }
  66. }
  67. }
  68. // given a start column and end column in absolute offsets within
  69. // a source file (0 - EOF), returns the relative line column positions.
  70. offsetToOriginalRelative (sourceMap, startCol, endCol) {
  71. const lines = sliceRange(this.lines, startCol, endCol, true)
  72. if (!lines.length) return {}
  73. const start = originalPositionTryBoth(
  74. sourceMap,
  75. lines[0].line,
  76. Math.max(0, startCol - lines[0].startCol)
  77. )
  78. if (!(start && start.source)) {
  79. return {}
  80. }
  81. let end = originalEndPositionFor(
  82. sourceMap,
  83. lines[lines.length - 1].line,
  84. endCol - lines[lines.length - 1].startCol
  85. )
  86. if (!(end && end.source)) {
  87. return {}
  88. }
  89. if (start.source !== end.source) {
  90. return {}
  91. }
  92. if (start.line === end.line && start.column === end.column) {
  93. end = originalPositionFor(sourceMap, {
  94. line: lines[lines.length - 1].line,
  95. column: endCol - lines[lines.length - 1].startCol,
  96. bias: LEAST_UPPER_BOUND
  97. })
  98. end.column -= 1
  99. }
  100. return {
  101. source: start.source,
  102. startLine: start.line,
  103. relStartCol: start.column,
  104. endLine: end.line,
  105. relEndCol: end.column
  106. }
  107. }
  108. relativeToOffset (line, relCol) {
  109. line = Math.max(line, 1)
  110. if (this.lines[line - 1] === undefined) return this.eof
  111. return Math.min(this.lines[line - 1].startCol + relCol, this.lines[line - 1].endCol)
  112. }
  113. }
  114. // this implementation is pulled over from istanbul-lib-sourcemap:
  115. // https://github.com/istanbuljs/istanbuljs/blob/master/packages/istanbul-lib-source-maps/lib/get-mapping.js
  116. //
  117. /**
  118. * AST ranges are inclusive for start positions and exclusive for end positions.
  119. * Source maps are also logically ranges over text, though interacting with
  120. * them is generally achieved by working with explicit positions.
  121. *
  122. * When finding the _end_ location of an AST item, the range behavior is
  123. * important because what we're asking for is the _end_ of whatever range
  124. * corresponds to the end location we seek.
  125. *
  126. * This boils down to the following steps, conceptually, though the source-map
  127. * library doesn't expose primitives to do this nicely:
  128. *
  129. * 1. Find the range on the generated file that ends at, or exclusively
  130. * contains the end position of the AST node.
  131. * 2. Find the range on the original file that corresponds to
  132. * that generated range.
  133. * 3. Find the _end_ location of that original range.
  134. */
  135. function originalEndPositionFor (sourceMap, line, column) {
  136. // Given the generated location, find the original location of the mapping
  137. // that corresponds to a range on the generated file that overlaps the
  138. // generated file end location. Note however that this position on its
  139. // own is not useful because it is the position of the _start_ of the range
  140. // on the original file, and we want the _end_ of the range.
  141. const beforeEndMapping = originalPositionTryBoth(
  142. sourceMap,
  143. line,
  144. Math.max(column - 1, 1)
  145. )
  146. if (beforeEndMapping.source === null) {
  147. return null
  148. }
  149. // Convert that original position back to a generated one, with a bump
  150. // to the right, and a rightward bias. Since 'generatedPositionFor' searches
  151. // for mappings in the original-order sorted list, this will find the
  152. // mapping that corresponds to the one immediately after the
  153. // beforeEndMapping mapping.
  154. const afterEndMapping = generatedPositionFor(sourceMap, {
  155. source: beforeEndMapping.source,
  156. line: beforeEndMapping.line,
  157. column: beforeEndMapping.column + 1,
  158. bias: LEAST_UPPER_BOUND
  159. })
  160. if (
  161. // If this is null, it means that we've hit the end of the file,
  162. // so we can use Infinity as the end column.
  163. afterEndMapping.line === null ||
  164. // If these don't match, it means that the call to
  165. // 'generatedPositionFor' didn't find any other original mappings on
  166. // the line we gave, so consider the binding to extend to infinity.
  167. originalPositionFor(sourceMap, afterEndMapping).line !==
  168. beforeEndMapping.line
  169. ) {
  170. return {
  171. source: beforeEndMapping.source,
  172. line: beforeEndMapping.line,
  173. column: Infinity
  174. }
  175. }
  176. // Convert the end mapping into the real original position.
  177. return originalPositionFor(sourceMap, afterEndMapping)
  178. }
  179. function originalPositionTryBoth (sourceMap, line, column) {
  180. let original = originalPositionFor(sourceMap, {
  181. line,
  182. column,
  183. bias: GREATEST_LOWER_BOUND
  184. })
  185. if (original.line === null) {
  186. original = originalPositionFor(sourceMap, {
  187. line,
  188. column,
  189. bias: LEAST_UPPER_BOUND
  190. })
  191. }
  192. // The source maps generated by https://github.com/istanbuljs/istanbuljs
  193. // (using @babel/core 7.7.5) have behavior, such that a mapping
  194. // mid-way through a line maps to an earlier line than a mapping
  195. // at position 0. Using the line at positon 0 seems to provide better reports:
  196. //
  197. // if (true) {
  198. // cov_y5divc6zu().b[1][0]++;
  199. // cov_y5divc6zu().s[3]++;
  200. // console.info('reachable');
  201. // } else { ... }
  202. // ^ ^
  203. // l5 l3
  204. const min = originalPositionFor(sourceMap, {
  205. line,
  206. column: 0,
  207. bias: GREATEST_LOWER_BOUND
  208. })
  209. if (min.line > original.line) {
  210. original = min
  211. }
  212. return original
  213. }
  214. // Not required since Node 12, see: https://github.com/nodejs/node/pull/27375
  215. const isPreNode12 = /^v1[0-1]\./u.test(process.version)
  216. function getShebangLength (source) {
  217. if (isPreNode12 && source.indexOf('#!') === 0) {
  218. const match = source.match(/(?<shebang>#!.*)/)
  219. if (match) {
  220. return match.groups.shebang.length
  221. }
  222. } else {
  223. return 0
  224. }
  225. }