Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fix(semantic): typeof operator cannot reference type-only import
  • Loading branch information
Dunqing committed Jul 27, 2024
commit 78110a0c65e76932c65fe00b6d92c4d8c7b24caa
10 changes: 10 additions & 0 deletions crates/oxc_semantic/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,13 +436,23 @@ 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.
if symbol_flag.is_value() && !flag.is_type_only() {
*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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
Expand Down