All files / lib/internal PluginOptions.js

100% Statements 21/21
100% Branches 2/2
100% Functions 3/3
100% Lines 20/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66    8x 8x     8x         45x   42x 42x     22x   22x       17x   16x   16x             15x     19x   19x       18x   16x         12x   12x               12x       8x  
"use strict";
 
const assert = require("assert");
const NodeModule = require("./NodeModule");
 
/** @type {WeakMap<object, PluginOptions>} */
const pluginOptionsCache = new WeakMap();
 
class PluginOptions {
  /** @param {import("@babel/core").PluginOptions} opts */
  static parse(opts) {
    assert.ok(!!opts, "invalid 'options': not an 'object'");
 
    const cached = pluginOptionsCache.get(opts);
    if (cached) return cached;
 
    const { modules, ...unknownPluginOptions } =
      /** @type {{ modules?: unknown }} */ (opts);
 
    assert.ok(
      Array.isArray(modules),
      "invalid 'options.modules': not an 'array'"
    );
    assert.ok(!!modules.length, "invalid 'options.modules': value is empty");
 
    const unknownPluginOptionKeys = Object.keys(unknownPluginOptions);
 
    assert.ok(
      !unknownPluginOptionKeys.length,
      `invalid 'options': has unknown properties (${unknownPluginOptionKeys.join(
        ", "
      )})`
    );
 
    const pluginOptions = new PluginOptions(
      new Map(
        modules.map((id, idx) => {
          const optionPath = `options.modules[${idx}]`;
 
          assert.ok(
            typeof id == "string",
            `invalid '${optionPath}': not a 'string'`
          );
          assert.ok(!!id.length, `invalid '${optionPath}': value is empty`);
 
          return [id, NodeModule.get(id)];
        })
      )
    );
 
    pluginOptionsCache.set(opts, pluginOptions);
 
    return pluginOptions;
  }
 
  /**
   * @param {Map<string, NodeModule>} modules
   * @protected
   */
  constructor(modules) {
    this.modules = modules;
  }
}
 
module.exports = PluginOptions;