From f022b8d4c7fc06bdc8ecb1977f5cfb00fc72bc3f Mon Sep 17 00:00:00 2001 From: "Matthew \"strager\" Glazar" Date: Fri, 13 Oct 2023 21:58:08 -0400 Subject: [PATCH] fix(typescript): allow namespace and interface/type with same name --- docs/CHANGELOG.md | 2 ++ src/quick-lint-js/fe/variable-analyzer.cpp | 4 ++++ ...ariable-analyzer-multiple-declarations.cpp | 21 +++++++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index a524b1a0f5..7a67c9bc30 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -49,6 +49,8 @@ Semantic Versioning. * `(T: T) => {}` (a generic arrow function with the same name for a run-time parameter and a generic parameter) no longer falsely reports [E0034][] ("redeclaration of variable"). + * A namespace with the same name as an interface or type alias no longer + falsely reports [E0034][] ("redeclaration of variable"). * Nested `module` declarations no longer falsely report [E0361][]. E0361's message has been changed: * Before: "module with string name is only allowed at the top level" diff --git a/src/quick-lint-js/fe/variable-analyzer.cpp b/src/quick-lint-js/fe/variable-analyzer.cpp index 7b3cdfcf06..26daf699a9 100644 --- a/src/quick-lint-js/fe/variable-analyzer.cpp +++ b/src/quick-lint-js/fe/variable-analyzer.cpp @@ -1053,6 +1053,7 @@ void Variable_Analyzer::report_error_if_variable_declaration_conflicts( (kind == VK::_interface && other_kind == VK::_class) || (kind == VK::_interface && other_kind == VK::_import) || (kind == VK::_interface && other_kind == VK::_interface) || + (kind == VK::_interface && other_kind == VK::_namespace) || (kind == VK::_interface && !is_type(other_kind)) || (kind == VK::_let && other_kind == VK::_namespace) || (kind == VK::_let && other_kind == VK::_type_alias) || @@ -1060,11 +1061,14 @@ void Variable_Analyzer::report_error_if_variable_declaration_conflicts( (kind == VK::_namespace && other_kind == VK::_const) || (kind == VK::_namespace && other_kind == VK::_enum) || (kind == VK::_namespace && other_kind == VK::_function) || + (kind == VK::_namespace && other_kind == VK::_interface) || (kind == VK::_namespace && other_kind == VK::_let) || (kind == VK::_namespace && other_kind == VK::_namespace) || + (kind == VK::_namespace && other_kind == VK::_type_alias) || (kind == VK::_namespace && other_kind == VK::_var) || (kind == VK::_type_alias && other_kind == VK::_const) || (kind == VK::_type_alias && other_kind == VK::_let) || + (kind == VK::_type_alias && other_kind == VK::_namespace) || (kind == VK::_type_alias && other_kind == VK::_var) || (kind == VK::_var && other_kind == VK::_catch) || (kind == VK::_var && other_kind == VK::_function) || diff --git a/test/test-variable-analyzer-multiple-declarations.cpp b/test/test-variable-analyzer-multiple-declarations.cpp index f7eeeafc9e..1f01be20a8 100644 --- a/test/test-variable-analyzer-multiple-declarations.cpp +++ b/test/test-variable-analyzer-multiple-declarations.cpp @@ -49,6 +49,27 @@ TEST(Test_Variable_Analyzer_Multiple_Declarations, typescript_analyze_options, default_globals); } +TEST(Test_Variable_Analyzer_Multiple_Declarations, + type_or_interface_does_not_conflict_with_namespace) { + test_parse_and_analyze(u8"namespace n {} type n = null;"_sv, no_diags, + typescript_analyze_options, default_globals); + test_parse_and_analyze(u8"namespace n {;} type n = null;"_sv, no_diags, + typescript_analyze_options, default_globals); + test_parse_and_analyze(u8"type n = null; namespace n {}"_sv, no_diags, + typescript_analyze_options, default_globals); + test_parse_and_analyze(u8"type n = null; namespace n {;}"_sv, no_diags, + typescript_analyze_options, default_globals); + + test_parse_and_analyze(u8"namespace n {} interface n {}"_sv, no_diags, + typescript_analyze_options, default_globals); + test_parse_and_analyze(u8"namespace n {;} interface n {}"_sv, no_diags, + typescript_analyze_options, default_globals); + test_parse_and_analyze(u8"interface n {} namespace n {}"_sv, no_diags, + typescript_analyze_options, default_globals); + test_parse_and_analyze(u8"interface n {} namespace n {;}"_sv, no_diags, + typescript_analyze_options, default_globals); +} + TEST(Test_Variable_Analyzer_Multiple_Declarations, namespace_can_be_declared_multiple_times) { test_parse_and_analyze(