-
Notifications
You must be signed in to change notification settings - Fork 47k
/
print-warnings.js
96 lines (87 loc) · 2.41 KB
/
print-warnings.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
* 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();
})
);