Skip to content
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

Rule: trailing-default-rule #716

Merged
merged 1 commit into from
May 14, 2024
Merged

Rule: trailing-default-rule #716

merged 1 commit into from
May 14, 2024

Conversation

anderseknert
Copy link
Member

Simple rule to check that default rules are placed before non-default rules.

Fixes #699

Copy link
Member

@charlieegan3 charlieegan3 left a comment

Choose a reason for hiding this comment

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

LGTM, but I'm not sure how the actual rule is working.

rule["default"] == true

name := ast.ref_to_string(rule.head.ref)
name in all_names(array.slice(input.rules, 0, i))
Copy link
Member

Choose a reason for hiding this comment

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

🤔 how should I read this? if the default rule is in the 0-ith range of rules? Perhaps it's too early, but it's not clear to me how this is working 😅

Copy link
Member Author

Choose a reason for hiding this comment

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

array.slice(input.rules, 0, i)

provides a slice of all the rules up until i, i.e the default rule.. and then we check if the same name of the default has been used for any of those.

Copy link
Member

Choose a reason for hiding this comment

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

Ahh, yes. that does make sense. I'd assumed that the implementation was going to be like: make sure that the rule is the first when ordered by line or similar, but this works too! thanks.

Simple rule to check that `default` rules are placed before non-default rules.

Fixes #699

Signed-off-by: Anders Eknert <anders@styra.com>
@anderseknert anderseknert merged commit a5aa616 into main May 14, 2024
3 checks passed
@anderseknert anderseknert deleted the default-first branch May 14, 2024 09:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

rule: Require ordering of default rule before other rule heads
2 participants