print-warnings.js — react Source File
Architecture documentation for print-warnings.js, a javascript file in the react codebase.
Entity Profile
Relationship Graph
Source Code
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
const {
parse,
SimpleTraverser: {traverse},
} = require('hermes-parser');
const fs = require('fs');
const through = require('through2');
const gs = require('glob-stream');
const {evalStringConcat} = require('../shared/evalToString');
const warnings = new Set();
function transform(file, enc, cb) {
fs.readFile(file.path, 'utf8', function (err, source) {
if (err) {
cb(err);
return;
}
let ast;
try {
ast = parse(source);
} catch (error) {
console.error('Failed to parse source file:', file.path);
throw error;
}
traverse(ast, {
enter() {},
leave(node) {
if (node.type !== 'CallExpression') {
return;
}
const callee = node.callee;
if (
callee.type === 'MemberExpression' &&
callee.object.type === 'Identifier' &&
callee.object.name === 'console' &&
callee.property.type === 'Identifier' &&
(callee.property.name === 'warn' || callee.property.name === 'error')
) {
// warning messages can be concatenated (`+`) at runtime, so here's
// a trivial partial evaluator that interprets the literal value
try {
const warningMsgLiteral = evalStringConcat(node.arguments[0]);
warnings.add(warningMsgLiteral);
} catch {
// Silently skip over this call. We have a lint rule to enforce
// that all calls are extractable, so if this one fails, assume
// it's intentional.
}
}
},
});
cb(null);
});
}
gs([
'packages/**/*.js',
'!packages/*/npm/**/*.js',
'!packages/react-devtools*/**/*.js',
'!**/__tests__/**/*.js',
'!**/__mocks__/**/*.js',
'!**/node_modules/**/*.js',
]).pipe(
through.obj(transform, cb => {
const warningsArray = Array.from(warnings);
warningsArray.sort();
process.stdout.write(
`/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
* @noformat
* @oncall react_core
*/
export default ${JSON.stringify(warningsArray, null, 2)};
`
);
cb();
})
);
Domain
Subdomains
Functions
Source
Frequently Asked Questions
What does print-warnings.js do?
print-warnings.js is a source file in the react codebase, written in javascript. It belongs to the BabelCompiler domain, Entrypoint subdomain.
What functions are defined in print-warnings.js?
print-warnings.js defines 1 function(s): transform.
Where is print-warnings.js in the architecture?
print-warnings.js is located at scripts/print-warnings/print-warnings.js (domain: BabelCompiler, subdomain: Entrypoint, directory: scripts/print-warnings).
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free