diff --git a/crates/oxc_linter/src/rules/eslint/no_unused_vars/tests/oxc.rs b/crates/oxc_linter/src/rules/eslint/no_unused_vars/tests/oxc.rs index cf7eb4211a133..c4ff0c45fdfb1 100644 --- a/crates/oxc_linter/src/rules/eslint/no_unused_vars/tests/oxc.rs +++ b/crates/oxc_linter/src/rules/eslint/no_unused_vars/tests/oxc.rs @@ -1083,7 +1083,7 @@ fn test_namespaces() { #[test] fn test_type_aliases() { - let pass = vec![]; + let pass = vec!["type Foo = string; export default Foo;"]; let fail = vec![ // usages within own declaration do not count diff --git a/crates/oxc_semantic/src/builder.rs b/crates/oxc_semantic/src/builder.rs index 0f9bc44e67da8..929516c3c180d 100644 --- a/crates/oxc_semantic/src/builder.rs +++ b/crates/oxc_semantic/src/builder.rs @@ -1845,6 +1845,13 @@ impl<'a> SemanticBuilder<'a> { /* cfg */ match kind { + AstKind::ExportDefaultDeclaration(decl) => { + // `export default Ident` + // ^^^^^ -> Can reference both type binding and value + if matches!(decl.declaration, ExportDefaultDeclarationKind::Identifier(_)) { + self.current_reference_flags = ReferenceFlags::Read | ReferenceFlags::Type; + } + } AstKind::ExportNamedDeclaration(decl) => { if decl.export_kind.is_type() { self.current_reference_flags = ReferenceFlags::Type; @@ -2033,7 +2040,8 @@ impl<'a> SemanticBuilder<'a> { AstKind::TSTypeName(_) => { self.current_reference_flags -= ReferenceFlags::Type; } - AstKind::ExportNamedDeclaration(_) + AstKind::ExportDefaultDeclaration(_) + | 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/ts/exports/default/type-alias.snap b/crates/oxc_semantic/tests/fixtures/oxc/ts/exports/default/type-alias.snap new file mode 100644 index 0000000000000..9b3d19ad438bc --- /dev/null +++ b/crates/oxc_semantic/tests/fixtures/oxc/ts/exports/default/type-alias.snap @@ -0,0 +1,36 @@ +--- +source: crates/oxc_semantic/tests/main.rs +input_file: crates/oxc_semantic/tests/fixtures/oxc/ts/exports/default/type-alias.ts +--- +[ + { + "children": [ + { + "children": [], + "flags": "ScopeFlags(StrictMode)", + "id": 1, + "node": "TSTypeAliasDeclaration", + "symbols": [] + } + ], + "flags": "ScopeFlags(StrictMode | Top)", + "id": 0, + "node": "Program", + "symbols": [ + { + "flags": "SymbolFlags(TypeAlias)", + "id": 0, + "name": "A", + "node": "TSTypeAliasDeclaration", + "references": [ + { + "flags": "ReferenceFlags(Type)", + "id": 0, + "name": "A", + "node_id": 6 + } + ] + } + ] + } +] diff --git a/crates/oxc_semantic/tests/fixtures/oxc/ts/exports/default/type-alias.ts b/crates/oxc_semantic/tests/fixtures/oxc/ts/exports/default/type-alias.ts new file mode 100644 index 0000000000000..db70465a7b890 --- /dev/null +++ b/crates/oxc_semantic/tests/fixtures/oxc/ts/exports/default/type-alias.ts @@ -0,0 +1,2 @@ +type A = string; +export default A; diff --git a/tasks/coverage/snapshots/semantic_typescript.snap b/tasks/coverage/snapshots/semantic_typescript.snap index dfda5632274e6..5755c54c1643f 100644 --- a/tasks/coverage/snapshots/semantic_typescript.snap +++ b/tasks/coverage/snapshots/semantic_typescript.snap @@ -574,6 +574,15 @@ rebuilt : ScopeId(0): [] Scope children mismatch: after transform: ScopeId(0): [ScopeId(1)] rebuilt : ScopeId(0): [] +Reference symbol mismatch for "Color": +after transform: SymbolId(0) "Color" +rebuilt : +Reference flags mismatch for "Color": +after transform: ReferenceId(0): ReferenceFlags(Type) +rebuilt : ReferenceId(0): ReferenceFlags(Read | Type) +Unresolved references mismatch: +after transform: [] +rebuilt : ["Color"] tasks/coverage/typescript/tests/cases/compiler/ambientClassDeclarationWithExtends.ts semantic error: Missing SymbolId: "D" @@ -9306,6 +9315,15 @@ rebuilt : ScopeId(0): [] Scope children mismatch: after transform: ScopeId(0): [ScopeId(1)] rebuilt : ScopeId(0): [] +Reference symbol mismatch for "Foo": +after transform: SymbolId(0) "Foo" +rebuilt : +Reference flags mismatch for "Foo": +after transform: ReferenceId(0): ReferenceFlags(Type) +rebuilt : ReferenceId(0): ReferenceFlags(Read | Type) +Unresolved references mismatch: +after transform: [] +rebuilt : ["Foo"] tasks/coverage/typescript/tests/cases/compiler/declarationEmitToDeclarationDirWithDeclarationOption.ts semantic error: Bindings mismatch: @@ -9314,6 +9332,15 @@ rebuilt : ScopeId(0): [] Scope children mismatch: after transform: ScopeId(0): [ScopeId(1)] rebuilt : ScopeId(0): [] +Reference symbol mismatch for "Foo": +after transform: SymbolId(0) "Foo" +rebuilt : +Reference flags mismatch for "Foo": +after transform: ReferenceId(0): ReferenceFlags(Type) +rebuilt : ReferenceId(0): ReferenceFlags(Read | Type) +Unresolved references mismatch: +after transform: [] +rebuilt : ["Foo"] tasks/coverage/typescript/tests/cases/compiler/declarationEmitTopLevelNodeFromCrossFile.ts semantic error: Bindings mismatch: @@ -9507,6 +9534,15 @@ rebuilt : ScopeId(0): [] Scope children mismatch: after transform: ScopeId(0): [ScopeId(1)] rebuilt : ScopeId(0): [] +Reference symbol mismatch for "Foo": +after transform: SymbolId(0) "Foo" +rebuilt : +Reference flags mismatch for "Foo": +after transform: ReferenceId(0): ReferenceFlags(Type) +rebuilt : ReferenceId(0): ReferenceFlags(Read | Type) +Unresolved references mismatch: +after transform: [] +rebuilt : ["Foo"] tasks/coverage/typescript/tests/cases/compiler/declarationEmitWithDefaultAsComputedName.ts semantic error: Bindings mismatch: @@ -23393,6 +23429,15 @@ rebuilt : ScopeId(0): [] Scope children mismatch: after transform: ScopeId(0): [ScopeId(1)] rebuilt : ScopeId(0): [] +Reference symbol mismatch for "I": +after transform: SymbolId(0) "I" +rebuilt : +Reference flags mismatch for "I": +after transform: ReferenceId(0): ReferenceFlags(Type) +rebuilt : ReferenceId(0): ReferenceFlags(Read | Type) +Unresolved references mismatch: +after transform: [] +rebuilt : ["I"] tasks/coverage/typescript/tests/cases/compiler/moduleAugmentationsBundledOutput1.ts semantic error: Bindings mismatch: @@ -37384,6 +37429,9 @@ rebuilt : ScopeId(0): [] Reference symbol mismatch for "Op": after transform: SymbolId(0) "Op" rebuilt : +Reference flags mismatch for "Op": +after transform: ReferenceId(0): ReferenceFlags(Read) +rebuilt : ReferenceId(0): ReferenceFlags(Read | Type) Unresolved references mismatch: after transform: [] rebuilt : ["Op"] @@ -52439,6 +52487,9 @@ rebuilt : ScopeId(1): [] Reference symbol mismatch for "doc": after transform: SymbolId(0) "doc" rebuilt : +Reference flags mismatch for "doc": +after transform: ReferenceId(1): ReferenceFlags(Read) +rebuilt : ReferenceId(0): ReferenceFlags(Read | Type) Unresolved references mismatch: after transform: ["Document", "Element", "HTMLElement"] rebuilt : ["HTMLElement", "doc"] @@ -52453,6 +52504,9 @@ rebuilt : ScopeId(1): [] Reference symbol mismatch for "doc": after transform: SymbolId(0) "doc" rebuilt : +Reference flags mismatch for "doc": +after transform: ReferenceId(1): ReferenceFlags(Read) +rebuilt : ReferenceId(0): ReferenceFlags(Read | Type) Unresolved references mismatch: after transform: ["Document", "Element", "HTMLElement"] rebuilt : ["HTMLElement", "doc"] @@ -52464,6 +52518,9 @@ rebuilt : ScopeId(0): [] Reference symbol mismatch for "val": after transform: SymbolId(0) "val" rebuilt : +Reference flags mismatch for "val": +after transform: ReferenceId(0): ReferenceFlags(Read) +rebuilt : ReferenceId(0): ReferenceFlags(Read | Type) Unresolved references mismatch: after transform: [] rebuilt : ["val"]