Upgrade Ava to v4
This commit is contained in:
parent
9a40cc5274
commit
ce89f1b611
1153 changed files with 27264 additions and 95308 deletions
78
node_modules/ava/lib/worker/line-numbers.js
generated
vendored
78
node_modules/ava/lib/worker/line-numbers.js
generated
vendored
|
|
@ -1,18 +1,27 @@
|
|||
import * as fs from 'node:fs';
|
||||
import {createRequire, findSourceMap} from 'node:module';
|
||||
import {pathToFileURL} from 'node:url';
|
||||
|
||||
import callsites from 'callsites';
|
||||
|
||||
const require = createRequire(import.meta.url);
|
||||
|
||||
function parse(file) {
|
||||
const fs = require('fs');
|
||||
// Avoid loading these until we actually need to select tests by line number.
|
||||
const acorn = require('acorn');
|
||||
const walk = require('acorn-walk');
|
||||
|
||||
const ast = acorn.parse(fs.readFileSync(file, 'utf8'), {
|
||||
ecmaVersion: 11,
|
||||
locations: true
|
||||
locations: true,
|
||||
sourceType: 'module',
|
||||
});
|
||||
|
||||
const locations = [];
|
||||
walk.simple(ast, {
|
||||
CallExpression(node) {
|
||||
locations.push(node.loc);
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
// Walking is depth-first, but we want to sort these breadth-first.
|
||||
|
|
@ -49,36 +58,65 @@ function findTest(locations, declaration) {
|
|||
return spans.pop();
|
||||
}
|
||||
|
||||
const range = (start, end) => new Array(end - start + 1).fill(start).map((element, index) => element + index);
|
||||
const range = (start, end) => Array.from({length: end - start + 1}).fill(start).map((element, index) => element + index);
|
||||
|
||||
module.exports = ({file, lineNumbers = []}) => {
|
||||
const translate = (sourceMap, pos) => {
|
||||
if (sourceMap === undefined) {
|
||||
return pos;
|
||||
}
|
||||
|
||||
const entry = sourceMap.findEntry(pos.line - 1, pos.column); // Source maps are 0-based
|
||||
return {
|
||||
line: entry.originalLine + 1, // Readjust for Acorn.
|
||||
column: entry.originalColumn,
|
||||
};
|
||||
};
|
||||
|
||||
export default function lineNumberSelection({file, lineNumbers = []}) {
|
||||
if (lineNumbers.length === 0) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// Avoid loading these until we actually need to select tests by line number.
|
||||
const callsites = require('callsites');
|
||||
const sourceMapSupport = require('source-map-support');
|
||||
|
||||
const locations = parse(file);
|
||||
const selected = new Set(lineNumbers);
|
||||
|
||||
let locations = parse(file);
|
||||
let lookedForSourceMap = false;
|
||||
let sourceMap;
|
||||
|
||||
return () => {
|
||||
if (!lookedForSourceMap) {
|
||||
lookedForSourceMap = true;
|
||||
|
||||
// The returned function is called *after* the file has been loaded.
|
||||
// Source maps are not available before then.
|
||||
sourceMap = findSourceMap(file);
|
||||
|
||||
if (sourceMap !== undefined) {
|
||||
locations = locations.map(({start, end}) => ({
|
||||
start: translate(sourceMap, start),
|
||||
end: translate(sourceMap, end),
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
// Assume this is called from a test declaration, which is located in the file.
|
||||
// If not… don't select the test!
|
||||
const callSite = callsites().find(callSite => callSite.getFileName() === file);
|
||||
const callSite = callsites().find(callSite => {
|
||||
const current = callSite.getFileName();
|
||||
if (file.startsWith('file://')) {
|
||||
return current.startsWith('file://') ? file === current : file === pathToFileURL(current).toString();
|
||||
}
|
||||
|
||||
return current.startsWith('file://') ? pathToFileURL(file).toString() === current : file === current;
|
||||
});
|
||||
if (!callSite) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// FIXME: This assumes the callSite hasn't already been adjusted. It's likely
|
||||
// that if `source-map-support/register` has been loaded, this would result
|
||||
// in the wrong location.
|
||||
const sourceCallSite = sourceMapSupport.wrapCallSite(callSite);
|
||||
const start = {
|
||||
line: sourceCallSite.getLineNumber(),
|
||||
column: sourceCallSite.getColumnNumber() - 1 // Use 0-indexed columns.
|
||||
};
|
||||
const start = translate(sourceMap, {
|
||||
line: callSite.getLineNumber(), // 1-based
|
||||
column: callSite.getColumnNumber() - 1, // Comes out as 1-based, Acorn wants 0-based
|
||||
});
|
||||
|
||||
const test = findTest(locations, start);
|
||||
if (!test) {
|
||||
|
|
@ -87,4 +125,4 @@ module.exports = ({file, lineNumbers = []}) => {
|
|||
|
||||
return range(test.start.line, test.end.line).some(line => selected.has(line));
|
||||
};
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue