Skip to content

Commit

Permalink
[presubmit] Add JS formatting for tools/system-analyzer
Browse files Browse the repository at this point in the history
Bug: v8:10670
Change-Id: Ifb653ada003719faff261b6e5b2169db37cffdaf
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2282522
Reviewed-by: Tamer Tas <tmrts@chromium.org>
Commit-Queue: Sathya Gunasekaran  <gsathya@chromium.org>
Cr-Commit-Position: refs/heads/master@{#68909}
  • Loading branch information
gsathya authored and Commit Bot committed Jul 17, 2020
1 parent b45752c commit 1ec8f1d
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
.cproject
.gclient_entries
.gdb_history
.jslint-cache
.landmines
.project
.pydevproject
Expand Down
10 changes: 10 additions & 0 deletions PRESUBMIT.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def _V8PresubmitChecks(input_api, output_api):
sys.path.append(input_api.os_path.join(
input_api.PresubmitLocalPath(), 'tools'))
from v8_presubmit import CppLintProcessor
from v8_presubmit import JSLintProcessor
from v8_presubmit import TorqueLintProcessor
from v8_presubmit import SourceProcessor
from v8_presubmit import StatusFilesProcessor
Expand All @@ -93,6 +94,11 @@ def FilterTorqueFile(affected_file):
affected_file,
white_list=(r'.+\.tq'))

def FilterJSFile(affected_file):
return input_api.FilterSourceFile(
affected_file,
white_list=(r'.+\.m?js'))

results = []
if not CppLintProcessor().RunOnFiles(
input_api.AffectedFiles(file_filter=FilterFile, include_deletes=False)):
Expand All @@ -101,6 +107,10 @@ def FilterTorqueFile(affected_file):
input_api.AffectedFiles(file_filter=FilterTorqueFile,
include_deletes=False)):
results.append(output_api.PresubmitError("Torque format check failed"))
if not JSLintProcessor().RunOnFiles(
input_api.AffectedFiles(file_filter=FilterJSFile,
include_deletes=False)):
results.append(output_api.PresubmitError("JS format check failed"))
if not SourceProcessor().RunOnFiles(
input_api.AffectedFiles(include_deletes=False)):
results.append(output_api.PresubmitError(
Expand Down
55 changes: 55 additions & 0 deletions tools/v8_presubmit.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,31 @@ def TorqueLintWorker(command):
print('Error running format-torque.py')
process.kill()

def JSLintWorker(command):
try:
file_name = command[-1]
with open(file_name, "r") as file_handle:
contents = file_handle.read()

process = subprocess.Popen(command, stdout=PIPE, stderr=subprocess.PIPE)
output, err = process.communicate()
rc = process.returncode
if rc != 0:
sys.stdout.write("error code " + str(rc) + " running clang-format.\n")
return rc

if output != contents:
sys.stdout.write(file_name + " requires formatting.\n")
return 1

return 0
except KeyboardInterrupt:
process.kill()
except Exception:
print('Error running clang-format. Please make sure you have depot_tools' +
' in your $PATH. Lint check skipped.')
process.kill()

class FileContentsCache(object):

def __init__(self, sums_file_name):
Expand Down Expand Up @@ -392,6 +417,33 @@ def GetProcessorScript(self):

return None, arguments

class JSLintProcessor(CacheableSourceFileProcessor):
"""
Check .{m}js file to verify they follow the JS Style guide.
"""
def __init__(self, use_cache=True):
super(JSLintProcessor, self).__init__(
use_cache=use_cache, cache_file_path='.jslint-cache',
file_type='JavaScript')

def IsRelevant(self, name):
return name.endswith('.js') or name.endswith('.mjs')

def GetPathsToSearch(self):
return ['tools/system-analyzer']

def GetProcessorWorker(self):
return JSLintWorker

def GetProcessorScript(self):
for path in [TOOLS_PATH] + os.environ["PATH"].split(os.pathsep):
path = path.strip('"')
clang_format = os.path.join(path, 'clang_format.py')
if os.path.isfile(clang_format):
return clang_format, []

return None, []

COPYRIGHT_HEADER_PATTERN = re.compile(
r'Copyright [\d-]*20[0-2][0-9] the V8 project authors. All rights reserved.')

Expand Down Expand Up @@ -708,6 +760,9 @@ def Main():
print("Running Torque formatting check...")
success &= TorqueLintProcessor(use_cache=use_linter_cache).RunOnPath(
workspace)
print("Running JavaScript formatting check...")
success &= JSLintProcessor(use_cache=use_linter_cache).RunOnPath(
workspace)
print("Running copyright header, trailing whitespaces and " \
"two empty lines between declarations check...")
success &= SourceProcessor().RunOnPath(workspace)
Expand Down

0 comments on commit 1ec8f1d

Please sign in to comment.