Skip to content

Commit

Permalink
fix(semantic): IdentifierReference within TSPropertySignature can…
Browse files Browse the repository at this point in the history
…not reference type-only import binding (#5441)

close: #5435

The behavior of `IdentifierReference` in `TSPropertySignature` is the same as in `TSTypeQuery`, both allow only reference value bindings and type-only import bindings.

I still use `ReferenceFlags::TSTypeQuery` here because I want to avoid producing many changes unrelated to the bug in this PR. I will refactor it in the follow-up PR soon
  • Loading branch information
Dunqing committed Sep 5, 2024
1 parent 0f50b1e commit d8b9909
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 20 deletions.
12 changes: 11 additions & 1 deletion crates/oxc_semantic/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1830,6 +1830,14 @@ impl<'a> SemanticBuilder<'a> {
AstKind::TSInterfaceHeritage(_) => {
self.current_reference_flags = ReferenceFlags::Type;
}
AstKind::TSPropertySignature(signature) => {
if signature.key.is_expression() {
// interface A { [prop]: string }
// ^^^^^ The property can reference value or [`SymbolFlags::TypeImport`] symbol
self.current_reference_flags =
ReferenceFlags::Read | ReferenceFlags::TSTypeQuery; // TODO: Should use another flag
}
}
AstKind::TSTypeQuery(_) => {
// type A = typeof a;
// ^^^^^^^^
Expand Down Expand Up @@ -1962,8 +1970,10 @@ impl<'a> SemanticBuilder<'a> {
}
}
AstKind::MemberExpression(_)
| AstKind::ExportNamedDeclaration(_)
| AstKind::TSTypeQuery(_)
| AstKind::ExportNamedDeclaration(_) => {
// Clear the reference flags that are set in AstKind::PropertySignature
| AstKind::PropertyKey(_) => {
self.current_reference_flags = ReferenceFlags::empty();
}
AstKind::AssignmentTarget(_) => self.current_reference_flags -= ReferenceFlags::Write,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
---
source: crates/oxc_semantic/tests/main.rs
input_file: crates/oxc_semantic/tests/fixtures/oxc/type-declarations/signatures/property-with-type-import.ts
---
[
{
"children": [
{
"children": [],
"flags": "ScopeFlags(StrictMode)",
"id": 1,
"node": "TSTypeAliasDeclaration",
"symbols": []
},
{
"children": [],
"flags": "ScopeFlags(StrictMode)",
"id": 2,
"node": "TSInterfaceDeclaration",
"symbols": []
}
],
"flags": "ScopeFlags(StrictMode | Top)",
"id": 0,
"node": "Program",
"symbols": [
{
"flags": "SymbolFlags(TypeImport)",
"id": 0,
"name": "X",
"node": "ImportDefaultSpecifier",
"references": [
{
"flags": "ReferenceFlags(Type | TSTypeQuery)",
"id": 0,
"name": "X",
"node_id": 15
}
]
},
{
"flags": "SymbolFlags(TypeAlias)",
"id": 1,
"name": "B",
"node": "TSTypeAliasDeclaration",
"references": [
{
"flags": "ReferenceFlags(Type)",
"id": 1,
"name": "B",
"node_id": 19
}
]
},
{
"flags": "SymbolFlags(Export | Interface)",
"id": 2,
"name": "A",
"node": "TSInterfaceDeclaration",
"references": []
}
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type X from 'mod';

type B = number;

export interface A {
[X]: B
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio
"node": "VariableDeclarator(x)",
"references": [
{
"flags": "ReferenceFlags(Read)",
"flags": "ReferenceFlags(Read | TSTypeQuery)",
"id": 0,
"name": "x",
"node_id": 14
Expand Down
18 changes: 0 additions & 18 deletions tasks/coverage/semantic_typescript.snap
Original file line number Diff line number Diff line change
Expand Up @@ -12996,9 +12996,6 @@ rebuilt : ScopeId(0): ["A", "B"]
Scope children mismatch:
after transform: ScopeId(0): [ScopeId(1), ScopeId(2), ScopeId(3), ScopeId(5), ScopeId(7), ScopeId(8), ScopeId(10)]
rebuilt : ScopeId(0): [ScopeId(1), ScopeId(2)]
Reference flags mismatch:
after transform: ReferenceId(12): ReferenceFlags(Type)
rebuilt : ReferenceId(0): ReferenceFlags(Read)
Unresolved references mismatch:
after transform: ["Extract", "Parameters"]
rebuilt : []
Expand Down Expand Up @@ -14600,15 +14597,6 @@ rebuilt : ScopeId(0): [ScopeId(1), ScopeId(2), ScopeId(4), ScopeId(6), Sc
Symbol reference IDs mismatch:
after transform: SymbolId(0): [ReferenceId(0), ReferenceId(1), ReferenceId(2), ReferenceId(3)]
rebuilt : SymbolId(0): [ReferenceId(0), ReferenceId(1), ReferenceId(2)]
Reference flags mismatch:
after transform: ReferenceId(1): ReferenceFlags(Type)
rebuilt : ReferenceId(0): ReferenceFlags(Read)
Reference flags mismatch:
after transform: ReferenceId(2): ReferenceFlags(Type)
rebuilt : ReferenceId(1): ReferenceFlags(Read)
Reference flags mismatch:
after transform: ReferenceId(3): ReferenceFlags(Type)
rebuilt : ReferenceId(2): ReferenceFlags(Read)

tasks/coverage/typescript/tests/cases/compiler/interfaceImplementation5.ts
semantic error: Bindings mismatch:
Expand Down Expand Up @@ -31028,12 +31016,6 @@ rebuilt : SymbolId(2): []
Symbol reference IDs mismatch:
after transform: SymbolId(4): [ReferenceId(12)]
rebuilt : SymbolId(3): []
Reference flags mismatch:
after transform: ReferenceId(1): ReferenceFlags(Type)
rebuilt : ReferenceId(0): ReferenceFlags(Read)
Reference flags mismatch:
after transform: ReferenceId(3): ReferenceFlags(Type)
rebuilt : ReferenceId(1): ReferenceFlags(Read)

tasks/coverage/typescript/tests/cases/conformance/classes/classExpression.ts
semantic error: Missing SymbolId: M
Expand Down

0 comments on commit d8b9909

Please sign in to comment.