Modify rule S1128: Adapt to LaYC (#2578)

This commit is contained in:
Yassin Kammoun 2023-07-20 15:10:01 +02:00 committed by GitHub
parent a278b31507
commit 639f9878ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,26 +1,30 @@
== Why is this an issue?
There's no reason to import modules you don't use; and every reason not to: doing so needlessly increases the load.
Unnecessary imports refer to importing modules, libraries, or dependencies that are not used or referenced anywhere in the code. These imports do not contribute to the functionality of the application and only add extra weight to the JavaScript bundle, leading to potential performance and maintainability issues.
=== Noncompliant code example
[source,javascript]
[source,javascript,diff-id=1,diff-type=noncompliant]
----
import A from 'a'; // Noncompliant, A isn't used
import A from 'a'; // Noncompliant: The imported symbol 'A' isn't used
import { B1 } from 'b';
console.log(B1);
----
=== Compliant solution
To mitigate the problems associated with unnecessary imports, you should regularly review and remove any imports that are not being used. Modern JavaScript build tools and bundlers often provide features like tree shaking, which eliminates unused code during the bundling process, resulting in a more optimized bundle size.
[source,javascript]
[source,javascript,diff-id=1,diff-type=compliant]
----
import { B1 } from 'b';
console.log(B1);
----
== Resources
=== Documentation
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import[MDN - ``++import++``]
=== Related rules
* S1481 - Unused local variables and functions should be removed
ifdef::env-github,rspecator-view[]
'''