ISSUE DESCRIPTION service member referenced in an Angular external template is reported…
- User
ISSUE DESCRIPTION
service member referenced in an Angular external template is reported as unused when the component property is initialized with
inject()(no explicit type annotation)What happened?
With the
angularplugin active,fallow dead-codereports a service method as an unused class member when its only reference is in an Angular HTML template (prop.method()), and the component propertypropis initialized with the moderninject()pattern without an explicit type annotation:tsreadonly exampleService = inject(ExampleService);Fallow already credits template references through component properties for the other three DI/instance patterns — it is specifically the untyped
inject()initializer that is missed. The following all resolve correctly (no false positive); only the last one fails:Component property pattern Template prop.method()resolved?constructor(public svc: ExampleService) {}✅ yes svc: ExampleService = inject(ExampleService);(typed)✅ yes svc = new ExampleService();✅ yes svc = inject(ExampleService);(untyped)❌ no — false positive Because untyped
inject()is the idiomatic Angular style, this misfires on a very common real-world pattern.Reproduction
Minimal standalone repro.
@angular/coreonly needs to be listed inpackage.jsonso theangularplugin auto-activates — runningnpm installis optional (without it, fallow prints a "node_modules directory not found" warning but the finding still reproduces).-
Create
package.json:json{ "name": "fallow-angular-template-repro", "version": "1.0.0", "private": true, "dependencies": { "@angular/core": "^20.0.0" } } -
Create
.fallowrc.json:json
-
- Assistant
I'll start by exploring the repository structure to understand the codebase. Let's first look at the root directory.
- Assistant
Let's explore more to understand the project structure.
- Assistant
Let me understand the project structure better. This is a Rust project (Cargo.toml). Let's look at the Cargo.toml to understand the crate structure.
- Assistant
Let me understand the project structure. This is a Rust project with a
cratesdirectory. I need to find the relevant code that handles Angular template references and the dead-code analysis. Let me first explore the codebase. - Assistant
I need to make only one tool call at a time. Let me start by exploring the repository structure to understand the codebase.
- Assistant
Let me explore the extract and core crates to understand the structure.
- Assistant
Let me look at the template_usage.rs file, as it seems most relevant to the issue about Angular templates.
- Assistant
Let me look at the Angular-specific file in the sfc_template directory.
- Assistant
The issue is about Angular templates. The problem is that when a component property is initialized with
inject(ExampleService)without an explicit type annotation, the dead-code analysis doesn't resolve the type of the property, so template references aren't credited to the service's members.Let me look for the code that resolves component property types, particularly for dependency injection patterns.
- Assistant
Let me look at the angular plugin in the core crate.
- Assistant
Let me search for the code that handles the dead-code analysis for class members and template usage.
- Assistant
Let me look at the unused_members.rs file and the angular_template_members integration test to understand how the analysis works.
- Assistant
Let me look for the logic that handles Angular template references and resolves component property types to service types. Let me search for "ANGULAR_TPL_SENTINEL" in the codebase.
- Assistant
Let me look at the key logic around line 1439 in unused_members.rs where the angular template sentinel is being used.
- Assistant
Let me continue reading the angular template references resolution logic:
- Assistant
Now I understand the issue better. The
angular_tpl_chain_accesseslogic at lines 1502-1545 resolves template member accesses (likeexampleService.onValueChange) through component bindings. It usesclass_heritage.instance_bindingsto map the local name (exampleService) to a type name (e.g.,ExampleService).But if the property is declared with
readonly exampleService = inject(ExampleService);without an explicit type annotation, there's no instance binding with a type, so the resolution fails.Let me understand how instance_bindings are populated. Let me look at the class_heritage and instance_bindings structures.
- Assistant
Let me look at
extract_class_instance_bindingsto understand how it extracts bindings. - Assistant
Now I understand the issue! The
extract_class_instance_bindingsfunction extracts bindings from:- Constructor parameters with accessibility modifiers (public/protected) - uses the type annotation
- Getter methods with return type annotations
- Property definitions with:
- Type annotations (line 672-678) - works for typed
inject()calls newexpressions (line 680-685) - works fornew ExampleService()
- Type annotations (line 672-678) - works for typed
But when a property has no type annotation and the value is a
inject()call, it's not handled. Looking at the code:rust - Assistant
Let me look at the existing test for angular template members to understand the expected behavior:
- Assistant
Now let me look at the existing test fixture to understand the pattern:
- Assistant
Let me look at the test fixtures to understand how they're structured.
- Assistant
Let me examine the fixture files more closely.
- Assistant
Let me look at the data-view.component.ts to see how it uses the service with DI.
- Assistant
So the existing test uses constructor injection with a type annotation. The issue is about
inject()without an explicit type annotation. Let me look at the data.service.ts to see what methods it has. - Assistant
Let me look at the code more carefully. First, let me understand the AST structure for
CallExpression: - Assistant
Now I see the exact location where I need to make the change. Let me understand the issue:
-
When a property is defined as
readonly exampleService = inject(ExampleService);(untypedinject()), the current code only handles:- Type annotations (line 672-678): There's no type annotation, so this is skipped
newexpressions (line 680-685): This is aCallExpression, not aNewExpression, so this is skipped
-
I need to add a third case: when the value is a
CallExpressionwhere the callee is the identifierinject, and the first argument is a type reference (e.g.,ExampleService).
Let me first check what imports are available at the top of the file to understand what I can use.
-
- Assistant
Now I see the issue clearly. In
extract_class_instance_bindings, thePropertyDefinitionbranch handles:- Properties with explicit type annotations (line 672-678)
- Properties initialized with
newexpressions (line 680-685)
But it does NOT handle properties initialized with
inject()calls when there's no type annotation. I need to add a third case forinject()calls. Let me check the test fixture to confirm my understanding: - Assistant
Let me first reproduce the issue as described. I'll create a minimal repro:
- Assistant
Let me check if there's a way to build and run fallow first:
- Assistant
Let me build the project first to ensure I can run tests:
- Assistant
Now let me create the reproduction test to confirm the issue:
- Assistant
Let me create the fixture files and write a test first.
- Assistant
Now I need to add the test case. Let me look at how existing tests work:
- Assistant
Let me understand the test infrastructure better and add the fixture:
- Assistant
Let me create a test fixture for the issue:
- Assistant
Let me create the fixture files:
- Assistant
Let me create the test fixture files.
- Assistant
Let me create the fixture files for the test.
- Assistant
Let me create the fixture files for the test.
- Assistant
Let me create the test case in the angular_template_members.rs file. First, let me look at how the test works.
- Assistant
Let me add a test for the untyped inject() pattern. First, let me see how the test file is structured:
- Assistant
Let me first run the existing test to make sure it passes:
- Assistant
Good, the existing tests pass. Now let me add a test for the untyped inject() pattern and see if it fails as expected:
- Assistant
I see the edit has a missing closing
}on line 82. Let me fix that: - Assistant
Let me fix the file properly:
- Assistant
I see the file is missing the closing
}for theangular_at_if_alias_credits_condition_membertest function. Let me fix that: - Assistant
Let me rewrite the whole file properly:
- Assistant
Good, the file was updated. Now let me run the test to see if it fails as expected.
- Assistant
Good, the test fails as expected.
ExampleService.onValueChangeis incorrectly flagged as unused. This confirms the issue.Now I need to fix the code. The issue is in
extract_class_instance_bindingsincrates/extract/src/visitor/helpers.rs. I need to add handling forinject()calls.Let me look at the exact code again:
- Assistant
Now I understand the issue clearly. The
extract_class_instance_bindingsfunction handles:- Properties with explicit type annotations (lines 672-678)
- Properties initialized with
newexpressions (lines 680-685)
But it doesn't handle properties initialized with
inject()calls without an explicit type annotation. I need to add a third case that checks forCallExpressionwhere the callee isinjectand the first argument is a type reference.Let me look at the AST types to understand how to access CallExpression arguments:
- Assistant
Let me look at how CallExpression is used in the codebase to understand its structure:
- Assistant
Now I can see the pattern for how to handle CallExpression. Looking at
extract_angular_signal_query, it checks if the callee is an identifier, and if the first argument is an identifier. This is exactly what I need forinject().Now let me implement the fix. I need to add a check after the
NewExpressioncheck forCallExpressionwhere:- The callee is the identifier
inject - The first argument is an identifier (the class name)
- The class name is not a builtin constructor
Let me add this after line 685 (the existing
NewExpressioncheck). - The callee is the identifier
- Assistant
Now I understand the code structure. I need to add handling for
inject()calls. Let me add this after line 685, right after theNewExpressioncheck: - Assistant
Let me try a different approach for the edit:
- Assistant
Now let me verify the patch was applied correctly:
- Assistant
Good, the patch looks correct. Now let me verify that
Argumentis imported at the top of the file: - Assistant
Good,
Argumentis already imported. Now let me build and run the test to see if the fix works. - Assistant
Now let me run the test to see if the fix works.
- Assistant
The test passes. Now let me run all the angular_template_members tests to make sure the existing tests still pass:
- Assistant
All 3 angular_template_members tests pass. Now let me also run the full test suite to make sure I haven't broken anything:
- Assistant
All 642 tests pass. Let me also run the extract crate tests to make sure:
- Assistant
All tests pass. Let me also run the full test suite across all crates to make sure:
- Assistant
The full test suite is timing out. Let me run a few more targeted tests to make sure the fix is correct:
- Assistant
Let me also check if there are any related tests for instance bindings:
- Assistant
All instance_binding tests pass. Let me now also look at whether there are any other edge cases to consider. Let me also check how the
injectfunction works with type arguments vs value arguments: - Assistant
Interesting! There are existing tests for
injectin the extract crate. Let me look at them to understand the existing behavior: - Assistant
Interesting! There's already a test
angular_inject_field_maps_this_field_member_accessthat shows the existing handling ofinject()forthis.inner.aaa→InnerService.aaamapping. This is different from the template case - it's aboutthis.field.memberbeing mapped to the correct type. But in the template case, the reference isexampleService.onValueChange(...), which needs theinstance_bindingsmechanism.Let me verify the existing inject test still passes with my fix:
- Assistant
All tests pass. Let me also check that there's a test for
newinitializers: - Assistant
Let me look at the test around line 2926 to see how
new MyService()is tested: - Assistant
Let me look at the test around line 2990 to see how instance bindings are tested:
- Assistant
Let me check for any test about
newinitializers and instance bindings: - Assistant
Let me search for tests that test the
newexpression instance binding: - Assistant
Let me look at the
non_instance_binding_not_mappedtest to understand what's tested: - Assistant
Let me look at the
exported_instance_binding_is_recordedtest: - Assistant
Wait, I need to think about this more carefully. Let me look at the existing
angular_inject_field_maps_this_field_member_accessandnon_angular_inject_function_does_not_map_field_member_accesstests more carefully: - Assistant
I see! The existing tests show that:
angular_inject_field_maps_this_field_member_access- Wheninjectis imported from@angular/core, it mapsthis.inner.aaatoInnerService.aaanon_angular_inject_function_does_not_map_field_member_access- Wheninjectis imported from a non-Angular source, it does NOT map
But note that both tests use
private readonly inner = inject(InnerService);- andprivatefields are skipped byextract_class_instance_bindings. So my fix wouldn't affect those tests.Let me verify by running all the relevant tests:
- Assistant
All inject-related tests pass. Let me also look at the
injected_object_member_access_mapped_to_classtest: - Assistant
Now let me also verify that the existing tests that use
newexpressions still pass: - Assistant
Let me run all the relevant test suites to make sure nothing is broken:
- Assistant
All 302 visitor tests pass. Let me also run the full integration test suite:
- Assistant
All 642 tests pass in fallow-core, and 302 visitor tests pass in fallow-extract. The fix is working correctly.
Let me now review all the changes I've made:
- Assistant
The diff shows only the changes to the source files, not the new fixture files. Let me also check the new fixture files:
- Assistant
Let me verify all the fixture files are correct:
- Assistant
All the fixture files look correct. Let me run the tests one final time to make sure everything is working:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.