Skip to content

Commit

Permalink
docs: improve docs URL resolution (jest-community#80)
Browse files Browse the repository at this point in the history
- Use `__filename` to determine the rule name and build the docs link. This also
  fixes an inadvertant bug where the URL ended with `.md.md`.
- Check the docs file exists and throw an error otherwise.
  • Loading branch information
sudo-suhas authored and SimenB committed Feb 16, 2018
1 parent e0160e5 commit faf0403
Show file tree
Hide file tree
Showing 16 changed files with 28 additions and 17 deletions.
2 changes: 1 addition & 1 deletion rules/consistent-test-it.js
Expand Up @@ -8,7 +8,7 @@ const isDescribe = require('./util').isDescribe;
module.exports = {
meta: {
docs: {
url: getDocsUrl('consistent-test-it.md'),
url: getDocsUrl(__filename),
},
fixable: 'code',
schema: [
Expand Down
2 changes: 1 addition & 1 deletion rules/lowercase-name.js
Expand Up @@ -50,7 +50,7 @@ const descriptionBeginsWithLowerCase = node => {
module.exports = {
meta: {
docs: {
url: getDocsUrl('lowercase-name.md'),
url: getDocsUrl(__filename),
},
},
create(context) {
Expand Down
2 changes: 1 addition & 1 deletion rules/no-disabled-tests.js
Expand Up @@ -22,7 +22,7 @@ function getName(node) {
module.exports = {
meta: {
docs: {
url: getDocsUrl('no-disabled-tests.md'),
url: getDocsUrl(__filename),
},
},
create(context) {
Expand Down
2 changes: 1 addition & 1 deletion rules/no-focused-tests.js
Expand Up @@ -22,7 +22,7 @@ const isCallToTestOnlyFunction = callee =>
module.exports = {
meta: {
docs: {
url: getDocsUrl('no-focused-tests.md'),
url: getDocsUrl(__filename),
},
},
create: context => ({
Expand Down
2 changes: 1 addition & 1 deletion rules/no-hooks.js
Expand Up @@ -5,7 +5,7 @@ const getDocsUrl = require('./util').getDocsUrl;
module.exports = {
meta: {
docs: {
url: getDocsUrl('no-hooks.md'),
url: getDocsUrl(__filename),
},
},
schema: [
Expand Down
2 changes: 1 addition & 1 deletion rules/no-identical-title.js
Expand Up @@ -40,7 +40,7 @@ const isFirstArgLiteral = node =>
module.exports = {
meta: {
docs: {
url: getDocsUrl('no-identical-title.md'),
url: getDocsUrl(__filename),
},
},
create(context) {
Expand Down
2 changes: 1 addition & 1 deletion rules/no-large-snapshots.js
Expand Up @@ -5,7 +5,7 @@ const getDocsUrl = require('./util').getDocsUrl;
module.exports = {
meta: {
docs: {
url: getDocsUrl('no-large-snapshots.md'),
url: getDocsUrl(__filename),
},
},
create(context) {
Expand Down
2 changes: 1 addition & 1 deletion rules/no-test-prefixes.js
Expand Up @@ -8,7 +8,7 @@ const isDescribe = require('./util').isDescribe;
module.exports = {
meta: {
docs: {
url: getDocsUrl('no-test-prefixes.md'),
url: getDocsUrl(__filename),
},
fixable: 'code',
},
Expand Down
2 changes: 1 addition & 1 deletion rules/prefer-expect-assertions.js
Expand Up @@ -65,7 +65,7 @@ const reportMsg = (context, node) => {
module.exports = {
meta: {
docs: {
url: getDocsUrl('prefer-expect-assertions.md'),
url: getDocsUrl(__filename),
},
},
create(context) {
Expand Down
2 changes: 1 addition & 1 deletion rules/prefer-to-be-null.js
Expand Up @@ -13,7 +13,7 @@ const method2 = require('./util').method2;
module.exports = {
meta: {
docs: {
url: getDocsUrl('prefer-to-be-null.md'),
url: getDocsUrl(__filename),
},
fixable: 'code',
},
Expand Down
2 changes: 1 addition & 1 deletion rules/prefer-to-be-undefined.js
Expand Up @@ -12,7 +12,7 @@ const method2 = require('./util').method2;
module.exports = {
meta: {
docs: {
url: getDocsUrl('prefer-to-be-undefined.md'),
url: getDocsUrl(__filename),
},
fixable: 'code',
},
Expand Down
2 changes: 1 addition & 1 deletion rules/prefer-to-have-length.js
Expand Up @@ -10,7 +10,7 @@ const method = require('./util').method;
module.exports = {
meta: {
docs: {
url: getDocsUrl('prefer-to-have-length.md'),
url: getDocsUrl(__filename),
},
fixable: 'code',
},
Expand Down
15 changes: 13 additions & 2 deletions rules/util.js
@@ -1,5 +1,7 @@
'use strict';

const fs = require('fs');
const path = require('path');
const pkg = require('../package.json');

const REPO_URL = 'https://github.com/jest-community/eslint-plugin-jest';
Expand Down Expand Up @@ -129,9 +131,18 @@ const isFunction = node =>
*
* @param {string} ruleName - Name of the eslint rule
* @returns {string} URL to the documentation for the given rule
* @throws {Error} If the documentation file for the given rule is not present.
*/
const getDocsUrl = ruleName =>
`${REPO_URL}/blob/v${pkg.version}/docs/rules/${ruleName}.md`;
const getDocsUrl = filename => {
const ruleName = path.basename(filename, '.js');

const docsFile = path.join(__dirname, `../docs/rules/${ruleName}.md`);
if (!fs.existsSync(docsFile)) {
throw new Error(`Could not find documentation file for rule "${ruleName}"`);
}

return `${REPO_URL}/blob/v${pkg.version}/docs/rules/${ruleName}.md`;
};

module.exports = {
method: method,
Expand Down
2 changes: 1 addition & 1 deletion rules/valid-describe.js
Expand Up @@ -30,7 +30,7 @@ const paramsLocation = params => {
module.exports = {
meta: {
docs: {
url: getDocsUrl('valid-describe.md'),
url: getDocsUrl(__filename),
},
},
create(context) {
Expand Down
2 changes: 1 addition & 1 deletion rules/valid-expect-in-promise.js
Expand Up @@ -128,7 +128,7 @@ const isHavingAsyncCallBackParam = testFunction => {
module.exports = {
meta: {
docs: {
url: getDocsUrl('valid-expect-in-promise.md'),
url: getDocsUrl(__filename),
},
},
create(context) {
Expand Down
2 changes: 1 addition & 1 deletion rules/valid-expect.js
Expand Up @@ -12,7 +12,7 @@ const expectProperties = ['not', 'resolves', 'rejects'];
module.exports = {
meta: {
docs: {
url: getDocsUrl('valid-expect.md'),
url: getDocsUrl(__filename),
},
},
create(context) {
Expand Down

0 comments on commit faf0403

Please sign in to comment.