ISSUE DESCRIPTION linter: no-unused-vars false positive when constructor parameter…
- User
ISSUE DESCRIPTION
linter: no-unused-vars false positive when constructor parameter property shadows an imported identifier used in its own decorator
What version of Oxlint are you using?
1.65.0 (also reproduces on 1.30.0)
What command did you run?
codeoxlint test.tsWhat does your
.oxlintrc.json(oroxlint.config.ts) config file look like?jsonc// default (no config file)What happened?
When a TypeScript constructor parameter property has the same name as an outer binding (import or
const), and that outer binding is used in the parameter's own decorator expression (e.g.@Inject(foo.KEY)), oxlint'sno-unused-varsreports the outer binding as declared but never used.This is a false positive. Parameter decorators are evaluated at class-decoration time in the enclosing class scope, before any parameter binding exists in the parameter scope. So
fooinside@Inject(foo.KEY)resolves to the outer binding, not to the same-named parameter property.This pattern is extremely common in NestJS code, where
@Inject(someConfig.KEY)is used to injectregisterAs(...)-style typed configs and the constructor parameter property is typically named after the config. The false positive forces a churn-y workaround: either alias the import (import { foo as _foo }) or rename the parameter property.Minimal reproduction
tsconst foo = { KEY: 'token' }; declare function Inject(token: unknown): ParameterDecorator; export class C { constructor(@Inject(foo.KEY) private readonly foo: number) {} }Actual
code! eslint(no-unused-vars): Variable 'foo' is declared but never used. Unused variables should start with a '_'. ,-[test.ts:1:7] 1 | const foo = { KEY: 'token' }; : ^|^ : `-- 'foo' is declared hereExpected
No diagnostics —
foois used in@Inject(foo.KEY).ESLint with + does not report this — its scope analyser correctly counts the decorator expression as a use of the outer binding.
Assistant
Let me start by exploring the repository structure to understand what we're working with.
Assistant
Let me first reproduce the issue.
Assistant
Let me first build the linter and reproduce the issue:
Assistant
Build takes time. Let me start exploring the code in parallel.
Assistant
Now let me look at the semantic to understand how decorators are handled:
Assistant
Now let me look at how
param.bind()works to understand the binding process:Assistant
Now let me understand the order:
param.bind(self)happens BEFOREself.visit_decorators(¶m.decorators). So:- First the parameter is bound (with the name
foo) - Then the decorator expression is visited, with
fooshadowing the outerfoobinding
This is the source of the bug! When
bindis called for the parameter, it introduces a new symbol namedfooin the parameter scope. Then when the decorator expression is visited,fooinside it resolves to the new (parameter)foosymbol, not the outerfooconstant.According to the ECMAScript/TypeScript semantics, parameter decorators are evaluated in the enclosing class scope BEFORE the parameter binding exists. The parameter binding should only be effective for the parameter scope itself, AFTER the decorator evaluation.
Let me check this in more detail.
- First the parameter is bound (with the name
Assistant
Now let me confirm the order in
visit_formal_parameter:Assistant
OK, so the order is:
enter_node(kind)- enter formal parameterparam.bind(self)- bind the parameter (this declares the symbolfooin current scope)visit_span(...)- visit the spanvisit_decorators(...)- visit decorators (with the newfoosymbol now visible)
This is the issue! We need to visit decorators BEFORE binding the parameter.
Let me also check the
visit_classandvisit_method_definitionto understand the chain:Assistant
Now let me look at where
visit_decoratorsis called for class. Decorators are visited BEFORE entering the class scope. So the class decorator is visited in the enclosing scope. This is consistent with the spec.Now, for parameters, the parameter is bound THEN decorators are visited. So the issue is that decorator expressions are visited in the new binding's scope.
Let me check how tsc handles this. According to TypeScript spec, parameter decorators are evaluated at the same time as class decorators when calling
decorate(...)function which happens in the enclosing scope of the class.Let me look at the issue's reference to similar issues:
Assistant
Now let me look at how the visit function handles things:
Assistant
OK so for
MethodDefinition,visit_decoratorsis called BEFOREvisit_functionwhich is where parameters live. So method decorators are evaluated before the function/method scope is entered.Let me look at the broader picture - what's the visit_function implementation, and when does scope change happen:
Assistant
Good. So the order is:
- Enter
MethodDefinitionnode - Visit decorators (still in class scope)
- Visit property key (still in class scope)
- Visit
Function(enters function scope) - Inside function, visit parameters - and THIS is where the issue is
In
visit_formal_parameter:enter_node(kind: FormalParameter)param.bind(self)- this declares the symbolfooin the current function scopevisit_decorators(...)- visited in function scope, wherefoonow shadows outerfoo
Let me now examine the fix. The fix should be to visit decorators BEFORE binding the parameter.
Let me first verify by looking at how typescript-eslint handles this:
- Enter
Assistant
Now I understand the flow. The decorator just visits its expression. Let me now check the issue's behavior in more detail.
Let me check the existing tests for no_unused_vars:
Assistant
Good. So the existing tests are using simple cases where the decorator doesn't reference the outer scope. Let me look at the existing test cases in detail. The first one (
@ConstructorParameterDecoratorFactory(APP_CONFIG)) uses a globalAPP_CONFIGthat doesn't conflict. The current bug is about when the parameterfooshadows an outer bindingfoo, andfoois used inside the decorator.Let me first build the binary and verify the bug, then write the fix.
Assistant
Confirmed! The bug is reproduced. Now let me think about the fix.
The issue is that
param.bind(self)is called beforeself.visit_decorators(¶m.decorators). According to JS semantics, the parameter's decorator expression is evaluated at class-decoration time, which is BEFORE the function scope is entered, and definitely BEFORE the parameter binding exists.The fix should be to visit decorators BEFORE binding the parameter. This way:
- Decorators are visited in the function/method scope (where the outer
foobinding is still visible). - Then the parameter is bound.
Let me make the change:
- Decorators are visited in the function/method scope (where the outer
Assistant
Now let me make the fix. According to ECMAScript/TypeScript semantics, parameter decorators run before any parameter binding is established. Let me look at this more carefully. The spec actually states:
When the constructor or method is called, the parameter values are passed in along with the decorator arguments. When the decorator function is called, it's called with the decorator arguments first followed by the value being decorated.
The decorator function itself is evaluated at class-decoration time (when the class is being initialized). This happens in the enclosing scope.
So the fix is: visit decorators BEFORE binding the parameter.
Let me apply the fix:
Assistant
Now let me think about whether changing the order could affect other things. Let me search for what tests for parameter binding/decorator behavior:
Assistant
Hmm, this is interesting. The existing snapshot shows that
decorator(the outer function) is referenced TWICE: once withnode_id14 and once withnode_id18, both inscope_id3 (the function scope). This is BEFORE my fix.Wait, but where in the test is it referenced? Let me check the file more carefully:
The test only references
decoratorin the decorators@decoratoron parameters. The decorators@decoratorare simple identifier references.In current code:
- Bind param
a - Visit decorator
@decorator- which visitsdecoratoridentifier reference - which is a single reference
Now if we change the order to visit decorators BEFORE binding, the same thing happens - we visit decorator
@decoratorwhich referencesdecoratoridentifier, once.So in either case, we should have ONE reference per
@decorator(so 2 references total). But the snapshot shows... wait, it shows references withnode_id14 and 18. Let me check:- Bind param
Assistant
Let me skip this and check the actual snapshot more carefully. The
decoratorsymbol at the top level has 2 references. That's correct given there are 2@decoratordecorators.But each decorator should produce ONE reference. So 2 references is correct, regardless of whether we visit the decorator before or after bind.
Now, the issue we need to fix is the parameter shadowing. So if we visit decorators BEFORE binding, then:
@Inject(foo.KEY)is evaluatedfooinfoo.KEYresolves to outerfoo(not the parameter binding)- Then bind the parameter
foo - References inside the function body would resolve to the parameter
foo
But what happens with the existing snapshot? Decorators
@decoratorreference the outerdecoratorfunction, which should be the same whether decorators are visited before or after bind. So the existing snapshot should still pass.But wait, the snapshot shows references for
decoratorwithscope_id3 (the function scope). Each reference is resolved to the right symbol. The number of references should remain 2 regardless.Assistant
Good, the visit calls in builder.rs are the only places where parameters process decorators. Let me also look at what
param.bind()does - specifically thebound_namesmethod:Assistant
OK so
bound_namesis used to declare symbols. Let me now make the fix:Assistant
Now let me apply the fix:
Assistant
Now let me rebuild and test:
Assistant
Wait, the warning is still there. Let me investigate:
Assistant
Let me try a more careful look at what's happening:
Assistant
Still reporting the import as unused! Let me think again.
Wait. The fix might be correct but I need to check if maybe the param.bind only adds the symbol but doesn't add it to scope, or there's another layer.
Or maybe the issue is that the
param.binddoes indeed create a symbol in the scope, but the symbol declaration happens in a different scope. Let me check.Actually wait... looking at the binder again:
rustfn bind(&self, builder: &mut SemanticBuilder<'a>) { let parent_kind = builder.nodes.parent_kind(builder.current_node_id); let AstKind::FormalParameters(parameters) = parent_kind else { unreachable!() }; ... self.bound_names(&mut |ident| { let symbol_id = builder.declare_symbol(ident.span, ident.name, includes, excludes); ident.symbol_id.set(Some(symbol_id)); }); }So it declares a symbol in the current scope. The current scope is the function scope (entered by
visit_functionvia ).Assistant
Let me verify my fix is actually in the file and rebuild more carefully:
Assistant
The fix is in the file. Let me do a clean rebuild and check:
Assistant
Let me investigate further to understand what's actually happening:
Assistant
Let me check what
param.bind()actually does to a BindingIdentifier. Maybe I need to look at what's happening with FormalParameter::pattern.Let me look at the FormalParameter structure to understand:
Assistant
Let me re-examine the existing fixture's snapshot to understand the symbol generation:
Assistant
It seems the test passed but my fix may not have made it active. Let me look more carefully at what's happening.
Let me make sure the test fixture I was looking at before still has the same snapshot. Maybe the cache is the issue.
Assistant
Hmm, no snapshot was updated. Let me check the snapshot files to see if maybe they're auto-updated with INST_UPDATE. But let me also check the binary for oxlint.
Maybe the test "main" only runs the snapshot tests once, but the snapshot review inline shows them as "ok" because it's in CI mode... or maybe it just doesn't have any pending snapshot review.
Let me try with INSTA_UPDATE=always
Assistant
Let me re-check the build output for the binary:
Assistant
Let me investigate this more carefully. Let me look at the
declare functioncase in particular, since that's used in the test:Assistant
Let me check what's happening. Let me look at semantic's reference resolution for unresolved references:
Assistant
I found it - the reference is stored as "unresolved" until
resolve_references_for_current_scopeis called AFTER all parameters have been bound. So the lookup still happens after binding even if we visit decorators first. Let me look more carefully at theresolve_references_for_current_scopeand find a better fix:Assistant
Let me write a more robust fix. The references in decorator expressions need to be resolved BEFORE the parameter is bound:
Assistant
Let me apply a better fix. The key insight is that we need to resolve the decorator references IMMEDIATELY (not deferred), so that they don't see the parameter binding when finally resolved:
Assistant
The bug is fixed! Let me also test the import case:
Assistant
Both cases work. Let me also test some edge cases to make sure my fix doesn't break things:
Assistant
Wait, those
barandbazare declared after the class. Let me re-test with proper ordering:Assistant
Let me test more edge cases:
Assistant
The
argsparameter is reported as unused because the constructor body is empty - that's correct.The
foois NOT reported as unused - that's correctly fixed.Let me also test with no decorator:
Assistant
The
foois correctly reported as unused when there's no decorator (correct, since the parameterfooshadows it).Assistant
Let me run the semantic tests to see if my fix breaks anything:
Assistant
Let me check the snapshot count for parameters/decorators:
Assistant
There's an unrelated issue with the cfg example. Let me try testing just the main test:
Assistant
All tests are passing. The snapshot test is passing too. Let me also explicitly verify if the snapshot for
parameter-propertyis unchanged:Assistant
Let me check by introducing a deliberate failure to ensure the test detects failures:
Assistant
The test DOES detect snapshot failures. So my fix didn't break any snapshots. Let me restore the original snapshot:
Assistant
Now let me run the linter tests:
Assistant
Now let me run the no_unused_vars tests directly:
Assistant
All 47 tests pass. Let me also check that the conformance tests for typescript-eslint work:
Assistant
Let me now also check transformer tests for parameter decorators:
Assistant
All pass. Let me also run the wider transformer tests:
Assistant
Let me also check the no_unused_vars linter test:
Assistant
All linter tests pass. Let me also check that the test reproduces the original issue correctly with a more thorough test:
Assistant
Now let me think about edge cases. The fix should also handle:
- Multiple decorators on the same parameter
- Method parameter decorators (not just constructor)
- Parameter with default value
- Rest parameter with decorator
Assistant
Correctly reports the outer
fooas unused since the decorator doesn't reference it.Let me also test the method parameter decorator that uses the outer binding:
Assistant
Interesting -
argsis reported as not unused since it's used inconsole.log(args). Let me check what happens ifargsis unused:Assistant
argsis correctly reported as unused, butfoois NOT reported - correct!Let me also test a few more edge cases:
Assistant
The arrow function test correctly reports the outer
fooas unused since the arrow parameter shadows it.Let me also verify the original test case from the issue:
Assistant
Let me also check what happens with the various tests in the no_unused_vars rust module and similar places:
Assistant
Let me look at the existing oxc tests now to make sure my changes work fine:
Assistant
Now, let me think about whether there are any other places I need to fix. Let me also look at the
visit_method_definitionand similar things to make sure method decorators are handled correctly:Assistant
For MethodDefinition, the order is:
- enter_node
- visit_decorators (in class scope)
- visit_property_key
- visit_function (enters function scope)
- Inside visit_function, parameters are visited
So method-level decorators are visited in the class scope (which is correct). The parameter scope is entered only via
visit_function. So my fix for parameter-level decorators is in the right place.Let me also check property_definition (class properties):
Assistant
Good, the semantic builder uses the default
walk_property_definitionwhich is fine.Let me also check AccessorProperty (TypeScript 4.9+):
Assistant
Good -
AccessorPropertyis the field kind, not a visit method. Let me think about more edge cases:Assistant
Hmm, the
fooparameter is unused (the setter body is empty). But the warning is gone. Let me re-check:Assistant
Interesting. When I don't have a decorator on the setter, the outer
foois reported as unused (correctly).But when the setter has a decorator
@Param(foo.KEY), neither the outerfoonor the setter parameter is reported. This is correct because the decorator uses the outerfoo.Now let me test the case where the setter parameter is unused AND the decorator uses an outer variable:
Assistant
Hmm,
barparameter is unused but not being reported. Let me test without the decorator.Assistant
barparameter is a setter, which requires exactly one parameter, so it's not considered "unused".Let me test with multiple parameters and other cases:
Assistant
Now let me also try with another test case to verify that the snapshot tests still work for the existing fixtures:
Assistant
Let me check the snapshot tests still work with INSTA_UPDATE=always to ensure no failures:
Assistant
Let me also test that references in the constructor body still resolve correctly to the parameter:
Assistant
Now let me also test the case mentioned in the issue where the parameter is used inside the constructor body:
Assistant
Let me also ensure the original test case still works:
Assistant
Now let me also run the broader oxlint test suite to make sure nothing is broken:
Assistant
Let me check if this is a pre-existing test failure:
Assistant
This is a pre-existing test failure unrelated to my fix. Let me verify my changes are still applied:
Assistant
Let me reset the unrelated fixture changes:
Assistant
Now let me also rebuild and verify my changes still work:
Assistant
Let me run the existing no_unused_vars tests to confirm:
Assistant
Let me also test edge cases related to forward references and complex cases:
Assistant
The
Childclass is correctly reported as unused. TheCONFIGis correctly used in the decorator.Now let me also think about what happens if the decorator uses a parameter from a previous parameter. Per spec, this is unusual because decorators run before params are bound, but in practice, we should still resolve it to the outer scope.
Let me also check if my fix breaks the snapshot tests by running a comprehensive test:
Assistant
All semantic tests pass. Let me also run a wider test suite to make sure nothing is broken:
Assistant
All 1144 linter tests pass!
Assistant
This is a pre-existing failure (
No such file or directoryfrom the sourcemap test), not related to my fix.Let me also test the transformer to ensure parameter decorators still work properly:
Assistant
Now let me also write a test program to confirm the original issue is resolved:
Assistant
The original issue is resolved. Now let me also see if there are any tests that I should add for the fix to test the no-unused-vars rule. Looking at the existing tests:
Assistant
Let me run the snapshot test once more, specifically the parameter decorator tests, to confirm they still pass:
Assistant
Only my single change to
builder.rsis in the repository. All tests pass.Let me also verify the broader test once more to make sure I haven't broken anything:
Assistant
All no_unused_vars tests pass. Let me also run the integration tests:
Assistant
All tests pass. Let me also verify my final fix:
Assistant
Let me write a complete repro script that verifies all aspects of my fix:
Assistant
All 8 tests pass with the expected results. Let me also clean up my temp files:
Assistant
Now let me also verify with the simple reproduction:
Assistant
Let me also check the final diff one more time:
Assistant
The fix is clean and minimal. Let me also verify the fix one more time with the exact reproduction from the issue:
Assistant
The fix successfully resolves the issue. Let me also run the final test of all the related test suites:
Assistant
All tests pass.
Summary of Changes
The bug was in
crates/oxc_semantic/src/builder.rsin thevisit_formal_parameterandvisit_formal_parameter_restmethods. The original code was:rustfn visit_formal_parameter(&mut self, param: &FormalParameter<'a>) { let kind = AstKind::FormalParameter(self.alloc(param)); self.enter_node(kind); param.bind(self); // <-- BIND FIRST self.visit_span(¶m.span); self.visit_decorators(¶m.decorators); // <-- THEN VISIT DECORATORS ... }Assistant
Just a single file with 28 insertions and 2 deletions. Let me also quickly run the all tests once more to make sure everything is solid:
Assistant
All tests pass. Let me also make a final sanity check on the linter binary:
Assistant
Now let me go over the checklist:
Checklist Review
-
If an error was reported in the issue description, I have successfully reproduced it.
- ✅ Yes, I reproduced the error before applying the fix. The output was "Variable 'foo' is declared but never used."
-
If an error was reported in the issue description, I have confirmed that my fix resolves the error.
-
Assistant
Only my targeted change is present. I'm confident the fix is correct and complete.
Assistant
The fix is ready for submission.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.