Update warnings extraction to hermes-parser (#27785)
`hermes-parser` is recommended for all Flow files as it supports the latest features. I noticed we were still using babel here. Test Plan: no diff in output before and after
Jan Kassens committed
Dec 4, 2023 at 13:25 UTC
f391cdab02e90d7e9156a7e6d20c7d92a3c375ac
1 file changed
+33
-50
scripts/print-warnings/print-warnings.js
+33
-50
@@ -6,29 +6,16 @@
6
*/
7
'use strict';
8
9
-const babelParser = require('@babel/parser');
9
+const {
10
+ parse,
11
+ SimpleTraverser: {traverse},
12
+} = require('hermes-parser');
13
const fs = require('fs');
14
const through = require('through2');
12
-const traverse = require('@babel/traverse').default;
15
const gs = require('glob-stream');
16
17
const {evalStringConcat} = require('../shared/evalToString');
18
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
-
19
const warnings = new Set();
20
21
function transform(file, enc, cb) {
@@ -40,40 +27,37 @@ function transform(file, enc, cb) {
27
28
let ast;
29
try {
43
- ast = babelParser.parse(source, parserOptions);
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, {
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
- }
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
}
76
- },
60
+ }
61
},
62
});
63
@@ -89,14 +73,13 @@ gs([
73
'!**/__tests__/**/*.js',
74
'!**/__mocks__/**/*.js',
75
'!**/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',
76
]).pipe(
77
through.obj(transform, cb => {
99
- process.stdout.write(Array.from(warnings).sort().join('\n') + '\n');
78
+ process.stdout.write(
79
+ Array.from(warnings, warning => JSON.stringify(warning))
80
+ .sort()
81
+ .join('\n') + '\n'
82
+ );
83
cb();
84
})
85
);