Skip to content

Commit 6f940c3

Browse files
authored
feat: Implement FlatRuleTester (#15519)
* 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
1 parent 0383591 commit 6f940c3

7 files changed

Lines changed: 3769 additions & 73 deletions

File tree

lib/config/flat-config-array.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ class FlatConfigArray extends ConfigArray {
5858
schema: flatConfigSchema
5959
});
6060

61-
this.unshift(...baseConfig);
61+
if (baseConfig[Symbol.iterator]) {
62+
this.unshift(...baseConfig);
63+
} else {
64+
this.unshift(baseConfig);
65+
}
6266
}
6367

6468
/* eslint-disable class-methods-use-this -- Desired as instance method */

lib/config/flat-config-helpers.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,47 @@ function getRuleFromConfig(ruleId, config) {
5757
return rule;
5858
}
5959

60+
/**
61+
* Gets a complete options schema for a rule.
62+
* @param {{create: Function, schema: (Array|null)}} rule A new-style rule object
63+
* @returns {Object} JSON Schema for the rule's options.
64+
*/
65+
function getRuleOptionsSchema(rule) {
66+
67+
if (!rule) {
68+
return null;
69+
}
70+
71+
const schema = rule.schema || rule.meta && rule.meta.schema;
72+
73+
if (Array.isArray(schema)) {
74+
if (schema.length) {
75+
return {
76+
type: "array",
77+
items: schema,
78+
minItems: 0,
79+
maxItems: schema.length
80+
};
81+
}
82+
return {
83+
type: "array",
84+
minItems: 0,
85+
maxItems: 0
86+
};
87+
88+
}
89+
90+
// Given a full schema, leave it alone
91+
return schema || null;
92+
}
93+
94+
6095
//-----------------------------------------------------------------------------
6196
// Exports
6297
//-----------------------------------------------------------------------------
6398

6499
module.exports = {
65100
parseRuleId,
66-
getRuleFromConfig
101+
getRuleFromConfig,
102+
getRuleOptionsSchema
67103
};

lib/config/rule-validator.js

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
//-----------------------------------------------------------------------------
1111

1212
const ajv = require("../shared/ajv")();
13-
const { parseRuleId, getRuleFromConfig } = require("./flat-config-helpers");
13+
const {
14+
parseRuleId,
15+
getRuleFromConfig,
16+
getRuleOptionsSchema
17+
} = require("./flat-config-helpers");
1418
const ruleReplacements = require("../../conf/replacements.json");
1519

1620
//-----------------------------------------------------------------------------
@@ -61,40 +65,6 @@ function throwRuleNotFoundError({ pluginName, ruleName }, config) {
6165
throw new TypeError(errorMessage);
6266
}
6367

64-
/**
65-
* Gets a complete options schema for a rule.
66-
* @param {{create: Function, schema: (Array|null)}} rule A new-style rule object
67-
* @returns {Object} JSON Schema for the rule's options.
68-
*/
69-
function getRuleOptionsSchema(rule) {
70-
71-
if (!rule) {
72-
return null;
73-
}
74-
75-
const schema = rule.schema || rule.meta && rule.meta.schema;
76-
77-
if (Array.isArray(schema)) {
78-
if (schema.length) {
79-
return {
80-
type: "array",
81-
items: schema,
82-
minItems: 0,
83-
maxItems: schema.length
84-
};
85-
}
86-
return {
87-
type: "array",
88-
minItems: 0,
89-
maxItems: 0
90-
};
91-
92-
}
93-
94-
// Given a full schema, leave it alone
95-
return schema || null;
96-
}
97-
9868
//-----------------------------------------------------------------------------
9969
// Exports
10070
//-----------------------------------------------------------------------------

0 commit comments

Comments
 (0)