forked from standard/standard
/
index.js
148 lines (125 loc) · 3.8 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
module.exports.lintText = lintText
module.exports.lintFiles = lintFiles
var deglob = require('deglob')
var dezalgo = require('dezalgo')
var eslint = require('eslint')
var extend = require('xtend')
var findRoot = require('find-root')
var fs = require('fs')
var os = require('os')
var path = require('path')
var pkgConfig = require('pkg-config')
var DEFAULT_PATTERNS = [
'**/*.js',
'**/*.jsx'
]
var DEFAULT_IGNORE_PATTERNS = [
'coverage/**',
'node_modules/**',
'**/*.min.js',
'**/bundle.js'
]
var DEFAULT_CONFIG = {
configFile: path.join(__dirname, 'rc', '.eslintrc'),
rulePaths: [ path.join(__dirname, 'rules') ],
useEslintrc: false
}
/**
* Lint text to enforce JavaScript Standard Style.
*
* @param {string} text file text to lint
* @param {Object=} opts options object
* @param {string=} opts.parser custom js parser (e.g. babel-eslint, esprima-fb)
* @param {function(Error, Object)} cb callback
*/
function lintText (text, opts, cb) {
if (typeof opts === 'function') {
cb = opts
opts = {}
}
opts = parseOpts(opts)
cb = dezalgo(cb)
var result
try {
result = new eslint.CLIEngine(opts._config).executeOnText(text)
} catch (err) {
return cb(err)
}
return cb(null, result)
}
/**
* Lint files to enforce JavaScript Standard Style.
*
* @param {Array.<string>} files file globs to lint
* @param {Object=} opts options object
* @param {Array.<String>=} opts.ignore file globs to ignore (has sane defaults)
* @param {string=} opts.cwd current working directory (default: process.cwd())
* @param {string=} opts.parser custom js parser (e.g. babel-eslint, esprima-fb)
* @param {function(Error, Object)} cb callback
*/
function lintFiles (files, opts, cb) {
if (typeof opts === 'function') {
cb = opts
opts = {}
}
opts = parseOpts(opts)
cb = dezalgo(cb)
if (typeof files === 'string') files = [ files ]
if (files.length === 0) files = DEFAULT_PATTERNS
var deglobOpts = {
ignore: opts.ignore,
cwd: opts.cwd,
useGitIgnore: true,
usePackageJson: true,
configKey: 'standard'
}
deglob(files, deglobOpts, function (err, allFiles) {
if (err) return cb(err)
// undocumented – do not use (used by bin/cmd.js)
if (opts._onFiles) opts._onFiles(allFiles)
var result
try {
result = new eslint.CLIEngine(opts._config).executeOnFiles(allFiles)
} catch (err) {
return cb(err)
}
return cb(null, result)
})
}
function parseOpts (opts) {
if (!opts) opts = {}
opts = extend(opts)
opts._config = extend(DEFAULT_CONFIG)
if (!opts.cwd) opts.cwd = process.cwd()
var ignore = DEFAULT_IGNORE_PATTERNS.slice(0) // passed into glob
if (opts.ignore) ignore.concat(opts.ignore)
opts.ignore = ignore
if (opts.parser) useCustomParser(opts.parser)
// Find package.json in the project root
var root
try {
root = findRoot(opts.cwd)
} catch (e) {}
if (root) {
var packageOpts = pkgConfig('standard', { root: false, cwd: opts.cwd })
if (packageOpts) {
// Use globals from package.json ("standard.global" property)
var globals = packageOpts.globals || packageOpts.global
if (globals) {
opts._config.globals = Array.isArray(globals)
? globals
: [ globals ]
}
// Use custom js parser from package.json ("standard.parser" property)
if (!opts.parser && packageOpts.parser) useCustomParser(packageOpts.parser)
}
}
function useCustomParser (parser) {
var configFile = JSON.parse(fs.readFileSync(DEFAULT_CONFIG.configFile, 'utf8'))
configFile.parser = parser
var tmpFilename = path.join(os.tmpdir(), '.eslintrc-' + parser)
fs.writeFileSync(tmpFilename, JSON.stringify(configFile))
opts._config.configFile = tmpFilename
}
return opts
}