-
Notifications
You must be signed in to change notification settings - Fork 31
Locals order #27
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
Locals order #27
Conversation
rules/terraform_locals_order.go
Outdated
|
|
||
| // Check checks whether single line comments is used | ||
| func (r *TerraformLocalsOrderRule) Check(runner tflint.Runner) error { | ||
| files, err := runner.GetFiles() |
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.
You can use GetLocals() instead of GetFiles().
| func (r *Runner) GetLocals() (map[string]*Local, hcl.Diagnostics) { |
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.
Thanks for the tip, I've checked Local struct, it contains name and Range, which contains file name and range in that file.
What we want to sort is the local argument inside a locals block, but not in a file range, since this Local struct lacks block information, I think it might be easier to analyze them in File->Block->Attribute way, could I keep this way?
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.
That makes sense. In this case, it certainly seems better to analyze by file.
rules/terraform_locals_order.go
Outdated
| formattedBlock := string(hclwrite.Format([]byte(suggestedBlock))) | ||
| return runner.EmitIssue( | ||
| r, | ||
| fmt.Sprintf("Recommended locals variable order:\n%s", formattedBlock), |
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.
It may be a bit difficult to print a suggestion to support both HCL native syntax and JSON syntax. Also, if possible, I would like to avoid multi-line messages. In this case, "local values must be in alphabetical order" would be good.
By the way, it's probably best to provide an autofix for such rules. See terraform-linters/tflint#266
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've removed multi-line suggestion as you suggested, but when we tried to print a multi-line suggestion, we found it's hard to preserve comment line inside code since all comments have been removed from AST. If we want to sort arguments, we need look-back for comment lines and move them too, so we just skip comments for now. I think if we'd like to implement this autofix feature, we should consider preserving comments lines too.
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.
Yes, the hclwrite package has a parser that generates AST nodes containing comments, so we'll need to use it to rewrite the code.
|
Thanks @wata727 , I'll try to fix as you suggested. |
|
Thanks @wata727 , I've updated this pr, would you please give it another review? Thanks! |
|
Thanks @wata727 , I've corrected code as you suggested, would you please give it another review? Thanks. |
wata727
left a comment
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. Thanks!
This pull request will enforce lexicographic order for attributes in a
localsblock. This is the first pull request for issue #22 . We've already implemented these rules in our own plugin, but it'll be a great help if this official plugin can accept these rules. @wata727 will you give this pr a review? Thanks!