Skip to content

Commit 4e0d154

Browse files
authoredOct 29, 2020
Merge pull request #228 from standard/remove-deglob
2 parents a54ccc0 + 6abd959 commit 4e0d154

File tree

3 files changed

+69
-57
lines changed

3 files changed

+69
-57
lines changed
 

‎index.js

+59-44
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,72 @@ 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+
}
142+
143+
if (!Array.isArray(opts.gitIgnoreFile)) {
144+
opts.gitIgnoreFile = [opts.gitIgnoreFile]
145+
}
146146

147-
if (!opts.cwd) opts.cwd = self.cwd
148147
opts.eslintConfig.cwd = opts.cwd
148+
opts.eslintConfig.fix = opts.fix
149+
150+
var packageOpts = {}
151+
var rootPath = null
149152

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

155-
var packageOpts = usePackageJson
156-
? pkgConf.sync(self.cmd, { cwd: opts.cwd })
157-
: {}
158+
if (!opts.usePackageJson) packageOpts = {}
158159

159-
if (!opts.ignore) opts.ignore = []
160-
addIgnore(packageOpts.ignore)
161160
if (!packageOpts.noDefaultIgnore) {
162161
addIgnore(DEFAULT_IGNORE)
163162
}
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+
}
164179

165180
addGlobals(packageOpts.globals || packageOpts.global)
166181
addGlobals(opts.globals || opts.global)
@@ -175,7 +190,7 @@ Linter.prototype.parseOpts = function (opts) {
175190

176191
if (self.customParseOpts) {
177192
var rootDir
178-
if (usePackageJson) {
193+
if (opts.usePackageJson) {
179194
var filePath = pkgConf.filepath(packageOpts)
180195
rootDir = filePath ? path.dirname(filePath) : opts.cwd
181196
} else {
@@ -186,7 +201,7 @@ Linter.prototype.parseOpts = function (opts) {
186201

187202
function addIgnore (ignore) {
188203
if (!ignore) return
189-
opts.ignore = opts.ignore.concat(ignore)
204+
opts.eslintConfig.ignorePattern = opts.eslintConfig.ignorePattern.concat(ignore)
190205
}
191206

192207
function addGlobals (globals) {

‎package.json

+9-11
Original file line numberDiff line numberDiff line change
@@ -38,26 +38,24 @@
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",
4544
"xdg-basedir": "^4.0.0"
4645
},
4746
"devDependencies": {
4847
"babel-eslint": "^10.1.0",
49-
"cross-spawn": "^7.0.2",
50-
"eslint": "^6.8.0",
51-
"eslint-config-standard": "^14.1.1",
52-
"eslint-config-standard-jsx": "^8.1.0",
53-
"eslint-plugin-import": "^2.20.2",
48+
"cross-spawn": "^7.0.3",
49+
"eslint": "^7.12.1",
50+
"eslint-config-standard": "^15.0.1",
51+
"eslint-config-standard-jsx": "^9.0.0",
52+
"eslint-plugin-import": "^2.22.1",
5453
"eslint-plugin-node": "^11.1.0",
5554
"eslint-plugin-promise": "^4.2.1",
56-
"eslint-plugin-react": "^7.19.0",
57-
"eslint-plugin-standard": "^4.0.1",
58-
"mkdirp": "^0.5.5",
59-
"standard": "^14.3.3",
60-
"tape": "^5.0.0"
55+
"eslint-plugin-react": "^7.21.5",
56+
"eslint-plugin-standard": "^4.0.2",
57+
"standard": "^15.0.1",
58+
"tape": "^5.0.1"
6159
},
6260
"engines": {
6361
"node": ">=8.10"

‎test/clone.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
var crossSpawn = require('cross-spawn')
44
var fs = require('fs')
5-
var mkdirp = require('mkdirp')
65
var path = require('path')
76
var test = require('tape')
87

@@ -18,7 +17,7 @@ const pkg = {
1817
test('test `standard` repo', function (t) {
1918
t.plan(1)
2019

21-
mkdirp.sync(TMP)
20+
fs.mkdirSync(TMP, { recursive: true })
2221

2322
var name = pkg.name
2423
var url = pkg.repo + '.git'

0 commit comments

Comments
 (0)
Please sign in to comment.