-
-
Notifications
You must be signed in to change notification settings - Fork 68
docs: add package import rule guide #752
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
Merged
HerringtonDarkholme
merged 6 commits into
ast-grep:main
from
sudeshgutta:docs/add-pkg-import-rule
Jun 2, 2025
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a359c9e
add match package import rule
sudeshgutta 928cd85
update match package import rule
sudeshgutta 70bb732
address pr comments
sudeshgutta 71d6466
address pr comments #2
sudeshgutta bb4df70
minor fix with playground
sudeshgutta 2dff6fe
Update match-package-import.md
HerringtonDarkholme File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| ## Match package import in Golang | ||
|
|
||
| * [Playground Link](/playground.html#eyJtb2RlIjoiQ29uZmlnIiwibGFuZyI6ImdvIiwicXVlcnkiOiIiLCJyZXdyaXRlIjoiIiwic3RyaWN0bmVzcyI6InNtYXJ0Iiwic2VsZWN0b3IiOiIiLCJjb25maWciOiJpZDogbWF0Y2gtaW1wb3J0LXBrZ1xubGFuZ3VhZ2U6IGdvXG5ydWxlOlxuICBraW5kOiBpbXBvcnRfc3BlY1xuICBoYXM6XG4gICAgcmVnZXg6IGdpdGh1Yi5jb20vZ29sYW5nLWp3dC9qd3QiLCJzb3VyY2UiOiJwYWNrYWdlIG1haW5cblxuaW1wb3J0IChcblx0XCJmbXRcIlxuXHRcImdpdGh1Yi5jb20vZ29sYW5nLWp3dC9qd3RcIiAgLy8gVGhpcyBtYXRjaGVzIHRoZSBBU1QgcnVsZVxuKVxuXG5mdW5jIG1haW4oKSB7XG5cdC8vIENyZWF0ZSBhIG5ldyB0b2tlblxuXHR0b2tlbiA6PSBqd3QuTmV3KGp3dC5TaWduaW5nTWV0aG9kSFMyNTYpXG5cdFxuXHQvLyBBZGQgc29tZSBjbGFpbXNcblx0dG9rZW4uQ2xhaW1zID0gand0Lk1hcENsYWltc3tcblx0XHRcInVzZXJcIjogXCJhbGljZVwiLFxuXHRcdFwicm9sZVwiOiBcImFkbWluXCIsXG5cdH1cblx0XG5cdC8vIFNpZ24gdGhlIHRva2VuXG5cdHRva2VuU3RyaW5nLCBlcnIgOj0gdG9rZW4uU2lnbmVkU3RyaW5nKFtdYnl0ZShcIm15LXNlY3JldFwiKSlcblx0aWYgZXJyICE9IG5pbCB7XG5cdFx0Zm10LlByaW50ZihcIkVycm9yIHNpZ25pbmcgdG9rZW46ICV2XFxuXCIsIGVycilcblx0XHRyZXR1cm5cblx0fVxuXHRcblx0Zm10LlByaW50ZihcIkdlbmVyYXRlZCB0b2tlbjogJXNcXG5cIiwgdG9rZW5TdHJpbmcpXG59In0=) | ||
|
|
||
| ### Description | ||
|
|
||
| A generic rule template for detecting imports of specific packages in Go source code. This rule can be customized to match any package by modifying the regex pattern, making it useful for security auditing, dependency management, and compliance checking. | ||
|
|
||
| This rule identifies Go import statements based on the configured regex pattern, including: | ||
|
|
||
| Direct imports: `import "package/name"` | ||
| Versioned imports: `import "package/name/v4"` | ||
| Subpackage imports: `import "package/name/subpkg"` | ||
| Grouped imports within `import () blocks` | ||
|
|
||
| ### YAML | ||
|
|
||
| ```yaml | ||
| id: match-package-import | ||
| language: go | ||
| rule: | ||
| kind: import_spec | ||
| has: | ||
| regex: PACKAGE_PATTERN_HERE | ||
| ``` | ||
|
|
||
| ### Example | ||
|
|
||
| JWT Library Detection | ||
|
|
||
| ```go | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "github.com/golang-jwt/jwt" // This matches the AST rule | ||
| ) | ||
|
|
||
| func main() { | ||
| // Create a new token | ||
| token := jwt.New(jwt.SigningMethodHS256) | ||
|
|
||
| // Add some claims | ||
| token.Claims = jwt.MapClaims{ | ||
| "user": "alice", | ||
| "role": "admin", | ||
| } | ||
|
|
||
| // Sign the token | ||
| tokenString, err := token.SignedString([]byte("my-secret")) | ||
| if err != nil { | ||
| fmt.Printf("Error signing token: %v\n", err) | ||
| return | ||
| } | ||
|
|
||
| fmt.Printf("Generated token: %s\n", tokenString) | ||
| } | ||
| ``` | ||
|
|
||
| ### Contributed by | ||
|
|
||
| [Sudesh Gutta](https://github.com/sudeshgutta) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.