Repository: facebook/jest
File: scripts/lintTs.mjs (JavaScript)

Code before:
 * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

import * as os from 'os';
import * as path from 'path';
import * as url from 'url';
import chalk from 'chalk';
import {ESLint} from 'eslint';
import pLimit from 'p-limit';
import {getPackagesWithTsConfig} from './buildUtils.mjs';

// we want to limit the number of processes we spawn
const cpus = Math.max(1, os.cpus().length - 1);
const mutex = pLimit(cpus);

const fix = process.argv.slice(2).some(arg => arg === '--fix');

const monorepoRoot = path.resolve(url.fileURLToPath(import.meta.url), '../..');

const packagesWithTs = getPackagesWithTsConfig()
  .map(({packageDir}) => packageDir)
  .concat(path.resolve(monorepoRoot, 'e2e'))
  // TODO: run all all projects
  .slice(0, 3);

try {
  await Promise.all(
    packagesWithTs.map(packageDir =>
      mutex(async () => {
        const eslint = new ESLint({
          cache: true,
          cacheLocation: path.resolve(packageDir, '.eslintcache'),
          cwd: monorepoRoot,
          extensions: ['.ts'],
          fix,
          overrideConfig: {
            extends: [
              'plugin:@typescript-eslint/recommended-requiring-type-checking',
            ],
            parser: '@typescript-eslint/parser',
            parserOptions: {
              project: ['./tsconfig.json', `${packageDir}/tsconfig.json`],
              tsconfigRootDir: monorepoRoot,
            },
            plugins: ['@typescript-eslint'],
            root: true,
          },
        });

Review comment (suggestion):
it's probably better with an allow list and deny list - that way we can fix one specific package instead of going one by one

Code after:
 * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

import * as os from 'os';
import * as path from 'path';
import * as url from 'url';
import chalk from 'chalk';
import {ESLint} from 'eslint';
import pLimit from 'p-limit';
import {getPackagesWithTsConfig} from './buildUtils.mjs';

// we want to limit the number of processes we spawn
const cpus = Math.max(1, os.cpus().length - 1);
const mutex = pLimit(cpus);

const fix = process.argv.slice(2).some(arg => arg === '--fix');

const monorepoRoot = path.resolve(url.fileURLToPath(import.meta.url), '../..');

// TODO: remove this list at some point and run against all packages
const packagesToTest = [
  'babel-jest',
  'babel-plugin-jest-hoist',
  'diff-sequences',
  'jest-source-map',
];

const packagesWithTs = getPackagesWithTsConfig()
  .map(({packageDir}) => packageDir)
  .concat(path.resolve(monorepoRoot, 'e2e'))
  .filter(packageDir => packagesToTest.some(pkg => packageDir.endsWith(pkg)));

const allLintResults = [];

try {
  await Promise.all(
    packagesWithTs.map(packageDir =>
      mutex(async () => {
        const eslint = new ESLint({
          cache: true,
          cacheLocation: path.resolve(packageDir, '.eslintcache'),
          cwd: monorepoRoot,
          extensions: ['.ts'],
          fix,
          fixTypes: ['problem', 'suggestion', 'layout'],
          overrideConfig: {
            extends: [
              'plugin:@typescript-eslint/recommended-requiring-type-checking',