sgrep: Install syntactic code search tool as an external linter.

Add sgrep (sgrep.dev) to tooling and include simple rule as
proof of concept. Included rule detects use of old django render
function.

Also added a rule that looks for if-else statements where both
code paths are identical.
This commit is contained in:
brendon 2020-03-18 16:32:26 -07:00 committed by Tim Abbott
parent f6503a4061
commit f65e6d0d94
6 changed files with 46 additions and 4 deletions

View File

@ -385,6 +385,9 @@ def main(options):
# Install shellcheck.
run_as_root(["tools/setup/install-shellcheck"])
# Install sgrep.
run_as_root(["tools/setup/install-sgrep"])
setup_venvs.main()
run_as_root(["cp", REPO_STOPWORDS_PATH, TSEARCH_STOPWORDS_PATH])

View File

@ -92,6 +92,11 @@ def run():
description="Checks commit messages for common formatting errors."
"(config: .gitlint)")
sgrep_command = ["sgrep-lint", "--config=./tools/sgrep.yml", "--error"]
linter_config.external_linter('sgrep-py', [*sgrep_command, "--lang=python"], ['py'],
description="Syntactic Grep (sgrep) Code Search Tool "
"(config: ./tools/sgrep.yml)")
@linter_config.lint
def custom_py():
# type: () -> int

View File

@ -415,9 +415,6 @@ python_rules = RuleList(
'description': "Don't use datetime in backend code.\n"
"See https://zulip.readthedocs.io/en/latest/contributing/code-style.html#naive-datetime-objects",
},
{'pattern': r'render_to_response\(',
'description': "Use render() instead of render_to_response().",
},
{'pattern': 'from os.path',
'description': "Don't use from when importing from the standard library",
},

19
tools/setup/install-sgrep Executable file
View File

@ -0,0 +1,19 @@
#!/usr/bin/env bash
set -e
version=0.4.9b5
tarball=sgrep-$version-ubuntu-16.04.tgz
sha256=9e57323fd0eb9133b7ff301a6be8361c073c3bfe6e6959ca1b622e5abc176e03
tarball_url=https://github.com/returntocorp/sgrep/releases/download/v$version/$tarball
if ! out="$(sgrep-lint --version 2>/dev/null)" || [[ "$out" != "$version" ]]
then
tmpdir="$(mktemp -d)"
trap 'rm -r "$tmpdir"' EXIT
cd "$tmpdir"
wget -nv "$tarball_url"
sha256sum -c <<< "$sha256 $tarball"
tar -xzf "$tarball" -C /usr/local/lib/
ln -sf /usr/local/lib/sgrep-lint-files/sgrep-lint /usr/local/bin/sgrep-lint
ln -sf /usr/local/lib/sgrep-lint-files/sgrep /usr/local/bin/sgrep
fi

18
tools/sgrep.yml Normal file
View File

@ -0,0 +1,18 @@
# See https://github.com/returntocorp/sgrep/blob/develop/docs/config.md for sgrep rule format
rules:
- id: deprecated-render-usage
pattern: django.shortcuts.render_to_response(...)
message: Use render() (from django.shortcuts) instead of render_to_response()
languages: [python]
severity: ERROR
- id: useless-if-body
patterns:
- pattern: |
if $X:
$S
else:
$S
message: "useless if statment; both blocks have the same body"
languages: [python]
severity: ERROR

View File

@ -34,4 +34,4 @@ DESKTOP_WARNING_VERSION = "5.0.0"
# historical commits sharing the same major version, in which case a
# minor version bump suffices.
PROVISION_VERSION = '75.5'
PROVISION_VERSION = '75.6'