ava/lib/extensions.js
Mark Wubben e8a497696a
Update all the things
* Test lockfile using npm 7.23

* Workflow updates

* Update codecov action
* Cache npm dependencies
* Simplify checkout

* Bump minimal Node.js versions

These should allow us to add features over AVA 4's lifetime using newly
available Node.js APIs.

* Update dev dependencies

* Update to TypeScript 4.4 and latest tsd

* Upgrade XO and reformat

* Update dependencies

* Rebuild lockfile

* Run XO with Node.js 16

It fails on older Node.js versions.
2021-09-12 22:00:38 +02:00

46 lines
1.1 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

export default function resolveExtensions(configuredExtensions, providers = []) {
// Combine all extensions possible for testing. Remove duplicate extensions.
const duplicates = new Set();
const seen = new Set();
const normalize = extensions => Array.isArray(extensions) ? extensions : Object.keys(extensions);
const combine = extensions => {
for (const ext of normalize(extensions)) {
if (seen.has(ext)) {
duplicates.add(ext);
} else {
seen.add(ext);
}
}
};
if (configuredExtensions !== undefined) {
combine(configuredExtensions);
}
for (const {main} of providers) {
combine(main.extensions);
}
if (duplicates.size > 0) {
throw new Error(`Unexpected duplicate extensions in options: ’${[...duplicates].join('’, ’')}’.`);
}
// Unless the default was used by providers, as long as the extensions aren't explicitly set, set the default.
if (configuredExtensions === undefined) {
if (!seen.has('cjs')) {
seen.add('cjs');
}
if (!seen.has('mjs')) {
seen.add('mjs');
}
if (!seen.has('js')) {
seen.add('js');
}
}
return [...seen];
}