1. 'use strict';
    
  2. 
    
  3. /* eslint-disable no-for-of-loops/no-for-of-loops */
    
  4. 
    
  5. const path = require('path');
    
  6. const {promisify} = require('util');
    
  7. const glob = promisify(require('glob'));
    
  8. const {ESLint} = require('eslint');
    
  9. 
    
  10. // Lint the final build artifacts. Helps catch bugs in our build pipeline.
    
  11. 
    
  12. function getFormat(filepath) {
    
  13.   if (filepath.includes('facebook')) {
    
  14.     if (filepath.includes('shims')) {
    
  15.       // We don't currently lint these shims. We rely on the downstream Facebook
    
  16.       // repo to transform them.
    
  17.       // TODO: Should we lint them?
    
  18.       return null;
    
  19.     }
    
  20.     return 'fb';
    
  21.   }
    
  22.   if (filepath.includes('react-native')) {
    
  23.     if (filepath.includes('shims')) {
    
  24.       // We don't currently lint these shims. We rely on the downstream Facebook
    
  25.       // repo to transform them.
    
  26.       // TODO: Should we lint them?
    
  27.       return null;
    
  28.     }
    
  29.     return 'rn';
    
  30.   }
    
  31.   if (filepath.includes('cjs')) {
    
  32.     if (
    
  33.       filepath.includes('react-server-dom-webpack-plugin') ||
    
  34.       filepath.includes('react-server-dom-webpack-node-register') ||
    
  35.       filepath.includes('react-suspense-test-utils')
    
  36.     ) {
    
  37.       return 'cjs2015';
    
  38.     }
    
  39.     return 'cjs';
    
  40.   }
    
  41.   if (filepath.includes('esm')) {
    
  42.     return 'esm';
    
  43.   }
    
  44.   if (filepath.includes('umd')) {
    
  45.     return 'umd';
    
  46.   }
    
  47.   if (
    
  48.     filepath.includes('oss-experimental') ||
    
  49.     filepath.includes('oss-stable')
    
  50.   ) {
    
  51.     // If a file in one of the open source channels doesn't match an earlier,
    
  52.     // more specific rule, then assume it's CommonJS.
    
  53.     return 'cjs';
    
  54.   }
    
  55.   throw new Error('Could not find matching lint format for file: ' + filepath);
    
  56. }
    
  57. 
    
  58. function getESLintInstance(format) {
    
  59.   return new ESLint({
    
  60.     useEslintrc: false,
    
  61.     overrideConfigFile: path.join(__dirname, `eslintrc.${format}.js`),
    
  62.     ignore: false,
    
  63.   });
    
  64. }
    
  65. 
    
  66. async function lint(eslint, filepaths) {
    
  67.   const results = await eslint.lintFiles(filepaths);
    
  68.   if (
    
  69.     results.some(result => result.errorCount > 0 || result.warningCount > 0)
    
  70.   ) {
    
  71.     process.exitCode = 1;
    
  72.     console.log(`Lint failed`);
    
  73.     const formatter = await eslint.loadFormatter('stylish');
    
  74.     const resultText = formatter.format(results);
    
  75.     console.log(resultText);
    
  76.   }
    
  77. }
    
  78. 
    
  79. async function lintEverything() {
    
  80.   console.log(`Linting build artifacts...`);
    
  81. 
    
  82.   const allFilepaths = await glob('build/**/*.js');
    
  83. 
    
  84.   const pathsByFormat = new Map();
    
  85.   for (const filepath of allFilepaths) {
    
  86.     const format = getFormat(filepath);
    
  87.     if (format !== null) {
    
  88.       const paths = pathsByFormat.get(format);
    
  89.       if (paths === undefined) {
    
  90.         pathsByFormat.set(format, [filepath]);
    
  91.       } else {
    
  92.         paths.push(filepath);
    
  93.       }
    
  94.     }
    
  95.   }
    
  96. 
    
  97.   const promises = [];
    
  98.   for (const [format, filepaths] of pathsByFormat) {
    
  99.     const eslint = getESLintInstance(format);
    
  100.     promises.push(lint(eslint, filepaths));
    
  101.   }
    
  102.   await Promise.all(promises);
    
  103. }
    
  104. 
    
  105. lintEverything().catch(error => {
    
  106.   process.exitCode = 1;
    
  107.   console.error(error);
    
  108. });