@@ -3,9 +3,42 @@ exports.__esModule = true;
3
3
4
4
const moduleRequire = require ( './module-require' ) . default ;
5
5
const extname = require ( 'path' ) . extname ;
6
+ const fs = require ( 'fs' ) ;
6
7
7
8
const log = require ( 'debug' ) ( 'eslint-plugin-import:parse' ) ;
8
9
10
+ function getBabelVisitorKeys ( parserPath ) {
11
+ if ( parserPath . endsWith ( 'index.js' ) ) {
12
+ const hypotheticalLocation = parserPath . replace ( 'index.js' , 'visitor-keys.js' ) ;
13
+ if ( fs . existsSync ( hypotheticalLocation ) ) {
14
+ const keys = moduleRequire ( hypotheticalLocation ) ;
15
+ return keys . default || keys ;
16
+ }
17
+ } else if ( parserPath . endsWith ( 'index.cjs' ) ) {
18
+ const hypotheticalLocation = parserPath . replace ( 'index.cjs' , 'worker/ast-info.cjs' ) ;
19
+ if ( fs . existsSync ( hypotheticalLocation ) ) {
20
+ const astInfo = moduleRequire ( hypotheticalLocation ) ;
21
+ return astInfo . getVisitorKeys ( ) ;
22
+ }
23
+ }
24
+ return null ;
25
+ }
26
+
27
+ function keysFromParser ( parserPath , parserInstance , parsedResult ) {
28
+ if ( / .* e s p r e e .* / . test ( parserPath ) ) {
29
+ return parserInstance . VisitorKeys ;
30
+ }
31
+ if ( / .* ( b a b e l - e s l i n t | @ b a b e l \/ e s l i n t - p a r s e r ) .* / . test ( parserPath ) ) {
32
+ return getBabelVisitorKeys ( parserPath ) ;
33
+ }
34
+ if ( / .* @ t y p e s c r i p t - e s l i n t \/ p a r s e r / . test ( parserPath ) ) {
35
+ if ( parsedResult ) {
36
+ return parsedResult . visitorKeys ;
37
+ }
38
+ }
39
+ return null ;
40
+ }
41
+
9
42
exports . default = function parse ( path , content , context ) {
10
43
11
44
if ( context == null ) throw new Error ( 'need context to parse properly' ) ;
@@ -45,20 +78,36 @@ exports.default = function parse(path, content, context) {
45
78
if ( typeof parser . parseForESLint === 'function' ) {
46
79
let ast ;
47
80
try {
48
- ast = parser . parseForESLint ( content , parserOptions ) . ast ;
81
+ const parserRaw = parser . parseForESLint ( content , parserOptions ) ;
82
+ ast = parserRaw . ast ;
83
+ return {
84
+ ast,
85
+ visitorKeys : keysFromParser ( parserPath , parser , parserRaw ) ,
86
+ } ;
49
87
} catch ( e ) {
50
88
console . warn ( ) ;
51
89
console . warn ( 'Error while parsing ' + parserOptions . filePath ) ;
52
90
console . warn ( 'Line ' + e . lineNumber + ', column ' + e . column + ': ' + e . message ) ;
53
91
}
54
92
if ( ! ast || typeof ast !== 'object' ) {
55
- console . warn ( '`parseForESLint` from parser `' + parserPath + '` is invalid and will just be ignored' ) ;
93
+ console . warn (
94
+ '`parseForESLint` from parser `' +
95
+ parserPath +
96
+ '` is invalid and will just be ignored' ,
97
+ ) ;
56
98
} else {
57
- return ast ;
99
+ return {
100
+ ast,
101
+ visitorKeys : keysFromParser ( parserPath , parser , undefined ) ,
102
+ } ;
58
103
}
59
104
}
60
105
61
- return parser . parse ( content , parserOptions ) ;
106
+ const keys = keysFromParser ( parserPath , parser , undefined ) ;
107
+ return {
108
+ ast : parser . parse ( content , parserOptions ) ,
109
+ visitorKeys : keys ,
110
+ } ;
62
111
} ;
63
112
64
113
function getParserPath ( path , context ) {
0 commit comments