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.  * @emails react-core
    
  8.  */
    
  9. 
    
  10. 'use strict';
    
  11. 
    
  12. const fs = require('fs');
    
  13. const path = require('path');
    
  14. const existingErrorMap = JSON.parse(
    
  15.   fs.readFileSync(path.resolve(__dirname, '../error-codes/codes.json'))
    
  16. );
    
  17. const messages = new Set();
    
  18. Object.keys(existingErrorMap).forEach(key =>
    
  19.   messages.add(existingErrorMap[key])
    
  20. );
    
  21. 
    
  22. /**
    
  23.  * The warning() function takes format strings as its second
    
  24.  * argument.
    
  25.  */
    
  26. 
    
  27. module.exports = {
    
  28.   meta: {
    
  29.     schema: [],
    
  30.   },
    
  31.   create(context) {
    
  32.     // we also allow literal strings and concatenated literal strings
    
  33.     function getLiteralString(node) {
    
  34.       if (node.type === 'Literal' && typeof node.value === 'string') {
    
  35.         return node.value;
    
  36.       } else if (node.type === 'BinaryExpression' && node.operator === '+') {
    
  37.         const l = getLiteralString(node.left);
    
  38.         const r = getLiteralString(node.right);
    
  39.         if (l !== null && r !== null) {
    
  40.           return l + r;
    
  41.         }
    
  42.       }
    
  43.       return null;
    
  44.     }
    
  45. 
    
  46.     return {
    
  47.       CallExpression: function (node) {
    
  48.         // This could be a little smarter by checking context.getScope() to see
    
  49.         // how warning/invariant was defined.
    
  50.         const isWarning =
    
  51.           node.callee.type === 'MemberExpression' &&
    
  52.           node.callee.object.type === 'Identifier' &&
    
  53.           node.callee.object.name === 'console' &&
    
  54.           node.callee.property.type === 'Identifier' &&
    
  55.           (node.callee.property.name === 'error' ||
    
  56.             node.callee.property.name === 'warn');
    
  57.         if (!isWarning) {
    
  58.           return;
    
  59.         }
    
  60.         const name = 'console.' + node.callee.property.name;
    
  61.         if (node.arguments.length < 1) {
    
  62.           context.report(node, '{{name}} takes at least one argument', {
    
  63.             name,
    
  64.           });
    
  65.           return;
    
  66.         }
    
  67.         const format = getLiteralString(node.arguments[0]);
    
  68.         if (format === null) {
    
  69.           context.report(
    
  70.             node,
    
  71.             'The first argument to {{name}} must be a string literal',
    
  72.             {name}
    
  73.           );
    
  74.           return;
    
  75.         }
    
  76.         if (format.length < 10 || /^[s\W]*$/.test(format)) {
    
  77.           context.report(
    
  78.             node,
    
  79.             'The {{name}} format should be able to uniquely identify this ' +
    
  80.               'warning. Please, use a more descriptive format than: {{format}}',
    
  81.             {name, format}
    
  82.           );
    
  83.           return;
    
  84.         }
    
  85.         // count the number of formatting substitutions, plus the first two args
    
  86.         const expectedNArgs = (format.match(/%s/g) || []).length + 1;
    
  87.         if (node.arguments.length !== expectedNArgs) {
    
  88.           context.report(
    
  89.             node,
    
  90.             'Expected {{expectedNArgs}} arguments in call to {{name}} based on ' +
    
  91.               'the number of "%s" substitutions, but got {{length}}',
    
  92.             {
    
  93.               expectedNArgs: expectedNArgs,
    
  94.               name,
    
  95.               length: node.arguments.length,
    
  96.             }
    
  97.           );
    
  98.         }
    
  99.       },
    
  100.     };
    
  101.   },
    
  102. };