Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Serialize parsers/processors in flat config #16944

Merged
merged 3 commits into from Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
61 changes: 56 additions & 5 deletions lib/config/flat-config-array.js
Expand Up @@ -36,6 +36,41 @@ 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;
nzakas marked this conversation as resolved.
Show resolved Hide resolved
}

// 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 @@ -145,16 +180,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 @@ -172,6 +216,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 @@ -185,11 +236,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