-
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 S7095: Unnecessary braces in string interpolation should …
…be removed (unnecessary_brace_in_string_interps) (#4325) Co-authored-by: antonioaversa <[email protected]>
- Loading branch information
1 parent
8fe5b5c
commit 4f54acf
Showing
3 changed files
with
95 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": "Unnecessary braces in string interpolation should be removed", | ||
"type": "CODE_SMELL", | ||
"status": "ready", | ||
"remediation": { | ||
"func": "Constant\/Issue", | ||
"constantCost": "1min" | ||
}, | ||
"tags": [ | ||
], | ||
"defaultSeverity": "Major", | ||
"ruleSpecification": "RSPEC-7095", | ||
"sqKey": "S7095", | ||
"scope": "All", | ||
"defaultQualityProfiles": ["Sonar way"], | ||
"quickfix": "unknown", | ||
"code": { | ||
"impacts": { | ||
"MAINTAINABILITY": "LOW" | ||
}, | ||
"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,70 @@ | ||
Unnecessary braces in string interpolation should be removed. | ||
|
||
== Why is this an issue? | ||
|
||
Dart https://dart.dev/language/built-in-types#strings[strings] support interpolation, which allows you to embed expressions within a string. | ||
|
||
[source,dart] | ||
---- | ||
String expression = 'words'; | ||
print('A string containing ${expression}'); | ||
---- | ||
|
||
When the expression is a simple identifier, and the identifier is followed in the string interpolation by a character which is not allowed in an identifier (e.g. whitespace, `:`, `-` but not a letter, digit, ``++_++``, ...), you can omit the curly braces. | ||
|
||
This makes the code more readable and concise. | ||
|
||
[source,dart] | ||
---- | ||
String expression = 'words'; | ||
print('A string containing $expression'); | ||
---- | ||
|
||
== How to fix it | ||
|
||
Remove the curly braces. | ||
|
||
=== Code examples | ||
|
||
==== Noncompliant code example | ||
|
||
[source,dart,diff-id=1,diff-type=noncompliant] | ||
---- | ||
String expression = 'words'; | ||
print('A string containing ${expression}'); | ||
---- | ||
|
||
==== Compliant solution | ||
|
||
[source,dart,diff-id=1,diff-type=compliant] | ||
---- | ||
String expression = 'words'; | ||
print('A string containing $expression'); | ||
---- | ||
|
||
== Resources | ||
|
||
=== Documentation | ||
|
||
* Dart Docs - https://dart.dev/tools/linter-rules/unnecessary_brace_in_string_interps[Dart Linter rule - unnecessary_brace_in_string_interps] | ||
* Dart Docs - https://dart.dev/language/built-in-types#strings[Language - Strings] | ||
|
||
ifdef::env-github,rspecator-view[] | ||
|
||
''' | ||
== Implementation Specification | ||
(visible only on this page) | ||
|
||
=== Message | ||
|
||
Unnecessary braces in a string interpolation. | ||
|
||
=== Highlighting | ||
|
||
The interpolation expression, including the ``++$++`` sign and the braces: e.g. ``++${expression}++`` in ``++'A string containing ${expression}'++``. | ||
|
||
''' | ||
== Comments And Links | ||
(visible only on this page) | ||
|
||
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 @@ | ||
{ | ||
} |