diff --git a/crates/oxc_semantic/src/builder.rs b/crates/oxc_semantic/src/builder.rs index 6bfc2567cbab0..0c17738e70300 100644 --- a/crates/oxc_semantic/src/builder.rs +++ b/crates/oxc_semantic/src/builder.rs @@ -436,6 +436,7 @@ impl<'a> SemanticBuilder<'a> { references.retain(|(id, flag)| { if flag.is_type() && symbol_flag.can_be_referenced_by_type() || flag.is_value() && symbol_flag.can_be_referenced_by_value() + || flag.is_ts_type_query() && symbol_flag.is_import() { // The non type-only ExportSpecifier can reference a type/value symbol, // If the symbol is a value symbol and reference flag is not type-only, remove the type flag. @@ -443,6 +444,15 @@ impl<'a> SemanticBuilder<'a> { *self.symbols.references[*id].flag_mut() -= ReferenceFlag::Type; } + // import type { T } from './mod'; type A = typeof T + // ^ can reference type-only import + // If symbol is type-import, we need to replace the ReferenceFlag::Value with ReferenceFlag::Type + if flag.is_ts_type_query() && symbol_flag.is_type_import() { + let reference_flag = self.symbols.references[*id].flag_mut(); + *reference_flag -= ReferenceFlag::Value; + *reference_flag |= ReferenceFlag::Type; + } + self.symbols.references[*id].set_symbol_id(symbol_id); resolved_references.push(*id); false @@ -1787,6 +1797,11 @@ impl<'a> SemanticBuilder<'a> { // ^^^^^^^^ self.current_reference_flag = ReferenceFlag::Read | ReferenceFlag::TSTypeQuery; } + AstKind::TSTypeParameterInstantiation(_) => { + // type A = typeof a; + // ^^^ avoid treat T as a value and TSTypeQuery + self.current_reference_flag -= ReferenceFlag::Read | ReferenceFlag::TSTypeQuery; + } AstKind::TSTypeName(_) => { match self.nodes.parent_kind(self.current_node_id) { Some( diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/import/type-default-value.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/import/type-default-value.snap index 61035781caaaa..5e76a73d37577 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/import/type-default-value.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/import/type-default-value.snap @@ -22,7 +22,14 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/import/type-def "id": 0, "name": "foo", "node": "ImportDefaultSpecifier", - "references": [] + "references": [ + { + "flag": "ReferenceFlag(Type | TSTypeQuery)", + "id": 0, + "name": "foo", + "node_id": 10 + } + ] }, { "flag": "SymbolFlags(TypeAlias)", diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/import/type-inline-value.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/import/type-inline-value.snap index bfa74a794e660..8558f7674d32a 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/import/type-inline-value.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/import/type-inline-value.snap @@ -22,7 +22,14 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/import/type-inl "id": 0, "name": "foo", "node": "ImportSpecifier", - "references": [] + "references": [ + { + "flag": "ReferenceFlag(Type | TSTypeQuery)", + "id": 0, + "name": "foo", + "node_id": 11 + } + ] }, { "flag": "SymbolFlags(TypeAlias)", diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/import/type-named-value.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/import/type-named-value.snap index 989e6b9f6bdc0..400b586b44a93 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/import/type-named-value.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/import/type-named-value.snap @@ -22,7 +22,14 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/import/type-nam "id": 0, "name": "foo", "node": "ImportSpecifier", - "references": [] + "references": [ + { + "flag": "ReferenceFlag(Type | TSTypeQuery)", + "id": 0, + "name": "foo", + "node_id": 11 + } + ] }, { "flag": "SymbolFlags(TypeAlias)", diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/instantiation-expressions/type-arguments2.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/instantiation-expressions/type-arguments2.snap index c16b63c00d7a1..07d2bb8a3ce8a 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/instantiation-expressions/type-arguments2.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/instantiation-expressions/type-arguments2.snap @@ -52,7 +52,14 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/instantiation-e "id": 4, "name": "T", "node": "TSTypeParameter", - "references": [] + "references": [ + { + "flag": "ReferenceFlag(Type)", + "id": 3, + "name": "T", + "node_id": 31 + } + ] } ] } diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/type-query-with-parameters.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/type-query-with-parameters.snap index 7561c89a0895e..cf1cc71c916dc 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/type-query-with-parameters.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/type-query-with-parameters.snap @@ -52,7 +52,14 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio "id": 4, "name": "T", "node": "TSTypeParameter", - "references": [] + "references": [ + { + "flag": "ReferenceFlag(Type)", + "id": 3, + "name": "T", + "node_id": 33 + } + ] } ] }