Skip to content

Commit f5f5e11

Browse files
authored
feat: Serialize parsers/processors in flat config (#16944)
* feat: Serialize parsers/processors in flat config This implements a check for a 'meta' key on parsers and processors that contains information to help flat config serialize these objects for use with caching and also --print-config on the command line. Fixes #16875 * Add support for non-meta properties * Fix edge case
1 parent caf08ce commit f5f5e11

2 files changed

Lines changed: 365 additions & 9 deletions

File tree

lib/config/flat-config-array.js

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,45 @@ function splitPluginIdentifier(identifier) {
3636
};
3737
}
3838

39+
/**
40+
* Returns the name of an object in the config by reading its `meta` key.
41+
* @param {Object} object The object to check.
42+
* @returns {string?} The name of the object if found or `null` if there
43+
* is no name.
44+
*/
45+
function getObjectId(object) {
46+
47+
// first check old-style name
48+
let name = object.name;
49+
50+
if (!name) {
51+
52+
if (!object.meta) {
53+
return null;
54+
}
55+
56+
name = object.meta.name;
57+
58+
if (!name) {
59+
return null;
60+
}
61+
}
62+
63+
// now check for old-style version
64+
let version = object.version;
65+
66+
if (!version) {
67+
version = object.meta && object.meta.version;
68+
}
69+
70+
// if there's a version then append that
71+
if (version) {
72+
return `${name}@${version}`;
73+
}
74+
75+
return name;
76+
}
77+
3978
const originalBaseConfig = Symbol("originalBaseConfig");
4079

4180
//-----------------------------------------------------------------------------
@@ -151,16 +190,25 @@ class FlatConfigArray extends ConfigArray {
151190

152191
// Check parser value
153192
if (languageOptions && languageOptions.parser) {
154-
if (typeof languageOptions.parser === "string") {
155-
const { pluginName, objectName: localParserName } = splitPluginIdentifier(languageOptions.parser);
193+
const { parser } = languageOptions;
194+
195+
if (typeof parser === "string") {
196+
const { pluginName, objectName: localParserName } = splitPluginIdentifier(parser);
156197

157-
parserName = languageOptions.parser;
198+
parserName = parser;
158199

159200
if (!plugins || !plugins[pluginName] || !plugins[pluginName].parsers || !plugins[pluginName].parsers[localParserName]) {
160201
throw new TypeError(`Key "parser": Could not find "${localParserName}" in plugin "${pluginName}".`);
161202
}
162203

163204
languageOptions.parser = plugins[pluginName].parsers[localParserName];
205+
} else if (typeof parser === "object") {
206+
parserName = getObjectId(parser);
207+
208+
if (!parserName) {
209+
invalidParser = true;
210+
}
211+
164212
} else {
165213
invalidParser = true;
166214
}
@@ -178,6 +226,13 @@ class FlatConfigArray extends ConfigArray {
178226
}
179227

180228
config.processor = plugins[pluginName].processors[localProcessorName];
229+
} else if (typeof processor === "object") {
230+
processorName = getObjectId(processor);
231+
232+
if (!processorName) {
233+
invalidProcessor = true;
234+
}
235+
181236
} else {
182237
invalidProcessor = true;
183238
}
@@ -191,11 +246,11 @@ class FlatConfigArray extends ConfigArray {
191246
value: function() {
192247

193248
if (invalidParser) {
194-
throw new Error("Caching is not supported when parser is an object.");
249+
throw new Error("Could not serialize parser object (missing 'meta' object).");
195250
}
196251

197252
if (invalidProcessor) {
198-
throw new Error("Caching is not supported when processor is an object.");
253+
throw new Error("Could not serialize processor object (missing 'meta' object).");
199254
}
200255

201256
return {

0 commit comments

Comments
 (0)