javascript:S4030 no-unused-collection crashes on Svelte use: directive

Language: JavaScript / TypeScript (Svelte)
Rule: S4030 (sonarjs/no-unused-collection)
Product: eslint-plugin-sonarjs 4.0.2 (standalone ESLint plugin, not via SonarQube/Cloud/IDE)

Description

More of a crash than a false-positive, but I was unsure where else to report it. The rule throws TypeError: Cannot read properties of null (reading 'type') when linting a Svelte file that contains a use: action directive.

The crash requires:

  1. The Svelte file is parsed with @typescript-eslint/parser (via svelte-eslint-parser)
  2. A use: directive references a function declaration (not an arrow function)
  3. The directive has a variable expression: <div use:fn={variable}></div>

Seems like the crash is actually unrelated to collections, happens from the parsing itself.

Reproduction

I prepared a minimal repro repository: github.com/valerio/sonarjs-svelte-use-crash

This is enough to trigger the crash (more details in the repo):

<script lang="ts">
  let x = 0
  function action() {}
</script>

<div use:action={x}></div>

Stack trace

TypeError: Cannot read properties of null (reading 'type')
Occurred while linting App.svelte:1
Rule: "sonarjs/no-unused-collection"
    at isElementWrite (eslint-plugin-sonarjs/cjs/helpers/ast.js:191:30)
    at isRead (eslint-plugin-sonarjs/cjs/S4030/rule.js:146:46)
    at isUnusedCollection (eslint-plugin-sonarjs/cjs/S4030/rule.js:112:18)

Versions

Package Version
eslint-plugin-sonarjs 4.0.2
eslint 10.0.3
eslint-plugin-svelte 3.15.2
svelte 5.53.9
typescript-eslint 8.57.0

Hi @valerio27, welcome to the community! :tada:

Thank you for such a detailed first report. The stack trace, minimal repro, and version table made this really easy to investigate and reproduce on our end. This is indeed a crash: the rule encounters Svelte-specific AST nodes produced by
svelte-eslint-parser that don’t follow standard ESTree conventions, and it doesn’t guard against them correctly.

We’ve created Jira to track the fix.
Sorry for the trouble, and thanks for kicking off your community presence with such a high-quality report; it really helps us make the tool better!

Hi @valerio27,

Good news; the fix for this crash has been merged and will be available in the next release of eslint-plugin-sonarjs.

Worth noting that this combination with svelte-eslint-parser is not an officially tested configuration, so if you run into anything else unexpected, feel free to report it.

Thanks again for the quality of your initial report; it made the investigation straightforward. :folded_hands:

3 Likes

Thanks for the quick responses! I’ll give the fixed release a try and report back.