Import for side-effects is flagged as "duplicated"

Versions: SQ 8.3, scanner 4.3.0, TS plugin 2.1, JS plugin 6.2.1
Rule: typescript:S3863

I’m using a library called PnP JS for accessing Sharepoint data via REST API. Their documentation includes samples like

import { sp } from "@pnp/sp";
import "@pnp/sp/webs";
import "@pnp/sp/lists";
import { IList } from "@pnp/sp/lists";

const lists = await sp.web.lists();

Note that there are two import statements that reference @pnp/sp/lists. The link above explains why:

the interface only import statement will be stripped when it is transpiled

Basically, their library design uses side-effect augmentation to add properties/methods to the prototype of their exported classes.In the above example, there is no sp.web until you import sp/webs, and sp.web doesn’t have a .lists method until you import sp/lists. (I’m not a fan of this pattern but I don’t really have a say in the matter.)

The subject rule flags the two import statements as “duplicated”. The problem is that, because IList is an interface, it only exists on the “type side” of TS, not on the “runtime side”, and if I remove import "@pnp/sp/lists"; there will be no import statement emitted in the transpiler output. I think the rule needs to be updated to be aware of the type of the things that are imported via the import { foo } from "bar" syntax, and not “count” the statement (for purposes of this rule) if all of the things have no runtime footprint.

Hello James,

Thank you for the feedback.

Indeed, this pattern seems to be quite common among certain JavaScript libraries to the extent that the rule deserves an exception for it.

Ticket created.

Regards,
Yassin

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.