Skip to content
This repository was archived by the owner on Aug 7, 2023. It is now read-only.

Commit 3fc198e

Browse files
committed
refactor: improve src/rules.js
1 parent 72f9000 commit 3fc198e

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

src/rules.js

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,54 @@
11
import ruleURI from 'eslint-rule-documentation'
22

3-
// Private properties
4-
const rules = Symbol('rules')
5-
63
/**
74
* Stores a list of rules from ESLint
85
*/
96
export default class Rules {
107
/**
118
* Instantiates a Rules object, optionally with an existing list of rules
12-
* @param {Array} newRules Array of Arrays of the rule and properties
9+
* @param {Array<Array<string, any>} newRules Array of Arrays of the rule and properties
1310
*/
1411
constructor(newRules) {
1512
this.replaceRules(newRules)
1613
}
1714

1815
/**
1916
* Process the updated rules into the local Map and call further update functions
20-
* @param {Array} newRules Array of Arrays of the rule and properties
17+
* @param {Array<Array<string, any>} newRules Array of Arrays of the rule and properties
2118
*/
2219
replaceRules(newRules) {
23-
this[rules] = new Map(newRules)
20+
if (this.rules !== undefined) {
21+
this.rules.clear()
22+
}
23+
24+
/** @type {Map<string, any>} */
25+
this.rules = new Map(newRules)
2426
}
2527

2628
/**
2729
* [getFixableRules description]
28-
* @return {Array} The ruleIds of the currently known fixable rules
30+
* @return {Array<string>} The ruleIds of the currently known fixable rules
2931
*/
3032
getFixableRules() {
31-
return Array.from(this[rules]).reduce((fixable, [rule, props]) => {
32-
if (props && props.meta && props.meta.fixable) {
33-
return [...fixable, rule]
33+
const ruleIds = []
34+
// eslint-disable-next-line no-restricted-syntax
35+
for (const [ruleId, ruleProps] of this.rules) {
36+
if (ruleProps && ruleProps.meta && ruleProps.meta.fixable) {
37+
ruleIds.push(ruleId)
3438
}
35-
return fixable
36-
}, [])
39+
}
40+
return ruleIds
3741
}
3842

3943
/**
4044
* Get the URL of the documentation for a rule, either from the rule's own
4145
* metadata, from eslint-rule-documentation's known rules, or the fallback URL
4246
* on how to add it to eslint-rule-documentation.
43-
* @param {String} ruleId The rule ID to get the documentation URL for
44-
* @return {String} URL of the rule documentation
47+
* @param {string} ruleId The rule ID to get the documentation URL for
48+
* @return {string} URL of the rule documentation
4549
*/
4650
getRuleUrl(ruleId) {
47-
const props = this[rules].get(ruleId)
51+
const props = this.rules.get(ruleId)
4852
if (props && props.meta && props.meta.docs && props.meta.docs.url) {
4953
// The rule has a documentation URL specified in its metadata
5054
return props.meta.docs.url
@@ -57,9 +61,9 @@ export default class Rules {
5761

5862
/**
5963
* Return the known rules.
60-
* @return {Map} The currently known rules
64+
* @return {Map<string, any>} The currently known rules
6165
*/
6266
getRules() {
63-
return new Map(this[rules])
67+
return this.rules
6468
}
6569
}

0 commit comments

Comments
 (0)