@@ -13,16 +13,35 @@ import {readFileSync} from 'fs';
13
13
import {
14
14
File as BabylonFile ,
15
15
Node as BabylonNode ,
16
+ BlockStatement as BabylonBlockStatement ,
17
+ ArrowFunctionExpression as BabylonArrowFunctionExpression ,
18
+ FunctionExpression as BabylonFunctionExpression ,
16
19
ExpressionStatement as BabylonExpressionStatement ,
17
20
CallExpression as BabylonCallExpression ,
18
21
} from '@babel/types' ;
19
- import { parse as babylonParse , BabylonOptions } from 'babylon ' ;
22
+ import { parse as babylonParse , ParserOptions as BabylonOptions } from '@babel/parser ' ;
20
23
import { ParsedNodeType , NamedBlock , ParsedRange , ParseResult , ParsedNode } from './parser_nodes' ;
21
24
22
25
// eslint-disable no-underscore-dangle
23
26
const _getASTfor = ( file : string , data ?: string ) : [ BabylonFile , string ] => {
24
27
const _data = data || readFileSync ( file ) . toString ( ) ;
25
- const config : BabylonOptions = { plugins : [ '*' ] as any [ ] , sourceType : 'module' } ;
28
+ const config : BabylonOptions = {
29
+ // https://babeljs.io/docs/en/v7-migration-api#babel-parser-known-as-babylon
30
+ plugins : [
31
+ 'asyncGenerators' ,
32
+ 'classProperties' ,
33
+ 'decorators-legacy' ,
34
+ 'doExpressions' ,
35
+ 'dynamicImport' ,
36
+ // 'exportExtensions', this doesn't seem to be a valid option according to type definitions.
37
+ 'flow' ,
38
+ 'functionBind' ,
39
+ 'functionSent' ,
40
+ 'jsx' ,
41
+ 'objectRestSpread' ,
42
+ ] ,
43
+ sourceType : 'module' ,
44
+ } ;
26
45
return [ babylonParse ( _data , config ) , _data ] ;
27
46
} ;
28
47
@@ -130,11 +149,11 @@ export const parse = (file: string, data?: string): ParseResult => {
130
149
// Look through the node's children
131
150
let child : ParsedNode | undefined ;
132
151
133
- if ( ! babylonParent . body ) {
152
+ if ( ! ( babylonParent as BabylonBlockStatement ) . body ) {
134
153
return ;
135
154
}
136
155
137
- babylonParent . body . forEach ( element => {
156
+ ( babylonParent as BabylonBlockStatement ) . body . forEach ( element => {
138
157
child = undefined ;
139
158
// Pull out the node
140
159
// const element = babylonParent.body[node];
@@ -148,7 +167,9 @@ export const parse = (file: string, data?: string): ParseResult => {
148
167
} else if ( element && element . type === 'VariableDeclaration' ) {
149
168
element . declarations
150
169
. filter ( declaration => declaration . init && isFunctionDeclaration ( declaration . init . type ) )
151
- . forEach ( declaration => searchNodes ( declaration . init . body , parent ) ) ;
170
+ . forEach ( declaration =>
171
+ searchNodes ( ( declaration . init as BabylonArrowFunctionExpression | BabylonFunctionExpression ) . body , parent )
172
+ ) ;
152
173
} else if (
153
174
element &&
154
175
element . type === 'ExpressionStatement' &&
@@ -157,17 +178,24 @@ export const parse = (file: string, data?: string): ParseResult => {
157
178
element . expression . right &&
158
179
isFunctionDeclaration ( element . expression . right . type )
159
180
) {
160
- searchNodes ( element . expression . right . body , parent ) ;
161
- } else if ( element . type === 'ReturnStatement' && element . argument . arguments ) {
162
- element . argument . arguments
181
+ searchNodes (
182
+ ( element . expression . right as BabylonArrowFunctionExpression | BabylonFunctionExpression ) . body ,
183
+ parent
184
+ ) ;
185
+ } else if ( element . type === 'ReturnStatement' && ( element ?. argument as BabylonCallExpression ) ?. arguments ) {
186
+ ( element . argument as BabylonCallExpression ) . arguments
163
187
. filter ( argument => isFunctionDeclaration ( argument . type ) )
164
- . forEach ( argument => searchNodes ( argument . body , parent ) ) ;
188
+ . forEach ( argument =>
189
+ searchNodes ( ( argument as BabylonArrowFunctionExpression | BabylonFunctionExpression ) . body , parent )
190
+ ) ;
165
191
}
166
192
167
193
if ( isFunctionCall ( element ) ) {
168
- element . expression . arguments
194
+ ( ( element as BabylonExpressionStatement ) . expression as BabylonCallExpression ) . arguments
169
195
. filter ( argument => isFunctionDeclaration ( argument . type ) )
170
- . forEach ( argument => searchNodes ( argument . body , child || parent ) ) ;
196
+ . forEach ( argument =>
197
+ searchNodes ( ( argument as BabylonArrowFunctionExpression | BabylonFunctionExpression ) . body , child || parent )
198
+ ) ;
171
199
}
172
200
} ) ;
173
201
} ;
0 commit comments