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

Commit

Permalink
Merge pull request #120 from vzamanillo/add-rule-description
Browse files Browse the repository at this point in the history
Add rule description to the lint message marker tooltip
  • Loading branch information
Arcanemagus authored Feb 11, 2019
2 parents 5050ef7 + 2699a00 commit 07a888d
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/linter-reek.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

// eslint-disable-next-line import/no-extraneous-dependencies, import/extensions
import { CompositeDisposable } from 'atom';
import * as RuleHelpers from './rule-helpers';

let helpers;
let path;
Expand Down Expand Up @@ -99,11 +100,13 @@ export default {
let match = parseRegex.exec(output);
while (match !== null) {
const line = Number.parseInt(match[1], 10) - 1;
const ruleLink = `https://github.com/troessner/reek/blob/master/docs/${match[3]}.md`;
const rule = match[3];
const ruleLink = `https://github.com/troessner/reek/blob/master/docs/${rule}.md`;
messages.push({
url: ruleLink,
description: () => RuleHelpers.getRuleMarkDown(rule),
severity: 'warning',
excerpt: `${match[2]}`,
excerpt: match[2],
location: {
file: filePath,
position: helpers.generateRange(TextEditor, line),
Expand Down
59 changes: 59 additions & 0 deletions lib/rule-helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
'use babel';

const DOCUMENTATION_LIFETIME = 86400 * 1000;
const docsRuleCache = new Map();

export function takeWhile(source, predicate) {
const result = [];
const { length } = source;
let i = 0;

while (i < length && predicate(source[i], i)) {
result.push(source[i]);
i += 1;
}

return result;
}

// Retrieves style guide documentation with cached responses
export async function getRuleMarkDown(rule) {
if (docsRuleCache.has(rule)) {
const cachedRule = docsRuleCache.get(rule);
if (new Date().getTime() >= cachedRule.expires) {
// If documentation is stale, clear cache
docsRuleCache.delete(rule);
} else {
return cachedRule.markdown;
}
}

let rawRuleMarkdown;
const response = await fetch(`https://raw.githubusercontent.com/troessner/reek/master/docs/${rule}.md`);
if (response.ok) {
rawRuleMarkdown = await response.text();
} else {
return `***\nError retrieving documentation: ${response.statusText}`;
}

const byLine = rawRuleMarkdown.split('\n');
const ruleAnchors = byLine.reduce(
(acc, line, idx) => (line.match(/## Introduction/g) ? acc.concat(idx) : acc),
[],
);

ruleAnchors.forEach((startingIndex) => {
const beginSearch = byLine.slice(startingIndex + 1);

// gobble the introduction text until you reach the next section
const documentationForRule = takeWhile(beginSearch, x => !x.match(/##/));
const markdownOutput = '***\n'.concat(documentationForRule.join('\n'));

docsRuleCache.set(rule, {
markdown: markdownOutput,
expires: new Date().getTime() + DOCUMENTATION_LIFETIME,
});
});

return docsRuleCache.get(rule).markdown;
}

0 comments on commit 07a888d

Please sign in to comment.