From 70093d42954ee8c995bb0cac695038b5212f19c4 Mon Sep 17 00:00:00 2001 From: Dunqing Date: Fri, 6 Sep 2024 14:02:28 +0800 Subject: [PATCH] fix(semantic): incorrect reference when MemberExpression used in TSPropertySignature --- crates/oxc_semantic/src/builder.rs | 9 ++++----- .../signatures/property-with-type-import.snap | 19 +++++++++++++++++++ .../signatures/property-with-type-import.ts | 10 +++++++--- .../signatures/property-computed-name2.snap | 2 +- 4 files changed, 31 insertions(+), 9 deletions(-) diff --git a/crates/oxc_semantic/src/builder.rs b/crates/oxc_semantic/src/builder.rs index b97c8286f6998b..c63a92ebeac140 100644 --- a/crates/oxc_semantic/src/builder.rs +++ b/crates/oxc_semantic/src/builder.rs @@ -1891,9 +1891,9 @@ impl<'a> SemanticBuilder<'a> { } } AstKind::MemberExpression(_) => { - if !self.current_reference_flags.is_type() { - self.current_reference_flags = ReferenceFlags::Read; - } + // A.B = 1; + // ^^^ we can't treat A as Write reference, because it's the property(B) of A that change + self.current_reference_flags -= ReferenceFlags::Write; } AstKind::AssignmentTarget(_) => { self.current_reference_flags |= ReferenceFlags::Write; @@ -1966,8 +1966,7 @@ impl<'a> SemanticBuilder<'a> { self.current_reference_flags -= ReferenceFlags::Read; } } - AstKind::MemberExpression(_) - | AstKind::ExportNamedDeclaration(_) + AstKind::ExportNamedDeclaration(_) | AstKind::TSTypeQuery(_) // Clear the reference flags that are set in AstKind::PropertySignature | AstKind::PropertyKey(_) => { diff --git a/crates/oxc_semantic/tests/fixtures/oxc/type-declarations/signatures/property-with-type-import.snap b/crates/oxc_semantic/tests/fixtures/oxc/type-declarations/signatures/property-with-type-import.snap index 2c844c0dd35a8c..20291d123f00c0 100644 --- a/crates/oxc_semantic/tests/fixtures/oxc/type-declarations/signatures/property-with-type-import.snap +++ b/crates/oxc_semantic/tests/fixtures/oxc/type-declarations/signatures/property-with-type-import.snap @@ -18,6 +18,13 @@ input_file: crates/oxc_semantic/tests/fixtures/oxc/type-declarations/signatures/ "id": 2, "node": "TSInterfaceDeclaration", "symbols": [] + }, + { + "children": [], + "flags": "ScopeFlags(StrictMode)", + "id": 3, + "node": "TSInterfaceDeclaration", + "symbols": [] } ], "flags": "ScopeFlags(StrictMode | Top)", @@ -35,6 +42,12 @@ input_file: crates/oxc_semantic/tests/fixtures/oxc/type-declarations/signatures/ "id": 0, "name": "X", "node_id": 15 + }, + { + "flags": "ReferenceFlags(Type)", + "id": 2, + "name": "X", + "node_id": 27 } ] }, @@ -49,6 +62,12 @@ input_file: crates/oxc_semantic/tests/fixtures/oxc/type-declarations/signatures/ "id": 1, "name": "B", "node_id": 19 + }, + { + "flags": "ReferenceFlags(Type)", + "id": 3, + "name": "B", + "node_id": 32 } ] }, diff --git a/crates/oxc_semantic/tests/fixtures/oxc/type-declarations/signatures/property-with-type-import.ts b/crates/oxc_semantic/tests/fixtures/oxc/type-declarations/signatures/property-with-type-import.ts index da4d768c6ba681..24d87a8c3f2fae 100644 --- a/crates/oxc_semantic/tests/fixtures/oxc/type-declarations/signatures/property-with-type-import.ts +++ b/crates/oxc_semantic/tests/fixtures/oxc/type-declarations/signatures/property-with-type-import.ts @@ -1,7 +1,11 @@ -import type X from 'mod'; +import type X from "mod"; type B = number; export interface A { - [X]: B -} \ No newline at end of file + [X]: B; +} + +export interface A { + [X.X]: B; +} diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/signatures/property-computed-name2.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/signatures/property-computed-name2.snap index 5b6304c581b019..a7fadc9a650fd6 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/signatures/property-computed-name2.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/signatures/property-computed-name2.snap @@ -39,7 +39,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio "node": "TSEnumDeclaration(Foo)", "references": [ { - "flags": "ReferenceFlags(Read)", + "flags": "ReferenceFlags(Type)", "id": 0, "name": "Foo", "node_id": 11