@@ -3,18 +3,18 @@ module.exports.cli = require('./bin/cmd')
3
3
4
4
module . exports . linter = Linter
5
5
6
- var deglob = require ( 'deglob' )
7
6
var os = require ( 'os' )
8
7
var path = require ( 'path' )
9
8
var pkgConf = require ( 'pkg-conf' )
9
+ var fs = require ( 'fs' )
10
10
11
11
var CACHE_HOME = require ( 'xdg-basedir' ) . cache || os . tmpdir ( )
12
12
13
- var DEFAULT_PATTERNS = [
14
- '**/* .js' ,
15
- '**/* .jsx' ,
16
- '**/* .mjs' ,
17
- '**/* .cjs'
13
+ var DEFAULT_EXTENSIONS = [
14
+ '.js' ,
15
+ '.jsx' ,
16
+ '.mjs' ,
17
+ '.cjs'
18
18
]
19
19
20
20
var DEFAULT_IGNORE = [
@@ -48,8 +48,9 @@ function Linter (opts) {
48
48
envs : [ ] ,
49
49
fix : false ,
50
50
globals : [ ] ,
51
- ignore : false ,
52
51
plugins : [ ] ,
52
+ ignorePattern : [ ] ,
53
+ extensions : DEFAULT_EXTENSIONS ,
53
54
useEslintrc : false
54
55
} , opts . eslintConfig )
55
56
@@ -109,58 +110,72 @@ Linter.prototype.lintFiles = function (files, opts, cb) {
109
110
opts = self . parseOpts ( opts )
110
111
111
112
if ( typeof files === 'string' ) files = [ files ]
112
- if ( files . length === 0 ) files = DEFAULT_PATTERNS
113
+ if ( files . length === 0 ) files = [ '.' ]
113
114
114
- var deglobOpts = {
115
- ignore : opts . ignore ,
116
- cwd : opts . cwd ,
117
- useGitIgnore : true ,
118
- usePackageJson : false
115
+ var result
116
+ try {
117
+ result = new self . eslint . CLIEngine ( opts . eslintConfig ) . executeOnFiles ( files )
118
+ } catch ( err ) {
119
+ return cb ( err )
119
120
}
120
121
121
- deglob ( files , deglobOpts , function ( err , allFiles ) {
122
- if ( err ) return cb ( err )
123
-
124
- var result
125
- try {
126
- result = new self . eslint . CLIEngine ( opts . eslintConfig ) . executeOnFiles ( allFiles )
127
- } catch ( err ) {
128
- return cb ( err )
129
- }
130
-
131
- if ( opts . fix ) {
132
- self . eslint . CLIEngine . outputFixes ( result )
133
- }
122
+ if ( opts . fix ) {
123
+ self . eslint . CLIEngine . outputFixes ( result )
124
+ }
134
125
135
- return cb ( null , result )
136
- } )
126
+ return cb ( null , result )
137
127
}
138
128
139
129
Linter . prototype . parseOpts = function ( opts ) {
140
130
var self = this
141
131
142
- if ( ! opts ) opts = { }
143
- opts = Object . assign ( { } , opts )
144
- opts . eslintConfig = Object . assign ( { } , self . eslintConfig )
145
- opts . eslintConfig . fix = ! ! opts . fix
132
+ opts = {
133
+ eslintConfig : { ...self . eslintConfig } ,
134
+ usePackageJson : true ,
135
+ useGitIgnore : true ,
136
+ gitIgnoreFile : [ '.gitignore' , '.git/info/exclude' ] ,
137
+ cwd : self . cwd ,
138
+ fix : false ,
139
+ ignore : [ ] ,
140
+ ...opts
141
+ }
142
+
143
+ if ( ! Array . isArray ( opts . gitIgnoreFile ) ) {
144
+ opts . gitIgnoreFile = [ opts . gitIgnoreFile ]
145
+ }
146
146
147
- if ( ! opts . cwd ) opts . cwd = self . cwd
148
147
opts . eslintConfig . cwd = opts . cwd
148
+ opts . eslintConfig . fix = opts . fix
149
+
150
+ var packageOpts = { }
151
+ var rootPath = null
149
152
150
- // If no usePackageJson option is given, default to `true`
151
- var usePackageJson = opts . usePackageJson != null
152
- ? opts . usePackageJson
153
- : true
153
+ if ( opts . usePackageJson || opts . useGitIgnore ) {
154
+ packageOpts = pkgConf . sync ( self . cmd , { cwd : opts . cwd } )
155
+ rootPath = path . dirname ( pkgConf . filepath ( packageOpts ) )
156
+ }
154
157
155
- var packageOpts = usePackageJson
156
- ? pkgConf . sync ( self . cmd , { cwd : opts . cwd } )
157
- : { }
158
+ if ( ! opts . usePackageJson ) packageOpts = { }
158
159
159
- if ( ! opts . ignore ) opts . ignore = [ ]
160
- addIgnore ( packageOpts . ignore )
161
160
if ( ! packageOpts . noDefaultIgnore ) {
162
161
addIgnore ( DEFAULT_IGNORE )
163
162
}
163
+ addIgnore ( packageOpts . ignore )
164
+
165
+ if ( opts . useGitIgnore ) {
166
+ opts . gitIgnoreFile
167
+ . map ( gitIgnoreFile => {
168
+ try {
169
+ return fs . readFileSync ( path . join ( rootPath , gitIgnoreFile ) , 'utf8' )
170
+ } catch ( err ) {
171
+ return null
172
+ }
173
+ } )
174
+ . filter ( Boolean )
175
+ . forEach ( gitignore => {
176
+ addIgnore ( gitignore . split ( / \r ? \n / ) )
177
+ } )
178
+ }
164
179
165
180
addGlobals ( packageOpts . globals || packageOpts . global )
166
181
addGlobals ( opts . globals || opts . global )
@@ -175,7 +190,7 @@ Linter.prototype.parseOpts = function (opts) {
175
190
176
191
if ( self . customParseOpts ) {
177
192
var rootDir
178
- if ( usePackageJson ) {
193
+ if ( opts . usePackageJson ) {
179
194
var filePath = pkgConf . filepath ( packageOpts )
180
195
rootDir = filePath ? path . dirname ( filePath ) : opts . cwd
181
196
} else {
@@ -186,7 +201,7 @@ Linter.prototype.parseOpts = function (opts) {
186
201
187
202
function addIgnore ( ignore ) {
188
203
if ( ! ignore ) return
189
- opts . ignore = opts . ignore . concat ( ignore )
204
+ opts . eslintConfig . ignorePattern = opts . eslintConfig . ignorePattern . concat ( ignore )
190
205
}
191
206
192
207
function addGlobals ( globals ) {
0 commit comments