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:
- The Svelte file is parsed with
@typescript-eslint/parser(viasvelte-eslint-parser) - A
use:directive references a function declaration (not an arrow function) - 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 |