forked from electron/electron
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlint.js
executable file
·219 lines (198 loc) · 7.52 KB
/
lint.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/env node
const { GitProcess } = require('dugite')
const childProcess = require('child_process')
const fs = require('fs')
const klaw = require('klaw')
const minimist = require('minimist')
const path = require('path')
const SOURCE_ROOT = path.normalize(path.dirname(__dirname))
const DEPOT_TOOLS = path.resolve(SOURCE_ROOT, '..', 'third_party', 'depot_tools')
const BLACKLIST = new Set([
['atom', 'browser', 'mac', 'atom_application.h'],
['atom', 'browser', 'mac', 'atom_application_delegate.h'],
['atom', 'browser', 'resources', 'win', 'resource.h'],
['atom', 'browser', 'notifications', 'mac', 'notification_center_delegate.h'],
['atom', 'browser', 'ui', 'cocoa', 'atom_menu_controller.h'],
['atom', 'browser', 'ui', 'cocoa', 'atom_ns_window.h'],
['atom', 'browser', 'ui', 'cocoa', 'atom_ns_window_delegate.h'],
['atom', 'browser', 'ui', 'cocoa', 'atom_preview_item.h'],
['atom', 'browser', 'ui', 'cocoa', 'atom_touch_bar.h'],
['atom', 'browser', 'ui', 'cocoa', 'atom_inspectable_web_contents_view.h'],
['atom', 'browser', 'ui', 'cocoa', 'event_dispatching_window.h'],
['atom', 'browser', 'ui', 'cocoa', 'touch_bar_forward_declarations.h'],
['atom', 'browser', 'ui', 'cocoa', 'NSColor+Hex.h'],
['atom', 'browser', 'ui', 'cocoa', 'NSString+ANSI.h'],
['atom', 'common', 'api', 'api_messages.h'],
['atom', 'common', 'common_message_generator.cc'],
['atom', 'common', 'common_message_generator.h'],
['atom', 'common', 'node_includes.h'],
['atom', 'node', 'osfhandle.cc'],
['spec', 'static', 'jquery-2.0.3.min.js']
].map(tokens => path.join(SOURCE_ROOT, ...tokens)))
function spawnAndCheckExitCode (cmd, args, opts) {
opts = Object.assign({ stdio: 'inherit' }, opts)
const status = childProcess.spawnSync(cmd, args, opts).status
if (status) process.exit(status)
}
const LINTERS = [ {
key: 'c++',
roots: ['atom'],
test: filename => filename.endsWith('.cc') || filename.endsWith('.h'),
run: (opts, filenames) => {
if (opts.fix) {
spawnAndCheckExitCode('python', ['script/run-clang-format.py', '--fix', ...filenames])
} else {
spawnAndCheckExitCode('python', ['script/run-clang-format.py', ...filenames])
}
const result = childProcess.spawnSync('cpplint.py', filenames, { encoding: 'utf8' })
// cpplint.py writes EVERYTHING to stderr, including status messages
if (result.stderr) {
for (const line of result.stderr.split(/[\r\n]+/)) {
if (line.length && !line.startsWith('Done processing ') && line !== 'Total errors found: 0') {
console.warn(line)
}
}
}
if (result.status) {
process.exit(result.status)
}
}
}, {
key: 'python',
roots: ['script'],
test: filename => filename.endsWith('.py'),
run: (opts, filenames) => {
const rcfile = path.join(DEPOT_TOOLS, 'pylintrc')
const args = ['--rcfile=' + rcfile, ...filenames]
const env = Object.assign({ PYTHONPATH: path.join(SOURCE_ROOT, 'script') }, process.env)
spawnAndCheckExitCode('pylint.py', args, { env })
}
}, {
key: 'javascript',
roots: ['lib', 'spec', 'script', 'default_app'],
ignoreRoots: ['spec/node_modules'],
test: filename => filename.endsWith('.js'),
run: (opts, filenames) => {
const cmd = path.join(SOURCE_ROOT, 'node_modules', '.bin', 'eslint')
const args = [ '--cache', ...filenames ]
if (opts.fix) args.unshift('--fix')
spawnAndCheckExitCode(cmd, args, { cwd: SOURCE_ROOT })
}
}, {
key: 'gn',
roots: ['.'],
test: filename => filename.endsWith('.gn') || filename.endsWith('.gni'),
run: (opts, filenames) => {
const allOk = filenames.map(filename => {
const env = Object.assign({
CHROMIUM_BUILDTOOLS_PATH: path.resolve(SOURCE_ROOT, '..', 'buildtools'),
DEPOT_TOOLS_WIN_TOOLCHAIN: '0'
}, process.env)
// Users may not have depot_tools in PATH.
env.PATH = `${env.PATH}${path.delimiter}${DEPOT_TOOLS}`
const args = ['format', filename]
if (!opts.fix) args.push('--dry-run')
const result = childProcess.spawnSync('gn', args, { env, stdio: 'inherit', shell: true })
if (result.status === 0) {
return true
} else if (result.status === 2) {
console.log(`GN format errors in "${filename}". Run 'gn format "${filename}"' or rerun with --fix to fix them.`)
return false
} else {
console.log(`Error running 'gn format --dry-run "${filename}"': exit code ${result.status}`)
return false
}
}).every(x => x)
if (!allOk) {
process.exit(1)
}
}
}]
function parseCommandLine () {
let help
const opts = minimist(process.argv.slice(2), {
boolean: [ 'c++', 'javascript', 'python', 'gn', 'help', 'changed', 'fix', 'verbose' ],
alias: { 'c++': ['cc', 'cpp', 'cxx'], javascript: ['js', 'es'], python: 'py', changed: 'c', help: 'h', verbose: 'v' },
unknown: arg => { help = true }
})
if (help || opts.help) {
console.log('Usage: script/lint.js [--cc] [--js] [--py] [-c|--changed] [-h|--help] [-v|--verbose] [--fix]')
process.exit(0)
}
return opts
}
async function findChangedFiles (top) {
const result = await GitProcess.exec(['diff', '--name-only', '--cached'], top)
if (result.exitCode !== 0) {
console.log('Failed to find changed files', GitProcess.parseError(result.stderr))
process.exit(1)
}
const relativePaths = result.stdout.split(/\r\n|\r|\n/g)
const absolutePaths = relativePaths.map(x => path.join(top, x))
return new Set(absolutePaths)
}
async function findMatchingFiles (top, test) {
return new Promise((resolve, reject) => {
const matches = []
klaw(top)
.on('end', () => resolve(matches))
.on('data', item => {
if (test(item.path)) {
matches.push(item.path)
}
})
})
}
async function findFiles (args, linter) {
let filenames = []
let whitelist = null
// build the whitelist
if (args.changed) {
whitelist = await findChangedFiles(SOURCE_ROOT)
if (!whitelist.size) {
return []
}
}
// accumulate the raw list of files
for (const root of linter.roots) {
const files = await findMatchingFiles(path.join(SOURCE_ROOT, root), linter.test)
filenames.push(...files)
}
for (const ignoreRoot of (linter.ignoreRoots) || []) {
const ignorePath = path.join(SOURCE_ROOT, ignoreRoot)
if (!fs.existsSync(ignorePath)) continue
const ignoreFiles = new Set(await findMatchingFiles(ignorePath, linter.test))
filenames = filenames.filter(fileName => !ignoreFiles.has(fileName))
}
// remove blacklisted files
filenames = filenames.filter(x => !BLACKLIST.has(x))
// if a whitelist exists, remove anything not in it
if (whitelist) {
filenames = filenames.filter(x => whitelist.has(x))
}
// it's important that filenames be relative otherwise clang-format will
// produce patches with absolute paths in them, which `git apply` will refuse
// to apply.
return filenames.map(x => path.relative(SOURCE_ROOT, x))
}
async function main () {
const opts = parseCommandLine()
// no mode specified? run 'em all
if (!opts['c++'] && !opts.javascript && !opts.python && !opts.gn) {
opts['c++'] = opts.javascript = opts.python = opts.gn = true
}
const linters = LINTERS.filter(x => opts[x.key])
for (const linter of linters) {
const filenames = await findFiles(opts, linter)
if (filenames.length) {
if (opts.verbose) { console.log(`linting ${filenames.length} ${linter.key} ${filenames.length === 1 ? 'file' : 'files'}`) }
linter.run(opts, filenames)
}
}
}
if (process.mainModule === module) {
main().catch((error) => {
console.error(error)
process.exit(1)
})
}