73 lines
1.5 KiB
Plaintext
73 lines
1.5 KiB
Plaintext
== Why is this an issue?
|
|
|
|
:noncompliant: javascript/noncompliant.adoc
|
|
|
|
:compliant: javascript/compliant.adoc
|
|
|
|
include::../description.adoc[]
|
|
|
|
=== Exceptions
|
|
|
|
This rule does not apply in JSX expressions to support conditional rendering and conditional attributes as long as the nesting happens in separate JSX expression containers, i.e. JSX elements embedding JavaScript code, as shown below:
|
|
|
|
[source,javascript]
|
|
----
|
|
return (
|
|
<>
|
|
{isLoading ? (
|
|
<Loader active />
|
|
) : (
|
|
<Panel label={isEditing ? 'Open' : 'Not open'}>
|
|
<a>{isEditing ? 'Close now' : 'Start now'}</a>
|
|
<Checkbox onClick={!saving ? setSaving(saving => !saving) : null} />
|
|
</Panel>
|
|
)}
|
|
</>
|
|
);
|
|
----
|
|
|
|
If you have nested ternaries in the same JSX expression container, refactor your logic into a separate function like that:
|
|
|
|
[source,javascript]
|
|
----
|
|
|
|
function myComponent(condition) {
|
|
if (condition < 0) {
|
|
return '<DownSign>it is negative</DownSign>';
|
|
} else if (condition > 0) {
|
|
return '<UpSign>it is positive</UpSign>';
|
|
} else {
|
|
return '<BarSign>it is zero</BarSign>';
|
|
}
|
|
}
|
|
|
|
return (
|
|
{myComponent(foo)}
|
|
);
|
|
----
|
|
|
|
== Resources
|
|
|
|
=== Articles & blog posts
|
|
* Sonar - https://www.sonarsource.com/blog/stop-nesting-ternaries-javascript/[Stop nesting ternaries in JavaScript]
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
include::../message.adoc[]
|
|
|
|
include::../highlighting.adoc[]
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
include::../comments-and-links.adoc[]
|
|
|
|
endif::env-github,rspecator-view[]
|
|
|
|
|