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