Skip to content

Commit

Permalink
fix(typescript): allow namespace and interface/type with same name
Browse files Browse the repository at this point in the history
  • Loading branch information
strager committed Oct 14, 2023
1 parent 9afb661 commit f022b8d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ Semantic Versioning.
* `<T>(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"
Expand Down
4 changes: 4 additions & 0 deletions src/quick-lint-js/fe/variable-analyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1053,18 +1053,22 @@ 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) ||
(kind == VK::_namespace && other_kind == VK::_class) ||
(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) ||
Expand Down
21 changes: 21 additions & 0 deletions test/test-variable-analyzer-multiple-declarations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit f022b8d

Please sign in to comment.