Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Node 11 no longer wraps scripts by default #15

Merged
merged 1 commit into from
Apr 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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