6
6
*/
7
7
'use strict' ;
8
8
9
- const babelParser = require ( '@babel/parser' ) ;
9
+ const {
10
+ parse,
11
+ SimpleTraverser : { traverse} ,
12
+ } = require ( 'hermes-parser' ) ;
10
13
const fs = require ( 'fs' ) ;
11
14
const through = require ( 'through2' ) ;
12
- const traverse = require ( '@babel/traverse' ) . default ;
13
15
const gs = require ( 'glob-stream' ) ;
14
16
15
17
const { evalStringConcat} = require ( '../shared/evalToString' ) ;
16
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
-
32
19
const warnings = new Set ( ) ;
33
20
34
21
function transform ( file , enc , cb ) {
@@ -40,40 +27,37 @@ function transform(file, enc, cb) {
40
27
41
28
let ast ;
42
29
try {
43
- ast = babelParser . parse ( source , parserOptions ) ;
30
+ ast = parse ( source ) ;
44
31
} catch ( error ) {
45
32
console . error ( 'Failed to parse source file:' , file . path ) ;
46
33
throw error ;
47
34
}
48
35
49
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.
75
59
}
76
- } ,
60
+ }
77
61
} ,
78
62
} ) ;
79
63
89
73
'!**/__tests__/**/*.js' ,
90
74
'!**/__mocks__/**/*.js' ,
91
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' ,
97
76
] ) . pipe (
98
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
+ ) ;
100
83
cb ( ) ;
101
84
} )
102
85
) ;
0 commit comments