@@ -18,28 +18,38 @@ function _getConfigFile(specifiedFile) {
18
18
return require ( path . join ( process . cwd ( ) , 'package.json' ) ) . main ;
19
19
}
20
20
21
- function _getConfigs ( configFile , files ) {
22
- const cliEngine = eslint . ESLint
23
- ? new eslint . ESLint ( {
21
+ async function _getConfigs ( configFile , files ) {
22
+ let isPathIgnored ;
23
+ let getConfigForFile ;
24
+
25
+ if ( eslint . ESLint ) {
26
+ const esLint = new eslint . ESLint ( {
24
27
// Ignore any config applicable depending on the location on the filesystem
25
28
useEslintrc : false ,
26
29
// Point to the particular config
27
30
overrideConfigFile : configFile
28
- } )
29
- : new eslint . CLIEngine ( {
31
+ } ) ;
32
+ isPathIgnored = esLint . isPathIgnored . bind ( esLint ) ;
33
+ getConfigForFile = esLint . calculateConfigForFile . bind ( esLint ) ;
34
+ } else {
35
+ const cliEngine = new eslint . CLIEngine ( {
30
36
// Ignore any config applicable depending on the location on the filesystem
31
37
useEslintrc : false ,
32
38
// Point to the particular config
33
39
configFile
34
40
} ) ;
41
+ isPathIgnored = cliEngine . isPathIgnored . bind ( cliEngine ) ;
42
+ getConfigForFile = cliEngine . getConfigForFile . bind ( cliEngine ) ;
43
+ }
35
44
36
- return new Set ( files
37
- . map ( filePath => cliEngine . isPathIgnored ( filePath ) ? false : cliEngine . getConfigForFile ( filePath ) )
38
- . filter ( Boolean ) ) ;
45
+ const configs = files . map ( async filePath => (
46
+ await isPathIgnored ( filePath ) ? false : getConfigForFile ( filePath )
47
+ ) ) ;
48
+ return new Set ( ( await Promise . all ( configs ) ) . filter ( Boolean ) ) ;
39
49
}
40
50
41
- function _getConfig ( configFile , files ) {
42
- return Array . from ( _getConfigs ( configFile , files ) ) . reduce ( ( prev , item ) => {
51
+ async function _getConfig ( configFile , files ) {
52
+ return Array . from ( await _getConfigs ( configFile , files ) ) . reduce ( ( prev , item ) => {
43
53
return Object . assign ( prev , item , { rules : Object . assign ( { } , prev . rules , item . rules ) } ) ;
44
54
} , { } ) ;
45
55
}
@@ -98,14 +108,7 @@ function _createExtensionRegExp(extensions) {
98
108
return new RegExp ( `.\\.(?:${ normalizedExts . join ( "|" ) } )$` ) ;
99
109
}
100
110
101
- function RuleFinder ( specifiedFile , { omitCore, includeDeprecated, ext = [ '.js' ] } ) {
102
- const configFile = _getConfigFile ( specifiedFile ) ;
103
-
104
- const extensionRegExp = _createExtensionRegExp ( ext ) ;
105
- const files = glob . sync ( `**/*` , { dot : true , matchBase : true } )
106
- . filter ( file => extensionRegExp . test ( file ) ) ;
107
-
108
- const config = _getConfig ( configFile , files ) ;
111
+ function RuleFinder ( config , { omitCore, includeDeprecated} ) {
109
112
let currentRuleNames = _getCurrentNamesRules ( config ) ;
110
113
if ( omitCore ) {
111
114
currentRuleNames = currentRuleNames . filter ( _isNotCore ) ;
@@ -143,6 +146,19 @@ function RuleFinder(specifiedFile, {omitCore, includeDeprecated, ext = ['.js']})
143
146
this . getDeprecatedRules = ( ) => getSortedRules ( deprecatedRuleNames ) ;
144
147
}
145
148
149
+ async function createRuleFinder ( specifiedFile , options ) {
150
+ const configFile = _getConfigFile ( specifiedFile ) ;
151
+
152
+ const { ext = [ '.js' ] } = options ;
153
+ const extensionRegExp = _createExtensionRegExp ( ext ) ;
154
+ const files = glob . sync ( `**/*` , { dot : true , matchBase : true } )
155
+ . filter ( file => extensionRegExp . test ( file ) ) ;
156
+
157
+ const config = await _getConfig ( configFile , files ) ;
158
+
159
+ return new RuleFinder ( config , options ) ;
160
+ }
161
+
146
162
module . exports = function ( specifiedFile , options = { } ) {
147
- return new RuleFinder ( specifiedFile , options ) ;
163
+ return createRuleFinder ( specifiedFile , options ) ;
148
164
} ;
0 commit comments