Skip to content

Commit

Permalink
chore: add --fix option to lint:cpp (electron#14977)
Browse files Browse the repository at this point in the history
* chore: add --fix option to lint:cpp

* fix lint errors
  • Loading branch information
nornagon authored and codebytere committed Oct 16, 2018
1 parent af0ac9b commit 2a4f5c3
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 29 deletions.
13 changes: 6 additions & 7 deletions atom/browser/atom_permission_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,12 @@ class AtomPermissionManager : public content::PermissionControllerDelegate {
bool user_gesture,
const base::Callback<void(blink::mojom::PermissionStatus)>& callback)
override;
int RequestPermissionWithDetails(
content::PermissionType permission,
content::RenderFrameHost* render_frame_host,
const GURL& requesting_origin,
bool user_gesture,
const base::DictionaryValue* details,
const StatusCallback& callback);
int RequestPermissionWithDetails(content::PermissionType permission,
content::RenderFrameHost* render_frame_host,
const GURL& requesting_origin,
bool user_gesture,
const base::DictionaryValue* details,
const StatusCallback& callback);
int RequestPermissions(
const std::vector<content::PermissionType>& permissions,
content::RenderFrameHost* render_frame_host,
Expand Down
5 changes: 2 additions & 3 deletions atom/browser/web_contents_permission_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ class WebContentsPermissionHelper

// Asynchronous Requests
void RequestFullscreenPermission(const base::Callback<void(bool)>& callback);
void RequestMediaAccessPermission(
const content::MediaStreamRequest& request,
content::MediaResponseCallback callback);
void RequestMediaAccessPermission(const content::MediaStreamRequest& request,
content::MediaResponseCallback callback);
void RequestWebNotificationPermission(
const base::Callback<void(bool)>& callback);
void RequestPointerLockPermission(bool user_gesture);
Expand Down
13 changes: 10 additions & 3 deletions script/lint.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ const LINTERS = [ {
roots: ['atom', 'brightray'],
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) {
Expand All @@ -61,7 +66,6 @@ const LINTERS = [ {
}
}
if (result.status) {
if (opts.fix) spawnAndCheckExitCode('python', ['script/run-clang-format.py', ...filenames])
process.exit(result.status)
}
}
Expand Down Expand Up @@ -157,7 +161,7 @@ async function findFiles (args, linter) {
if (args.changed) {
whitelist = await findChangedFiles(SOURCE_ROOT)
if (!whitelist.size) {
return filenames
return []
}
}

Expand All @@ -183,7 +187,10 @@ async function findFiles (args, linter) {
filenames = filenames.filter(x => whitelist.has(x))
}

return filenames
// 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 () {
Expand Down
43 changes: 27 additions & 16 deletions script/run-clang-format.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ def run_clang_format_diff(args, file_name):
except IOError as exc:
raise DiffError(str(exc))
invocation = [args.clang_format_executable, file_name]
if args.fix:
invocation.append('-i')
try:
proc = subprocess.Popen(
' '.join(invocation),
Expand All @@ -129,6 +131,8 @@ def run_clang_format_diff(args, file_name):
if proc.returncode:
raise DiffError("clang-format exited with status {}: '{}'".format(
proc.returncode, file_name), errs)
if args.fix:
return None, errs
return make_diff(file_name, original, outs), errs


Expand Down Expand Up @@ -190,6 +194,10 @@ def main():
help='comma separated list of file extensions (default: {})'.format(
DEFAULT_EXTENSIONS),
default=DEFAULT_EXTENSIONS)
parser.add_argument(
'--fix',
help='if specified, reformat files in-place',
action='store_true')
parser.add_argument(
'-r',
'--recursive',
Expand Down Expand Up @@ -281,8 +289,9 @@ def main():
njobs = multiprocessing.cpu_count() + 1
njobs = min(len(files), njobs)

patch_file = tempfile.NamedTemporaryFile(delete=False,
prefix='electron-format-')
if not args.fix:
patch_file = tempfile.NamedTemporaryFile(delete=False,
prefix='electron-format-')

if njobs == 1:
# execute directly instead of in a pool,
Expand Down Expand Up @@ -316,20 +325,22 @@ def main():
sys.stderr.writelines(errs)
if outs == []:
continue
if not args.quiet:
print_diff(outs, use_color=colored_stdout)
for line in outs:
patch_file.write(line)
patch_file.write('\n')
if retcode == ExitStatus.SUCCESS:
retcode = ExitStatus.DIFF

if patch_file.tell() == 0:
patch_file.close()
os.unlink(patch_file.name)
else:
print("\nTo patch these files, run:\n$ git apply {}\n"
.format(patch_file.name))
if not args.fix:
if not args.quiet:
print_diff(outs, use_color=colored_stdout)
for line in outs:
patch_file.write(line)
patch_file.write('\n')
if retcode == ExitStatus.SUCCESS:
retcode = ExitStatus.DIFF

if not args.fix:
if patch_file.tell() == 0:
patch_file.close()
os.unlink(patch_file.name)
else:
print("\nTo patch these files, run:\n$ git apply {}\n"
.format(patch_file.name))

return retcode

Expand Down

0 comments on commit 2a4f5c3

Please sign in to comment.