Skip to content

Latest commit

 

History

History
83 lines (70 loc) · 2.59 KB

categorise-eslint-errors.md

File metadata and controls

83 lines (70 loc) · 2.59 KB

Categorising eslint errors

// First run eslint with --format json flag and output it to eslint.json
// Then run node this_script.js eslint.json

const fs = require('fs');
const axios = require('axios');
const _ = require('lodash');

const REPO_URL = 'https://github.com/org/repo'

function fileToIndividualMessages(fileObj) {
  return fileObj.messages.map((message) => {
    return {
      ...message,
      filePath: fileObj.filePath,
    };
  });
}

// https://github.com/sindresorhus/p-map-series
// MIT Copyright (c) Sindre Sorhus <[email protected]> (sindresorhus.com)
async function mapSeriesAsync(iterable, cb) {
  const results = [];
  let index = 0;
  for (const item of iterable) {
    const result = await cb(await item, index++);
    results.push(result);
  }
  return results;
}

function ruleIdToUrl(ruleId) {
  if (_.startsWith(ruleId, 'sonarjs/')) {
    const [__, sonarRule] = ruleId.split('/');
    return `https://github.com/SonarSource/eslint-plugin-sonarjs/blob/master/docs/rules/${sonarRule}.md`;
  } else if (_.startsWith(ruleId, '@typescript-eslint/')) {
    const [__, tsRule] = ruleId.split('/');
    return `https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/${tsRule}.md`;
  }

  return `https://eslint.org/docs/rules/${ruleId}`;
}

function getGhLink(message) {
  const file = getRelativeFilePath(message.filePath);
  return `${REPO_URL}/blob/master/${file}#L${message.line}-L${message.endLine}`;
}

function getRelativeFilePath(fullPath) {
  return fullPath.split('ui/console/')[1];
}

async function main() {
  const lints = JSON.parse(fs.readFileSync(process.argv[2], { encoding: 'utf8' }));
  const allMessages = _.flatten(lints.map(fileToIndividualMessages));
  const groupedById = _.groupBy(allMessages, (m) => m.ruleId);

  console.log('# ESLint rules');
  const arr = _.map(groupedById, (val, key) => ({ messages: val, ruleId: key }));
  const sortedArr = _.sortBy(arr, i => i.messages.length);
  await mapSeriesAsync(sortedArr, ({ messages, ruleId }) => {
    const link = ruleIdToUrl(ruleId);
    console.log(`## [${ruleId}](${link}) (${messages.length})\n`);
    _.forEach(messages, message => {
      const file = getRelativeFilePath(message.filePath);
      let lineInfo = `L${message.line}`;
      if (message.line !== message.endLine) {
        lineInfo += `-L${message.endLine}`;
      }
      console.log(`* [${file}:${lineInfo}](${getGhLink(message)})`);
      const quote = message.message.split('\n').map(line => `    > ${line}`).join('/n');
      console.log(`${quote}\n`);
    });
  });
}

main().catch((err) => {
  throw err;
});