From 399e1e54ea0a1770c58c116bc3826167eb20f1bc Mon Sep 17 00:00:00 2001 From: Jason Kim Date: Fri, 25 Oct 2024 16:22:19 -0700 Subject: [PATCH] [#24671] YSQL: add postgres linter Summary: Add a basic linter for postgres code using [arc lint script-and-regex][1]. Implement the script using a bash script that runs some grep searches on the given file. It is not powerful enough to check for certain things such as whether indentation is aligned properly with respect to other lines, but it is still better to have some basic enforcement over nothing. [1]: https://secure.phabricator.com/book/phabricator/article/arcanist_lint_script_and_regex/ Jira: DB-13743 Test Plan: manually check arc lint on a few postgres files Close: #24671 Reviewers: mihnea, tfoucher Reviewed By: tfoucher Subscribers: yql Differential Revision: https://phorge.dev.yugabyte.com/D36535 --- .arclint | 22 ++++++++++++++++++++-- src/postgres/ybsimplelint.sh | 13 +++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) create mode 100755 src/postgres/ybsimplelint.sh diff --git a/.arclint b/.arclint index b8766f017bae..17dc556b0380 100644 --- a/.arclint +++ b/.arclint @@ -8,7 +8,6 @@ "([.]sql$)", "([.]txt$)", "(^src/odyssey/.*$)", - "(^src/postgres/.*$)", "(^src/yb/integration-tests/upgrade-tests/builds.xml$)", "(^[.]gitmodules$)", "(^managed/src/main/resources/alert/alert_templates[.]yml$)", @@ -101,19 +100,25 @@ "(^docs/.*[.]md$)", "(^[.]fossa[.]yml$)", "(^managed/.*[.]conf)", + "(^src/postgres/)", "(^[.]gitignore$)" ] }, "pycodestyle": { "type": "pep8", "include": "([.]py$)", - "exclude": "(^thirdparty/|^[.]ycm_extra_conf[.]py$)", + "exclude": [ + "(^[.]ycm_extra_conf[.]py$)", + "(^src/postgres/)", + "(^thirdparty/)" + ], "bin": "pycodestyle", "flags": ["--max-line-length=100"] }, "googlecpplint": { "type": "googlecpplint", "exclude": [ + "(^src/postgres/)", "(^src/yb/yql/cql/ql/kwlist[.]h$)", "(^src/yb/gutil/linux_syscall_support[.]h$)", "(^src/yb/gutil/cycleclock-inl[.]h$)", @@ -140,6 +145,19 @@ "(^build/c[+][+]11$)": "disabled", "(.*)": "error" } + }, + "postgres": { + "type": "script-and-regex", + "exclude": [ + "(^src/yb/)" + ], + "include": [ + "([.]c$)", + "([.]h$)" + ], + "script-and-regex.script": "src/postgres/ybsimplelint.sh", + "script-and-regex.regex": + "/^(?P\\w+):(?P\\w+):(?P\\d+):(?P.*)/m" } } } diff --git a/src/postgres/ybsimplelint.sh b/src/postgres/ybsimplelint.sh new file mode 100755 index 000000000000..b85393764442 --- /dev/null +++ b/src/postgres/ybsimplelint.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash +# Simple linter for postgres code. +grep -nE '\s+$' "$1" | sed 's/^/error:trailing_whitespace:/' +grep -nvE '^( * {0,3}\S|$)' "$1" | sed 's/^/error:leading_whitespace:/' +grep -nE '(\)|else|else if)\s+{$' "$1" | sed 's/^/warning:likely_bad_brace:/' + +grep -nE ',\s*errmsg(_plural)?\(' "$1" | sed 's/^/error:missing_linebreak_before_errmsg:/' +grep -nE ',\s*errdetail(_plural)?\(' "$1" | sed 's/^/error:missing_linebreak_before_errdetail:/' +grep -nE ',\s*errhint\(' "$1" | sed 's/^/error:missing_linebreak_before_errhint:/' + +grep -nE 'errmsg(_plural)?\("[A-Z][a-z]' "$1" | sed 's/^/warning:likely_bad_capitalization_in_errmsg:/' +grep -nE 'errdetail(_plural)?\("[a-z]' "$1" | sed 's/^/warning:likely_bad_lowercase_in_errdetail:/' +grep -nE 'errhint\("[a-z]' "$1" | sed 's/^/warning:likely_bad_lowercase_in_errhint:/'