Skip to content

Commit

Permalink
Extend tools/cross/format.py to handle JS too
Browse files Browse the repository at this point in the history
  • Loading branch information
npaun committed Aug 16, 2024
1 parent 7082e84 commit d347b12
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ on:
paths:
- src/**
- .clang-format
- .prettierc.json
push:
branches:
- main
Expand All @@ -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
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
60 changes: 50 additions & 10 deletions tools/cross/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand All @@ -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]:
Expand All @@ -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__":
Expand Down

0 comments on commit d347b12

Please sign in to comment.