Skip to content

Make line number selection work with @ava/babel & @ava/typescript #2473

Closed
@novemberborn

Description

@novemberborn

Line number selection relies on AVA parsing the test file. This does not work when you use a require hook (such as ts-node/register). That's OK. However we do want it to work with our Babel and TypeScript providers.

Ultimately, we need start and end lines & columns for each call expression in the test file. First we need to initialize the providers earlier:

// TODO: Initialize providers here, then pass to lineNumberSelection() so they
// can be used to parse the test file.
let checkSelectedByLineNumbers;
try {
checkSelectedByLineNumbers = lineNumberSelection({
file: options.file,
lineNumbers: options.lineNumbers
});

See here:

const extensionsToLoadAsModules = Object.entries(options.moduleTypes)
.filter(([, type]) => type === 'module')
.map(([extension]) => extension);
// Install before processing options.require, so if helpers are added to the
// require configuration the *compiled* helper will be loaded.
const {projectDir, providerStates = []} = options;
const providers = providerStates.map(({type, state}) => {
if (type === 'babel') {
const provider = providerManager.babel(projectDir).worker({extensionsToLoadAsModules, state});
runner.powerAssert = provider.powerAssert;
return provider;
}
if (type === 'typescript') {
return providerManager.typescript(projectDir).worker({extensionsToLoadAsModules, state});
}
return null;
}).filter(provider => provider !== null);

Then, depending on the extension of the test file, we need to get the provider to give us the call locations. For normal JS files you can find that logic here:

function parse(file) {
const fs = require('fs');
const acorn = require('acorn');
const walk = require('acorn-walk');
const ast = acorn.parse(fs.readFileSync(file, 'utf8'), {
ecmaVersion: 11,
locations: true
});
const locations = [];
walk.simple(ast, {
CallExpression(node) {
locations.push(node.loc);
}
});
// Walking is depth-first, but we want to sort these breadth-first.
locations.sort((a, b) => {
if (a.start.line === b.start.line) {
return a.start.column - b.start.column;
}
return a.start.line - b.start.line;
});
return locations;
}

For @ava/babel we need to parse the test file using Babel, with all the configured plugins active and whatnot.

For @ava/typescript, hopefully we can use typescript itself as a parser?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions