rspec/rules/S3512/dart/rule.adoc

82 lines
2.1 KiB
Plaintext

== Why is this an issue?
In Dart there's an ability to use template literals instead of concatenation. Since their use is clearer and more concise, they are preferred.
=== Exceptions
Concatenation of string literals, without any variables, is allowed, both using `+` and using adjacent strings. Those are typically used for multiline strings.
Raw string literals are also an exception to this rule, since they don't support interpolation.
[source,dart]
----
var s1 =
'hello\n' +
'world'; // OK
var s2 =
'hello\n'
'world'; // OK
var s3 = r'hello\n' + s1; // OK
----
The multiline strings like `s1` and `s2` above can also be written as follows:
[source,dart]
----
var s1Alternative = '''
hello
world'''; // OK
----
=== Noncompliant code example
[source,dart,diff-id=1,diff-type=noncompliant]
----
void sayHello(name) {
print('hello ' + name + '!'); // Noncompliant
}
----
=== Compliant solution
[source,dart,diff-id=1,diff-type=compliant]
----
void sayHello(name) {
print('hello $name!');
}
----
== Resources
=== Documentation
* Dart Docs - https://dart.dev/tools/linter-rules/prefer_interpolation_to_compose_strings[Dart Linter rule - prefer_interpolation_to_compose_strings]
* Dart Docs - https://dart.dev/language/built-in-types#strings[Dart language - strings]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Use interpolation to compose strings and values.
=== Highlighting
The entire string concatenation expression, including the operands.
If a non-parenthesized sequence of concatenations is detected, a single issue is reported, on the first part of it: e.g. in `'a' + s1 + 'a'` and `s1 + 'a' + s1`, only `'a' + s1` and `s1 + 'a'` is reported, respectively.
An exception is made for single string variables or literals in parentheses, where the outer concatenation is reported: e.g. in `'a' + (s1)`, the entire expression is reported.
In parenthesized expressions, only innermost concatenations are reported: e.g. in `'a' + (s1 + 'a')`, only `s1 + 'a'` is reported.
'''
== Comments And Links
(visible only on this page)
endif::env-github,rspecator-view[]