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 babelParser = require('@babel/parser');
    
  10. const fs = require('fs');
    
  11. const through = require('through2');
    
  12. const traverse = require('@babel/traverse').default;
    
  13. const gs = require('glob-stream');
    
  14. 
    
  15. const {evalStringConcat} = require('../shared/evalToString');
    
  16. 
    
  17. const parserOptions = {
    
  18.   sourceType: 'module',
    
  19.   // babelParser has its own options and we can't directly
    
  20.   // import/require a babel preset. It should be kept **the same** as
    
  21.   // the `babel-plugin-syntax-*` ones specified in
    
  22.   // https://github.com/facebook/fbjs/blob/master/packages/babel-preset-fbjs/configure.js
    
  23.   plugins: [
    
  24.     'classProperties',
    
  25.     'flow',
    
  26.     'jsx',
    
  27.     'trailingFunctionCommas',
    
  28.     'objectRestSpread',
    
  29.   ],
    
  30. };
    
  31. 
    
  32. const warnings = new Set();
    
  33. 
    
  34. function transform(file, enc, cb) {
    
  35.   fs.readFile(file.path, 'utf8', function (err, source) {
    
  36.     if (err) {
    
  37.       cb(err);
    
  38.       return;
    
  39.     }
    
  40. 
    
  41.     let ast;
    
  42.     try {
    
  43.       ast = babelParser.parse(source, parserOptions);
    
  44.     } catch (error) {
    
  45.       console.error('Failed to parse source file:', file.path);
    
  46.       throw error;
    
  47.     }
    
  48. 
    
  49.     traverse(ast, {
    
  50.       CallExpression: {
    
  51.         exit: function (astPath) {
    
  52.           const callee = astPath.get('callee');
    
  53.           if (
    
  54.             callee.matchesPattern('console.warn') ||
    
  55.             callee.matchesPattern('console.error')
    
  56.           ) {
    
  57.             const node = astPath.node;
    
  58.             if (node.callee.type !== 'MemberExpression') {
    
  59.               return;
    
  60.             }
    
  61.             if (node.callee.property.type !== 'Identifier') {
    
  62.               return;
    
  63.             }
    
  64.             // warning messages can be concatenated (`+`) at runtime, so here's
    
  65.             // a trivial partial evaluator that interprets the literal value
    
  66.             try {
    
  67.               const warningMsgLiteral = evalStringConcat(node.arguments[0]);
    
  68.               warnings.add(JSON.stringify(warningMsgLiteral));
    
  69.             } catch (error) {
    
  70.               // Silently skip over this call. We have a lint rule to enforce
    
  71.               // that all calls are extractable, so if this one fails, assume
    
  72.               // it's intentional.
    
  73.               return;
    
  74.             }
    
  75.           }
    
  76.         },
    
  77.       },
    
  78.     });
    
  79. 
    
  80.     cb(null);
    
  81.   });
    
  82. }
    
  83. 
    
  84. gs([
    
  85.   'packages/**/*.js',
    
  86.   '!packages/*/npm/**/*.js',
    
  87.   '!packages/shared/consoleWithStackDev.js',
    
  88.   '!packages/react-devtools*/**/*.js',
    
  89.   '!**/__tests__/**/*.js',
    
  90.   '!**/__mocks__/**/*.js',
    
  91.   '!**/node_modules/**/*.js',
    
  92.   // TODO: The newer Flow type syntax in this file breaks the parser and I can't
    
  93.   // figure out how to get Babel to parse it. I wasted too much time on
    
  94.   // something so unimportant so I'm skipping this for now. There's no actual
    
  95.   // code or warnings in this file anyway.
    
  96.   '!packages/shared/ReactTypes.js',
    
  97. ]).pipe(
    
  98.   through.obj(transform, cb => {
    
  99.     process.stdout.write(Array.from(warnings).sort().join('\n') + '\n');
    
  100.     cb();
    
  101.   })
    
  102. );