This repository has been archived by the owner on Aug 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pull all the logic related to storing the list of known rules and accessing it out into a class of its own.
- Loading branch information
1 parent
37553d9
commit afc5af5
Showing
5 changed files
with
125 additions
and
111 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
'use babel' | ||
|
||
import Rules from '../src/rules' | ||
|
||
describe('The Rules class', () => { | ||
describe('updateRules', () => { | ||
const ruleArray = [ | ||
['foo', { meta: { fixable: true } }], | ||
['bar', { meta: {} }] | ||
] | ||
|
||
it('adds new rules', () => { | ||
const rules = new Rules() | ||
expect(rules.getRules()).toEqual(new Map()) | ||
rules.updateRules(ruleArray) | ||
expect(rules.getRules()).toEqual(new Map(ruleArray)) | ||
}) | ||
|
||
it('removes old rules', () => { | ||
const rules = new Rules() | ||
rules.updateRules(ruleArray) | ||
expect(rules.getRules()).toEqual(new Map(ruleArray)) | ||
rules.updateRules([]) | ||
expect(rules.getRules()).toEqual(new Map()) | ||
}) | ||
|
||
it('updates the fixableRules list', () => { | ||
const rules = new Rules() | ||
expect(rules.getFixableRules()).toEqual([]) | ||
rules.updateRules(ruleArray) | ||
expect(rules.getFixableRules()).toEqual(['foo']) | ||
}) | ||
}) | ||
|
||
describe('getRuleUrl', () => { | ||
it('works with rules that define their own URL', () => { | ||
const rules = new Rules() | ||
rules.updateRules([['foo', { meta: { docs: { url: 'bar' } } }]]) | ||
expect(rules.getRuleUrl('foo')).toBe('bar') | ||
}) | ||
|
||
it('works with rules defined in eslint-rule-documentation', () => { | ||
const rules = new Rules() | ||
const url = 'https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/no-duplicates.md' | ||
expect(rules.getRuleUrl('import/no-duplicates')).toBe(url) | ||
}) | ||
|
||
it('gives a fallback URL on how to add a rule URL', () => { | ||
const rules = new Rules() | ||
const url = 'https://github.com/jfmengels/eslint-rule-documentation/blob/master/contributing.md' | ||
expect(rules.getRuleUrl('foo/bar')).toBe(url) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import ruleURI from 'eslint-rule-documentation' | ||
|
||
/** | ||
* Stores a list of rules from ESLint | ||
*/ | ||
export default class Rules { | ||
/** | ||
* Instantiates a Rules object, optionally with an existing list of rules | ||
* @param {Array} newRules Array of Arrays of the rule and properties | ||
*/ | ||
constructor(newRules) { | ||
this.rules = new Map() | ||
if (newRules) { | ||
this.updateRules(newRules) | ||
} | ||
} | ||
|
||
/** | ||
* Process the updated rules into the local Map and call further update functions | ||
* @param {Array} updatedRules Array of Arrays of the rule and properties | ||
*/ | ||
updateRules(updatedRules) { | ||
this.rules.clear() | ||
updatedRules.forEach(([rule, props]) => this.rules.set(rule, props)) | ||
} | ||
|
||
/** | ||
* [getFixableRules description] | ||
* @return {Array} The ruleIds of the currently known fixable rules | ||
*/ | ||
getFixableRules() { | ||
return Array.from(this.rules).reduce((fixable, [rule, props]) => { | ||
if (props.meta && props.meta.fixable) { | ||
return [...fixable, rule] | ||
} | ||
return fixable | ||
}, []) | ||
} | ||
|
||
/** | ||
* Get the URL of the documentation for a rule, either from the rule's own | ||
* metadata, from eslint-rule-documentation's known rules, or the fallback URL | ||
* on how to add it to eslint-rule-documentation. | ||
* @param {String} ruleId The rule ID to get the documentation URL for | ||
* @return {String} URL of the rule documentation | ||
*/ | ||
getRuleUrl(ruleId) { | ||
const props = this.rules.get(ruleId) | ||
if (props && props.meta && props.meta.docs && props.meta.docs.url) { | ||
// The rule has a documentation URL specified in its metadata | ||
return props.meta.docs.url | ||
} | ||
|
||
// The rule didn't specify a URL in its metadata, or was not currently known | ||
// somehow. Attempt to determine a URL using eslint-rule-documentation. | ||
return ruleURI(ruleId).url | ||
} | ||
|
||
/** | ||
* Return the known rules. | ||
* @return {Map} The currently known rules | ||
*/ | ||
getRules() { | ||
return new Map(this.rules) | ||
} | ||
} |