Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • C create-react-app
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 1,547
    • Issues 1,547
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 417
    • Merge requests 417
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • Meta
  • create-react-app
  • Merge requests
  • !10326

typescriptFormatter.js to support ESLint

  • Review changes

  • Download
  • Email patches
  • Plain diff
Open Administrator requested to merge github/fork/johnnyreilly/patch-1 into main Jan 02, 2021
  • Overview 2
  • Commits 1
  • Pipelines 0
  • Changes 1

Created by: johnnyreilly

Hello everybody!

I've a mini PR which you're welcome to not take, but it would make me very happy if you did 😄

I am an inveterate fiddler and tweaker. As such, I'm the sort of person who likes to either eject my create-react-app or use something like craco to customise it. I'm aware that this is on me and that there are no guarantees offered for customisation. Nevertheless, I've a tiny tweak to offer that I think might still be worth considering.

A common thing for me to do when tweaking is to use the fork-ts-checker-webpack-plugin to handle ESLint as well as type checking. There's definitely some ego in there; it was me that added this ability to the plugin. However, it's also that I appreciate using a consistent mechanism for reporting issues, be they type checking or lint issues.

Consider the following craco recipe:

/* eslint-disable @typescript-eslint/no-var-requires */
const ESLintPlugin = require('eslint-webpack-plugin');
const ForkTsCheckerWebpackPlugin = require('react-dev-utils/ForkTsCheckerWebpackPlugin');

const { throwUnexpectedConfigError } = require('@craco/craco');

const throwError = (message) =>
    throwUnexpectedConfigError({
        packageName: 'craco',
        githubRepo: 'gsoft-inc/craco',
        message,
        githubIssueQuery: 'webpack',
    });

module.exports = {
    webpack: {
        configure: (webpackConfig, { env, paths }) => {
            console.log('removing ESLintPlugin');
            const indexOfESLintPlugin = webpackConfig.plugins.findIndex((plugin) => plugin instanceof ESLintPlugin);
            if (indexOfESLintPlugin === -1) throwError('failed to find ESLintPlugin');
            const removed = webpackConfig.plugins.splice(indexOfESLintPlugin, 1);
            if (removed.length !== 1) throwError('failed to remove ESLintPlugin');
            console.log('removed ESLintPlugin', indexOfESLintPlugin);

            console.log('activate ESLint in fork-ts-checker-webpack-plugin');
            const indexOfForkTsCheckerWebpackPlugin = webpackConfig.plugins.findIndex(
                (plugin) => plugin instanceof ForkTsCheckerWebpackPlugin
            );
            if (indexOfForkTsCheckerWebpackPlugin === -1) throwError('failed to find ESLintPlugin');
            const forkTsCheckerWebpackPlugin = webpackConfig.plugins[indexOfForkTsCheckerWebpackPlugin];
            forkTsCheckerWebpackPlugin.eslint = true;
            forkTsCheckerWebpackPlugin.eslintOptions = {
                cache: false,
                eslintPath: require.resolve('eslint'),
                resolvePluginsRelativeTo: __dirname,
            };
            console.log('activated ESLint in fork-ts-checker-webpack-plugin');

            return webpackConfig;
        },
    },
};

This removes the eslint-webpack-plugin and turns on ESLint support in the fork-ts-checker-webpack-plugin. Running this config on a project with a simple linting issue in it surfaces up the following:

Please note, this works fine apart from one thing; the undefined error message. This is because the origin is "eslint" and that key does not exist in issueOrigins. Hence the undefined as issueOrigins[origin] fails to look up an entry.

If we add in an eslint entry like so:

const issueOrigins = {
  typescript: 'TypeScript',
  internal: 'fork-ts-checker-webpack-plugin',
   eslint: 'ESLint' // prevents 'undefined error' message if people have enabled ESLint via fork-ts-checker-webpack-plugin
};

Then this issue is resolved and we get a nice console message like so:

Of course, this is a niche issue, but I know I'm not the only person who would love this to be taken. So... what do you think?

Assignee
Assign to
Reviewers
Request review from
Time tracking
Source branch: github/fork/johnnyreilly/patch-1