1 | /**
|
---|
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
|
---|
5 | */
|
---|
6 |
|
---|
7 | //------------------------------------------------------------------------------
|
---|
8 | // Private
|
---|
9 | //------------------------------------------------------------------------------
|
---|
10 |
|
---|
11 | const RULE_SEVERITY_STRINGS = ["off", "warn", "error"],
|
---|
12 | RULE_SEVERITY = RULE_SEVERITY_STRINGS.reduce((map, value, index) => {
|
---|
13 | map[value] = index;
|
---|
14 | return map;
|
---|
15 | }, {}),
|
---|
16 | VALID_SEVERITIES = [0, 1, 2, "off", "warn", "error"];
|
---|
17 |
|
---|
18 | //------------------------------------------------------------------------------
|
---|
19 | // Public Interface
|
---|
20 | //------------------------------------------------------------------------------
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Normalizes the severity value of a rule's configuration to a number
|
---|
24 | * @param {(number|string|[number, ...*]|[string, ...*])} ruleConfig A rule's configuration value, generally
|
---|
25 | * received from the user. A valid config value is either 0, 1, 2, the string "off" (treated the same as 0),
|
---|
26 | * the string "warn" (treated the same as 1), the string "error" (treated the same as 2), or an array
|
---|
27 | * whose first element is one of the above values. Strings are matched case-insensitively.
|
---|
28 | * @returns {(0|1|2)} The numeric severity value if the config value was valid, otherwise 0.
|
---|
29 | */
|
---|
30 | function getRuleSeverity(ruleConfig) {
|
---|
31 | const severityValue = Array.isArray(ruleConfig) ? ruleConfig[0] : ruleConfig;
|
---|
32 |
|
---|
33 | if (severityValue === 0 || severityValue === 1 || severityValue === 2) {
|
---|
34 | return severityValue;
|
---|
35 | }
|
---|
36 |
|
---|
37 | if (typeof severityValue === "string") {
|
---|
38 | return RULE_SEVERITY[severityValue.toLowerCase()] || 0;
|
---|
39 | }
|
---|
40 |
|
---|
41 | return 0;
|
---|
42 | }
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Converts old-style severity settings (0, 1, 2) into new-style
|
---|
46 | * severity settings (off, warn, error) for all rules. Assumption is that severity
|
---|
47 | * values have already been validated as correct.
|
---|
48 | * @param {Object} config The config object to normalize.
|
---|
49 | * @returns {void}
|
---|
50 | */
|
---|
51 | function normalizeToStrings(config) {
|
---|
52 |
|
---|
53 | if (config.rules) {
|
---|
54 | Object.keys(config.rules).forEach(ruleId => {
|
---|
55 | const ruleConfig = config.rules[ruleId];
|
---|
56 |
|
---|
57 | if (typeof ruleConfig === "number") {
|
---|
58 | config.rules[ruleId] = RULE_SEVERITY_STRINGS[ruleConfig] || RULE_SEVERITY_STRINGS[0];
|
---|
59 | } else if (Array.isArray(ruleConfig) && typeof ruleConfig[0] === "number") {
|
---|
60 | ruleConfig[0] = RULE_SEVERITY_STRINGS[ruleConfig[0]] || RULE_SEVERITY_STRINGS[0];
|
---|
61 | }
|
---|
62 | });
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Determines if the severity for the given rule configuration represents an error.
|
---|
68 | * @param {int|string|Array} ruleConfig The configuration for an individual rule.
|
---|
69 | * @returns {boolean} True if the rule represents an error, false if not.
|
---|
70 | */
|
---|
71 | function isErrorSeverity(ruleConfig) {
|
---|
72 | return getRuleSeverity(ruleConfig) === 2;
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Checks whether a given config has valid severity or not.
|
---|
77 | * @param {number|string|Array} ruleConfig The configuration for an individual rule.
|
---|
78 | * @returns {boolean} `true` if the configuration has valid severity.
|
---|
79 | */
|
---|
80 | function isValidSeverity(ruleConfig) {
|
---|
81 | let severity = Array.isArray(ruleConfig) ? ruleConfig[0] : ruleConfig;
|
---|
82 |
|
---|
83 | if (typeof severity === "string") {
|
---|
84 | severity = severity.toLowerCase();
|
---|
85 | }
|
---|
86 | return VALID_SEVERITIES.indexOf(severity) !== -1;
|
---|
87 | }
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Checks whether every rule of a given config has valid severity or not.
|
---|
91 | * @param {Object} config The configuration for rules.
|
---|
92 | * @returns {boolean} `true` if the configuration has valid severity.
|
---|
93 | */
|
---|
94 | function isEverySeverityValid(config) {
|
---|
95 | return Object.keys(config).every(ruleId => isValidSeverity(config[ruleId]));
|
---|
96 | }
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Normalizes a value for a global in a config
|
---|
100 | * @param {(boolean|string|null)} configuredValue The value given for a global in configuration or in
|
---|
101 | * a global directive comment
|
---|
102 | * @returns {("readable"|"writeable"|"off")} The value normalized as a string
|
---|
103 | * @throws Error if global value is invalid
|
---|
104 | */
|
---|
105 | function normalizeConfigGlobal(configuredValue) {
|
---|
106 | switch (configuredValue) {
|
---|
107 | case "off":
|
---|
108 | return "off";
|
---|
109 |
|
---|
110 | case true:
|
---|
111 | case "true":
|
---|
112 | case "writeable":
|
---|
113 | case "writable":
|
---|
114 | return "writable";
|
---|
115 |
|
---|
116 | case null:
|
---|
117 | case false:
|
---|
118 | case "false":
|
---|
119 | case "readable":
|
---|
120 | case "readonly":
|
---|
121 | return "readonly";
|
---|
122 |
|
---|
123 | default:
|
---|
124 | throw new Error(`'${configuredValue}' is not a valid configuration for a global (use 'readonly', 'writable', or 'off')`);
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | export {
|
---|
129 | getRuleSeverity,
|
---|
130 | normalizeToStrings,
|
---|
131 | isErrorSeverity,
|
---|
132 | isValidSeverity,
|
---|
133 | isEverySeverityValid,
|
---|
134 | normalizeConfigGlobal
|
---|
135 | };
|
---|