All files / lib/internal parsePluginOptions.js

67.31% Statements 70/104
40% Branches 2/5
100% Functions 1/1
67.31% Lines 70/104

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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 1051x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x       3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x                     3x                                           3x 3x  
"use strict";
 
const assert = require("assert");
 
/**
 * @typedef {object} ModuleConfig
 * @property {string} name
 * @property {string} [indexFile]
 */
 
/**
 * @typedef {object} PluginOptions
 * @property {ModuleConfig[]} modules
 */
 
/**
 * @param {import("@babel/core").PluginOptions} pluginOptions
 * @returns {PluginOptions}
 */
module.exports = function parsePluginOptions(pluginOptions) {
  assert.ok(
    Object.prototype.toString.call(pluginOptions) === "[object Object]",
    "invalid 'options': not an 'object'"
  );
 
  const { modules, ...unknownPluginOptions } =
    /** @type {{ modules?: unknown }} */ (pluginOptions);
 
  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(
      ", "
    )})`
  );
 
  return {
    modules: modules.map((moduleConfig, idx) => {
      const errorPath = `options.modules[${idx}]`;
 
      if (typeof moduleConfig == "string") {
        assert.ok(
          !!moduleConfig.length,
          `invalid '${errorPath}': value is empty`
        );
 
        return { name: moduleConfig };
      }

      assert.ok(
        typeof moduleConfig == "object" && !!moduleConfig,
        `invalid '${errorPath}': not a 'string' or an 'object'`
      );
 
      const { name, indexFile, ...unknownModuleConfigProperties } =
        /** @type {ModuleConfig} */ (moduleConfig);
 
      assert.ok(
        typeof name == "string",
        `invalid '${errorPath}.name': not a 'string'`
      );
      assert.ok(!!name, `invalid '${errorPath}.name': value is empty`);
 
      if (indexFile != null) {
        assert.ok(
          typeof indexFile == "string",
          `invalid '${errorPath}.indexFile': not a 'string'`
        );
        assert.ok(
          !!indexFile,
          `invalid '${errorPath}.indexFile': value is empty`
        );
      }

      if (indexFile != null && indexFile !== name) {
        const [nameID] = name.split("/");
        const [indexFileID] = indexFile.split("/");

        assert.ok(
          nameID === indexFileID,
          `invalid '${errorPath}': '${indexFile}' not belong to '${name}' package`
        );
      }

      const unknownModuleConfigPropertyNames = Object.keys(
        unknownModuleConfigProperties
      );
      assert.ok(
        !unknownModuleConfigPropertyNames.length,
        `invalid '${errorPath}': has unknown properties ${unknownModuleConfigPropertyNames.join(
          ", "
        )}`
      );

      return { name, indexFile };
    }),
  };
};