.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / @eslint / eslintrc / lib / config-array / config-dependency.js
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
19 const util = require("util");
20
21 /**
22  * The class is to store parsers or plugins.
23  * This class hides the loaded object from `JSON.stringify()` and `console.log`.
24  * @template T
25  */
26 class ConfigDependency {
27
28     /**
29      * Initialize this instance.
30      * @param {Object} data The dependency data.
31      * @param {T} [data.definition] The dependency if the loading succeeded.
32      * @param {Error} [data.error] The error object if the loading failed.
33      * @param {string} [data.filePath] The actual path to the dependency if the loading succeeded.
34      * @param {string} data.id The ID of this dependency.
35      * @param {string} data.importerName The name of the config file which loads this dependency.
36      * @param {string} data.importerPath The path to the config file which loads this dependency.
37      */
38     constructor({
39         definition = null,
40         error = null,
41         filePath = null,
42         id,
43         importerName,
44         importerPath
45     }) {
46
47         /**
48          * The loaded dependency if the loading succeeded.
49          * @type {T|null}
50          */
51         this.definition = definition;
52
53         /**
54          * The error object if the loading failed.
55          * @type {Error|null}
56          */
57         this.error = error;
58
59         /**
60          * The loaded dependency if the loading succeeded.
61          * @type {string|null}
62          */
63         this.filePath = filePath;
64
65         /**
66          * The ID of this dependency.
67          * @type {string}
68          */
69         this.id = id;
70
71         /**
72          * The name of the config file which loads this dependency.
73          * @type {string}
74          */
75         this.importerName = importerName;
76
77         /**
78          * The path to the config file which loads this dependency.
79          * @type {string}
80          */
81         this.importerPath = importerPath;
82     }
83
84     // eslint-disable-next-line jsdoc/require-description
85     /**
86      * @returns {Object} a JSON compatible object.
87      */
88     toJSON() {
89         const obj = this[util.inspect.custom]();
90
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
96         return obj;
97     }
98
99     // eslint-disable-next-line jsdoc/require-description
100     /**
101      * @returns {Object} an object to display by `console.log()`.
102      */
103     [util.inspect.custom]() {
104         const {
105             definition: _ignore, // eslint-disable-line no-unused-vars
106             ...obj
107         } = this;
108
109         return obj;
110     }
111 }
112
113 /** @typedef {ConfigDependency<import("../../shared/types").Parser>} DependentParser */
114 /** @typedef {ConfigDependency<import("../../shared/types").Plugin>} DependentPlugin */
115
116 module.exports = { ConfigDependency };