fix(isolated_declarations): add mapped-type constraint to the scope (#4037)

Fixes https://github.com/oxc-project/oxc/issues/4020

Example:
```ts
import {K} from 'foo'
import {T} from 'bar'

export interface I {
    prop: {[key in K]: T}
}
```

Before:
```ts
import {T} from 'bar'
export interface I {
    prop: {[key in K]: T}
}
```

After:
```ts
import {K} from 'foo'
import {T} from 'bar'
export interface I {
    prop: {[key in K]: T}
}
```

TSC:
```ts
import { K } from 'foo';
import { T } from 'bar';
export interface I {
    prop: {
        [key in K]: T;
    };
}

```
This commit is contained in:
Egor Blinov 2024-07-03 03:45:03 +02:00 committed by GitHub
parent b007553ebe
commit c043bec674
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 22 additions and 3 deletions

View file

@ -261,10 +261,9 @@ impl<'a> Visit<'a> for ScopeTree<'a> {
}
}
/// `type D<T> = { [K in keyof T]: K };`
/// `type D = { [key in keyof T]: K };`
/// ^^^^^^^^^^^^^^^^^^^^
/// `K` is a type parameter
/// We need to add `K` to the scope
/// We need to add both `T` and `K` to the scope
fn visit_ts_mapped_type(&mut self, ty: &TSMappedType<'a>) {
// copy from walk_ts_mapped_type
self.enter_scope(ScopeFlags::empty());
@ -272,6 +271,9 @@ impl<'a> Visit<'a> for ScopeTree<'a> {
if let Some(name) = &ty.name_type {
self.visit_ts_type(name);
}
if let Some(constraint) = &ty.type_parameter.constraint {
self.visit_ts_type(constraint);
}
if let Some(type_annotation) = &ty.type_annotation {
self.visit_ts_type(type_annotation);
}

View file

@ -0,0 +1,6 @@
import {K} from 'foo'
import {T} from 'bar'
export interface I {
prop: {[key in K]: T}
}

View file

@ -0,0 +1,11 @@
---
source: crates/oxc_isolated_declarations/tests/mod.rs
input_file: crates/oxc_isolated_declarations/tests/fixtures/mapped-types.ts
---
==================== .D.TS ====================
import { K } from 'foo';
import { T } from 'bar';
export interface I {
prop: { [key in K] : T};
}