blob: c06fd92a383809cf8faacd980c83f1c63b639796 [file] [log] [blame]
Tim van der Lippe2c891972021-07-29 16:22:50 +01001/**
2 * @fileoverview Flat Config Array
3 * @author Nicholas C. Zakas
4 */
5
6"use strict";
7
8//-----------------------------------------------------------------------------
9// Requirements
10//-----------------------------------------------------------------------------
11
12const { ConfigArray, ConfigArraySymbol } = require("@humanwhocodes/config-array");
13const { flatConfigSchema } = require("./flat-config-schema");
14const { RuleValidator } = require("./rule-validator");
15const { defaultConfig } = require("./default-config");
16const recommendedConfig = require("../../conf/eslint-recommended");
17const allConfig = require("../../conf/eslint-all");
18
19//-----------------------------------------------------------------------------
20// Helpers
21//-----------------------------------------------------------------------------
22
23const ruleValidator = new RuleValidator();
24
25/**
26 * Splits a plugin identifier in the form a/b/c into two parts: a/b and c.
27 * @param {string} identifier The identifier to parse.
28 * @returns {{objectName: string, pluginName: string}} The parts of the plugin
29 * name.
30 */
31function splitPluginIdentifier(identifier) {
32 const parts = identifier.split("/");
33
34 return {
35 objectName: parts.pop(),
36 pluginName: parts.join("/")
37 };
38}
39
40//-----------------------------------------------------------------------------
41// Exports
42//-----------------------------------------------------------------------------
43
44/**
45 * Represents an array containing configuration information for ESLint.
46 */
47class FlatConfigArray extends ConfigArray {
48
49 /**
50 * Creates a new instance.
51 * @param {*[]} configs An array of configuration information.
52 * @param {{basePath: string, baseConfig: FlatConfig}} options The options
53 * to use for the config array instance.
54 */
Tim van der Lippe0ceb4652022-01-06 14:23:36 +010055 constructor(configs, { basePath, baseConfig = defaultConfig } = {}) {
Tim van der Lippe2c891972021-07-29 16:22:50 +010056 super(configs, {
57 basePath,
58 schema: flatConfigSchema
59 });
60
Tim van der Lippe0ceb4652022-01-06 14:23:36 +010061 this.unshift(...baseConfig);
Tim van der Lippe2c891972021-07-29 16:22:50 +010062 }
63
Tim van der Lippe0fb47802021-11-08 16:23:10 +000064 /* eslint-disable class-methods-use-this -- Desired as instance method */
Tim van der Lippe2c891972021-07-29 16:22:50 +010065 /**
66 * Replaces a config with another config to allow us to put strings
67 * in the config array that will be replaced by objects before
68 * normalization.
69 * @param {Object} config The config to preprocess.
70 * @returns {Object} The preprocessed config.
71 */
72 [ConfigArraySymbol.preprocessConfig](config) {
73 if (config === "eslint:recommended") {
74 return recommendedConfig;
75 }
76
77 if (config === "eslint:all") {
78 return allConfig;
79 }
80
81 return config;
82 }
83
84 /**
85 * Finalizes the config by replacing plugin references with their objects
86 * and validating rule option schemas.
87 * @param {Object} config The config to finalize.
88 * @returns {Object} The finalized config.
89 * @throws {TypeError} If the config is invalid.
90 */
91 [ConfigArraySymbol.finalizeConfig](config) {
92
93 const { plugins, languageOptions, processor } = config;
94
95 // Check parser value
96 if (languageOptions && languageOptions.parser && typeof languageOptions.parser === "string") {
97 const { pluginName, objectName: parserName } = splitPluginIdentifier(languageOptions.parser);
98
99 if (!plugins || !plugins[pluginName] || !plugins[pluginName].parsers || !plugins[pluginName].parsers[parserName]) {
100 throw new TypeError(`Key "parser": Could not find "${parserName}" in plugin "${pluginName}".`);
101 }
102
103 languageOptions.parser = plugins[pluginName].parsers[parserName];
104 }
105
106 // Check processor value
107 if (processor && typeof processor === "string") {
108 const { pluginName, objectName: processorName } = splitPluginIdentifier(processor);
109
110 if (!plugins || !plugins[pluginName] || !plugins[pluginName].processors || !plugins[pluginName].processors[processorName]) {
111 throw new TypeError(`Key "processor": Could not find "${processorName}" in plugin "${pluginName}".`);
112 }
113
114 config.processor = plugins[pluginName].processors[processorName];
115 }
116
117 ruleValidator.validate(config);
118
119 return config;
120 }
Tim van der Lippe0fb47802021-11-08 16:23:10 +0000121 /* eslint-enable class-methods-use-this -- Desired as instance method */
Tim van der Lippe2c891972021-07-29 16:22:50 +0100122
123}
124
125exports.FlatConfigArray = FlatConfigArray;