-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create rule S6893: Ensure whitespace in-between braces in template di…
…rectives (#3581)
- Loading branch information
1 parent
fcf8910
commit 1598ec1
Showing
3 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,23 @@ | ||
{ | ||
"title": "Ensure whitespace in-between braces in template directives", | ||
"type": "CODE_SMELL", | ||
"status": "ready", | ||
"remediation": { | ||
"func": "Constant\/Issue", | ||
"constantCost": "1min" | ||
}, | ||
"tags": [ | ||
], | ||
"defaultSeverity": "Major", | ||
"ruleSpecification": "RSPEC-6893", | ||
"sqKey": "S6893", | ||
"scope": "All", | ||
"defaultQualityProfiles": ["Sonar way"], | ||
"quickfix": "unknown", | ||
"code": { | ||
"impacts": { | ||
"MAINTAINABILITY": "MEDIUM" | ||
}, | ||
"attribute": "FORMATTED" | ||
} | ||
} |
This file contains 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,69 @@ | ||
This rule raises an issue when template directives do not have a whitespace after the opening brace and before the closing brace. It's based on best practices in Helm and emphasizes the importance of maintaining this whitespace for better readability and understanding of the code. | ||
|
||
== Why is this an issue? | ||
The absence of whitespace in template directives in Helm can lead to several issues: | ||
|
||
**Readability**: Code is read more often than it is written. When template directives are tightly packed without whitespaces, it can be challenging to distinguish between the braces and the content inside. This can slow down the process of understanding the code, especially for those who are new to the codebase. | ||
|
||
**Maintainability**: Hard-to-read code is also hard to maintain. If a developer struggles to understand what a piece of code does due to poor formatting, they may introduce bugs when they try to modify it. | ||
|
||
In summary, not having a whitespace after the opening brace and before the closing brace in template directives can make the code harder to read, understand, and maintain. | ||
|
||
|
||
== How to fix it in Helm | ||
To fix this issue, add a whitespace after the opening brace and before the closing brace in template directives. Also, ensure there is a whitespace after opening chomping braces `{{-` and before closing chomping braces `-}}`. | ||
|
||
=== Code examples | ||
|
||
==== Noncompliant code example | ||
|
||
[source,text,diff-id=1,diff-type=noncompliant] | ||
---- | ||
apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
name: {{.Values.podName}} # Noncompliant | ||
spec: | ||
containers: | ||
- name: {{-.Values.containerName-}} # Noncompliant | ||
---- | ||
|
||
==== Compliant solution | ||
|
||
[source,text,diff-id=1,diff-type=compliant] | ||
---- | ||
apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
name: {{ .Values.podName }} | ||
spec: | ||
containers: | ||
- name: {{- .Values.containerName -}} | ||
---- | ||
|
||
|
||
== Resources | ||
=== Documentation | ||
* Helm Best Practices - https://helm.sh/docs/chart_best_practices/templates/#formatting-templates[Formatting Templates] | ||
* Go Documentation - https://pkg.go.dev/text/template?utm_source=godoc#hdr-Text_and_spaces[Text and spaces] | ||
|
||
ifdef::env-github,rspecator-view[] | ||
|
||
''' | ||
== Implementation Specification | ||
(visible only on this page) | ||
|
||
=== Message | ||
|
||
If possible try to display a specific message tailored to the issue. | ||
|
||
* Add a whitespace after `{{` in the template directive. | ||
* Add a whitespace before `}}` in the template directive. | ||
* Add a whitespace after `{{-` in the template directive. | ||
* Add a whitespace before `}}-` in the template directive. | ||
|
||
|
||
=== Highlighting | ||
|
||
* Highlight the template directive that does not have a whitespace after opening braces `{{`, `{{-` and/ or before closing braces `}}`, `-}}` | ||
endif::env-github,rspecator-view[] |
This file contains 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,2 @@ | ||
{ | ||
} |