From 5c3ff6761d50cf7f191f5e2641f750a2b6ac2e14 Mon Sep 17 00:00:00 2001 From: Nicholas Paun Date: Fri, 16 Aug 2024 09:29:13 -0700 Subject: [PATCH] Extend tools/cross/format.py to handle JS too --- .github/workflows/lint.yml | 5 ++++ package.json | 3 +- tools/cross/format.py | 60 +++++++++++++++++++++++++++++++------- 3 files changed, 57 insertions(+), 11 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index fcd0f16a569..72b1570591a 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -5,6 +5,7 @@ on: paths: - src/** - .clang-format + - .prettierc.json push: branches: - main @@ -24,8 +25,12 @@ jobs: chmod +x llvm.sh sudo ./llvm.sh 18 sudo apt-get install -y --no-install-recommends clang-format-18 + curl -fsSL https://deb.nodesource.com/setup_22.x | sudo bash + sudo apt-get install -y nodejs + npm i -g prettier - name: Lint run: | python3 ./tools/cross/format.py --check env: CLANG_FORMAT: clang-format-18 + PRETTIER: prettier diff --git a/package.json b/package.json index 8a1fef5b9ef..c02143b4952 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,8 @@ "name": "@cloudflare/workerd-root", "private": true, "scripts": { - "lint": "eslint types/src" + "lint": "eslint types/src", + "prettier": "prettier" }, "dependencies": { "capnp-ts": "^0.7.0", diff --git a/tools/cross/format.py b/tools/cross/format.py index 81153e79daf..078bebeaa73 100644 --- a/tools/cross/format.py +++ b/tools/cross/format.py @@ -5,9 +5,16 @@ import re import subprocess from argparse import ArgumentParser, Namespace -from typing import List, Optional +from typing import List, Optional, Tuple, Set CLANG_FORMAT = os.environ.get("CLANG_FORMAT", "clang-format") +PRETTIER = os.environ.get("PRETTIER") + + +if PRETTIER: + PRETTIER = [PRETTIER] +else: + PRETTIER = ["npm", "run", "-s", "prettier", "--"] def parse_args() -> Namespace: @@ -69,16 +76,16 @@ def check_clang_format() -> bool: exit(1) -def find_cpp_files(dir_path) -> List[str]: +def find_files_by_exts(dir_path: str, exts: Tuple[str, ...]) -> List[str]: files = [] for root, _, filenames in os.walk(dir_path): for filename in filenames: - if filename.endswith((".c++", ".h")): + if filename.endswith(exts): files.append(os.path.join(root, filename)) return files -def clang_format(files: List[str], check: bool = False): +def clang_format(files: List[str], check: bool = False) -> None: if not files: logging.info("No changes to format") exit(0) @@ -93,6 +100,19 @@ def clang_format(files: List[str], check: bool = False): subprocess.run([CLANG_FORMAT, "--verbose", "-i"] + files, check=False) +def prettier(files: List[str], check: bool = False) -> None: + if not files: + logging.info("No changes to format") + exit(0) + if check: + result = subprocess.run([*PRETTIER, "--check", *files]) + if result.returncode != 0 : + logging.error("Code has lint. Fix with python ./tools/cross/format.py") + exit(1) + else: + subprocess.run([*PRETTIER, "--write", *files]) + + def git_get_modified_files( target: str, source: Optional[str], staged: bool ) -> List[str]: @@ -114,19 +134,39 @@ def git_get_modified_files( return files_in_diff +def format_cpp(files_in_diff: Optional[Set[str]], check: bool) -> None: + cpp_files = set(find_files_by_exts("src/workerd", (".c++", ".h"))) + + if files_in_diff: + cpp_files &= files_in_diff + + clang_format(list(cpp_files), check) + + +def format_js(files_in_diff: Optional[Set[str]], check: bool) -> None: + js_files = set(find_files_by_exts("src", (".js", ".ts", ".json"))) + + if files_in_diff: + js_files &= files_in_diff + + prettier(list(js_files), check) + + def main(): options = parse_args() check_clang_format() - cpp_files = find_cpp_files("src/workerd") if options.subcommand == "git": - files_in_diff = git_get_modified_files( + files_in_diff = set(git_get_modified_files( options.target, options.source, options.staged - ) - cpp_files = list(set(cpp_files) & set(files_in_diff)) + )) + else: + files_in_diff = None - clang_format(cpp_files, options.check) + + format_cpp(files_in_diff, options.check) + format_js(files_in_diff, options.check) - # TODO: lint js, ts, bazel files + # TODO: lint bazel files if __name__ == "__main__":