From 7fa90c775ea28f72536d646e3ece9cb59771cd85 Mon Sep 17 00:00:00 2001 From: egor Date: Wed, 16 Jan 2019 17:06:32 +0100 Subject: [PATCH] Complete TODO: handle custom modification functions as well as regexp https://github.com/src-d/style-analyzer/issues/526#issuecomment-454801024 Signed-off-by: egor --- lookout/style/format/benchmarks/generate_smoke.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/lookout/style/format/benchmarks/generate_smoke.py b/lookout/style/format/benchmarks/generate_smoke.py index 0ff2f606d..9634fbc4e 100644 --- a/lookout/style/format/benchmarks/generate_smoke.py +++ b/lookout/style/format/benchmarks/generate_smoke.py @@ -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 @@ -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({