Skip to content

Commit 71212c8

Browse files
committedOct 28, 2020
BREAKING: Use eslint for file ignoring; remove deblog
Ignore patterns should follow the .gitignore format rather than the `glob` pattern. So, you can use dir/ instead of dir/** now. There may be subtle differences in behavior. Fixes: #226
1 parent a54ccc0 commit 71212c8

File tree

2 files changed

+56
-44
lines changed

2 files changed

+56
-44
lines changed
 

‎index.js

+56-43
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ module.exports.cli = require('./bin/cmd')
33

44
module.exports.linter = Linter
55

6-
var deglob = require('deglob')
76
var os = require('os')
87
var path = require('path')
98
var pkgConf = require('pkg-conf')
9+
var fs = require('fs')
1010

1111
var CACHE_HOME = require('xdg-basedir').cache || os.tmpdir()
1212

13-
var DEFAULT_PATTERNS = [
14-
'**/*.js',
15-
'**/*.jsx',
16-
'**/*.mjs',
17-
'**/*.cjs'
13+
var DEFAULT_EXTENSIONS = [
14+
'.js',
15+
'.jsx',
16+
'.mjs',
17+
'.cjs'
1818
]
1919

2020
var DEFAULT_IGNORE = [
@@ -48,8 +48,9 @@ function Linter (opts) {
4848
envs: [],
4949
fix: false,
5050
globals: [],
51-
ignore: false,
5251
plugins: [],
52+
ignorePattern: [],
53+
extensions: DEFAULT_EXTENSIONS,
5354
useEslintrc: false
5455
}, opts.eslintConfig)
5556

@@ -109,58 +110,70 @@ Linter.prototype.lintFiles = function (files, opts, cb) {
109110
opts = self.parseOpts(opts)
110111

111112
if (typeof files === 'string') files = [files]
112-
if (files.length === 0) files = DEFAULT_PATTERNS
113+
if (files.length === 0) files = ['.']
113114

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)
119120
}
120121

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+
}
134125

135-
return cb(null, result)
136-
})
126+
return cb(null, result)
137127
}
138128

139129
Linter.prototype.parseOpts = function (opts) {
140130
var self = this
141131

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+
}
146142

147-
if (!opts.cwd) opts.cwd = self.cwd
148-
opts.eslintConfig.cwd = opts.cwd
143+
if (!Array.isArray(opts.gitIgnoreFile)) {
144+
opts.gitIgnoreFile = [opts.gitIgnoreFile]
145+
}
149146

150-
// If no usePackageJson option is given, default to `true`
151-
var usePackageJson = opts.usePackageJson != null
152-
? opts.usePackageJson
153-
: true
147+
opts.eslintConfig.cwd = opts.cwd
148+
opts.eslintConfig.fix = opts.fix
154149

155-
var packageOpts = usePackageJson
150+
var packageOpts = opts.usePackageJson
156151
? pkgConf.sync(self.cmd, { cwd: opts.cwd })
157152
: {}
158153

159-
if (!opts.ignore) opts.ignore = []
160-
addIgnore(packageOpts.ignore)
154+
var rootPath = opts.usePackageJson
155+
? path.dirname(pkgConf.filepath(packageOpts))
156+
: null
157+
161158
if (!packageOpts.noDefaultIgnore) {
162159
addIgnore(DEFAULT_IGNORE)
163160
}
161+
addIgnore(packageOpts.ignore)
162+
163+
if (opts.useGitIgnore) {
164+
opts.gitIgnoreFile
165+
.map(gitIgnoreFile => {
166+
try {
167+
return fs.readFileSync(path.join(rootPath, gitIgnoreFile), 'utf8')
168+
} catch (err) {
169+
return null
170+
}
171+
})
172+
.filter(Boolean)
173+
.forEach(gitignore => {
174+
addIgnore(gitignore.split(/\r?\n/))
175+
})
176+
}
164177

165178
addGlobals(packageOpts.globals || packageOpts.global)
166179
addGlobals(opts.globals || opts.global)
@@ -175,7 +188,7 @@ Linter.prototype.parseOpts = function (opts) {
175188

176189
if (self.customParseOpts) {
177190
var rootDir
178-
if (usePackageJson) {
191+
if (opts.usePackageJson) {
179192
var filePath = pkgConf.filepath(packageOpts)
180193
rootDir = filePath ? path.dirname(filePath) : opts.cwd
181194
} else {
@@ -186,7 +199,7 @@ Linter.prototype.parseOpts = function (opts) {
186199

187200
function addIgnore (ignore) {
188201
if (!ignore) return
189-
opts.ignore = opts.ignore.concat(ignore)
202+
opts.eslintConfig.ignorePattern = opts.eslintConfig.ignorePattern.concat(ignore)
190203
}
191204

192205
function addGlobals (globals) {

‎package.json

-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
"test": "standard && tape test/clone.js test/api.js"
3939
},
4040
"dependencies": {
41-
"deglob": "^4.0.1",
4241
"get-stdin": "^8.0.0",
4342
"minimist": "^1.2.5",
4443
"pkg-conf": "^3.1.0",

0 commit comments

Comments
 (0)
Please sign in to comment.