Skip to content

Commit

Permalink
feat: Implement FlatRuleTester (#15519)
Browse files Browse the repository at this point in the history
* feat: Implement FlatRuleTester

Implements FlatRuleTester as a way to allow devs to test their rules
against flat config.

Refs #13481

* more test fixes

* Finish tests

* Remove TODO comments

* Added subclassing tests

* Add FlatConfigArray test

* Incorporate feedback

* Fix comment

* Incorporate feedback

* Fix up typedefs

* Fix parser wrapping in all scenarios
  • Loading branch information
nzakas committed Feb 2, 2022
1 parent 0383591 commit 6f940c3
Show file tree
Hide file tree
Showing 7 changed files with 3,769 additions and 73 deletions.
6 changes: 5 additions & 1 deletion lib/config/flat-config-array.js
Expand Up @@ -58,7 +58,11 @@ class FlatConfigArray extends ConfigArray {
schema: flatConfigSchema
});

this.unshift(...baseConfig);
if (baseConfig[Symbol.iterator]) {
this.unshift(...baseConfig);
} else {
this.unshift(baseConfig);
}
}

/* eslint-disable class-methods-use-this -- Desired as instance method */
Expand Down
38 changes: 37 additions & 1 deletion lib/config/flat-config-helpers.js
Expand Up @@ -57,11 +57,47 @@ function getRuleFromConfig(ruleId, config) {
return rule;
}

/**
* Gets a complete options schema for a rule.
* @param {{create: Function, schema: (Array|null)}} rule A new-style rule object
* @returns {Object} JSON Schema for the rule's options.
*/
function getRuleOptionsSchema(rule) {

if (!rule) {
return null;
}

const schema = rule.schema || rule.meta && rule.meta.schema;

if (Array.isArray(schema)) {
if (schema.length) {
return {
type: "array",
items: schema,
minItems: 0,
maxItems: schema.length
};
}
return {
type: "array",
minItems: 0,
maxItems: 0
};

}

// Given a full schema, leave it alone
return schema || null;
}


//-----------------------------------------------------------------------------
// Exports
//-----------------------------------------------------------------------------

module.exports = {
parseRuleId,
getRuleFromConfig
getRuleFromConfig,
getRuleOptionsSchema
};
40 changes: 5 additions & 35 deletions lib/config/rule-validator.js
Expand Up @@ -10,7 +10,11 @@
//-----------------------------------------------------------------------------

const ajv = require("../shared/ajv")();
const { parseRuleId, getRuleFromConfig } = require("./flat-config-helpers");
const {
parseRuleId,
getRuleFromConfig,
getRuleOptionsSchema
} = require("./flat-config-helpers");
const ruleReplacements = require("../../conf/replacements.json");

//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -61,40 +65,6 @@ function throwRuleNotFoundError({ pluginName, ruleName }, config) {
throw new TypeError(errorMessage);
}

/**
* Gets a complete options schema for a rule.
* @param {{create: Function, schema: (Array|null)}} rule A new-style rule object
* @returns {Object} JSON Schema for the rule's options.
*/
function getRuleOptionsSchema(rule) {

if (!rule) {
return null;
}

const schema = rule.schema || rule.meta && rule.meta.schema;

if (Array.isArray(schema)) {
if (schema.length) {
return {
type: "array",
items: schema,
minItems: 0,
maxItems: schema.length
};
}
return {
type: "array",
minItems: 0,
maxItems: 0
};

}

// Given a full schema, leave it alone
return schema || null;
}

//-----------------------------------------------------------------------------
// Exports
//-----------------------------------------------------------------------------
Expand Down

0 comments on commit 6f940c3

Please sign in to comment.