[d565449] | 1 | /**
|
---|
| 2 | * @fileoverview Shared functions to work with configs.
|
---|
| 3 | * @author Nicholas C. Zakas
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | //-----------------------------------------------------------------------------
|
---|
| 9 | // Functions
|
---|
| 10 | //-----------------------------------------------------------------------------
|
---|
| 11 |
|
---|
| 12 | /**
|
---|
| 13 | * Parses a ruleId into its plugin and rule parts.
|
---|
| 14 | * @param {string} ruleId The rule ID to parse.
|
---|
| 15 | * @returns {{pluginName:string,ruleName:string}} The plugin and rule
|
---|
| 16 | * parts of the ruleId;
|
---|
| 17 | */
|
---|
| 18 | function parseRuleId(ruleId) {
|
---|
| 19 | let pluginName, ruleName;
|
---|
| 20 |
|
---|
| 21 | // distinguish between core rules and plugin rules
|
---|
| 22 | if (ruleId.includes("/")) {
|
---|
| 23 |
|
---|
| 24 | // mimic scoped npm packages
|
---|
| 25 | if (ruleId.startsWith("@")) {
|
---|
| 26 | pluginName = ruleId.slice(0, ruleId.lastIndexOf("/"));
|
---|
| 27 | } else {
|
---|
| 28 | pluginName = ruleId.slice(0, ruleId.indexOf("/"));
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | ruleName = ruleId.slice(pluginName.length + 1);
|
---|
| 32 | } else {
|
---|
| 33 | pluginName = "@";
|
---|
| 34 | ruleName = ruleId;
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | return {
|
---|
| 38 | pluginName,
|
---|
| 39 | ruleName
|
---|
| 40 | };
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | /**
|
---|
| 44 | * Retrieves a rule instance from a given config based on the ruleId.
|
---|
| 45 | * @param {string} ruleId The rule ID to look for.
|
---|
| 46 | * @param {FlatConfig} config The config to search.
|
---|
| 47 | * @returns {import("../shared/types").Rule|undefined} The rule if found
|
---|
| 48 | * or undefined if not.
|
---|
| 49 | */
|
---|
| 50 | function getRuleFromConfig(ruleId, config) {
|
---|
| 51 |
|
---|
| 52 | const { pluginName, ruleName } = parseRuleId(ruleId);
|
---|
| 53 |
|
---|
| 54 | const plugin = config.plugins && config.plugins[pluginName];
|
---|
| 55 | let rule = plugin && plugin.rules && plugin.rules[ruleName];
|
---|
| 56 |
|
---|
| 57 |
|
---|
| 58 | // normalize function rules into objects
|
---|
| 59 | if (rule && typeof rule === "function") {
|
---|
| 60 | rule = {
|
---|
| 61 | create: rule
|
---|
| 62 | };
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | return rule;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | /**
|
---|
| 69 | * Gets a complete options schema for a rule.
|
---|
| 70 | * @param {{create: Function, schema: (Array|null)}} rule A new-style rule object
|
---|
| 71 | * @returns {Object} JSON Schema for the rule's options.
|
---|
| 72 | */
|
---|
| 73 | function getRuleOptionsSchema(rule) {
|
---|
| 74 |
|
---|
| 75 | if (!rule) {
|
---|
| 76 | return null;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | const schema = rule.schema || rule.meta && rule.meta.schema;
|
---|
| 80 |
|
---|
| 81 | if (Array.isArray(schema)) {
|
---|
| 82 | if (schema.length) {
|
---|
| 83 | return {
|
---|
| 84 | type: "array",
|
---|
| 85 | items: schema,
|
---|
| 86 | minItems: 0,
|
---|
| 87 | maxItems: schema.length
|
---|
| 88 | };
|
---|
| 89 | }
|
---|
| 90 | return {
|
---|
| 91 | type: "array",
|
---|
| 92 | minItems: 0,
|
---|
| 93 | maxItems: 0
|
---|
| 94 | };
|
---|
| 95 |
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | // Given a full schema, leave it alone
|
---|
| 99 | return schema || null;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 |
|
---|
| 103 | //-----------------------------------------------------------------------------
|
---|
| 104 | // Exports
|
---|
| 105 | //-----------------------------------------------------------------------------
|
---|
| 106 |
|
---|
| 107 | module.exports = {
|
---|
| 108 | parseRuleId,
|
---|
| 109 | getRuleFromConfig,
|
---|
| 110 | getRuleOptionsSchema
|
---|
| 111 | };
|
---|