-
-
Notifications
You must be signed in to change notification settings - Fork 353
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add URL to rule documentation to the metadata #141
Conversation
ESLint v4.15.0 added an official location for rules to store a URL to their documentation in the rule metadata in eslint/eslint#9788. This adds the URL to all the existing rules so anything consuming them can know where their documentation is without having to resort to external packages to guess.
I believe it would be a lot more maintainable if we created a utility function called |
Add a utility function `getDocsUrl` in order to determine what the URL of the rule should be. Although not currently used here this function is written to support an optional second paramater of the commit hash to link the documentation to, in case this is needed later.
Moved to that model @SamVerschueren 😉. Since I didn't see any existing place to put this function I created a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would make ruleName optional as well and extract from module.parent.filename
inside the utility function.
rules/utils/get-docs-url.js
Outdated
|
||
const repoUrl = 'https://github.com/sindresorhus/eslint-plugin-unicorn'; | ||
|
||
function getDocsUrl(ruleName, givenCommitHash) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Export this one directly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rename givenCommitMash
to commitHash
. Below, write it like this
commitHash = commitHash || 'master';
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Export this one directly.
The rest of this was written in CommonJS, so that's the style I was following. I'll switch it to a module
👍.
Rename
givenCommitHash
tocommitHash
.
It's generally a bad idea to assign to parameters, but I can change it to that style.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I meant module.exports = (ruleName, commit:ash) => {
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Strings are immutable. It's just the same as a default value, only one line extra. It's a bad idea when an object is passed in and you'd write obj.commitHash = obj.commitHash || 'master'
rules/utils/get-docs-url.js
Outdated
@@ -0,0 +1,13 @@ | |||
'use strict'; | |||
|
|||
const repoUrl = 'https://github.com/sindresorhus/eslint-plugin-unicorn'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move it to the top
Use `module.parent.filename` to automatically determine the rule name if a specific one is not passed in.
Is that more along the lines of what you were thinking @SamVerschueren? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some minor improvements. Looks good overall! Reviewing on mobile so might be missing some other small things :).
rules/utils/get-docs-url.js
Outdated
|
||
const repoUrl = 'https://github.com/sindresorhus/eslint-plugin-unicorn'; | ||
|
||
module.exports = function (ruleName, commitHash) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use an arrow function
rules/utils/get-docs-url.js
Outdated
@@ -0,0 +1,11 @@ | |||
'use strict'; | |||
const {basename} = require('path'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use const path = require('path')
rules/utils/get-docs-url.js
Outdated
const repoUrl = 'https://github.com/sindresorhus/eslint-plugin-unicorn'; | ||
|
||
module.exports = function (ruleName, commitHash) { | ||
ruleName = ruleName || basename(module.parent.filename).replace('.js', ''); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just pass .js
as second argument to path.basename()
. See https://nodejs.org/api/path.html#path_path_basename_path_ext
test/get-docs-url.js
Outdated
import getDocsUrl from '../rules/utils/get-docs-url'; | ||
|
||
test('returns the URL of the a named rule\'s documentation', t => { | ||
t.plan(1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove
test/get-docs-url.js
Outdated
}); | ||
|
||
test('returns the URL of the a named rule\'s documentation at a commit hash', t => { | ||
t.plan(1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove
test/get-docs-url.js
Outdated
}); | ||
|
||
test('determines the rule name from the file', t => { | ||
t.plan(1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove
Updated again @SamVerschueren 😉. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One minor comment but looks good to me. One thing I thought about as well, but need a second opinion from @sindresorhus, is that we could also write docs: getDocs()
which returns the entire docs object instead of what we have now.
rules/utils/get-docs-url.js
Outdated
|
||
const repoUrl = 'https://github.com/sindresorhus/eslint-plugin-unicorn'; | ||
|
||
module.exports = (ruleName, commitHash) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure about the commit hash as we don't use it currently.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's used in a few other plugins that I sent PRs to, so I figured I would leave it in here. I can remove it if you want though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should remove it as long as we don't need it. It adds "complexity" and should be maintained later on if perhaps the function gets more complex. But that's up to Sindre to decide.
Thanks for adding this @Arcanemagus :) |
ESLint v4.15.0 added an official location for rules to store a URL to their documentation in the rule metadata in eslint/eslint#9788. This adds the URL to all the existing rules so anything consuming them can know where their documentation is without having to resort to external packages to guess.