Skip to content

Commit

Permalink
fix(semantic): typeof operator cannot reference type-only import (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing committed Jul 27, 2024
1 parent ef0e953 commit cb2fa49
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
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

0 comments on commit cb2fa49

Please sign in to comment.