Skip to content

Commit a523132

Browse files
committed
Update warnings extraction to hermes-parser
1 parent e3fb6ac commit a523132

File tree

1 file changed

+33
-50
lines changed

1 file changed

+33
-50
lines changed

scripts/print-warnings/print-warnings.js

Lines changed: 33 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,16 @@
66
*/
77
'use strict';
88

9-
const babelParser = require('@babel/parser');
9+
const {
10+
parse,
11+
SimpleTraverser: {traverse},
12+
} = require('hermes-parser');
1013
const fs = require('fs');
1114
const through = require('through2');
12-
const traverse = require('@babel/traverse').default;
1315
const gs = require('glob-stream');
1416

1517
const {evalStringConcat} = require('../shared/evalToString');
1618

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-
3219
const warnings = new Set();
3320

3421
function transform(file, enc, cb) {
@@ -40,40 +27,37 @@ function transform(file, enc, cb) {
4027

4128
let ast;
4229
try {
43-
ast = babelParser.parse(source, parserOptions);
30+
ast = parse(source);
4431
} catch (error) {
4532
console.error('Failed to parse source file:', file.path);
4633
throw error;
4734
}
4835

4936
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.
7559
}
76-
},
60+
}
7761
},
7862
});
7963

@@ -89,14 +73,13 @@ gs([
8973
'!**/__tests__/**/*.js',
9074
'!**/__mocks__/**/*.js',
9175
'!**/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',
9776
]).pipe(
9877
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+
);
10083
cb();
10184
})
10285
);

0 commit comments

Comments
 (0)