Skip to content

Commit

Permalink
Reuse the DelegatingLinter class when possible.
Browse files Browse the repository at this point in the history
Summary:
It turns out that while khan-linter collects files up by linter, it
does it by linter *object*, not linter *class*.  And I was creating a
new DelegatingLinter object for every file, meaning we weren't doing
any collecting at all.  Now we are.

Test Plan:
In webapp-root I ran
    git ls-files '*.yaml' | xargs ka-lint -v
and saw just one "Running DelegatingLinter" line, whereas before I saw
one per file.

Reviewers: benkraft

Reviewed By: benkraft

Subscribers: aviddy, kevinb

Differential Revision: https://phabricator.khanacademy.org/D62483
  • Loading branch information
csilvers committed Apr 21, 2020
1 parent d8361cb commit b2bf12e
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions runlint.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,18 @@ def _find_repo_config(file_to_lint):
_REPO_CONFIG_CACHE[repo_config_file])


_DELEGATING_LINTER_CACHE = {}


def _get_delegating_linter(cmd, repo_config_dir):
"""Return a (hopefully cached) linter running this cmd from this dir."""
key = (tuple(cmd), repo_config_dir)
if key not in _DELEGATING_LINTER_CACHE:
_DELEGATING_LINTER_CACHE[key] = linters.DelegatingLinter(
cmd, repo_config_dir, _get_logger())
return _DELEGATING_LINTER_CACHE[key]


def _get_linters_for_file(file_to_lint, lang, propose_arc_fixes):
"""Return the linters we wish to run for this file.
Expand Down Expand Up @@ -694,10 +706,8 @@ def _get_linters_for_file(file_to_lint, lang, propose_arc_fixes):
if repo_config:
cmds = repo_config.get('khan-linter-overrides', {}).get(file_lang)
if cmds is not None:
return [
linters.DelegatingLinter(cmd, repo_config_dir, _get_logger())
for cmd in cmds
]
return [_get_delegating_linter(cmd, repo_config_dir)
for cmd in cmds]

# We support multiple configuration files for eslint and our graphql
# schema linter, , which allows runlint to run against subrepos with
Expand Down

0 comments on commit b2bf12e

Please sign in to comment.