Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

source.js 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. const CovLine = require('./line')
  2. const { GREATEST_LOWER_BOUND, LEAST_UPPER_BOUND } = require('source-map').SourceMapConsumer
  3. module.exports = class CovSource {
  4. constructor (sourceRaw, wrapperLength) {
  5. sourceRaw = sourceRaw.trimEnd()
  6. this.lines = []
  7. this.eof = sourceRaw.length
  8. this.shebangLength = getShebangLength(sourceRaw)
  9. this.wrapperLength = wrapperLength - this.shebangLength
  10. this._buildLines(sourceRaw)
  11. }
  12. _buildLines (source) {
  13. let position = 0
  14. let ignoreCount = 0
  15. let ignoreAll = false
  16. for (const [i, lineStr] of source.split(/(?<=\r?\n)/u).entries()) {
  17. const line = new CovLine(i + 1, position, lineStr)
  18. if (ignoreCount > 0) {
  19. line.ignore = true
  20. ignoreCount--
  21. } else if (ignoreAll) {
  22. line.ignore = true
  23. }
  24. this.lines.push(line)
  25. position += lineStr.length
  26. const ignoreToken = this._parseIgnore(lineStr)
  27. if (!ignoreToken) continue
  28. line.ignore = true
  29. if (ignoreToken.count !== undefined) {
  30. ignoreCount = ignoreToken.count
  31. }
  32. if (ignoreToken.start || ignoreToken.stop) {
  33. ignoreAll = ignoreToken.start
  34. ignoreCount = 0
  35. }
  36. }
  37. }
  38. /**
  39. * Parses for comments:
  40. * c8 ignore next
  41. * c8 ignore next 3
  42. * c8 ignore start
  43. * c8 ignore stop
  44. * @param {string} lineStr
  45. * @return {{count?: number, start?: boolean, stop?: boolean}|undefined}
  46. */
  47. _parseIgnore (lineStr) {
  48. const testIgnoreNextLines = lineStr.match(/^\W*\/\* c8 ignore next (?<count>[0-9]+) *\*\/\W*$/)
  49. if (testIgnoreNextLines) {
  50. return { count: Number(testIgnoreNextLines.groups.count) }
  51. }
  52. // Check if comment is on its own line.
  53. if (lineStr.match(/^\W*\/\* c8 ignore next *\*\/\W*$/)) {
  54. return { count: 1 }
  55. }
  56. if (lineStr.match(/\/\* c8 ignore next \*\//)) {
  57. // Won't ignore successive lines, but the current line will be ignored.
  58. return { count: 0 }
  59. }
  60. const testIgnoreStartStop = lineStr.match(/\/\* c8 ignore (?<mode>start|stop) *\*\//)
  61. if (testIgnoreStartStop) {
  62. if (testIgnoreStartStop.groups.mode === 'start') return { start: true }
  63. if (testIgnoreStartStop.groups.mode === 'stop') return { stop: true }
  64. }
  65. }
  66. // given a start column and end column in absolute offsets within
  67. // a source file (0 - EOF), returns the relative line column positions.
  68. offsetToOriginalRelative (sourceMap, startCol, endCol) {
  69. const lines = this.lines.filter((line, i) => {
  70. return startCol <= line.endCol && endCol >= line.startCol
  71. })
  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. let end = originalEndPositionFor(
  79. sourceMap,
  80. lines[lines.length - 1].line,
  81. endCol - lines[lines.length - 1].startCol
  82. )
  83. if (!(start && end)) {
  84. return {}
  85. }
  86. if (!(start.source && 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 = sourceMap.originalPositionFor({
  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 = sourceMap.generatedPositionFor({
  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. sourceMap.originalPositionFor(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 sourceMap.originalPositionFor(afterEndMapping)
  178. }
  179. function originalPositionTryBoth (sourceMap, line, column) {
  180. let original = sourceMap.originalPositionFor({
  181. line,
  182. column,
  183. bias: GREATEST_LOWER_BOUND
  184. })
  185. if (original.line === null) {
  186. original = sourceMap.originalPositionFor({
  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 = sourceMap.originalPositionFor({
  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. }