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.
Assistant
I'll start by exploring the repository structure to understand the codebase.
Assistant
Let me look at the Vue scanner for comparison since it has the analogous functionality:
Assistant
Now let me look at the shared utilities:
Assistant
Now let me look at the template_usage module that contains the analysis logic:
Assistant
Now let me look at how Svelte components are processed, particularly the bound_targets:
Assistant
Let me look at the rest of the Svelte scanner, especially the part that handles statement contexts:
Assistant
Now let me look at the Svelte tests for member_accesses:
Assistant
So the existing Svelte tests already handle
() => counter.bump()andcounter.valueas member accesses. Now let me look at the Svelte bound_targets helper and look at the issue more carefully:Assistant
Let me look at how the issue repros. First, let me find the import in the issue's about asciimoo/hister repo usage. The issue is about Svelte
resultState.pin(...)inResultActionsMenu.svelte. Let me look at how Vue handlesevent_handler_member_call_maps_script_instance_to_classto understand the analog flow:Looking at the Vue test
event_handler_member_call_maps_script_instance_to_class:rustlet usage = collect_template_usage_with_bound_targets( "<template><button @click=\"counter.bump()\">{{ counter.value }}</button></template>", &imported(&[]), &bound_targets(&[("counter", "Counter")]), );And the Svelte test
event_handler_arrow_member_access_maps_script_instance_to_class:rustlet usage = collect_template_usage_with_bound_targets( "<button onclick={() => counter.bump()}>{counter.value}</button>", &imported(&[]), &bound_targets(&[("counter", "Counter")]), );Assistant
Now let me look at how the issue manifests. The issue is that when the markup has
resultState.pin(...)as a method call expression, it should be captured as a member access. Let me write a simple test to reproduce:Assistant
Let me find the test for the issue. First, let me look at how it currently behaves. Let me look at examples of Svelte 5 syntax (with
onclick={...}- a new style):Assistant
Let me now check the actual usage from the issue. The issue says
resultState.pin(...)works inResultActionsMenu.svelte. Let me look at the relevant code more carefully. The Vue test usescounter.bump()directly while the Svelte test uses an arrow function. Let me check if direct calls likeresultState.pin(...)work in Svelte:Assistant
Actually let me just look at the existing Svelte tests to see what works and what doesn't:
Assistant
Let me actually run the existing Svelte test to see the current state:
Assistant
Good. Tests pass. Now let me write a real test to see if the issue is reproducible. Let me write a new test that captures the case from the issue:
Assistant
Let me add this temporarily to the existing test file:
Assistant
Let me add a temporary test to check the actual issue:
Assistant
Now let me modify the svelte.rs to add the test for the issue. Let me back up the file first:
Assistant
I have a formatting issue. Let me fix it:
Assistant
All three pass! The Svelte scanner does seem to capture the method call. So now I need to understand why the issue exists. Let me re-read the issue:
Class methods called only from a Svelte
<template>via a member expression (for exampleresultState.pin(...)in markup) are reported asunused-class-member.So perhaps the bug is not in the scanner. Let me look at how the unused-class-members rule processes the member_accesses. Let me find that code:
Assistant
Now let me look at how bound_targets are processed for Svelte:
Assistant
Let me look at
binding_target_names:Assistant
Now let me look at how Svelte processes these - check the visit_impl for class detection:
Assistant
OK so the flow is:
- Svelte
parse_sfc_to_moduleextracts script blocks (including the<script context="module">block). - For each non-context-module script, it calls
is_template_visible_scriptwhich returns true. - For each template-visible script, it adds imports to
template_visible_importsandbinding_target_namestotemplate_visible_bound_targets. - After processing all scripts, it calls
apply_template_usagewithcollect_template_usage_with_bound_targets.
So the issue might not be in the Svelte scanner at all. Let me actually reproduce the issue. Let me look at the tests in the test fixture folders:
- Svelte
Assistant
Now let me look at the test integration files:
Assistant
OK so the existing tests already cover the
onclick={() => counter.bump()}case. The issue is for direct method calls without arrow function, likeonclick={resultState.pin}. Let me look at the actual issue from asciimoo/hister to better understand:Let me revert my test additions and look at how the issue manifests:
Assistant
Now let me actually reproduce the issue. The issue states
resultState.pin(...)but I think this is probably actually a complex expression. Let me check what it could look like in the actual Svelte source. Looking at the issue more carefully:The repo currently sets
rules.unused-class-members: "off"to work around it.So this is a real issue. Let me look at the test that exists for
svelte_template_event_handlers_mark_class_members_used:Assistant
Now let me run the existing test:
Assistant
Good, the existing test passes. So when does the issue manifest? The issue says Svelte methods called only from Svelte template via member expression are reported as unused-class-member. Let me look at how the asciimoo/hister example might work:
The pattern is
resultState.pin(...)where the template has a method call (not just an arrow function). Let me see if I can create a small test fixture for the actual issue:Assistant
Now let me look at how svelte-project is set up. Let me modify App.svelte to add a direct method call case (no arrow function):
Assistant
Now let me create a test fixture for the actual issue. First, let me look at the issue more carefully. Let me check the CHANGELOG.md:
Assistant
Let me look at the recent Svelte-related commits:
Assistant
OK, so there was a recent fix for Svelte
onclick={() => counter.bump()}handling. The issue says:Class methods called only from a Svelte
<template>via a member expression (for exampleresultState.pin(...)in markup) are reported asunused-class-member.So the actual issue is when
resultState.pin(...)is used directly without arrow function. Let me test this:Assistant
Let me now test the actual analyzer with this. First, let me create a temporary fixture directory:
Assistant
Interesting! In this test,
pinis correctly credited. The issue mentions thatResultState.onOpen,.pin,.updateLabel, and.addSkipRuleare all reported unused. Let me try to look at the actual repo to understand the difference. The actual repo has multiple methods. Let me try a more complex test:Assistant
I can reproduce the issue. Only
pinis used (because I only referenced it in App.svelte). Let me update the App.svelte to reference more methods:Assistant
That works. So the existing direct
onclick={resultState.pin}works. But the issue says it doesn't. Let me look at the asciimoo/hister repo pattern. Let me search for typical svelte 5 syntax:The issue might be about when
resultState.pin(...)is used as a method call (e.g.resultState.pin()) versus just a method reference (e.g.resultState.pin). Or when used inside attribute expressions. Let me try the case from the asciimoo/hister repo's ResultActionsMenu.svelte:Assistant
This also works. So what's the issue? Let me look at the actual asciimoo/hister repo. The issue says
ResultActionsMenu.sveltehasresultState.pin(...)in markup. Let me look at how that file might look. It's a Menu component, so it might pass to child components:Assistant
Now I've reproduced the issue! When the Svelte component is invoked from another Svelte component via props, the bound_targets don't get the actual class - they only get the type, so
resultState.pinetc. are not detected as member accesses onResultState.Let me look at how the issue is processed:
Assistant
So the issue is that the
resultStateis destructured from$props()with a type annotation, so the extractor doesn't see it asnew ResultState()(which would generate a bound_target). Instead, the type is justResultState.Let me look at how Vue handles this case:
Assistant
OK, so for Vue, the script also has
const counter = new Counter()so the bound_target gets set. Now, what about when the prop comes from a parent? Let me check the Vue test more carefully. Let me also look at the svelte test fixture:Assistant
OK, so the Svelte test uses
const counter = new Counter()in the script, which is what creates thebound_target. The issue is that when the script only haslet { resultState }: { resultState: ResultState } = $props(), the extractor doesn't know thatresultStateis an instance ofResultState.Now let me look at how to detect this. The issue is that the type annotation in the destructuring pattern (
{ resultState }: { resultState: ResultState }) should be enough to know thatresultStateis an instance ofResultState.Let me look at the visitor:
Assistant
OK, so
record_typed_bindingis used to register bindings that come from type annotations. Let me see where it's called:Assistant
Now let me look for where destructuring with type annotation is processed:
Assistant
So the current code only handles
BindingPattern::BindingIdentifier(single variable), but notBindingPattern::ObjectPattern(destructuring). The destructuring case isn't currently handled. Let me see the full visit_variable_declaration:Assistant
I see now. The current
visit_variable_declarationonly handles the case ofBindingPattern::BindingIdentifier(single variable). For object destructuring with a type annotation (e.g.,let { resultState }: { resultState: ResultState } = $props()), it doesn't extract the type annotation to register the binding target.Let me look at how Vue handles this case. Looking at the Svelte test more carefully - the test uses
const counter = new Counter(). The issue is about alet { resultState }: { resultState: ResultState } = $props()pattern. So this is a Svelte 5 props destructuring with type annotation.Let me check what Svelte 4 vs Svelte 5 syntax is:
- Svelte 4:
export let resultState: ResultState(declaring a prop with type annotation) - Svelte 5:
let { resultState }: { resultState: ResultState } = $props()
Let me test both patterns:
- Svelte 4:
Assistant
Confirmed! This is the issue. When the script has
let { resultState }: { resultState: ResultState } = $props()(destructuring with type annotation), the type annotation is not being used to register the binding_target forresultState.Now let me look at how to fix this. The fix needs to be in the visitor to handle the case where:
- The pattern is an
ObjectPattern(orArrayPattern) - There's a type annotation on the declarator
- The type annotation is an object type with named properties
Let me look at how this is handled in Angular (since it deals with constructor injection types):
- The pattern is an
Assistant
Let me look at
extract_nested_type_bindingsto understand how to extract types from an object type annotation:Assistant
Now I understand. The
extract_nested_type_bindingsfunction already handles the case ofTSTypeLiteral(object type). It returns paths like("resultState", "ResultState"). So if I call it on a type annotation of an object pattern, I should be able to register the binding target for each property.Let me look at
record_typed_bindingagain:Assistant
record_typed_bindingalready handles both:- Direct type annotation like
const x: Foo = ...→binding_target_names: {x: "Foo"} - Nested object type annotation like
const x: { foo: Foo } = ...→binding_target_names: {"x.foo": "Foo"}
So we just need to call
record_typed_bindingfor the destructured properties when the destructuring pattern is anObjectPattern.Let me look at
extract_object_pattern_bindingsto see how to map destructured names:- Direct type annotation like
Assistant
Now I need to find the place to add the handling. Let me look at the relevant part of
visit_variable_declaration:Assistant
The current code only handles the case where the binding is a single identifier. Let me look at how to extend this to also handle object patterns.
The logic should be:
- If the pattern is
ObjectPatternand there's a type annotation:- For each destructured property in the pattern
- For each nested type binding from the type annotation
- Try to match the destructured name to the type binding
- Register the binding target
Actually, looking at this more carefully, the type annotation of the binding is the WHOLE type. So if we have
let { resultState }: { resultState: ResultState } = ..., the type annotation is what we have. The destructured corresponds to the type binding .- If the pattern is
Assistant
Let me look at the SFC's
template_visible_bound_targetsextraction:Assistant
So the bound_targets from the script extraction is passed to
collect_template_usage_with_bound_targets. Now let me look at how the template scanner uses bound_targets:Assistant
So the bound_targets needs to have the local name (e.g.
resultState) map to the class name (ResultState). Theresolve_bound_member_accessesusesresolve_bound_object_nameto remap.Now I need to fix
visit_variable_declarationto handleObjectPattern(and possiblyArrayPattern) with type annotations. Let me look at the Svelte 4 syntax withexport let:Assistant
OK so
export let resultState: ResultState(Svelte 4 syntax) works. The issue is specifically with Svelte 5 destructuring syntaxlet { resultState }: { resultState: ResultState } = $props(). Let me also test other variations:Assistant
Confirmed. The issue is reproducible. Now let me think about the fix.
The fix needs to be in
visit_variable_declaration. When we have:typescriptlet { resultState }: { resultState: ResultState } = $props();The pattern is
ObjectPattern, the type annotation is{ resultState: ResultState }. We need to registerresultState→ResultStateinbinding_target_names.Let me also think about other patterns. What about Svelte's
export let { resultState }: { resultState: ResultState }? Or with array patterns? Or with renaming? Or nested destructuring?Let me look at what the existing code does for
let { foo } = { foo: ... }without type annotation. It doesn't register any binding_target_names. The current code only handlesBindingIdentifier. The issue says:Expected: credit a method called as inside a Svelte when resolves to an instance of the class, parallel to the existing Vue/Angular template member-access tracking.
Assistant
Let me look at how the existing record_typed_binding works with destructuring. The
extract_nested_type_bindingsreturns pairs like("foo", "Foo")for{ foo: Foo }. If I have a singleBindingIdentifier x: Foo, thenrecord_typed_binding("x", type_annotation)would insert"x": "Foo". But forBindingIdentifier x: { foo: Foo }, it would insert"x.foo": "Foo".So the issue with destructuring is that the existing
record_typed_bindingdoesn't help when we have a multi-property object pattern. Let me check what happens if I havelet { foo }: { foo: Foo } = ...and callrecord_typed_bindingon the full annotation:extract_nested_type_bindings({ foo: Foo })returns[("foo", "Foo")]. Then the existing code insertsformat!("{}.{}", "", "foo")="foo"→"Foo". That actually might work for the simple case! But it won't handle the case of multiple destructured properties with the same name, etc.Let me think more carefully. The existing function takes a single . For an object pattern, we don't have a single name; we have multiple. So we need to handle each property separately.
Assistant
Let me look at the existing tests to understand the conventions:
Assistant
Now let me think about the fix. The simplest approach is to:
- When we encounter a
BindingPattern::ObjectPatterninvisit_variable_declarationwith a type annotation - Walk the pattern and the type annotation in parallel
- For each property that maps to a
BindingIdentifier, look up the type from the type annotation and register the binding target
Actually, looking at this more carefully, I think a simpler approach is:
- If the pattern is an
ObjectPatternwith a type annotation that is aTSTypeLiteral - Walk the pattern and the type literal in parallel
- For each matched property name, register the binding
Let me also consider: what if the type annotation is a
TSTypeReference(e.g.,let { resultState }: Props = $props(), wherePropsis an interface)? Let me test that:- When we encounter a
Assistant
Both inline type literal and interface-based type annotation have the same issue. For the interface case, we would need to look up the interface declaration to find the property types. That's a more involved change. Let me focus on the inline type literal case for now since that's the simpler and more common case.
Let me look at the oxc AST for ObjectPattern and TSPropertySignature:
Assistant
OK, now I understand the pattern. Let me design my fix.
I'll create a helper function that processes an
ObjectPatternwith a type annotation. The function will:- For each property in the ObjectPattern
- Find the corresponding property in the TSTypeLiteral
- Extract the type name and register the binding target
Let me also think about edge cases:
- Nested destructuring:
let { a, b: { c } }: { a: A, b: { c: C } } = ... - Renamed destructuring:
let { a: renamedA }: { a: A } = ... - Default values:
let { a = 'default' }: { a?: A } = ... - Array pattern:
let [a, b]: [A, B] = ...
For now, let me focus on the common case: simple object destructuring with type annotation.
Let me look at what the issue specifically says about the fix:
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.Assistant
Now let me design the fix. I'll add a new function
record_typed_object_bindingthat processes an ObjectPattern with a type annotation, and call it fromvisit_variable_declaration. The function will:- Check if the type annotation is a TSTypeLiteral
- For each property in the ObjectPattern, look up the corresponding type
- Register the binding target
Let me also handle the case where the type annotation is a TSTypeReference (e.g., interface name) by looking up the interface declaration.
Actually, for the scope of this issue, let me focus on:
- The common Svelte 5 props destructuring case:
let { resultState }: { resultState: ResultState } = $props()
And for Svelte 4, the existing code already handles
export let resultState: ResultState.Let me also look at how the issue is phrased more carefully:
Class methods called only from a Svelte
<template>via a member expression (for example in markup) are reported as . Fallow tracks template-visible identifiers but not member-expression method calls on a bound instance inside Svelte markup.Assistant
Now let me look at the
BindingPropertystructure:Assistant
Now let me look at what the SFC's
template_visible_bound_targetsdoes:Assistant
Now let me actually write the fix. I'll modify the
visit_variable_declarationto also handleObjectPatternwith a type annotation. I'll add a helper function:Assistant
Now I need to understand the approach better. Let me look at the current handling of destructuring:
Assistant
So the current code only handles the case where the binding is a single identifier with a type annotation. I need to extend this to also handle the case where the binding is an
ObjectPatternwith a type annotation.Let me design the fix:
-
If the binding is
ObjectPatternand the type annotation is aTSTypeLiteral:- For each property in the ObjectPattern
- Find the corresponding property in the TSTypeLiteral
- Extract the type name from the TSTypeLiteral's property
- Register the binding target
-
The type annotation might also be a
TSTypeReference(e.g.,let { a }: MyInterface = ...). In that case, we need to look up the interface declaration. This is more involved.
For now, let me focus on the
TSTypeLiteralcase (inline type) since that's the most common case for Svelte 5 props. I'll also add support for theTSTypeReferencecase if it's straightforward.Let me also think about how to handle the case where the type annotation is a
TSTypeReferenceto a local interface:typescript-
Assistant
OK so the visitor records the interface declaration as a local declaration. Let me see if I can look it up:
Assistant
The
LocalTypeDeclarationis just a name and span, not the actual declaration body. So I can't look up the interface body from the visitor.For now, let me focus on the inline type literal case. This is the most common case for Svelte 5 props:
typescriptlet { resultState }: { resultState: ResultState } = $props();Let me now write the fix. I'll add a helper function that processes an
ObjectPatternwith a type annotation:Assistant
Now let me write the fix. I'll modify
visit_variable_declarationto also handle theObjectPatterncase:Assistant
Now let me write the fix. I'll replace this section:
Assistant
Now let me add the helper functions. Let me find a good place:
Assistant
Now let me add the helper functions after
record_typed_binding:Assistant
Now let me build and test:
Assistant
Let me fix this:
Assistant
Let me use
&*property_nameorproperty_name.as_ref():Assistant
Now let me test:
Assistant
Hmm, still all unused. Let me debug. The issue is that the bound_targets is populated from the script, but then in
apply_template_usage, it's used to remap member accesses. Let me look at how the template usage is built:Assistant
With
console.log(resultState.pin())in the script,pinis correctly detected as used. So the bound_targets IS being set correctly. But the template usage isn't being detected. Let me add some debug output:Assistant
So
pinis now being detected. Let me addonOpento template:Assistant
Wait! That output is from the previous test. Let me run it again to be sure:
Assistant
pinandonOpenare now correctly being detected. Let me try the full test:Assistant
Excellent! The fix works! Now let me also verify the interface-based case:
Assistant
The interface case doesn't work because we're not resolving the interface to its body. That's a more involved change. Let me check if the interface case is critical for the issue. The issue mentions:
ResultState.onOpen,.pin,.updateLabel, and.addSkipRuleare each invoked from.sveltetemplatesThis could be either inline type or interface. Let me look at the asciimoo/hister repo to see which pattern is used. But for now, the inline type case works. Let me also add support for the interface case.
Actually, looking at this more carefully, I think the more common pattern in Svelte 5 is the inline type literal. Let me focus on that and also try to support the interface case.
Let me look at how to resolve a TSTypeReference to its declaration. The visitor already records interface declarations in
local_type_declarations. But the visitor doesn't have the actual body of the interface, just the name and span.Let me look at a different approach. I can store the interface bodies during the walk:
Assistant
Let me look at what
local_type_declarationscontains:Assistant
The
local_type_declarationsjust stores name and span, not the body. I would need to either:- Store the interface body in the visitor
- Walk the type reference to find the interface body
Let me look at if there's a simpler way. Actually, the issue says the issue is for
resultState.pin(...)etc. This is the inline type literal case. Let me also try thePropstype alias:Assistant
The type alias case also doesn't work. For now, let me focus on the inline type literal case which is the most common. The interface/type alias case is more complex and would require resolving the type reference. Let me check if the issue mentions specifically inline type or could be either:
The issue says:
ResultState.onOpen,.pin,.updateLabel, and.addSkipRuleare each invoked from.sveltetemplates (resultState.pin(...)inResultActionsMenu.svelte) yet reported unused.The phrase
resultState.pin(...)in markup could be from either case. Let me look at the asciimoo/hister repo to see what pattern is used.Actually, let me focus on the more impactful change. The inline type literal case is common in Svelte 5. The interface/type alias case is also common but requires more infrastructure. Let me also implement the interface/type alias case to be thorough.
Let me first see if the visitor already has access to the interface bodies:
Assistant
The visitor doesn't store interface bodies. Let me look at how it could be done. Actually, let me look at what the issue is about more carefully.
Looking at the test fixture in the svelte-project, the
App.svelteuses:sveltelet { resultState }: { resultState: ResultState } = $props();Actually, I need to check the original test fixture. Let me look at the original existing test:
Assistant
Let me update the test fixture to also include the destructuring case (which is the most common Svelte 5 pattern):
Assistant
OK, so the existing fixture uses
const counter = new Counter()which is the Svelte 4 / Vue style. I need to add a new fixture that tests the Svelte 5 destructuring case.Let me first add a test case in the existing test. Let me look at the existing test:
Assistant
Now let me first look at the existing tests that pass to make sure I haven't broken anything:
Assistant
All existing tests pass. Now let me also look at the other related tests:
Assistant
All 523 integration tests pass. Now let me also think about edge cases:
- Nested destructuring:
let { a: { b } }: { a: { b: B } } = ... - Renamed destructuring:
let { a: renamedA }: { a: A } = ... - Default values:
let { a = 'default' }: { a?: A } = ... - Array pattern:
let [a, b]: [A, B] = ...
Let me also add support for the interface/type alias case. Actually, for now let me focus on making the common case work and write a test for it. Let me also test edge cases:
- Nested destructuring:
Assistant
Renamed destructuring works! Now let me test nested destructuring:
Assistant
Wait, that test was a bit weird. Let me try nested destructuring:
Assistant
Nested destructuring works. Let me also test array pattern:
Assistant
Array pattern doesn't work. The issue is that
[ResultState]is aTSTypeLiteral(tuple type literal), not an object type literal. The property is not aTSPropertySignaturebut aTSIndexSignature. Let me check:Assistant
Hmm, for a tuple type like
[A, B], the members would be TSPropertySignature with numeric keys, but oxc might represent them differently. Let me look at the AST more carefully:Actually, for tuple types
[A, B], the AST is different. It's not a TSTypeLiteral. Let me check:Assistant
Tuple types are represented as
TSTupleType, notTSTypeLiteral. Let me update my fix to handle both:Assistant
Let me fix this to handle both TSTypeLiteral (object type) and TSTupleType (tuple type):
Assistant
So
TSTupleElementinherits fromTSType. TheTSTypevariants are inherited. So I need to useTSType::...patterns. Let me look at the structure:Assistant
OK so
TSTupleType.element_typesis aVec<TSTupleElement>. TheTSTupleElementis an enum withTSOptionalType,TSRestType, and theTSTypevariants inherited. So I need to use a match on theTSTupleElementand extract the type from it.Let me look at how to extract the TSType from TSTupleElement. Since TSTupleElement inherits from TSType via @inherit, it should be a TSType. Let me check:
Assistant
Let me look at how oxc handles this. The
TSTupleElementinherits fromTSType, so it should be matchable as aTSType:Assistant
So
TSTupleElementis an enum that has all theTSTypevariants as well asTSOptionalTypeandTSRestType. I can match on theTSTypevariants directly. Let me update my fix:Assistant
Let me take a different approach. The issue is that I need to extract the TSType from TSTupleElement. Since TSTupleElement inherits from TSType, I can use
el.as_ref()to get a&TSType. Let me look at the oxc API:Assistant
Let me look at how the visitor walks TSTupleElement:
Assistant
There's a
to_ts_type()method. Let me look for it:Assistant
So
TSTupleElementhasto_ts_typeandas_ts_typemethods. Let me useas_ts_type():Assistant
Let me look at how to use as_ts_type:
Assistant
Let me look at the macro definition:
Assistant
OK so
as_ts_typeis a method that returnsOption<&TSType>. Let me use it:Assistant
Let me think more carefully. The
TSTupleElementhasas_ts_type()which returnsOption<&TSType>. I can use that to get the TSType. Then I need to construct a&TSTypeAnnotationfrom that, but that's not straightforward.Actually, the simplest approach is to extract the type name directly from the TSType. Let me look at how the existing code does this:
Assistant
So
extract_type_annotation_nametakes a&TSTypeAnnotation. I can either:- Modify
extract_type_annotation_nameto also take a&TSType - Create a helper to extract the type name directly from a
&TSType
Let me look at
extract_type_reference_name:- Modify
Assistant
extract_type_reference_nametakes a&TSType. I can use that directly. Let me update my fix:Assistant
Now let me write a proper fix:
Assistant
I need to add
extract_type_reference_nameto the import. Let me check the helper file:Assistant
extract_type_reference_nameis public. Let me add it to the import:Assistant
Let me test the array pattern:
Assistant
Array pattern works now! Let me test the multi-element case:
Assistant
Array pattern with multiple elements works! Now let me make sure all existing tests still pass:
Assistant
All 523 integration tests pass. Now let me also run the unit tests:
Assistant
There's one test failure. Let me check if it's related to my changes:
Assistant
This test failure is about a read-only directory test, not related to my changes. Let me check if it was failing before my changes:
Assistant
OK so the test failures are pre-existing and not related to my changes. Let me restore my changes:
Assistant
Good, my changes are back. Now let me also test more edge cases:
Assistant
Default value with
?doesn't work because we need to handleAssignmentPattern(the?makes it an optional). Let me check:Assistant
The default value with
=wraps the binding in anAssignmentPattern. I need to handle that case too. Let me update:Assistant
Let me update
collect_typed_object_bindingsto also handleAssignmentPattern:Assistant
The default value case now works for
pin. Let me also test the same for array:Assistant
Array with default doesn't work. Let me update the array case too:
Assistant
Hmm, still not working for array with default. Let me check if the type is
TSTupleTypeorTSTypeLiteral:Assistant
The issue might be that
[ResultState?]is being parsed as aTSArrayType(array of optional ResultState) rather than aTSTupleType. Let me try with explicit tuple syntax:Assistant
Array with default value now works! Let me run all the existing tests to make sure nothing is broken:
Assistant
All tests pass. Now let me also think about the interface case. Let me look at the asciimoo/hister repo to see what pattern is used:
Looking at the issue again:
The repo currently sets
rules.unused-class-members: "off"to work around it.The interface case might also be used. Let me add support for it. The approach would be:
- When the type annotation is a
TSTypeReference(e.g.,Props) - Look up the interface/type alias declaration
- Extract the type literal from it
- Process the object/array pattern
For this, I need to be able to look up the type declarations. The visitor already tracks interface declarations in
local_type_declarations(just name + span). I'd need to also store the body.Let me look at the existing visitor state to see what's tracked:
- When the type annotation is a
Assistant
OK, so the visitor already tracks local type declarations. But it doesn't track their bodies. Let me think about the simplest way to handle this.
For the interface/type alias case, I can:
- During the walk, store a map of
name -> bodyfor local type declarations - When processing a destructuring with a type reference, look up the body
- Process the body the same way as the inline type
But this is more complex. For now, let me focus on the inline type case which is the most common and is what the issue seems to be about.
Actually, let me also add support for the interface case since it's straightforward. I'll store the interface bodies during the walk:
- During the walk, store a map of
Assistant
Let me think about this. The approach to support interface/type alias would require:
- Storing the interface/type alias bodies during the walk
- Looking them up when processing destructuring patterns
This is a bigger change. Let me think about whether the interface case is critical for the issue.
The issue mentions:
Assistant
So the visitor has
local_declaration_nameswhich is populated as declarations are seen. But it doesn't store the bodies. Let me look at the approach used in the existing code for handling forward references.Actually, the simplest approach is to do a separate pass after the main walk. Let me look at how the existing code handles similar cases:
Assistant
OK so the visitor does multiple resolve passes. Adding interface/type alias support would require:
- Storing the interface/type alias bodies during the walk
- Adding a resolve pass that processes destructuring patterns with type references
This is a bigger change. For now, let me focus on the inline type case. The issue is most likely about the inline type case, and that's what I've fixed.
Let me also test the Vue case to make sure I haven't broken anything:
Assistant
Vue tests still pass. Now let me look at how the Svelte test fixture could be updated to include the destructuring case. Let me look at the test fixture and see if I should add it:
Assistant
I should not modify the existing fixture as the test says. But I can add a new fixture to test the destructuring case. Let me create a new fixture:
Wait, I cannot modify existing tests or add new tests. Let me re-read the instructions:
UNDER NO CIRCUMSTANCES should you modify existing tests or add new tests to the repository.
OK, so I can't add new tests. But I can verify that the existing test for the arrow function case still passes (which it does). And I can verify that the fix works with a manual reproduction (which I've done).
Let me think about what else I need to do. Let me also check the case where the destructuring has no type annotation but uses
newto create the instance:Assistant
This is a different case - destructuring from
new ResultState(). This is a JavaScript-level destructuring, not a type annotation. This would require a different approach (trackingnewexpressions in the script and binding the destructured names to the class methods).Actually, this is similar to the existing handling for
const x = new Foo()wherex.bar()is tracked. The case here isconst { bar } = new Foo()wherebar()should be tracked asFoo.bar(). But this is a separate case and probably already doesn't work even for non-Svelte files. Let me check:Assistant
OK so this case (
const { pin } = new ResultState()) doesn't work for plain TS either. This is a pre-existing limitation that's not in scope for this issue. The issue is specifically about type-annotated destructuring.Let me also test that the
resultState(local) shadows correctly. Let me look at the existing test for shadowing:Assistant
Let me also look at how the
localsare used in the Svelte scanner. Thelocalsparameter is used to determine which identifiers are local to the template (e.g.,{#each rows as row}introducesrowas a local). My fix doesn't affect the Svelte scanner - it only affects the visitor that populates thebound_targets. So the existing shadowing logic should still work.Let me test this:
Assistant
Shadowing works correctly - all 5 methods are reported as unused because the template uses
resultStateas a number (from{#each ... as resultState}), which shadows the script-levelresultState. The Svelte scanner correctly handles this.Now let me also check that my fix works for the case where the prop name is different from the local name:
Assistant
Renamed destructuring works. Now let me also make sure I haven't broken the existing test:
Assistant
Let me also run the visitor tests:
Assistant
All 1638 tests pass. Let me also run the core tests:
Assistant
All 2415 core tests pass. Let me also run the integration tests:
Assistant
All 523 integration tests pass. Now let me think about what else I need to consider.
Let me look at the current state of my changes:
Assistant
Good. Let me also think about whether I need to handle the case where the type annotation is a
TSTypeReferenceto an interface. Let me look at what would be needed.Actually, looking at this more carefully, the issue is about the Svelte 5 props destructuring case, which is typically:
let { resultState }: { resultState: ResultState } = $props();(inline type literal)let { resultState }: Props = $props();(named interface)
Let me also add support for the interface case. I'll need to:
- Store interface/type alias declarations with their bodies
- Look them up when processing destructuring patterns
Let me see if I can do this more simply. The current approach of
record_typed_object_bindingis called fromvisit_variable_declaration. By the time we reach alet { resultState }: Props = $props(), theinterface Props {...}declaration might or might not have been visited yet.Let me look at the AST visit order:
Assistant
The visitor doesn't override
visit_program. Let me look at how the visitor walks:Assistant
The visitor doesn't override
visit_programso it uses the default walk. The walk traverses statements in order, so declarations are visited in source order.Assistant
OK, the Vue scanner also uses
merge_expression_usage_with_bound_targets. So the fix is just in the visitor to populate the bound_targets correctly.Now let me think about whether the fix is complete. Let me also test the case where the type annotation is a
TSTypeReference(interface) to see if the existing behavior is at least better:Assistant
Interface case still doesn't work. Let me see if I can add support for it. The approach would be:
- Store interface declarations with their bodies in the visitor state
- Look them up when processing destructuring patterns
But this requires more changes. Let me think about the trade-off. The interface case is common, but it would require more code. Let me try a simpler approach: walk the type reference to find the interface declaration by its name.
Actually, looking at the visitor more carefully, I see that
local_declaration_namesis populated as declarations are seen. So when we process a destructuring pattern, the interface might or might not be in the set. For the common case where the interface is declared before the destructuring, it would be in the set.But the visitor doesn't have the body of the interface, just the name. So I'd need to:
- Store the body when the interface is declared
- Look it up when processing destructuring
Let me do this. I'll add a to the visitor state. Wait, actually the issue is that the visitor only tracks as a with name and span. The body is not stored.
Assistant
The visitor doesn't have a lifetime parameter on the struct itself. It has one on the
Visitimpl. So storing the body would be complex.For now, let me focus on the inline type case which is what the issue is most likely about. The interface case is a separate enhancement.
Actually, let me look at the issue once more. The issue says:
Class methods called only from a Svelte
<template>via a member expression (for exampleresultState.pin(...)in markup) are reported asunused-class-member.The example
resultState.pin(...)doesn't specify the syntax. Let me check the asciimoo/hister repo to see what's used.Actually, I don't have access to that repo. But looking at the issue, the user says the methods are "each invoked from templates". This is a general statement.
Assistant
Function parameters don't work with my fix. But this is a different case - the bound_target would need to be set in the function scope, not module scope. The Svelte template scanner only looks at module-scope bindings. So function parameters wouldn't be picked up by the template scanner anyway.
Let me also test the case where the type annotation is a union type:
Assistant
Union type with null works!
extract_type_annotation_namehandles theT | nullcase. Now let me also test with a type alias:Assistant
Type alias still doesn't work. As I said, the interface/type alias case requires more work.
Let me also look at the current state of my changes to make sure the code is clean:
Assistant
The code looks clean. Let me also check the
path_prefixparameter - I don't think I'm using it correctly. Let me check:Actually, I see that I'm using
path_prefixonly for tracking the path for nested object patterns, but I'm not actually using it in the binding name. The binding is always the local identifier name (e.g.,resultState). The path is just for the type lookup. Let me clean this up:Actually wait, the
path_prefixis used to track the type path for nested lookups, but the binding name is always the local identifier. Sopath_prefixis only used for the recursive call. Let me clean this up:Assistant
The fix still works. Let me also run all the tests:
Assistant
All tests pass. Let me also run the clippy:
Assistant
Let me fix the clippy warnings:
Assistant
Clippy passes. Let me also run the full clippy:
Assistant
All clippy checks pass. Let me also run rustfmt:
Assistant
Now let me run the full test suite one more time:
Assistant
All tests pass. Let me also verify my final reproduction:
Assistant
The fix works as expected. Only
ResultState.unusedis reported as unused, which is correct.Let me also clean up the temporary test files:
Assistant
Let me also look at the final diff:
Assistant
The diff is clean and focused. Let me also think about whether I need to handle the case where the type annotation is an interface. The issue mentions
resultState.pin(...)which is the typical Svelte 5 pattern. Let me see if I can also add support for the interface case as a follow-up.Actually, let me also test the original test case (the one with the existing fixture) to make sure I haven't broken anything:
Assistant
All Svelte tests pass. Now let me also think about whether I should add support for the interface case. The issue is specifically about the Svelte 5 props destructuring, and the most common pattern is the inline type literal. The interface case is a separate enhancement.
Let me also think about edge cases:
- What if the type annotation is a
TSTypeReferenceto a generic type? e.g.,let { resultState }: Wrapper<ResultState> = ... - What if the type annotation is a
TSIntersectionType? e.g.,let { resultState }: A & B = ...
For now, these are not handled, but they are edge cases that can be added later.
- What if the type annotation is a
Assistant
The Vue scanner also uses
merge_expression_usage_with_bound_targets. So the fix is just in the visitor to populate the bound_targets correctly.Let me also check that the fix doesn't break the existing Vue tests:
Assistant
All Vue tests pass.
Let me also run the full test suite one more time to make sure everything is good:
Assistant
All tests pass. Let me also do a final sanity check with the original reproduction:
Assistant
The fix works. Only
ResultState.unusedis reported as unused, which is correct.Let me also check the original code without my fix:
Assistant
Without my fix, all 5 methods are reported as unused. Let me restore my fix:
Assistant
My fix resolves the issue. Let me clean up:
Assistant
Let me also check the final diff one more time:
Assistant
Only one file changed, with 150 insertions and 7 deletions. The change is focused and minimal.
Let me also verify the final state of the file:
Assistant
Both rustfmt and clippy pass. Let me also run the final test suite:
Assistant
All 523 integration tests pass.
Now let me go through the checklist:
Checklist:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.