Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: ignore context.TODO and context.Background #35

Merged
merged 1 commit into from
Jan 14, 2025

Conversation

Crocmagnon
Copy link
Owner

Related to #34

@Crocmagnon Crocmagnon mentioned this pull request Jan 13, 2025
continue
}
}
}
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ldez would you mind giving this a look please? I'm unsure whether there's a better (simpler) way of ignoring context.Background and context.TODO.

Copy link
Contributor

@ldez ldez Jan 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the right way.

I recommend creating a function to improve the maintenance and decrease the cyclomatic complexity:

		// Ignore [context.Background] & [context.TODO].
		if isContextFunction(assignStmt.Rhs[0], "Background", "TODO") {
			continue
		}
func isContextFunction(exp ast.Expr, fnName ...string) bool {
	call, ok := exp.(*ast.CallExpr)
	if !ok {
		return false
	}

	selector, ok := call.Fun.(*ast.SelectorExpr)
	if !ok {
		return false
	}

	ident, ok := selector.X.(*ast.Ident)
	if !ok {
		return false
	}

	return ident.Name == "context" && slices.Contains(fnName, selector.Sel.Name)
}

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@Crocmagnon Crocmagnon force-pushed the feat/ignore-builtins branch 2 times, most recently from 878151a to 326edc3 Compare January 14, 2025 16:02
@Crocmagnon Crocmagnon force-pushed the feat/ignore-builtins branch from 326edc3 to 5030c3a Compare January 14, 2025 16:02
@Crocmagnon Crocmagnon merged commit 54e593c into master Jan 14, 2025
10 checks passed
@Crocmagnon Crocmagnon deleted the feat/ignore-builtins branch January 14, 2025 16:05
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants