config-dependency.js 3.6 KB

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