mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 12:19:15 +00:00
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:
parent
b007553ebe
commit
c043bec674
3 changed files with 22 additions and 3 deletions
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
6
crates/oxc_isolated_declarations/tests/fixtures/mapped-types.ts
vendored
Normal file
6
crates/oxc_isolated_declarations/tests/fixtures/mapped-types.ts
vendored
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
import {K} from 'foo'
|
||||
import {T} from 'bar'
|
||||
|
||||
export interface I {
|
||||
prop: {[key in K]: T}
|
||||
}
|
||||
|
|
@ -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};
|
||||
}
|
||||
Loading…
Reference in a new issue