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

eslint v8 integration #1156

Open
finalight opened this issue Jun 30, 2023 · 3 comments
Open

eslint v8 integration #1156

finalight opened this issue Jun 30, 2023 · 3 comments

Comments

@finalight
Copy link

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

  • List of files that has eslint warnings

Actual Result

  • 0 tests got checked
@phenomnomnominal
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

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

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

No branches or pull requests

2 participants