Skip to content

Commit

Permalink
fix: Allow specifying arbitrarily named processors in configs
Browse files Browse the repository at this point in the history
  • Loading branch information
matwilko committed Jul 17, 2023
1 parent dee2c45 commit e41d4ea
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
16 changes: 3 additions & 13 deletions lib/config/flat-config-schema.js
Expand Up @@ -188,18 +188,6 @@ function assertIsRuleSeverity(ruleId, value) {
}
}

/**
* Validates that a given string is the form pluginName/objectName.
* @param {string} value The string to check.
* @returns {void}
* @throws {TypeError} If the string isn't in the correct format.
*/
function assertIsPluginMemberName(value) {
if (!/[@a-z0-9-_$]+(?:\/(?:[a-z0-9-_$]+))+$/iu.test(value)) {
throw new TypeError(`Expected string in the form "pluginName/objectName" but found "${value}".`);
}
}

/**
* Validates that a value is an object.
* @param {any} value The value to check.
Expand Down Expand Up @@ -323,7 +311,9 @@ const processorSchema = {
merge: "replace",
validate(value) {
if (typeof value === "string") {
assertIsPluginMemberName(value);
if (!value.includes("/")) {
throw new TypeError(`Expected string in the form "pluginName/processorName" but found "${value}".`);
}
} else if (value && typeof value === "object") {
if (typeof value.preprocess !== "function" || typeof value.postprocess !== "function") {
throw new TypeError("Object must have a preprocess() and a postprocess() method.");
Expand Down
39 changes: 37 additions & 2 deletions tests/lib/config/flat-config-array.js
Expand Up @@ -127,6 +127,20 @@ async function assertInvalidConfig(values, message) {
}, message);
}

/**
* Asserts that a given set of configs results is a valid config.
* @param {*[]} values An array of configs to use in the config array.
* @returns {void}
* @throws {Error} If the config is invalid.
*/
async function assertValidConfig(values) {
const configs = createFlatConfigArray(values);

await configs.normalize();

configs.getConfig("foo.js"); // should not throw error
}

/**
* Normalizes the rule configs to an array with severity to match
* how Flat Config merges rule options.
Expand Down Expand Up @@ -908,13 +922,34 @@ describe("FlatConfigArray", () => {
});
});

it("should not error when an extension-named processor string is used", async () => {

await assertValidConfig([
{
plugins: {
foo: {
processors: {
".yaml": {
preprocess() {},
postprocess() {}
}
}
}
}
},
{
processor: "foo/.yaml"
}
]);
});

it("should error when an invalid string is used", async () => {

await assertInvalidConfig([
{
processor: "foo"
}
], "pluginName/objectName");
], "pluginName/processorName");
});

it("should error when an empty string is used", async () => {
Expand All @@ -923,7 +958,7 @@ describe("FlatConfigArray", () => {
{
processor: ""
}
], "pluginName/objectName");
], "pluginName/processorName");
});

it("should error when an invalid processor is used", async () => {
Expand Down

0 comments on commit e41d4ea

Please sign in to comment.