extensions.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. module.exports = (enhancementsOnly, babelConfig) => {
  2. const {extensions: full = []} = babelConfig || {};
  3. // Combine all extensions possible for testing. Remove duplicate extensions.
  4. const duplicates = [];
  5. const seen = new Set();
  6. for (const ext of [...enhancementsOnly, ...full]) {
  7. if (seen.has(ext)) {
  8. duplicates.push(ext);
  9. } else {
  10. seen.add(ext);
  11. }
  12. }
  13. // Decide if and where to add the default `js` extension. Keep in mind it's not
  14. // added if extensions have been explicitly given.
  15. if (!seen.has('js')) {
  16. if (babelConfig && full.length === 0) {
  17. seen.add('js');
  18. full.push('js');
  19. }
  20. if (!babelConfig && enhancementsOnly.length === 0) {
  21. seen.add('js');
  22. enhancementsOnly.push('js');
  23. }
  24. } else if (babelConfig && full.length === 0) {
  25. // If Babel is not disabled, and has the default extensions (or, explicitly,
  26. // no configured extensions), thes the `js` extension must have come from
  27. // the `enhancementsOnly` value. That's not allowed since it'd be a
  28. // roundabout way of disabling Babel.
  29. throw new Error(`Cannot specify generic 'js' extension without disabling AVA's Babel usage.`);
  30. }
  31. if (duplicates.length > 0) {
  32. throw new Error(`Unexpected duplicate extensions in options: '${duplicates.join('\', \'')}'.`);
  33. }
  34. const all = [...seen];
  35. return {all, enhancementsOnly, full};
  36. };