Create rule S7059: Constructors should not contain asynchronous operations

Description

Issue link: https://github.com/SonarSource/SonarJS/issues/4423


  1.  

    1.  

      1.  

        1. Detection

  • Forbid the pattern await new <XXX>

  • Exclude Promise or anything extending Promise (see if TypeScript compiler type-check can help)

Noncompliant

Compliant

Activity

Show:

Victor DiezSeptember 3, 2024 at 2:29 PM

Changed the scope of this rule to not to use asynchronous code in constructors

Eric MorandJuly 15, 2024 at 12:53 PM
Edited

This may be tricky because the most famous source-map library provided by Mozilla actually exposes asynchronous constructors and it would put every project that uses that library in an uncomfortable situation:



We could add an exception, though.

Also, my personal opinion here: class constructors are factories. I think that whatever is allowed with functional factories (like createCat) should be allowed with class constructors. Allowing a pattern on certain factories and not some other sounds strange to me.

And, I’m not sure I understand the Why here: what issue do asynchronous constructors create that this rule would prevent? And, coming back to the factory concept, what issues would this rule prevent that don’t also exist with asynchronous functional factories? If we want to implement this rule, I think we need to identify and document the adaptability issues that it mitigates.

Done
Pinned fields
Click on the next to a field label to start pinning.

Details

Assignee

Reporter

Labels

Fix versions

Priority

Sentry

Created April 24, 2024 at 2:20 PM
Updated October 16, 2024 at 12:53 PM
Resolved September 11, 2024 at 9:31 AM