-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore(lint): add ast-grep lint rules and CI workflow #14364
Conversation
0a8a2df
to
d18348b
Compare
fd8613a
to
8040409
Compare
.github/workflows/build_and_test.yml
Outdated
# NOTE: this is basically an inline of the official, public gh action | ||
# (https://github.com/ast-grep/action). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The official action is a full-on JavaScript wrapper around npm install ast-grep && ast-grep scan [...]
. With all the noise about github actions and needing to audit/lock things down, it makes more sense to just maintain these <10 lines of code ourselves.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I love it 😍 !
🏆
Yup! The config file (
|
really cool stuff 👍🏼 |
I love ast-grep 🦍❤️⚡ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
great change!
Calling test setup helper function in the wrong scope
good to see complaints, could you help me understand this rule, why are these use case not good?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is really good stuff 🚀
@iykon sure! The pattern that I want to catch and discourage is performing any test/fixture setup tasks at the module/file level or directly within a local function create_temp_file(context)
print("creating temp file in " .. context .. " context")
-- simulate slowness
ngx.sleep(1)
end
local temp = create_temp_file("file")
describe("my test suite #suite", function()
local other_temp = create_temp_file("describe()")
it("my test case #testcase", function()
-- do something with `temp`
end)
it("my second test case #testcase", function()
-- do something with `other_temp`
end)
end) If we put this into a file and evaluate its behavior, some problems appear:
Even just listing test cases with no intent to run anything calls
Slow test execution due to eagerly performing unnecessary work is one problem. The bigger problem is that these kinds of setup functions often have side effects that live beyond the execution of the test suite (filesystem, database, etc):
Test execution order might not be deterministic, so this might not fail right away. Maybe most of the time the order of steps 2 and 3 are reversed, and everything works just fine. We have soooo much of this in our test code right now, and it's really painful to debug. |
ed23be6
to
872bafe
Compare
0deb95c
to
a48ac6c
Compare
a48ac6c
to
77857e4
Compare
Summary
This is an experiment in using ast-grep to craft our own semantic lint rules.
ast-grep
is a tool for querying source code in a (relatively) language-agnostic manner. It allows us to write lint rules that target patterns that are specific to our codebase and therefore not covered by tools likeluacheck
.In practice I expect this to be used mostly for lua files, but in theory we could use it to write lints for all sorts of languages/filetypes.
CI Components
Everything here runs in the new
ast-grep.yml
workflow, which is triggered on a PR event with changes to anyast-grep
-related files (rules, config, etc) or any*.lua
source code files.Lint
Errors and warnings behave as expected and create annotations on the job summary when encountered:
screenshot
Self Test
ast-grep
has a test feature which allows you to check your rules against code snippets. Additionally, the workflow runs some bash glue code that checks for well-formed rule files and, importantly, requires that all rules have tests.New Lints
I've added 3 lints to start with:
ngx-log-string-concat
kong/**
ngx.log()
assert-eventually-terminated
**/*_spec.lua
assert.eventually()
call sitehelpers-outside-of-setup
**/*_spec.lua
spec.helpers
functions outside ofsetup()
Some things could be warnings today but elevated to error level in the future.
KAG-6656