]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/shared/config-ops.js
2 * @fileoverview Config file operations. This file must be usable in the browser,
3 * so no Node-specific code can be here.
4 * @author Nicholas C. Zakas
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const RULE_SEVERITY_STRINGS
= ["off", "warn", "error"],
13 RULE_SEVERITY
= RULE_SEVERITY_STRINGS
.reduce((map
, value
, index
) => {
17 VALID_SEVERITIES
= [0, 1, 2, "off", "warn", "error"];
19 //------------------------------------------------------------------------------
21 //------------------------------------------------------------------------------
26 * Normalizes the severity value of a rule's configuration to a number
27 * @param {(number|string|[number, ...*]|[string, ...*])} ruleConfig A rule's configuration value, generally
28 * received from the user. A valid config value is either 0, 1, 2, the string "off" (treated the same as 0),
29 * the string "warn" (treated the same as 1), the string "error" (treated the same as 2), or an array
30 * whose first element is one of the above values. Strings are matched case-insensitively.
31 * @returns {(0|1|2)} The numeric severity value if the config value was valid, otherwise 0.
33 getRuleSeverity(ruleConfig
) {
34 const severityValue
= Array
.isArray(ruleConfig
) ? ruleConfig
[0] : ruleConfig
;
36 if (severityValue
=== 0 || severityValue
=== 1 || severityValue
=== 2) {
40 if (typeof severityValue
=== "string") {
41 return RULE_SEVERITY
[severityValue
.toLowerCase()] || 0;
48 * Converts old-style severity settings (0, 1, 2) into new-style
49 * severity settings (off, warn, error) for all rules. Assumption is that severity
50 * values have already been validated as correct.
51 * @param {Object} config The config object to normalize.
54 normalizeToStrings(config
) {
57 Object
.keys(config
.rules
).forEach(ruleId
=> {
58 const ruleConfig
= config
.rules
[ruleId
];
60 if (typeof ruleConfig
=== "number") {
61 config
.rules
[ruleId
] = RULE_SEVERITY_STRINGS
[ruleConfig
] || RULE_SEVERITY_STRINGS
[0];
62 } else if (Array
.isArray(ruleConfig
) && typeof ruleConfig
[0] === "number") {
63 ruleConfig
[0] = RULE_SEVERITY_STRINGS
[ruleConfig
[0]] || RULE_SEVERITY_STRINGS
[0];
70 * Determines if the severity for the given rule configuration represents an error.
71 * @param {int|string|Array} ruleConfig The configuration for an individual rule.
72 * @returns {boolean} True if the rule represents an error, false if not.
74 isErrorSeverity(ruleConfig
) {
75 return module
.exports
.getRuleSeverity(ruleConfig
) === 2;
79 * Checks whether a given config has valid severity or not.
80 * @param {number|string|Array} ruleConfig The configuration for an individual rule.
81 * @returns {boolean} `true` if the configuration has valid severity.
83 isValidSeverity(ruleConfig
) {
84 let severity
= Array
.isArray(ruleConfig
) ? ruleConfig
[0] : ruleConfig
;
86 if (typeof severity
=== "string") {
87 severity
= severity
.toLowerCase();
89 return VALID_SEVERITIES
.indexOf(severity
) !== -1;
93 * Checks whether every rule of a given config has valid severity or not.
94 * @param {Object} config The configuration for rules.
95 * @returns {boolean} `true` if the configuration has valid severity.
97 isEverySeverityValid(config
) {
98 return Object
.keys(config
).every(ruleId
=> this.isValidSeverity(config
[ruleId
]));
102 * Normalizes a value for a global in a config
103 * @param {(boolean|string|null)} configuredValue The value given for a global in configuration or in
104 * a global directive comment
105 * @returns {("readable"|"writeable"|"off")} The value normalized as a string
106 * @throws Error if global value is invalid
108 normalizeConfigGlobal(configuredValue
) {
109 switch (configuredValue
) {
127 throw new Error(`'${configuredValue}' is not a valid configuration for a global (use 'readonly', 'writable', or 'off')`);