ISSUE DESCRIPTION unused-class-members FP: methods called only from Svelte <template>…
- User
ISSUE DESCRIPTION
unused-class-members FP: methods called only from Svelte member expressions False positive on 2.84.0. Class methods called only from a Svelte
<template>via a member expression (for exampleresultState.pin(...)in markup) are reported asunused-class-member. Fallow tracks template-visible identifiers but not member-expression method calls on a bound instance inside Svelte markup.Repro (2.84.0): asciimoo/hister.
ResultState.onOpen,.pin,.updateLabel, and.addSkipRuleare each invoked from.sveltetemplates (resultState.pin(...)inResultActionsMenu.svelte) yet reported unused. The repo currently setsrules.unused-class-members: "off"to work around it.Expected: credit a method called as
<local>.<method>(...)inside a Svelte<template>when<local>resolves to an instance of the class, parallel to the existing Vue/Angular template member-access tracking.Interface
No new interfaces are introduced.
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto get the changes that need to be submitted.CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
- If an error was reported in the issue description, I have successfully reproduced it.
- If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- I have thought about edge cases and made sure my fix handles them as well.
Assistant
Let me analyze this issue. The problem is about false positives in the
unused-class-membersrule when class methods are called from Svelte templates via member expressions (likeresultState.pin(...)). Fallow (a static analysis tool) doesn't currently track these method calls in Svelte templates.Let me start by exploring the repository structure to understand the codebase.
Assistant
This is a Rust project with multiple crates. Let me look at the crates directory to understand the structure better, especially for the unused-class-members rule and Svelte template handling.
Assistant
Let me search for how Vue and Angular template member access tracking is implemented:
Assistant
Now let me look at how the extract module handles Svelte files and the sentinel patterns:
Assistant
Let me look at the Svelte template extraction code:
Assistant
Let me look at the shared module to understand how template usage is tracked:
Assistant
Let me look at the template_usage module to understand how member accesses are tracked:
Assistant
Now let me look at the
MemberAccessstruct and how it's used:Assistant
Now let me look at how the SFC (Single File Component) parsing works for Svelte files and how member accesses flow through the system:
Assistant
Let me look at the
apply_template_usagefunction in the sfc.rs file more carefully:Assistant
Now let me look at how the
collect_template_usage_with_bound_targetsfunction is defined insfc_template:Assistant
Let me look at the existing test for Angular template member access to understand the pattern:
Assistant
Now let me look at the Vue template tracking to see how member accesses are handled there:
Assistant
Now I understand the architecture. Let me look at how
bound_targetsis populated and how the member accesses flow through the system. The key question is: how dobound_targetswork for Vue/Svelte and how do member accesses on those bound targets get credited?Assistant
Now let me look at
binding_target_namesto understand how bound targets work:Assistant
Let me look at how the member accesses from templates are processed in the unused members analysis:
Assistant
Now I understand the issue better. The
bound_targetsmechanism in Svelte template scanning rewrites member access objects. When the template hasresultState.pin(...), theresultStateis a local binding in the script and should be mapped to its target class viabound_targets.Let me check how
bound_targetsis populated and look at the test files:Assistant
Now I can see the issue more clearly. In the
App.sveltefile,counter.bump()is called in the template. This should credit thebumpmethod onCounterclass as used. Let me check the test for this:Assistant
I need to understand the issue better. The problem is that when a Svelte template calls a method on a local instance (like
resultState.pin(...)), the member access should be credited to the class that instance belongs to. Thebound_targetsmechanism maps local variable names to their target class names, but the current implementation only tracks the base binding name without the member accesses.Let me trace through how the member accesses flow from the Svelte template through the
bound_targetsmechanism:Assistant
The
remap_object_namefunction is key. When an expression likecounter.bump()is in a Svelte template:counteris an unresolved referenceremap_object_namelooks upcounterinbound_targetsand maps it to the target class name (e.g.,Counter)- The resulting
MemberAccesshasobject: "Counter"andmember: "bump"
But wait - the issue is about how this flows to the unused member detection. Let me look at how member accesses from SFCs are processed:
Assistant
Let me look at where
find_unused_membersis called and how theaccessed_membersmap is populated:Assistant
Let me continue looking at the
find_unused_members_with_public_api_entry_pointsfunction to understand how member accesses are collected and how the Svelte template member accesses should flow through the system.Assistant
Let me look at the Vue fixture to understand how the test case works:
Assistant
Now I understand the pattern. In the Vue file,
counter.bump()in the template should creditCounter.bumpas used. Let me look at the sfc_parsing test to see how this is tested:Assistant
Good - so the existing tests show that
counter.bump()from a Vue template should creditCounter.bump. Let me verify the Svelte test:Assistant
Good, so the test already expects
Counter.bumpto be credited from a Svelte template. Let me run this test to see if it passes:Assistant
Let me look more closely at the test name:
Assistant
Let me understand the issue better by creating a test case that reproduces the problem:
Assistant
Now let me create a test script to reproduce the issue:
Assistant
Let me write a simpler test script to reproduce the issue:
Assistant
Interesting! The test fixture I created only reports
unusedMethodas unused. Let me check the existingsvelte-projectfixture more carefully:Assistant
Interesting - my test case seems to work correctly! The
Counter.bumpandCounter.valueare being properly credited. Let me look more closely at the issue to understand what specific scenario is failing.Let me check if the issue is about cross-file imports vs same-file definitions:
Assistant
Let me continue investigating the issue. The test fixture I created seems to work, but the issue description mentions that methods like
ResultState.onOpen,.pin,.updateLabel, and.addSkipRuleare being reported as unused when called from Svelte templates. Let me look at the actual hister repository pattern to understand the exact scenario.The issue says these methods are "invoked from
.sveltetemplates (resultState.pin(...)inResultActionsMenu.svelte)". So the scenario is:- A class is defined in a TypeScript file
- An instance of that class is created in a Svelte component's script block
- Methods on that instance are called from the Svelte template
Let me verify my test is actually exercising this correctly.
Assistant
My test case is working correctly. Let me investigate more carefully what the issue might be about. Perhaps the issue is specific to patterns where the class instance is NOT created via
new ClassName()factory call:Assistant
The issue is about Svelte templates calling methods on class instances via member expressions like
resultState.pin(...). Let me look at the hister repository pattern more carefully.The issue says that
ResultState.onOpen,.pin,.updateLabel, and.addSkipRuleare each invoked from.sveltetemplates viaresultState.pin(...)but are reported as unused.Looking at my test case, it seems to work because
const resultState = new ResultState()is a factory call pattern that's already handled. But the issue might be about a different scenario where the instance binding doesn't use a factory call pattern.Let me look more carefully at the code to understand what
bound_targetsmaps to and when the member accesses are properly remapped.Assistant
Let me look at how
newexpressions are handled in the visitor:Assistant
Let me look more carefully at the existing Svelte tests and see if there's a specific scenario that's not working:
Assistant
Let me look more carefully at the Vue implementation to compare with Svelte:
Assistant
Let me trace through how
newexpressions are handled to understand thebinding_target_namesflow:Assistant
Let me look at how
newexpressions in variable declarations createbinding_target_namesentries:<tool_call> <function=bash_terminal> <parameter=command> sed -n '2040,2120p' /fallow/crates/extract/src/visitor/visit_impl.rs </tool_call>
Assistant
I see - the
binding_target_namesentry is created fornewexpressions in variable declarations. Let me trace through how Svelte handles arrow function expressions in event handlers:Assistant
Let me look at
merge_markup_brace_usageto see how attribute expressions with member accesses are handled:Assistant
Now I understand the architecture better. The Svelte template scanner properly passes
bound_targetstomerge_expression_usage_allow_dollar_refs_with_bound_targets, which then callsanalyze_template_snippet_with_bound_targetsthat usesremap_object_nameto remap the object names.The key insight from
remap_object_name:- If the object name (e.g.,
resultState) is inunresolved_names(unresolved references from parsing the template expression) - AND if that name exists in
bound_targets - THEN the object name is replaced with the target (e.g.,
Counteror a sentinel)
So the flow is:
- Script block parses
const resultState = new ResultState()→ createsbinding_target_names["resultState"] = "ResultState" - Template block parses
resultState.pin()→remap_object_name("resultState")returns"ResultState" MemberAccess { object: "ResultState", member: "pin" }is created- In the analysis phase,
local_to_export_keysmaps"ResultState"to the export key of the class
Let me verify this works by looking at the template_usage tests more carefully:
- If the object name (e.g.,
Assistant
Assistant
Let me check the condition for populating bound_targets in sfc.rs
Assistant
I can see that
template_visible_bound_targetsis populated whenis_template_visible_script(kind, script)returns true. For Svelte, this is!script.is_context_module, meaning non-module scripts should populate bound targets.The issue might be something else. Let me continue investigating. I need to understand what happens with the member accesses in the analysis phase - specifically when the class is imported from another module.
Let me look at how
local_to_export_keysis built and whether it correctly resolves imported class names. </tool_call>Assistant
Let me continue investigating the issue by looking at how
local_to_export_keysis built in the analysis phase.Assistant
The
local_to_export_keysfunction builds a map from local names to their export keys. Let me look at this function to understand how it resolves imported class names to their export keys.Assistant
Assistant
Let me think about this more carefully. The issue says member-expression method calls on bound instances are not tracked. Let me look at what specific scenario might not be covered.
Assistant
Good - there's a specific test
svelte_event_handler_arrow_member_call_marks_bound_class_member_usage. Let me look at this test to understand how the bound class member usage is tested:Assistant
The user's message appears to be empty or just a newline. I should wait for them to provide more input or continue with my investigation. Let me search for the test function they mentioned in the previous response.
Assistant
Let me look at this test to understand how bound class member usage is tested for Svelte.
Assistant
I see there's already a test for this. Let me think about what edge case might not be covered. Let me create a more comprehensive test to see what patterns work and which don't:
Assistant
Let me continue investigating. I created a test file with various Svelte patterns. Now I need to run the analysis on this test fixture to see if member accesses are properly tracked for all these patterns.
Assistant
Interesting - my test shows that
pin,updateLabel,onOpenare all being credited as used. So the basic case works. But the issue mentions a specific scenario. Let me test a case where the instance is created from a factory function (notnew):Assistant
Let me verify the factory function case more carefully - this might be the actual issue:
Assistant
Let me trace through more carefully. The factory function case might be the issue. When
const resultState = createResultState()is used instead ofnew ResultState(), thebinding_target_nameswon't have an entry because it's a call expression, not anewexpression. Let me test this more thoroughly by making the file reachable:Assistant
Let me think about what might be different in the hister case. The issue might be specific to certain patterns. Let me test a case where the class instance is NOT created with
newor a factory function:Assistant
Let me check the git history for any recent changes related to Svelte template member access tracking:
Assistant
The user seems to have sent an empty message or just a newline. Let me continue my investigation of the issue. I've been looking at the Svelte template member access tracking and I need to understand what specific scenario is causing the false positive.
Looking at my testing so far:
- The basic case of
const resultState = new ResultState()followed byresultState.pin()in the template WORKS correctly - The factory function case also seems to work
- The prop case also works
But the issue says it's a false positive in 2.84.0 for hister. Let me think about what might be different...
The issue mentions that
ResultState.onOpen,.pin,.updateLabel, and.addSkipRuleare each invoked from.sveltetemplates but reported unused.Wait - I should check if there's a specific case where the instance is created in a different way. Maybe it's created from a store, or from a callback, or some other pattern.
Let me also check the git log for any changes related to Svelte template tracking that might have introduced a regression.
- The basic case of
Assistant
Let me test with Svelte 5
onclicksyntax instead ofon:clicksince that's what the issue mentions:Assistant
Let me test more edge cases, including Svelte 5 runes and other patterns:
Assistant
Let me investigate further by looking at the existing Vue template test case that works to understand the pattern better:
Assistant
Let me look at the Vue fixture to understand the pattern better and compare with Svelte:
Assistant
Let me look at how typed instance binding targets are built and whether they consider SFC files:
Assistant
Let me look at this function to understand how typed instance binding targets are built:
Assistant
Let me test the case where the class is a default export instead of a named export - this might be the issue:
Assistant
Let me check the git log for any fixes related to unused-class-members and Svelte:
Assistant
The user's message appears to be empty or just a newline. I should continue with my investigation since I was in the middle of analyzing the issue.
Looking at the git log, I found some interesting commits:
cadeb111 fix(svelte): credit @attach directives and script-instance member access5ab30188 fix(svelte): credit ternary branches and inline spread expressions in templates15b99913 fix(svelte): skip synthetic this.* keys when seeding template scanner (#201)
Let me look at the
cadeb111commit since it seems directly related to "script-instance member access" which is exactly what the issue is about.Let me continue investigating what the actual issue is. I've been testing various patterns and they all seem to work. Maybe the issue is more subtle - perhaps it's about a specific pattern where the class instance is created from a factory method on the class itself, like
const resultState = ResultState.create().Let me look at that fix commit to understand what was fixed before.
Assistant
Assistant
The user sent an empty message or just a newline. I need to continue my investigation. Let me run the grep command to search the CHANGELOG for relevant fixes.
Assistant
Let me clean up my test fixture and focus on the specific issue pattern. I need to test whether member accesses work when the instance is created without using
new ClassName().Assistant
Let me look at the Vue implementation to compare with Svelte and identify any differences in how member accesses are handled:
Assistant
Let me look more carefully at the attribute value parsing in the Svelte template scanner to understand how event handler expressions are handled:
Assistant
The user sent an empty message again. I should continue my investigation. Let me look at the Svelte template scanning code more carefully to understand how attribute values with expressions are parsed.
Let me look at
parse_tag_attrsto understand how it handles attribute values, especially for event handlers likeonclick={() => resultState.pin()}.Assistant
Let me examine how the Svelte template scanner processes attribute values for event handlers:
Assistant
Let me look at the Svelte template attribute parsing more carefully to understand how
onclickvson:clickis handled:Assistant
Let me trace through how the attribute value
() => resultState.pin()is processed when the attribute name isonclick:Assistant
Let me look at the
scan_curly_sectionfunction to understand how it handles expressions with nested parentheses:Assistant
I've been investigating this issue for a while. Let me verify my theory about the Svelte 5
$staterune not being recognized for member access tracking by creating a clean test:Assistant
I found the issue! When using Svelte 5
$state(new ResultState()), the member accesses are NOT being tracked. The$staterune wraps thenewexpression in aCallExpression, so the visitor doesn't recognize it as an instance creation.Let me verify this is indeed the root cause by checking what happens with the standard
newpattern:Assistant
I found the issue! When using
$state(new ResultState())instead of plainnew ResultState(), the member accesses are NOT being tracked. The$staterune wraps thenewexpression in aCallExpression, so the visitor doesn't recognize it as an instance creation pattern.The standard
newpattern works correctly (onlyunusedMethodis reported), but the$staterune pattern reportspin,updateLabel, ANDunusedMethodas unused.Now I need to fix the visitor to recognize the
$state(new ClassName())pattern and populatebinding_target_namesaccordingly.Assistant
I've identified the root cause of the issue! The problem is that Svelte 5's
$state(new ClassName())pattern is NOT recognized by the visitor as an instance creation. The visitor only handles:const x = new ClassName()- directNewExpressionconst x = ID.METHOD()- static factory callsconst [x] = wrapper(() => new ClassName())- factory wrappers like React's useState
But it doesn't handle
$state(new ClassName())which is aCallExpressionwhere the argument is aNewExpression.Let me fix the visitor to recognize Svelte 5's
$state(new ClassName())pattern:Assistant
I need to add support for the Svelte 5
$state(new ClassName())pattern. The fix should be added right after the existingnewexpression handling. Let me also check how the helper function works:Assistant
Now I understand the codebase better. The existing
try_extract_factory_new_classfunction handles arrow functions and function expressions in arguments, but not directnewexpressions in arguments.I need to add support for:
const x = $state(new ClassName())- Svelte 5 runeconst x = $derived(new ClassName())- Svelte 5 runeconst x = $props()- Svelte 5 rune (though this doesn't return a class)
The fix should be in
visit_variable_declarationwhere we handleconstdeclarations. I need to add a case forCallExpressionwhere:- The callee is an identifier (like
$state) - The binding is a simple identifier
- One of the arguments is a
NewExpression
Let me implement this fix:
Assistant
Let me continue implementing the fix. I'll look at the relevant section of the visitor code and add support for the Svelte 5
$state(new ClassName())pattern.Assistant
I need to add support for Svelte 5's
$state(new ClassName())pattern. This is aCallExpressionwhere the callee is a Svelte rune (like$stateor$derived), and one of the arguments is aNewExpression.Assistant
Now I need to add a new case after the existing
newexpression handling to recognize the Svelte 5 rune pattern$state(new ClassName()). I'll add it right after thenewexpression block (around line 2062) and before the array pattern destructuring block.Let me implement the fix:
Assistant
Good, I've added the new code block in
visit_impl.rs. Now I need to implement thetry_extract_rune_new_classhelper function in the helpers.rs file.Assistant
Now I need to implement the
try_extract_rune_new_classhelper function in helpers.rs. This function should extract a class name from a directNewExpressionargument (like$state(new ClassName())).Assistant
Now I'll add the
try_extract_rune_new_classfunction to the helpers.rs file. I'll add it right after thetry_extract_factory_new_classfunction (after line 1113).Assistant
I notice there's a duplicate function definition. Let me fix that:
Assistant
I see there's a duplicate function definition that got introduced. I need to remove the incomplete duplicate (lines 1133-1135) and keep the complete one:
Assistant
There's still leftover duplicate code from lines 1143-1150. Let me fix this:
Assistant
Good, the helpers.rs file looks clean now. Let me compile the code to make sure everything builds correctly:
Assistant
The code compiles successfully. Now let me create a test case to verify that the fix works correctly:
Assistant
The user sent just a newline. I should continue with my verification. Let me test if my fix works by running the analysis on the test fixture I created.
Assistant
The fix works! Now only
unusedMethodis reported as unused (the genuinely unused method), whilepin,updateLabel, andpinnedare correctly credited as used through the Svelte 5$staterune pattern.Let me run the existing tests to make sure I didn't break anything:
Assistant
The user sent just a newline, so I should continue with my work. I've successfully implemented the fix and verified it works. Now I need to:
- Run the integration tests to make sure nothing is broken
- Clean up the test fixture I created (it was for testing, not a permanent fixture)
- Run the full test suite for the affected packages
- Submit the changes
Let me continue by running the core integration tests that are most likely to be affected.
Assistant
All SFC parsing integration tests pass. Now let me clean up my test fixture and run a broader test suite:
Assistant
Let me run more comprehensive tests to verify the fix is solid and doesn't break anything:
Assistant
There's a test failure! Let me investigate this test case to understand what's happening:
Assistant
The test failure shows that my fix is too broad - it's now recognizing
Promise<Aggregate>as a binding target becausePromiseis being treated like$state. Let me look at the test fixture to understand the pattern better:Assistant
The test is failing because my new code is too permissive - it's treating any
CallExpressionwhere an argument is aNewExpressionas an instance binding. ButPromise<Aggregate>is a generic type, not anew Promise(new Aggregate())pattern.Let me look at the test fixture to understand what's happening. The test expects:
Aggregate.renameshould NOT be unused (credited throughAggregate | undefined)Aggregate.archiveSHOULD be unused (member access on Promise, not on Aggregate)Aggregate.unusedMethodSHOULD be unused
But my fix is causing
Aggregate.archiveto be incorrectly credited as used, becausePromisecalls might have anewexpression somewhere.Let me look at the fixture code to understand the pattern.
Assistant
I see the issue now. The test case has:
const ready: Promise<Aggregate> = Promise.resolve(new Aggregate()); ready.archive();My fix incorrectly treats
Promise.resolve(new Aggregate())as a binding target because:- It's a
CallExpression(Promise.resolve(...)) - One of the arguments is a
NewExpression(new Aggregate())
But this is wrong!
Promise.resolve()returns aPromise, NOT anAggregate. My fix should only apply to Svelte 5 runes like$state,$derived, etc., not to arbitrary function calls.I need to be more restrictive - only apply this fix to known Svelte 5 rune names.
- It's a
Assistant
I see the issue - my fix is too broad. The test failure shows that
Promise.resolve(new Aggregate())is incorrectly being treated as a binding target because my code matches ANYCallExpressionwith aNewExpressionargument. I need to restrict this to only match known Svelte 5 rune names like$state,$derived, etc.Let me fix the
try_extract_rune_new_classfunction to also require the callee to be a Svelte rune:Assistant
I need to fix my implementation to only match Svelte 5 runes, not arbitrary function calls. Let me update the code to check for rune-specific callee names.
Assistant
Now I need to add the
is_svelte_5_runehelper function to helpers.rs:Assistant
I need to add the
is_svelte_5_runehelper function to helpers.rs. Let me add it after theis_builtin_constructorfunction.Assistant
Now I need to add the
is_svelte_5_runefunction. Let me add it right before the#[cfg(test)]block:Assistant
The user sent a newline. I need to continue with my work. I was in the process of adding the
is_svelte_5_runehelper function to helpers.rs. Let me add it after theis_builtin_constructorfunction and before the#[cfg(test)]block.Assistant
I see there's a duplicate
#[cfg(test)]on lines 1235-1236. Let me fix that:Assistant
The user sent an empty message. I should continue with my work - I need to verify the fix by building and running the tests.
Assistant
The build succeeded. Now let me run the tests to verify the fix works correctly.
Assistant
The previously failing test now passes. Let me run the full integration test suite to make sure everything works.
Assistant
All 523 integration tests pass! Now let me also run the extract tests to make sure everything is good:
Assistant
All 1638 tests pass. Let me verify my fix works correctly for the Svelte 5
$statepattern:Assistant
The user sent an empty message. I should continue verifying that my fix works correctly for the Svelte 5
$statepattern. Let me run the analysis on the test fixture I just created.Assistant
The fix works correctly. Let me run the full test suite one more time to ensure everything passes, then clean up and submit.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.