1
1
import ruleURI from 'eslint-rule-documentation'
2
2
3
- // Private properties
4
- const rules = Symbol ( 'rules' )
5
-
6
3
/**
7
4
* Stores a list of rules from ESLint
8
5
*/
9
6
export default class Rules {
10
7
/**
11
8
* 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
13
10
*/
14
11
constructor ( newRules ) {
15
12
this . replaceRules ( newRules )
16
13
}
17
14
18
15
/**
19
16
* 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
21
18
*/
22
19
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 )
24
26
}
25
27
26
28
/**
27
29
* [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
29
31
*/
30
32
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 )
34
38
}
35
- return fixable
36
- } , [ ] )
39
+ }
40
+ return ruleIds
37
41
}
38
42
39
43
/**
40
44
* Get the URL of the documentation for a rule, either from the rule's own
41
45
* metadata, from eslint-rule-documentation's known rules, or the fallback URL
42
46
* 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
45
49
*/
46
50
getRuleUrl ( ruleId ) {
47
- const props = this [ rules ] . get ( ruleId )
51
+ const props = this . rules . get ( ruleId )
48
52
if ( props && props . meta && props . meta . docs && props . meta . docs . url ) {
49
53
// The rule has a documentation URL specified in its metadata
50
54
return props . meta . docs . url
@@ -57,9 +61,9 @@ export default class Rules {
57
61
58
62
/**
59
63
* Return the known rules.
60
- * @return {Map } The currently known rules
64
+ * @return {Map<string, any> } The currently known rules
61
65
*/
62
66
getRules ( ) {
63
- return new Map ( this [ rules ] )
67
+ return this . rules
64
68
}
65
69
}
0 commit comments