Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @fileoverview Flat Config Array |
| 3 | * @author Nicholas C. Zakas |
| 4 | */ |
| 5 | |
| 6 | "use strict"; |
| 7 | |
| 8 | //----------------------------------------------------------------------------- |
| 9 | // Requirements |
| 10 | //----------------------------------------------------------------------------- |
| 11 | |
| 12 | const { ConfigArray, ConfigArraySymbol } = require("@humanwhocodes/config-array"); |
| 13 | const { flatConfigSchema } = require("./flat-config-schema"); |
| 14 | const { RuleValidator } = require("./rule-validator"); |
| 15 | const { defaultConfig } = require("./default-config"); |
| 16 | const recommendedConfig = require("../../conf/eslint-recommended"); |
| 17 | const allConfig = require("../../conf/eslint-all"); |
| 18 | |
| 19 | //----------------------------------------------------------------------------- |
| 20 | // Helpers |
| 21 | //----------------------------------------------------------------------------- |
| 22 | |
| 23 | const 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 | */ |
| 31 | function 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 | */ |
| 47 | class 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 Lippe | 0ceb465 | 2022-01-06 14:23:36 +0100 | [diff] [blame^] | 55 | constructor(configs, { basePath, baseConfig = defaultConfig } = {}) { |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 56 | super(configs, { |
| 57 | basePath, |
| 58 | schema: flatConfigSchema |
| 59 | }); |
| 60 | |
Tim van der Lippe | 0ceb465 | 2022-01-06 14:23:36 +0100 | [diff] [blame^] | 61 | this.unshift(...baseConfig); |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 62 | } |
| 63 | |
Tim van der Lippe | 0fb4780 | 2021-11-08 16:23:10 +0000 | [diff] [blame] | 64 | /* eslint-disable class-methods-use-this -- Desired as instance method */ |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 65 | /** |
| 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 Lippe | 0fb4780 | 2021-11-08 16:23:10 +0000 | [diff] [blame] | 121 | /* eslint-enable class-methods-use-this -- Desired as instance method */ |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 122 | |
| 123 | } |
| 124 | |
| 125 | exports.FlatConfigArray = FlatConfigArray; |