fix(semantic): insert binding into correct scope for TSInferType (#9567)

This commit is contained in:
camc314 committed 2025-03-05 17:03:32 +00:00
1 parent be5e5dcc80
commit bbb4f984f0
13 files changed
+33 -16

No files matched your search

+3 -1
View File
@@ -282,7 +282,9 @@ pub use match_ts_type;
/// ## Reference
/// * [TypeScript Handbook - Conditional Types](https://www.typescriptlang.org/docs/handbook/2/conditional-types.html)
#[ast(visit)]
#[scope]
#[scope(
flags = ScopeFlags::TsConditional,
)]
#[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ESTree)]
pub struct TSConditionalType<'a> {
+1 -1
View File
@@ -3302,7 +3302,7 @@ pub mod walk {
visitor.enter_node(kind);
visitor.visit_span(&it.span);
visitor.visit_ts_type(&it.check_type);
visitor.enter_scope(ScopeFlags::empty(), &it.scope_id);
visitor.enter_scope(ScopeFlags::TsConditional, &it.scope_id);
visitor.visit_ts_type(&it.extends_type);
visitor.visit_ts_type(&it.true_type);
visitor.leave_scope();
@@ -3466,7 +3466,7 @@ pub mod walk_mut {
visitor.enter_node(kind);
visitor.visit_span(&mut it.span);
visitor.visit_ts_type(&mut it.check_type);
visitor.enter_scope(ScopeFlags::empty(), &it.scope_id);
visitor.enter_scope(ScopeFlags::TsConditional, &it.scope_id);
visitor.visit_ts_type(&mut it.extends_type);
visitor.visit_ts_type(&mut it.true_type);
visitor.leave_scope();
+14 -1
View File
@@ -439,9 +439,22 @@ impl<'a> Binder<'a> for TSModuleDeclaration<'a> {
impl<'a> Binder<'a> for TSTypeParameter<'a> {
fn bind(&self, builder: &mut SemanticBuilder) {
let symbol_id = builder.declare_symbol(
let scope_id = if matches!(
builder.nodes.parent_kind(builder.current_node_id),
Some(AstKind::TSInferType(_))
) {
builder
.scope
.ancestors(builder.current_scope_id)
.find(|scope_id| builder.scope.get_flags(*scope_id).is_ts_conditional())
} else {
None
};
let symbol_id = builder.declare_symbol_on_scope(
self.name.span,
&self.name.name,
scope_id.unwrap_or(builder.current_scope_id),
SymbolFlags::TypeParameter,
SymbolFlags::TypeParameterExcludes,
);
@@ -9,7 +9,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio
"children": [
{
"children": [],
"flags": "ScopeFlags(StrictMode)",
"flags": "ScopeFlags(StrictMode | TsConditional)",
"id": 2,
"node": "TSConditionalType",
"symbols": [
@@ -31,7 +31,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio
},
{
"children": [],
"flags": "ScopeFlags(StrictMode)",
"flags": "ScopeFlags(StrictMode | TsConditional)",
"id": 3,
"node": "TSConditionalType",
"symbols": [
@@ -9,7 +9,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio
"children": [
{
"children": [],
"flags": "ScopeFlags(StrictMode)",
"flags": "ScopeFlags(StrictMode | TsConditional)",
"id": 2,
"node": "TSConditionalType",
"symbols": [
@@ -9,7 +9,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio
"children": [
{
"children": [],
"flags": "ScopeFlags(StrictMode)",
"flags": "ScopeFlags(StrictMode | TsConditional)",
"id": 2,
"node": "TSConditionalType",
"symbols": [
@@ -9,7 +9,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio
"children": [
{
"children": [],
"flags": "ScopeFlags(StrictMode)",
"flags": "ScopeFlags(StrictMode | TsConditional)",
"id": 2,
"node": "TSConditionalType",
"symbols": [
@@ -9,7 +9,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio
"children": [
{
"children": [],
"flags": "ScopeFlags(StrictMode)",
"flags": "ScopeFlags(StrictMode | TsConditional)",
"id": 2,
"node": "TSConditionalType",
"symbols": [
@@ -9,7 +9,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio
"children": [
{
"children": [],
"flags": "ScopeFlags(StrictMode)",
"flags": "ScopeFlags(StrictMode | TsConditional)",
"id": 2,
"node": "TSConditionalType",
"symbols": [
@@ -16,7 +16,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio
"children": [
{
"children": [],
"flags": "ScopeFlags(StrictMode)",
"flags": "ScopeFlags(StrictMode | TsConditional)",
"id": 3,
"node": "TSConditionalType",
"symbols": [
+5
View File
@@ -77,6 +77,7 @@ bitflags! {
const SetAccessor = 1 << 8;
const CatchClause = 1 << 9;
const DirectEval = 1 << 10; // <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval#direct_and_indirect_eval>
const TsConditional = 1 << 11;
const Var = Self::Top.bits() | Self::Function.bits() | Self::ClassStaticBlock.bits() | Self::TsModuleBlock.bits();
}
}
@@ -148,6 +149,10 @@ impl ScopeFlags {
self.contains(Self::CatchClause)
}
pub fn is_ts_conditional(&self) -> bool {
self.contains(Self::TsConditional)
}
#[inline]
pub fn contains_direct_eval(&self) -> bool {
self.contains(Self::DirectEval)
@@ -13901,7 +13901,7 @@ semantic error: Scope children mismatch:
after transform: ScopeId(0): [ScopeId(1), ScopeId(6), ScopeId(7)]
rebuilt : ScopeId(0): []
Unresolved references mismatch:
after transform: ["Pick", "U"]
after transform: ["Pick"]
rebuilt : []
tasks/coverage/typescript/tests/cases/compiler/inferFromGenericFunctionReturnTypes2.ts
@@ -41049,9 +41049,6 @@ tasks/coverage/typescript/tests/cases/conformance/types/conditional/inferTypes2.
semantic error: Scope children mismatch:
after transform: ScopeId(0): [ScopeId(1), ScopeId(3), ScopeId(4), ScopeId(6), ScopeId(9), ScopeId(10), ScopeId(12)]
rebuilt : ScopeId(0): [ScopeId(1), ScopeId(2)]
Unresolved references mismatch:
after transform: ["P", "foo", "foo2"]
rebuilt : ["foo", "foo2"]
tasks/coverage/typescript/tests/cases/conformance/types/conditional/inferTypesWithExtends1.ts
semantic error: Bindings mismatch: