main
js 105 lines 3.14 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 * @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 (
77 (format.length < 10 || /^[s\W]*$/.test(format)) &&
78 format !== '%s\n\n%s\n'
79 ) {
80 context.report(
81 node,
82 'The {{name}} format should be able to uniquely identify this ' +
83 'warning. Please, use a more descriptive format than: {{format}}',
84 {name, format}
85 );
86 return;
87 }
88 // count the number of formatting substitutions, plus the first two args
89 const expectedNArgs = (format.match(/%[so]/g) || []).length + 1;
90 if (node.arguments.length !== expectedNArgs) {
91 context.report(
92 node,
93 'Expected {{expectedNArgs}} arguments in call to {{name}} based on ' +
94 'the number of "%s" substitutions, but got {{length}}',
95 {
96 expectedNArgs: expectedNArgs,
97 name,
98 length: node.arguments.length,
99 }
100 );
101 }
102 },
103 };
104 },
105 };