Skip to content

Commit

Permalink
feat: Serialize parsers/processors in flat config (#16944)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
nzakas committed Mar 8, 2023
1 parent caf08ce commit f5f5e11
Show file tree
Hide file tree
Showing 2 changed files with 365 additions and 9 deletions.
65 changes: 60 additions & 5 deletions lib/config/flat-config-array.js
Expand Up @@ -36,6 +36,45 @@ function splitPluginIdentifier(identifier) {
};
}

/**
* Returns the name of an object in the config by reading its `meta` key.
* @param {Object} object The object to check.
* @returns {string?} The name of the object if found or `null` if there
* is no name.
*/
function getObjectId(object) {

// first check old-style name
let name = object.name;

if (!name) {

if (!object.meta) {
return null;
}

name = object.meta.name;

if (!name) {
return null;
}
}

// now check for old-style version
let version = object.version;

if (!version) {
version = object.meta && object.meta.version;
}

// if there's a version then append that
if (version) {
return `${name}@${version}`;
}

return name;
}

const originalBaseConfig = Symbol("originalBaseConfig");

//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -151,16 +190,25 @@ class FlatConfigArray extends ConfigArray {

// Check parser value
if (languageOptions && languageOptions.parser) {
if (typeof languageOptions.parser === "string") {
const { pluginName, objectName: localParserName } = splitPluginIdentifier(languageOptions.parser);
const { parser } = languageOptions;

if (typeof parser === "string") {
const { pluginName, objectName: localParserName } = splitPluginIdentifier(parser);

parserName = languageOptions.parser;
parserName = parser;

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

languageOptions.parser = plugins[pluginName].parsers[localParserName];
} else if (typeof parser === "object") {
parserName = getObjectId(parser);

if (!parserName) {
invalidParser = true;
}

} else {
invalidParser = true;
}
Expand All @@ -178,6 +226,13 @@ class FlatConfigArray extends ConfigArray {
}

config.processor = plugins[pluginName].processors[localProcessorName];
} else if (typeof processor === "object") {
processorName = getObjectId(processor);

if (!processorName) {
invalidProcessor = true;
}

} else {
invalidProcessor = true;
}
Expand All @@ -191,11 +246,11 @@ class FlatConfigArray extends ConfigArray {
value: function() {

if (invalidParser) {
throw new Error("Caching is not supported when parser is an object.");
throw new Error("Could not serialize parser object (missing 'meta' object).");
}

if (invalidProcessor) {
throw new Error("Caching is not supported when processor is an object.");
throw new Error("Could not serialize processor object (missing 'meta' object).");
}

return {
Expand Down

0 comments on commit f5f5e11

Please sign in to comment.