typescript:S3001 false-positive when "delete" operator used on optional properties object

Product: SonarCloud
Plugin: javascript
Version: 8.9 (build 17411)
Rule Key: typescript:S3001

When delete operator is used on optional properties of object, an issue is raised (details):

image

It looks like a compliant code though.


Consider this package.json:

{
    "name": "sample",
    "version": "1.0.0",
    "main": "main.ts",
    "dependencies": {
      "aws-sdk": "^2.1058.0",
      "proxy-agent": "^5.0.0",
      "typescript": "^4.5.4",
      "@types/node": "^17.0.9"
    }
}

And this main.ts:

import { config } from 'aws-sdk';
import proxy from 'proxy-agent';

console.log('--- Config default---');
console.log(config.httpOptions);

console.log('deletion: ' + delete config.httpOptions?.agent);

console.log('--- Config default after deletion ---');
console.log(config.httpOptions);

config.update({
  httpOptions: { agent: proxy('http://proxy.company.com') },
});

console.log('--- Config after update ---');
console.log(config.httpOptions);

console.log('deletion: ' + delete config.httpOptions?.agent);

console.log('--- Config after deletion ---');
console.log(config.httpOptions);

Output after tsc --esModuleInterop true main.ts && node main.js:

 --- Config default---
{ timeout: 120000 }
deletion: true
--- Config default after deletion ---
{ timeout: 120000 }
--- Config after update ---
{
  timeout: 120000,
  agent: ProxyAgent {
    proxy: Url {
      protocol: 'http:',
      slashes: true,
      auth: null,
      host: 'proxy.company.com',
      port: null,
      hostname: 'proxy.company.com',
      hash: null,
      search: null,
      query: null,
      pathname: '/',
      path: '/',
      href: 'http://proxy.company.com/'
    },
    proxyUri: 'http://proxy.company.com',
    proxyFn: [Function: httpOrHttpsProxy]
  }
}
deletion: true
--- Config after deletion ---
{ timeout: 120000 }

The deletion is done as expected.

Hello Alix,

Welcome back!

Indeed, raising an issue on an object optional property is definitely a false-positive.

I created the following ticket to address that as soon as possible.

Many thanks,
Yassin

1 Like

Hello Yassin,

Many thanks !

Best regards

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