Skip to content

Commit

Permalink
Complete TODO: handle custom modification functions as well as regexp
Browse files Browse the repository at this point in the history
  • Loading branch information
egor committed Jan 16, 2019
1 parent 1099721 commit bc33690
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lookout/style/format/benchmarks/generate_smoke.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import re
import shutil
import tarfile
from types import FunctionType
from typing import Tuple

from dulwich.index import blob_from_path_and_stat, build_index_from_tree
Expand Down Expand Up @@ -103,14 +104,20 @@ def generate_smoke_entry(inputpath: str, outputpath: str, force: bool = False) -
writer = csv.DictWriter(index_file, fieldnames=["repo", "style", "from", "to"])
writer.writeheader()
for style_name, (pattern, repl) in js_format_rules.items():
# TODO(zurk): handle custom modification functions as well as regexp.
pattern = re.compile(pattern)
if isinstance(pattern, str):
pattern_ = re.compile(pattern)

def pattern(repl, code):
return re.sub(pattern_, repl, code)
else:
assert isinstance(pattern, FunctionType), "Expected function, got %s" % \
type(pattern)
for repopath in repopaths:
repo = Repo(str(repopath))
repo.hooks.clear() # Speed up dulwich by ~25%
for filepath in repopath.glob("**/*.js"):
code = filepath.read_text()
new_code = re.sub(pattern, repl, code)
new_code = pattern(repl, code)
filepath.write_text(new_code)
from_commit, to_commit = commit_style(repo, style_name)
writer.writerow({
Expand Down

0 comments on commit bc33690

Please sign in to comment.