Skip to content
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

Merged
merged 7 commits into from
Jan 14, 2018

Conversation

Arcanemagus
Copy link
Contributor

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.

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.
@SamVerschueren
Copy link
Contributor

I believe it would be a lot more maintainable if we created a utility function called getDocsURL() which uses module.parent.filename to dynamically calculate the last bit of the URL. All the other stuff is static.

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.
@Arcanemagus
Copy link
Contributor Author

Arcanemagus commented Jan 9, 2018

Moved to that model @SamVerschueren 😉. Since I didn't see any existing place to put this function I created a util folder in the rules folder. I also added some basic test for this new utility function.

Copy link
Contributor

@SamVerschueren SamVerschueren left a 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.


const repoUrl = 'https://github.com/sindresorhus/eslint-plugin-unicorn';

function getDocsUrl(ruleName, givenCommitHash) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Export this one directly.

Copy link
Contributor

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';

Copy link
Contributor Author

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 to commitHash.

It's generally a bad idea to assign to parameters, but I can change it to that style.

Copy link
Contributor

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) => {

Copy link
Contributor

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'

@@ -0,0 +1,13 @@
'use strict';

const repoUrl = 'https://github.com/sindresorhus/eslint-plugin-unicorn';
Copy link
Contributor

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.
@Arcanemagus
Copy link
Contributor Author

Is that more along the lines of what you were thinking @SamVerschueren?

Copy link
Contributor

@SamVerschueren SamVerschueren left a 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 :).


const repoUrl = 'https://github.com/sindresorhus/eslint-plugin-unicorn';

module.exports = function (ruleName, commitHash) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use an arrow function

@@ -0,0 +1,11 @@
'use strict';
const {basename} = require('path');
Copy link
Contributor

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')

const repoUrl = 'https://github.com/sindresorhus/eslint-plugin-unicorn';

module.exports = function (ruleName, commitHash) {
ruleName = ruleName || basename(module.parent.filename).replace('.js', '');
Copy link
Contributor

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

import getDocsUrl from '../rules/utils/get-docs-url';

test('returns the URL of the a named rule\'s documentation', t => {
t.plan(1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove

});

test('returns the URL of the a named rule\'s documentation at a commit hash', t => {
t.plan(1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove

});

test('determines the rule name from the file', t => {
t.plan(1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove

@Arcanemagus
Copy link
Contributor Author

Updated again @SamVerschueren 😉.

Copy link
Contributor

@SamVerschueren SamVerschueren left a 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.


const repoUrl = 'https://github.com/sindresorhus/eslint-plugin-unicorn';

module.exports = (ruleName, commitHash) => {
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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.

@sindresorhus sindresorhus merged commit 875344d into sindresorhus:master Jan 14, 2018
@sindresorhus
Copy link
Owner

Thanks for adding this @Arcanemagus :)

@Arcanemagus Arcanemagus deleted the rules-docs-url branch January 15, 2018 09:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants