Skip to content

Commit

Permalink
Merge a877a0e into 48ef382
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoe committed Apr 7, 2019
2 parents 48ef382 + a877a0e commit f5dcc8b
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 157 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
language: node_js
node_js:
- "10.10"
- "11"
after_success: npm run coverage
24 changes: 14 additions & 10 deletions lib/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ const CovBranch = require('./branch')
const CovLine = require('./line')
const CovFunction = require('./function')

// Injected when Node.js is loading script into isolate.
// see: https://github.com/nodejs/node/pull/21573.
const cjsWrapperLength = require('module').wrapper[0].length

const isWindows = process.platform === 'win32'
const isNode10 = !!process.version.match(/^v10/)

// Injected when Node.js is loading script into isolate pre Node 11.
// see: https://github.com/nodejs/node/pull/21573.
const cjsWrapperLength = isNode10 ? require('module').wrapper[0].length : 0

module.exports = class CovScript {
constructor (scriptPath, wrapperLength) {
Expand All @@ -17,19 +18,22 @@ module.exports = class CovScript {
const source = fs.readFileSync(path, 'utf8')
this.path = path
this.wrapperLength = wrapperLength === undefined ? cjsWrapperLength : wrapperLength
this.wrapperLength -= shebangLength(source)
const shebangLength = getShebangLength(source)
this.wrapperLength -= shebangLength
this.lines = []
this.branches = []
this.functions = []
this.eof = -1
this._buildLines(source, this.lines)
this._buildLines(source, this.lines, shebangLength)
}
_buildLines (source, lines) {
_buildLines (source, lines, shebangLength) {
let position = 0
const separator = isWindows ? '\r\n' : '\n'
source.split(separator).forEach((lineStr, i) => {
;(source.trim()).split(separator).forEach((lineStr, i) => {
this.eof = position + lineStr.length
lines.push(new CovLine(i + 1, position, this.eof))
const line = new CovLine(i + 1, position, this.eof)
if (i === 0 && shebangLength !== 0) line.count = 1
lines.push(line)
position += lineStr.length + separator.length
})
}
Expand Down Expand Up @@ -135,7 +139,7 @@ module.exports = class CovScript {
}
}

function shebangLength (source) {
function getShebangLength (source) {
if (source.indexOf('#!') === 0) {
const match = source.match(/(?<shebang>#!.*)/)
if (match) {
Expand Down
Loading

0 comments on commit f5dcc8b

Please sign in to comment.