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