• 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 `ExtractedConfig` class.
15 *
16 * `ExtractedConfig` class expresses a final configuration for a specific file.
17 *
18 * It provides one method.
19 *
20 * - `toCompatibleObjectAsConfigFileContent()`
21 *      Convert this configuration to the compatible object as the content of
22 *      config files. It converts the loaded parser and plugins to strings.
23 *      `CLIEngine#getConfigForFile(filePath)` method uses this method.
24 *
25 * `ConfigArray#extractConfig(filePath)` creates a `ExtractedConfig` instance.
26 *
27 * @author Toru Nagashima <https://github.com/mysticatea>
28 */
29"use strict";
30
31const { IgnorePattern } = require("./ignore-pattern");
32
33// For VSCode intellisense
34/** @typedef {import("../../shared/types").ConfigData} ConfigData */
35/** @typedef {import("../../shared/types").GlobalConf} GlobalConf */
36/** @typedef {import("../../shared/types").SeverityConf} SeverityConf */
37/** @typedef {import("./config-dependency").DependentParser} DependentParser */
38/** @typedef {import("./config-dependency").DependentPlugin} DependentPlugin */
39
40/**
41 * Check if `xs` starts with `ys`.
42 * @template T
43 * @param {T[]} xs The array to check.
44 * @param {T[]} ys The array that may be the first part of `xs`.
45 * @returns {boolean} `true` if `xs` starts with `ys`.
46 */
47function startsWith(xs, ys) {
48    return xs.length >= ys.length && ys.every((y, i) => y === xs[i]);
49}
50
51/**
52 * The class for extracted config data.
53 */
54class ExtractedConfig {
55    constructor() {
56
57        /**
58         * The config name what `noInlineConfig` setting came from.
59         * @type {string}
60         */
61        this.configNameOfNoInlineConfig = "";
62
63        /**
64         * Environments.
65         * @type {Record<string, boolean>}
66         */
67        this.env = {};
68
69        /**
70         * Global variables.
71         * @type {Record<string, GlobalConf>}
72         */
73        this.globals = {};
74
75        /**
76         * The glob patterns that ignore to lint.
77         * @type {(((filePath:string, dot?:boolean) => boolean) & { basePath:string; patterns:string[] }) | undefined}
78         */
79        this.ignores = void 0;
80
81        /**
82         * The flag that disables directive comments.
83         * @type {boolean|undefined}
84         */
85        this.noInlineConfig = void 0;
86
87        /**
88         * Parser definition.
89         * @type {DependentParser|null}
90         */
91        this.parser = null;
92
93        /**
94         * Options for the parser.
95         * @type {Object}
96         */
97        this.parserOptions = {};
98
99        /**
100         * Plugin definitions.
101         * @type {Record<string, DependentPlugin>}
102         */
103        this.plugins = {};
104
105        /**
106         * Processor ID.
107         * @type {string|null}
108         */
109        this.processor = null;
110
111        /**
112         * The flag that reports unused `eslint-disable` directive comments.
113         * @type {boolean|undefined}
114         */
115        this.reportUnusedDisableDirectives = void 0;
116
117        /**
118         * Rule settings.
119         * @type {Record<string, [SeverityConf, ...any[]]>}
120         */
121        this.rules = {};
122
123        /**
124         * Shared settings.
125         * @type {Object}
126         */
127        this.settings = {};
128    }
129
130    /**
131     * Convert this config to the compatible object as a config file content.
132     * @returns {ConfigData} The converted object.
133     */
134    toCompatibleObjectAsConfigFileContent() {
135        const {
136            /* eslint-disable no-unused-vars */
137            configNameOfNoInlineConfig: _ignore1,
138            processor: _ignore2,
139            /* eslint-enable no-unused-vars */
140            ignores,
141            ...config
142        } = this;
143
144        config.parser = config.parser && config.parser.filePath;
145        config.plugins = Object.keys(config.plugins).filter(Boolean).reverse();
146        config.ignorePatterns = ignores ? ignores.patterns : [];
147
148        // Strip the default patterns from `ignorePatterns`.
149        if (startsWith(config.ignorePatterns, IgnorePattern.DefaultPatterns)) {
150            config.ignorePatterns =
151                config.ignorePatterns.slice(IgnorePattern.DefaultPatterns.length);
152        }
153
154        return config;
155    }
156}
157
158module.exports = { ExtractedConfig };
159