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

Commit

Permalink
Utilize URL from rule metadata
Browse files Browse the repository at this point in the history
The rule URL is now pulled directly from the metadata if it is
available, falling back to the current method of using
`eslint-rule-documentation` if the rule doesn't specify this for itself.
  • Loading branch information
Arcanemagus committed Jan 15, 2018
1 parent c8ee067 commit 2968c44
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
17 changes: 17 additions & 0 deletions spec/helpers-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,21 @@ describe('The helper module', () => {
expect(Helpers.getFixableRules()).toEqual(['foo'])
})
})

describe('getRuleUrl', () => {
it('works with rules that define their own URL', () => {
Helpers.updateRules([['foo', { meta: { docs: { url: 'bar' } } }]])
expect(Helpers.getRuleUrl('foo')).toBe('bar')
})

it('works with rules defined in eslint-rule-documentation', () => {
const url = 'https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/no-duplicates.md'
expect(Helpers.getRuleUrl('import/no-duplicates')).toBe(url)
})

it('gives a fallback URL on how to add a rule URL', () => {
const url = 'https://github.com/jfmengels/eslint-rule-documentation/blob/master/contributing.md'
expect(Helpers.getRuleUrl('foo/bar')).toBe(url)
})
})
})
21 changes: 20 additions & 1 deletion src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,25 @@ const generateInvalidTrace = async ({
}
}

/**
* 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
*/
export function getRuleUrl(ruleId) {
const props = lintRules.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
}

/**
* Given a raw response from ESLint, this processes the messages into a format
* compatible with the Linter API.
Expand Down Expand Up @@ -280,7 +299,7 @@ export async function processESLintMessages(messages, textEditor, showRule, work
}

if (ruleId) {
ret.url = ruleURI(ruleId).url
ret.url = getRuleUrl(ruleId)
}

let range
Expand Down

0 comments on commit 2968c44

Please sign in to comment.