mirror of
https://github.com/zebrajr/react.git
synced 2025-12-06 12:20:20 +01:00
Uses the layout of the build artifact directory to infer the format of a given file, and which lint rules to apply. This has the effect of decoupling the lint build job from the existing Rollup script, so that if we ever add additional post-processing, or if we replace Rollup, it will still work. But the immediate motivation is to replace the separate "stable" and "experimental" lint-build jobs with a single combined job.
109 lines
2.9 KiB
JavaScript
109 lines
2.9 KiB
JavaScript
'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 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('umd')) {
|
|
return 'umd';
|
|
}
|
|
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('build2/**/*.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);
|
|
});
|