Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeapage committed Feb 12, 2020
1 parent 6f62ea2 commit c169da3
Show file tree
Hide file tree
Showing 5 changed files with 282 additions and 177 deletions.
6 changes: 3 additions & 3 deletions lib/line.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = class CovLine {
constructor (line, startCol, lineStr) {
constructor(line, startCol, lineStr) {
this.line = line
// note that startCol and endCol are absolute positions
// within a file, not relative to the line.
Expand All @@ -13,13 +13,13 @@ module.exports = class CovLine {

// we start with all lines having been executed, and work
// backwards zeroing out lines based on V8 output.
this.count = 1
// this.count = 1

// set by source.js during parsing, if /* c8 ignore next */ is found.
this.ignore = false
}

toIstanbul () {
toIstanbul() {
return {
start: {
line: this.line,
Expand Down
155 changes: 102 additions & 53 deletions lib/source.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const CovLine = require('./line')
const { GREATEST_LOWER_BOUND, LEAST_UPPER_BOUND } = require('source-map').SourceMapConsumer

module.exports = class CovSource {
constructor (sourceRaw, wrapperLength) {
constructor(sourceRaw, wrapperLength) {
sourceRaw = sourceRaw.trimEnd()
this.lines = []
this.eof = sourceRaw.length
Expand All @@ -11,7 +11,7 @@ module.exports = class CovSource {
this._buildLines(sourceRaw)
}

_buildLines (source) {
_buildLines(source) {
let position = 0
let ignoreCount = 0
for (const [i, lineStr] of source.split(/(?<=\r?\n)/u).entries()) {
Expand All @@ -27,7 +27,7 @@ module.exports = class CovSource {
}
}

_parseIgnoreNext (lineStr, line) {
_parseIgnoreNext(lineStr, line) {
const testIgnoreNextLines = lineStr.match(/^\W*\/\* c8 ignore next (?<count>[0-9]+)? *\*\/\W*$/)
if (testIgnoreNextLines) {
line.ignore = true
Expand All @@ -47,53 +47,97 @@ module.exports = class CovSource {

// given a start column and end column in absolute offsets within
// a source file (0 - EOF), returns the relative line column positions.
offsetToOriginalRelative (sourceMap, startCol, endCol) {
offsetToOriginalRelative(sourceMap, startCol, endCol) {
const lines = this.lines.filter((line, i) => {
return startCol <= line.endCol && endCol >= line.startCol
})
if (!lines.length) return {}
if (!lines.length) {
console.log('unable to find lines?!')
return []
}

const start = originalPositionTryBoth(
// this range covers multiple files, so we need to split it up
let thisStartCol = startCol;
let thisStartLine = 0;
let thisEndCol = startCol + 1;
let thisEndLine = 0;
let lastEndLine = thisEndLine;
if (lines[thisEndLine].endCol <= thisEndCol) {
thisEndLine++;
}
let lastEnd;
let thisStart = originalPositionTryBoth(
sourceMap,
lines[0].line,
startCol - lines[0].startCol
)
let end = originalEndPositionFor(
sourceMap,
lines[lines.length - 1].line,
endCol - lines[lines.length - 1].startCol
)

if (!(start && end)) {
return {}
}

if (!(start.source && end.source)) {
return {}
}

if (start.source !== end.source) {
return {}
}

if (start.line === end.line && start.column === end.column) {
end = sourceMap.originalPositionFor({
line: lines[lines.length - 1].line,
column: endCol - lines[lines.length - 1].startCol,
bias: LEAST_UPPER_BOUND
})
end.column -= 1
);
let returnLocs = [];
while (thisEndCol <= endCol) {
const thisEnd = originalEndPositionFor(
sourceMap,
lines[thisEndLine].line,
thisEndCol - lines[thisEndLine].startCol
);

const isGoingForward = thisEnd && thisStart && thisEnd.line >= thisStart.line && (thisEnd.line > thisStart.line || thisEnd.column >= thisStart.column);
if (thisEnd && thisEnd.source === thisStart.source && thisEndCol !== endCol && isGoingForward) {
lastEnd = thisEnd;
} else {
// Give up if we werent previously valid to ignore non mapped blocks inside the region
if (lastEnd && thisStart.source) {

if (thisStart.line === lastEnd.line && thisStart.column === lastEnd.column) {
const potentialLastEnd = sourceMap.originalPositionFor({
line: lines[lastEndLine].line,
column: (thisEndCol - 1) - lines[lastEndLine].startCol,
bias: LEAST_UPPER_BOUND
})
potentialLastEnd.column -= 1
if (lastEnd.source === potentialLastEnd.source) {
lastEnd = potentialLastEnd
}
}

// ignore 0 length maps
//if (thisStart.line !== lastEnd.line && thisStart.column !== lastEnd.column) {
if (thisStart.source.indexOf('App.tsx') >= 0) {
console.log(thisStartCol + '(' + startCol + ')', ':', thisEndCol + '(' + endCol + ')',
{ l: lines[thisStartLine].line, c: thisStartCol - lines[thisStartLine].startCol },
{ l: lines[lastEndLine].line, c: (thisEndCol - 1) - lines[lastEndLine].startCol },
'->', thisStart.source, { l: thisStart.line, c: thisStart.column }, { l: lastEnd.line, c: lastEnd.column })
}
returnLocs.push({
sourceFile: thisStart.source,
startLine: thisStart.line,
relStartCol: thisStart.column,
endLine: lastEnd.line,
relEndCol: lastEnd.column
});
//}
//console.log('found range', returnLocs[returnLocs.length - 1]);
lastEnd = null;
} else {
//console.log('something broken', lastEnd, thisStart.source)
}
thisStartCol = thisEndCol;
thisStartLine = thisEndLine;
thisStart = originalPositionTryBoth(
sourceMap,
lines[thisEndLine].line,
Math.max(0, thisEndCol - lines[thisEndLine].startCol)
);
}
thisEndCol++;
lastEndLine = thisEndLine;
if (lines[thisEndLine].endCol < thisEndCol) {
thisEndLine++;
}
}

return {
startLine: start.line,
relStartCol: start.column,
endLine: end.line,
relEndCol: end.column
}
return returnLocs;
}

relativeToOffset (line, relCol) {
relativeToOffset(line, relCol) {
line = Math.max(line, 1)
if (this.lines[line - 1] === undefined) return this.eof
return Math.min(this.lines[line - 1].startCol + relCol, this.lines[line - 1].endCol)
Expand Down Expand Up @@ -121,7 +165,7 @@ module.exports = class CovSource {
* that generated range.
* 3. Find the _end_ location of that original range.
*/
function originalEndPositionFor (sourceMap, line, column) {
function originalEndPositionFor(sourceMap, line, column) {
// Given the generated location, find the original location of the mapping
// that corresponds to a range on the generated file that overlaps the
// generated file end location. Note however that this position on its
Expand Down Expand Up @@ -149,14 +193,14 @@ function originalEndPositionFor (sourceMap, line, column) {
bias: LEAST_UPPER_BOUND
})
if (
// If this is null, it means that we've hit the end of the file,
// so we can use Infinity as the end column.
// If this is null, it means that we've hit the end of the file,
// so we can use Infinity as the end column.
afterEndMapping.line === null ||
// If these don't match, it means that the call to
// 'generatedPositionFor' didn't find any other original mappings on
// the line we gave, so consider the binding to extend to infinity.
sourceMap.originalPositionFor(afterEndMapping).line !==
beforeEndMapping.line
// If these don't match, it means that the call to
// 'generatedPositionFor' didn't find any other original mappings on
// the line we gave, so consider the binding to extend to infinity.
sourceMap.originalPositionFor(afterEndMapping).line !==
beforeEndMapping.line
) {
return {
source: beforeEndMapping.source,
Expand All @@ -169,24 +213,29 @@ function originalEndPositionFor (sourceMap, line, column) {
return sourceMap.originalPositionFor(afterEndMapping)
}

function originalPositionTryBoth (sourceMap, line, column) {
const original = sourceMap.originalPositionFor({
function originalPositionTryBoth(sourceMap, line, column) {
let original = sourceMap.originalPositionFor({
line,
column,
bias: GREATEST_LOWER_BOUND
})
if (original.line === null) {
return sourceMap.originalPositionFor({
original = sourceMap.originalPositionFor({
line,
column,
bias: LEAST_UPPER_BOUND
})
} else {
return original
}

if (original && original.source && original.source.indexOf('tsx') > 0) {
// console.log({ line, column }, 'became', original)
}

return original

}

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

0 comments on commit c169da3

Please sign in to comment.