From 7c8709db0d2eb232c08eaadc05875e45dfe06e1e Mon Sep 17 00:00:00 2001 From: Aleksey Khoroshilov Date: Thu, 8 Dec 2022 16:15:43 +0700 Subject: [PATCH] Add web dev style guide presubmit checks and --fix support. --- PRESUBMIT.py | 19 +++++++++++ build/commands/lib/util.js | 3 ++ build/commands/scripts/commands.js | 1 + chromium_src/tools/web_dev_style/eslint.py | 24 ++++++++++++++ patches/tools-web_dev_style-eslint.py.patch | 12 +++++++ script/brave_node.py | 35 +++++++++++++++++++++ script/chromium_presubmit_overrides.py | 17 ++++++++-- 7 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 chromium_src/tools/web_dev_style/eslint.py create mode 100644 patches/tools-web_dev_style-eslint.py.patch create mode 100755 script/brave_node.py diff --git a/PRESUBMIT.py b/PRESUBMIT.py index 0ba90622ce6..2ad2d4fdf7b 100644 --- a/PRESUBMIT.py +++ b/PRESUBMIT.py @@ -6,6 +6,8 @@ import os import sys +import override_utils + USE_PYTHON3 = True PRESUBMIT_VERSION = '2.0.0' @@ -124,3 +126,20 @@ def CheckLicense(input_api, output_api): output_api.PresubmitPromptWarning(expected_license_message, items=bad_files)) return result + + +def CheckWebDevStyle(input_api, output_api): + results = [] + try: + old_sys_path = sys.path[:] + cwd = input_api.PresubmitLocalPath() + sys.path += [input_api.os_path.join(cwd, '..', 'tools')] + # pylint: disable=import-error, import-outside-toplevel + from web_dev_style import presubmit_support + with override_utils.override_scope_variable(output_api, + 'PresubmitPromptWarning', + output_api.PresubmitError): + results += presubmit_support.CheckStyle(input_api, output_api) + finally: + sys.path = old_sys_path + return results diff --git a/build/commands/lib/util.js b/build/commands/lib/util.js index d7f631adbda..0a88f351505 100644 --- a/build/commands/lib/util.js +++ b/build/commands/lib/util.js @@ -710,6 +710,9 @@ const util = { if (options.verbose) { args.push(...Array(options.verbose).fill('--verbose')) } + if (options.fix) { + cmd_options.env.PRESUBMIT_FIX = '1' + } util.run(cmd, args, cmd_options) }, diff --git a/build/commands/scripts/commands.js b/build/commands/scripts/commands.js index 35431a6c641..deb4c7c9253 100644 --- a/build/commands/scripts/commands.js +++ b/build/commands/scripts/commands.js @@ -277,6 +277,7 @@ program .option('--files ', 'semicolon-separated list files to run presubmit on') .option('--verbose [arg]', 'pass --verbose 2 for more debugging info', JSON.parse) + .option('--fix', 'try to fix found issues automatically') .action(util.presubmit) program diff --git a/chromium_src/tools/web_dev_style/eslint.py b/chromium_src/tools/web_dev_style/eslint.py new file mode 100644 index 00000000000..df548c8f6ff --- /dev/null +++ b/chromium_src/tools/web_dev_style/eslint.py @@ -0,0 +1,24 @@ +# Copyright (c) 2022 The Brave Authors. All rights reserved. +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this file, +# You can obtain one at https://mozilla.org/MPL/2.0/. + +import os + +import brave_node +import override_utils + + +@override_utils.override_function(globals()) +def Run(_original_function, **kwargs): + node_args = [ + brave_node.PathInNodeModules('eslint', 'bin', 'eslint'), + '--quiet', + '--resolve-plugins-relative-to', + brave_node.PathInNodeModules(), + ] + if os.environ.get('PRESUBMIT_FIX') == '1': + node_args += ['--fix'] + node_args += kwargs['args'] + + return brave_node.RunNode(node_args) diff --git a/patches/tools-web_dev_style-eslint.py.patch b/patches/tools-web_dev_style-eslint.py.patch new file mode 100644 index 00000000000..d947ffa7d23 --- /dev/null +++ b/patches/tools-web_dev_style-eslint.py.patch @@ -0,0 +1,12 @@ +diff --git a/tools/web_dev_style/eslint.py b/tools/web_dev_style/eslint.py +index 18be4f4b076888f18538323096e84d52a8659a5c..e29cc0173d69cb9ff5d972b7c210bf25714b5049 100755 +--- a/tools/web_dev_style/eslint.py ++++ b/tools/web_dev_style/eslint.py +@@ -26,6 +26,7 @@ def Run(os_path=None, args=None): + ] + args) + + ++from import_inline import inline_file_from_src; inline_file_from_src("brave/chromium_src/tools/web_dev_style/eslint.py", globals(), locals()) + if __name__ == '__main__': + import os + import sys diff --git a/script/brave_node.py b/script/brave_node.py new file mode 100755 index 00000000000..9d11670d276 --- /dev/null +++ b/script/brave_node.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python3 +# Copyright (c) 2022 The Brave Authors. All rights reserved. +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this file, +# You can obtain one at https://mozilla.org/MPL/2.0/. + +import subprocess +import sys +import os + +NODE_MODULES = os.path.join(os.path.dirname(__file__), '..', 'node_modules') + + +def PathInNodeModules(*args): + return os.path.join(NODE_MODULES, *args) + + +def RunNode(cmd_parts): + cmd = ['node'] + cmd_parts + process = subprocess.Popen(cmd, + cwd=os.getcwd(), + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + universal_newlines=True) + stdout, stderr = process.communicate() + + if process.returncode != 0: + err = stderr if len(stderr) > 0 else stdout + raise RuntimeError('Command \'%s\' failed\n%s' % (' '.join(cmd), err)) + + return stdout + + +if __name__ == '__main__': + RunNode(sys.argv[1:]) diff --git a/script/chromium_presubmit_overrides.py b/script/chromium_presubmit_overrides.py index 79d541960d0..304f25810f0 100644 --- a/script/chromium_presubmit_overrides.py +++ b/script/chromium_presubmit_overrides.py @@ -9,6 +9,7 @@ import copy import inspect +import os import re import sys @@ -129,7 +130,18 @@ def override_canned_checks(canned_checks): 'bypass_warnings': False, 'check_python': True, } - result = original_check(input_api, output_api, **kwargs) + + # pylint: disable=import-outside-toplevel + import git_cl + + def RunGitWithCode(original_function, args, **kwargs): + if input_api.PRESUBMIT_FIX and '--dry-run' in args: + args.remove('--dry-run') + return original_function(args, **kwargs) + + with override_utils.override_scope_function(git_cl, RunGitWithCode): + result = original_check(input_api, output_api, **kwargs) + # If presubmit generates "Please run git cl format --js" message, we # should replace the command with "npm run format -- --js". The # order of these replacements ensure we do this properly. @@ -169,9 +181,10 @@ def override_canned_checks(canned_checks): # Overrides canned checks and installs per-check file filter. def modify_input_api(input_api): + input_api.DEFAULT_FILES_TO_SKIP += (*config['default_files_to_skip'], ) + input_api.PRESUBMIT_FIX = os.environ.get('PRESUBMIT_FIX') == '1' override_canned_checks(input_api.canned_checks) setup_per_check_file_filter(input_api) - input_api.DEFAULT_FILES_TO_SKIP += (*config['default_files_to_skip'], ) # Disables checks or forces presubmit errors for checks listed in the config.