phenomnomnominal / betterer

betterer makes it easier to make incremental improvements to your codebase
MIT License
569 stars 38 forks source link

eslint v8 integration #1156

Open finalight opened 1 year ago

finalight commented 1 year ago

In eslint v7, i could easily check for the linting rules without the need for async await

however, in v8, it's a different story; I am not sure how can I export a function to a config that requires Promise of the eslint to be resolved

import type { BettererFileTest } from '@betterer/betterer';
import { eslint } from '@betterer/eslint';
import type { Linter } from 'eslint';
import * as ESLint from 'eslint';
import * as _ from 'lodash';

const severity = {
  off: 0,
  warn: 1,
  error: 2,
} as const;

const parseSeverity = (entry: Linter.RuleEntry) => {
  const level = Array.isArray(entry) ? entry[0] : entry;

  return typeof level === 'string' ? severity[level] : level;
};

const parseRules = async (configFile: string) => {
  const eslintObj = new ESLint.ESLint({ overrideConfigFile: configFile });
  const fileConfig = await eslintObj.calculateConfigForFile(__filename);

  if (fileConfig) {
    return fileConfig.rules;
  }
  return {};
};

const getExtraRules = async () => {
  const rules = await parseRules('./.eslintrc.old.js');
  const bettererRules = await parseRules('./.eslintrc.js');

  const extraRules = Object.fromEntries(
    Object.entries(bettererRules).filter(
      (value): value is [string, Linter.RuleEntry] => {
        const [rule, entry] = value;

        return (
          entry !== undefined &&
          parseSeverity(entry) === severity.error &&
          !_.isEqual(entry, rules[rule])
        );
      },
    ),
  );

  return extraRules;
};

const getConfig = async () => {
  const config: Record<string, BettererFileTest> = {
    '.eslintrc.betterer.js': eslint(await getExtraRules()).include(
      './**/*.{js,ts}',
    ),
  };
  return config;
};

export default getConfig();

how i would run the command

yarn betterer:base -config ./.betterer.eslint.ts --results .betterer.eslint.results

Expected Result

Actual Result

phenomnomnominal commented 1 year ago

What version of Betterer are you on? You should be able to use a function for the config which wraps eslint, and it can be async

finalight commented 1 year ago

What version of Betterer are you on? You should be able to use a function for the config which wraps eslint, and it can be async

i'm on v4, should i upgrade to v5 first?

phenomnomnominal commented 1 year ago

Yeah, I don’t think there’s a way to make it work in v4!