Home / File/ index.js — react Source File

index.js — react Source File

Architecture documentation for index.js, a javascript file in the react codebase.

Entity Profile

Relationship Graph

Source Code

'use strict';

/* eslint-disable no-for-of-loops/no-for-of-loops */

const path = require('path');
const {promisify} = require('util');
const glob = promisify(require('glob'));
const {ESLint} = require('eslint');

// Lint the final build artifacts. Helps catch bugs in our build pipeline.

function getFormat(filepath) {
  if (filepath.includes('facebook')) {
    if (filepath.includes('shims')) {
      // We don't currently lint these shims. We rely on the downstream Facebook
      // repo to transform them.
      // TODO: Should we lint them?
      return null;
    }
    return 'fb';
  }
  if (filepath.includes('react-native')) {
    if (filepath.includes('shims')) {
      // We don't currently lint these shims. We rely on the downstream Facebook
      // repo to transform them.
      // TODO: Should we lint them?
      return null;
    }
    return 'rn';
  }
  if (filepath.includes('cjs')) {
    if (
      filepath.includes('react-server-dom-webpack-plugin') ||
      filepath.includes('react-server-dom-webpack-node-register') ||
      filepath.includes('react-suspense-test-utils')
    ) {
      return 'cjs2015';
    }
    return 'cjs';
  }
  if (filepath.includes('esm')) {
    return 'esm';
  }
  if (
    filepath.includes('oss-experimental') ||
    filepath.includes('oss-stable')
  ) {
    // If a file in one of the open source channels doesn't match an earlier,
    // more specific rule, then assume it's CommonJS.
    return 'cjs';
  }
  throw new Error('Could not find matching lint format for file: ' + filepath);
}

function getESLintInstance(format) {
  return new ESLint({
    useEslintrc: false,
    overrideConfigFile: path.join(__dirname, `eslintrc.${format}.js`),
    ignore: false,
  });
}

async function lint(eslint, filepaths) {
  const results = await eslint.lintFiles(filepaths);
  if (
    results.some(result => result.errorCount > 0 || result.warningCount > 0)
  ) {
    process.exitCode = 1;
    console.log(`Lint failed`);
    const formatter = await eslint.loadFormatter('stylish');
    const resultText = formatter.format(results);
    console.log(resultText);
  }
}

async function lintEverything() {
  console.log(`Linting build artifacts...`);

  const allFilepaths = await glob('build/**/*.js');

  const pathsByFormat = new Map();
  for (const filepath of allFilepaths) {
    const format = getFormat(filepath);
    if (format !== null) {
      const paths = pathsByFormat.get(format);
      if (paths === undefined) {
        pathsByFormat.set(format, [filepath]);
      } else {
        paths.push(filepath);
      }
    }
  }

  const promises = [];
  for (const [format, filepaths] of pathsByFormat) {
    const eslint = getESLintInstance(format);
    promises.push(lint(eslint, filepaths));
  }
  await Promise.all(promises);
}

lintEverything().catch(error => {
  process.exitCode = 1;
  console.error(error);
});

Domain

Subdomains

Frequently Asked Questions

What does index.js do?
index.js is a source file in the react codebase, written in javascript. It belongs to the BabelCompiler domain, Optimization subdomain.
What functions are defined in index.js?
index.js defines 4 function(s): getESLintInstance, getFormat, lint, lintEverything.
Where is index.js in the architecture?
index.js is located at scripts/rollup/validate/index.js (domain: BabelCompiler, subdomain: Optimization, directory: scripts/rollup/validate).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free