main
js 96 lines 2.41 KB
Raw
1 /**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7 'use strict';
8
9 const {
10 parse,
11 SimpleTraverser: {traverse},
12 } = require('hermes-parser');
13 const fs = require('fs');
14 const through = require('through2');
15 const gs = require('glob-stream');
16
17 const {evalStringConcat} = require('../shared/evalToString');
18
19 const warnings = new Set();
20
21 function transform(file, enc, cb) {
22 fs.readFile(file.path, 'utf8', function (err, source) {
23 if (err) {
24 cb(err);
25 return;
26 }
27
28 let ast;
29 try {
30 ast = parse(source);
31 } catch (error) {
32 console.error('Failed to parse source file:', file.path);
33 throw error;
34 }
35
36 traverse(ast, {
37 enter() {},
38 leave(node) {
39 if (node.type !== 'CallExpression') {
40 return;
41 }
42 const callee = node.callee;
43 if (
44 callee.type === 'MemberExpression' &&
45 callee.object.type === 'Identifier' &&
46 callee.object.name === 'console' &&
47 callee.property.type === 'Identifier' &&
48 (callee.property.name === 'warn' || callee.property.name === 'error')
49 ) {
50 // warning messages can be concatenated (`+`) at runtime, so here's
51 // a trivial partial evaluator that interprets the literal value
52 try {
53 const warningMsgLiteral = evalStringConcat(node.arguments[0]);
54 warnings.add(warningMsgLiteral);
55 } catch {
56 // Silently skip over this call. We have a lint rule to enforce
57 // that all calls are extractable, so if this one fails, assume
58 // it's intentional.
59 }
60 }
61 },
62 });
63
64 cb(null);
65 });
66 }
67
68 gs([
69 'packages/**/*.js',
70 '!packages/*/npm/**/*.js',
71 '!packages/react-devtools*/**/*.js',
72 '!**/__tests__/**/*.js',
73 '!**/__mocks__/**/*.js',
74 '!**/node_modules/**/*.js',
75 ]).pipe(
76 through.obj(transform, cb => {
77 const warningsArray = Array.from(warnings);
78 warningsArray.sort();
79 process.stdout.write(
80 `/**
81 * Copyright (c) Meta Platforms, Inc. and affiliates.
82 *
83 * This source code is licensed under the MIT license found in the
84 * LICENSE file in the root directory of this source tree.
85 *
86 * @flow strict
87 * @noformat
88 * @oncall react_core
89 */
90
91 export default ${JSON.stringify(warningsArray, null, 2)};
92 `
93 );
94 cb();
95 })
96 );