config-dependency.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /**
  2. * @fileoverview `ConfigDependency` class.
  3. *
  4. * `ConfigDependency` class expresses a loaded parser or plugin.
  5. *
  6. * If the parser or plugin was loaded successfully, it has `definition` property
  7. * and `filePath` property. Otherwise, it has `error` property.
  8. *
  9. * When `JSON.stringify()` converted a `ConfigDependency` object to a JSON, it
  10. * omits `definition` property.
  11. *
  12. * `ConfigArrayFactory` creates `ConfigDependency` objects when it loads parsers
  13. * or plugins.
  14. *
  15. * @author Toru Nagashima <https://github.com/mysticatea>
  16. */
  17. "use strict";
  18. const util = require("util");
  19. /**
  20. * The class is to store parsers or plugins.
  21. * This class hides the loaded object from `JSON.stringify()` and `console.log`.
  22. * @template T
  23. */
  24. class ConfigDependency {
  25. /**
  26. * Initialize this instance.
  27. * @param {Object} data The dependency data.
  28. * @param {T} [data.definition] The dependency if the loading succeeded.
  29. * @param {Error} [data.error] The error object if the loading failed.
  30. * @param {string} [data.filePath] The actual path to the dependency if the loading succeeded.
  31. * @param {string} data.id The ID of this dependency.
  32. * @param {string} data.importerName The name of the config file which loads this dependency.
  33. * @param {string} data.importerPath The path to the config file which loads this dependency.
  34. */
  35. constructor({
  36. definition = null,
  37. error = null,
  38. filePath = null,
  39. id,
  40. importerName,
  41. importerPath
  42. }) {
  43. /**
  44. * The loaded dependency if the loading succeeded.
  45. * @type {T|null}
  46. */
  47. this.definition = definition;
  48. /**
  49. * The error object if the loading failed.
  50. * @type {Error|null}
  51. */
  52. this.error = error;
  53. /**
  54. * The loaded dependency if the loading succeeded.
  55. * @type {string|null}
  56. */
  57. this.filePath = filePath;
  58. /**
  59. * The ID of this dependency.
  60. * @type {string}
  61. */
  62. this.id = id;
  63. /**
  64. * The name of the config file which loads this dependency.
  65. * @type {string}
  66. */
  67. this.importerName = importerName;
  68. /**
  69. * The path to the config file which loads this dependency.
  70. * @type {string}
  71. */
  72. this.importerPath = importerPath;
  73. }
  74. // eslint-disable-next-line jsdoc/require-description
  75. /**
  76. * @returns {Object} a JSON compatible object.
  77. */
  78. toJSON() {
  79. const obj = this[util.inspect.custom]();
  80. // Display `error.message` (`Error#message` is unenumerable).
  81. if (obj.error instanceof Error) {
  82. obj.error = { ...obj.error, message: obj.error.message };
  83. }
  84. return obj;
  85. }
  86. // eslint-disable-next-line jsdoc/require-description
  87. /**
  88. * @returns {Object} an object to display by `console.log()`.
  89. */
  90. [util.inspect.custom]() {
  91. const {
  92. definition: _ignore, // eslint-disable-line no-unused-vars
  93. ...obj
  94. } = this;
  95. return obj;
  96. }
  97. }
  98. /** @typedef {ConfigDependency<import("../../shared/types").Parser>} DependentParser */
  99. /** @typedef {ConfigDependency<import("../../shared/types").Plugin>} DependentPlugin */
  100. module.exports = { ConfigDependency };